nmea.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #ifdef PC_BUILD
  2. #include "../gps-test-tool/main.h"
  3. #else
  4. #include "main.h"
  5. #endif
  6. /*----------------------------------------------------*/
  7. /* Get a line received from GPS module */
  8. /*----------------------------------------------------*/
  9. uint16_t get_line ( /* 0:line incomplete or timed out, >0: Number of bytes received. */
  10. char *buff,
  11. uint16_t sz_buf
  12. )
  13. {
  14. char c;
  15. static uint16_t i = 0;
  16. uint16_t ret_len;
  17. set_timer(recv_timeout, 1000);
  18. for (;;) {
  19. wdt_reset();
  20. if (FLAGS & (F_LVD | F_POWEROFF))
  21. return 0; /* A brownout is detected */
  22. if (timer_expired(recv_timeout))
  23. return 0; /* timeout; continue the main loop */
  24. if (System.keypress) /* process user keypress */
  25. return 0;
  26. if (!uart0_test()) {
  27. sleep();
  28. continue;
  29. }
  30. c = (char)uart0_get();
  31. if (i == 0 && c != '$' )
  32. continue; /* Find start of line */
  33. if (c == '\n' || c == '\r') {
  34. buff[i++] = '\r';
  35. buff[i++] = '\n';
  36. buff[i++] = '\0'; /* add null termination for string */
  37. break; /* EOL */
  38. }
  39. buff[i++] = c;
  40. uart1_put(c);
  41. if (i >= sz_buf - 3) /* keep 3 bytes for terminating character */
  42. i = 0; /* Buffer overflow (abort this line) */
  43. }
  44. ret_len = i;
  45. i = 0;
  46. if (ret_len > 0) {
  47. uart1_put('\r');
  48. uart1_put('\n');
  49. }
  50. return ret_len;
  51. }
  52. /* Compare sentence header string */
  53. BYTE gp_comp (const char *str1, __flash const char *str2)
  54. {
  55. char c;
  56. do {
  57. c = pgm_read_byte(str2++);
  58. } while (c && c == *str1++);
  59. return c;
  60. }
  61. #define FIELD_BUF_LEN 32
  62. /* Get a column item */
  63. static
  64. const char* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  65. const char* buf, /* Pointer to the sentence */
  66. BYTE col /* Column number (0 is the 1st item) */
  67. ) {
  68. BYTE c;
  69. static char field_buf[FIELD_BUF_LEN];
  70. unsigned char length = 0;
  71. while (col) {
  72. do {
  73. c = *buf++;
  74. if (c <= ' ') return NULL;
  75. } while (c != ',');
  76. col--;
  77. }
  78. while (*buf && *buf != ',' && length < FIELD_BUF_LEN-1) {
  79. field_buf[length++] = *buf++;
  80. }
  81. field_buf[length] = '\0';
  82. return field_buf;
  83. }
  84. unsigned int gp_val(const char *db, unsigned char count) {
  85. unsigned int out = 0;
  86. while (count--) {
  87. unsigned char n;
  88. n = *db - '0';
  89. if (n >= 10)
  90. return 0;
  91. out *= 10;
  92. out += n;
  93. db++;
  94. }
  95. return out;
  96. }
  97. static time_t gp_rmc_parse(const char *str) {
  98. const char *p;
  99. struct tm tmc;
  100. p = gp_col(str, 1); /* Get h:m:s */
  101. if (!p)
  102. return 0;
  103. tmc.tm_hour = gp_val(p, 2);
  104. tmc.tm_min = gp_val(p+2, 2);
  105. tmc.tm_sec = gp_val(p+4, 2);
  106. p = gp_col(str, 9); /* Get y:m:d */
  107. if (!p)
  108. return 0;
  109. tmc.tm_mday = gp_val(p, 2);
  110. tmc.tm_mon = gp_val(p+2, 2) - 1;
  111. tmc.tm_year = gp_val(p+4, 2) + 100;
  112. utc = mktime(&tmc); /* Check time validity */
  113. if (utc == -1)
  114. return 0;
  115. p = gp_col(str, 2); /* Get status */
  116. if (!p || *p != 'A') {
  117. System.location_valid = LOC_INVALID;
  118. FLAGS &= ~F_GPSOK;
  119. return 0; /* Return 0 even is time is valid (comes from module's internal RTC) */
  120. }
  121. FLAGS |= F_GPSOK;
  122. return utc;
  123. }
  124. static void gp_gga_parse(const char *str) {
  125. const char *p;
  126. double tmp;
  127. p = gp_col(str, 7); /* satellites used */
  128. System.satellites_used = atoi(p);
  129. if (System.satellites_used >= System.conf.min_sats) {
  130. System.sat_count_low = 0;
  131. } else {
  132. System.sat_count_low = 1;
  133. }
  134. /* check validity */
  135. p = gp_col(str, 6);
  136. if (*p == '0') {
  137. System.location_valid = LOC_INVALID;
  138. return;
  139. }
  140. if (!System.sat_count_low)
  141. System.location_valid = LOC_VALID_NEW; /* don't accept the coordinates otherwise, even if reported valid */
  142. /* parse location */
  143. p = gp_col(str, 2); /* latitude */
  144. location.lat = gp_val(p, 2); /* degrees */
  145. p += 2;
  146. xatof(&p, &tmp); /* minutes */
  147. tmp /= 60; /* convert minutes to degrees */
  148. location.lat += tmp;
  149. p = gp_col(str, 3); /* N/S */
  150. if (*p != 'N')
  151. location.lat = -location.lat;
  152. p = gp_col(str, 4); /* longitude */
  153. location.lon = gp_val(p, 3); /* degrees */
  154. p += 3;
  155. xatof(&p, &tmp); /* minutes */
  156. tmp /= 60; /* convert minutes to degrees */
  157. location.lon += tmp;
  158. p = gp_col(str, 5); /* E/W */
  159. if (*p != 'E')
  160. location.lon = -location.lon;
  161. p = gp_col(str, 6); /* fix type */
  162. if (*p == '2')
  163. System.sbas = 1;
  164. else
  165. System.sbas = 0;
  166. p = gp_col(str, 9); /* MSL altitude */
  167. xatof(&p, &tmp);
  168. location.alt = tmp;
  169. location.time = utc; /* parsed from RMC */
  170. }
  171. static void gp_vtg_parse(const char *str) {
  172. const char *p;
  173. double speed;
  174. p = gp_col(str, 9);
  175. if (*p == 'N') /* Not valid */
  176. return;
  177. p = gp_col(str, 7); /* speed in km/h */
  178. xatof(&p, &speed);
  179. System.speed = speed+0.5;
  180. }
  181. /*$PMTK355*31<CR><LF>
  182. Return $PMTK001,355,3,1,0,0*2E “$PMTK001,355,3,GLON_Enable,BEIDOU_Enable,GALILEO_Enable”
  183. The GLONASS search mode is enabled. */
  184. static void pmtk001_parse(const char *str) {
  185. const char *p;
  186. /* check validity */
  187. p = gp_col(str, 1);
  188. if (strcmp_P(p, PSTR("355"))) /* not the PMTK355 reply */
  189. return;
  190. p = gp_col(str, 2);
  191. if (*p == '0' || *p == '1') { /* invalid / unsupported */
  192. System.gps_only = 1;
  193. xputs_P(PSTR("GPS only\r\n"));
  194. } else {
  195. System.gps_only = 0;
  196. xputs_P(PSTR("Multi GNSS\r\n"));
  197. }
  198. gps_initialize();
  199. }
  200. unsigned char nmea_checksum(const char *str) {
  201. unsigned char cs = 0;
  202. while (*str && *str != '*') {
  203. cs ^= *str++; /* NMEA checksum is quite primitive, but still should catch simple bitstream errors or truncated lines */
  204. }
  205. return cs;
  206. }
  207. time_t gps_parse(const char *str) { /* Get all required data from NMEA sentences */
  208. signed int len = strlen(str)-2; /* remove final \r\n */
  209. const char *checksum;
  210. unsigned char calc_checksum, inc_checksum;
  211. signed int i;
  212. char c;
  213. if (len < 4)
  214. return 0; /* each message must contain $.*xx where . is one or more actual data characters and xx is the checksum */
  215. for (i = len-1; i && i >= (len-2); i--) { /* find checksum */
  216. if (str[i] == '*')
  217. break;
  218. }
  219. checksum = str+i+1; /* skip * character */
  220. inc_checksum = 0; /* parse hex string */
  221. while (*checksum && isalnum(*checksum)) {
  222. inc_checksum *= 16;
  223. c = *checksum++;
  224. if (c >= '0' && c <= '9') {
  225. inc_checksum += c - '0';
  226. } else if (c >= 'a' && c <= 'f') {
  227. inc_checksum += c - 'a' + 10;
  228. } else if (c >= 'A' && c <= 'F') {
  229. inc_checksum += c - 'A' + 10;
  230. } else {
  231. xputs_P(PSTR("Invalid NMEA: malformed checksum\r\n"));
  232. return 0; /* invalid checksum character */
  233. }
  234. }
  235. str++; /* drop initial $ */
  236. calc_checksum = nmea_checksum(str);
  237. if (inc_checksum != calc_checksum) {
  238. xputs_P(PSTR("Invalid NMEA checksum received\r\n"));
  239. return 0;
  240. }
  241. if (!gp_comp(str, PSTR("GPRMC")) || !gp_comp(str, PSTR("GNRMC")) || !gp_comp(str, PSTR("BDRMC")) || !gp_comp(str, PSTR("GARMC"))) {
  242. return gp_rmc_parse(str);
  243. }
  244. if (!gp_comp(str, PSTR("GPGGA")) || !gp_comp(str, PSTR("GNGGA")) || !gp_comp(str, PSTR("BDGGA")) || !gp_comp(str, PSTR("GAGGA"))) {
  245. gp_gga_parse(str);
  246. return 0;
  247. }
  248. if (!gp_comp(str, PSTR("GPVTG")) || !gp_comp(str, PSTR("GNVTG"))) {
  249. gp_vtg_parse(str);
  250. return 0;
  251. }
  252. if (!System.gps_initialized && !gp_comp(str, PSTR("PMTK011"))) {
  253. gps_initialize();
  254. return 0;
  255. }
  256. if (!gp_comp(str, PSTR("PMTK001"))) {
  257. pmtk001_parse(str);
  258. return 0;
  259. }
  260. return 0;
  261. }
  262. void uart0_put_wrap(int c) {
  263. uart0_put((char)c);
  264. }
  265. void gps_initialize(void) {
  266. /*
  267. * PMTK355: query gnss search mode (will fail if only GPS is supported)
  268. * PMTK353: set gnss search mode (GPS/Galileo/Glonass/Beidou)
  269. * PMTK313: enable SBAS
  270. */
  271. switch (System.gps_initialized) {
  272. case GPS_INIT_NOT_INITIALIZED:
  273. xfprintf(uart0_put_wrap, PSTR("$PMTK355*31\r\n"));
  274. xputs_P(PSTR("GNSS mode query sent\r\n"));
  275. System.gps_initialized = GPS_INIT_QUERY_SENT;
  276. break;
  277. case GPS_INIT_QUERY_SENT:
  278. if (get_flag(CONFFLAG_ENABLE_SBAS)) {
  279. xfprintf(uart0_put_wrap, PSTR("$PMTK313,1*2E\r\n"));
  280. xputs_P(PSTR("SBAS enable sent\r\n"));
  281. } else {
  282. xfprintf(uart0_put_wrap, PSTR("$PMTK313,0*2F\r\n"));
  283. xputs_P(PSTR("SBAS disable sent\r\n"));
  284. }
  285. if (!System.gps_only) {
  286. switch (System.conf.gnss_mode) {
  287. default:
  288. case GNSS_MODE_GPS_GLONASS_GALILEO:
  289. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,1,1,0,0*2A\r\n"));
  290. break;
  291. case GNSS_MODE_GPS:
  292. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,0*2A\r\n"));
  293. break;
  294. case GNSS_MODE_GPS_GALILEO:
  295. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,1,0,0*2B\r\n"));
  296. break;
  297. case GNSS_MODE_GALILEO:
  298. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,1,0,0*2A\r\n"));
  299. break;
  300. case GNSS_MODE_GPS_BEIDOU:
  301. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,1*2B\r\n"));
  302. break;
  303. case GNSS_MODE_BEIDOU:
  304. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,0,0,1*2A\r\n"));
  305. break;
  306. }
  307. xputs_P(PSTR("GNSS mode setting sent\r\n"));
  308. } else {
  309. System.conf.gnss_mode = GNSS_MODE_GPS;
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. void check_min_sat_limit(void) {
  317. if (System.conf.min_sats > 6 && (System.conf.gnss_mode == GNSS_MODE_GPS || System.conf.gnss_mode == GNSS_MODE_GALILEO || System.conf.gnss_mode == GNSS_MODE_BEIDOU)) {
  318. /* by geometry, max visible number is 6..12 */
  319. System.conf.min_sats = 6;
  320. }
  321. }