nmea.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. if (System.conf.uart_log_mode != UART_LOG_MODE_SYSTEM)
  41. uart1_put(c);
  42. if (i >= sz_buf - 3) /* keep 3 bytes for terminating character */
  43. i = 0; /* Buffer overflow (abort this line) */
  44. }
  45. ret_len = i;
  46. i = 0;
  47. if (ret_len > 0 && System.conf.uart_log_mode != UART_LOG_MODE_SYSTEM) {
  48. uart1_put('\r');
  49. uart1_put('\n');
  50. }
  51. return ret_len;
  52. }
  53. /* Compare sentence header string */
  54. BYTE gp_comp (const char *str1, __flash const char *str2)
  55. {
  56. char c;
  57. do {
  58. c = pgm_read_byte(str2++);
  59. } while (c && c == *str1++);
  60. return c;
  61. }
  62. #define FIELD_BUF_LEN 32
  63. /* Get a column item */
  64. static
  65. const char* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  66. const char* buf, /* Pointer to the sentence */
  67. BYTE col /* Column number (0 is the 1st item) */
  68. ) {
  69. BYTE c;
  70. static char field_buf[FIELD_BUF_LEN];
  71. unsigned char length = 0;
  72. while (col) {
  73. do {
  74. c = *buf++;
  75. if (c <= ' ') return NULL;
  76. } while (c != ',');
  77. col--;
  78. }
  79. while (*buf && *buf != ',' && length < FIELD_BUF_LEN-1) {
  80. field_buf[length++] = *buf++;
  81. }
  82. field_buf[length] = '\0';
  83. return field_buf;
  84. }
  85. unsigned int gp_val(const char *db, unsigned char count) {
  86. unsigned int out = 0;
  87. while (count--) {
  88. unsigned char n;
  89. n = *db - '0';
  90. if (n >= 10)
  91. return 0;
  92. out *= 10;
  93. out += n;
  94. db++;
  95. }
  96. return out;
  97. }
  98. static time_t gp_rmc_parse(const char *str) {
  99. const char *p;
  100. struct tm tmc;
  101. p = gp_col(str, 1); /* Get h:m:s */
  102. if (!p)
  103. return 0;
  104. tmc.tm_hour = gp_val(p, 2);
  105. tmc.tm_min = gp_val(p+2, 2);
  106. tmc.tm_sec = gp_val(p+4, 2);
  107. p = gp_col(str, 9); /* Get y:m:d */
  108. if (!p)
  109. return 0;
  110. tmc.tm_mday = gp_val(p, 2);
  111. tmc.tm_mon = gp_val(p+2, 2) - 1;
  112. tmc.tm_year = gp_val(p+4, 2) + 100;
  113. utc = mktime(&tmc); /* Check time validity */
  114. if (utc == -1)
  115. return 0;
  116. p = gp_col(str, 2); /* Get status */
  117. if (!p || *p != 'A') {
  118. System.location_valid = LOC_INVALID;
  119. FLAGS &= ~F_GPSOK;
  120. return 0; /* Return 0 even is time is valid (comes from module's internal RTC) */
  121. }
  122. FLAGS |= F_GPSOK;
  123. return utc;
  124. }
  125. static void gp_gga_parse(const char *str) {
  126. const char *p;
  127. double tmp;
  128. p = gp_col(str, 7); /* satellites used */
  129. System.satellites_used = atoi(p);
  130. if (System.satellites_used >= System.conf.min_sats) {
  131. System.sat_count_low = 0;
  132. } else {
  133. System.sat_count_low = 1;
  134. }
  135. /* check validity */
  136. p = gp_col(str, 6);
  137. if (*p == '0') {
  138. System.location_valid = LOC_INVALID;
  139. return;
  140. }
  141. if (!System.sat_count_low)
  142. System.location_valid = LOC_VALID_NEW; /* don't accept the coordinates otherwise, even if reported valid */
  143. /* parse location */
  144. p = gp_col(str, 2); /* latitude */
  145. location.lat = gp_val(p, 2); /* degrees */
  146. p += 2;
  147. xatof(&p, &tmp); /* minutes */
  148. tmp /= 60; /* convert minutes to degrees */
  149. location.lat += tmp;
  150. p = gp_col(str, 3); /* N/S */
  151. if (*p != 'N')
  152. location.lat = -location.lat;
  153. p = gp_col(str, 4); /* longitude */
  154. location.lon = gp_val(p, 3); /* degrees */
  155. p += 3;
  156. xatof(&p, &tmp); /* minutes */
  157. tmp /= 60; /* convert minutes to degrees */
  158. location.lon += tmp;
  159. p = gp_col(str, 5); /* E/W */
  160. if (*p != 'E')
  161. location.lon = -location.lon;
  162. p = gp_col(str, 6); /* fix type */
  163. if (*p == '2')
  164. System.sbas = 1;
  165. else
  166. System.sbas = 0;
  167. p = gp_col(str, 9); /* MSL altitude */
  168. xatof(&p, &tmp);
  169. location.alt = tmp;
  170. location.time = utc; /* parsed from RMC */
  171. }
  172. /* Speed samples are only trusted from a valid, reasonably accurate fix:
  173. * a single sample from a poor fix (e.g. HDOP 16 with only a few satellites)
  174. * would otherwise distort the whole-session average by itself. */
  175. #define SPEED_SAMPLE_HDOP_MAX_X100 500 /* reject samples with HDOP > 5.00 */
  176. #define SPEED_SAMPLE_CAP_MULT 3 /* also cap any single sample at this multiple of the running average */
  177. static void gp_vtg_parse(const char *str) {
  178. const char *p;
  179. double speed;
  180. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  181. unsigned char fix_trustworthy = (FLAGS & F_GPSOK) && System.fix_type != FIX_TYPE_UNKNOWN
  182. && System.hdop_x100 <= SPEED_SAMPLE_HDOP_MAX_X100;
  183. p = gp_col(str, 9);
  184. if (!p || *p == 'N') /* Not valid, or field missing on this receiver's NMEA dialect */
  185. return;
  186. p = gp_col(str, 7); /* speed in km/h */
  187. if (!p)
  188. return;
  189. xatof(&p, &speed);
  190. System.speed = speed+0.5;
  191. /* Average speed: fixed-point accumulation of instantaneous samples,
  192. * excluding time spent paused, instead of dividing distance by time. */
  193. if (!paused && fix_trustworthy) {
  194. unsigned long int sample_x100 = (unsigned long int)(speed*100.0 + 0.5);
  195. if (System.speed_sample_count) {
  196. unsigned long int avg_x100 = System.speed_accum_x100 / System.speed_sample_count;
  197. unsigned long int cap_x100 = avg_x100 * SPEED_SAMPLE_CAP_MULT;
  198. if (cap_x100 && sample_x100 > cap_x100)
  199. sample_x100 = cap_x100;
  200. }
  201. System.speed_accum_x100 += sample_x100;
  202. System.speed_sample_count++;
  203. }
  204. }
  205. static void gp_gsa_parse(const char *str) {
  206. const char *p;
  207. double hdop;
  208. p = gp_col(str, 2); /* fix type: 1 no fix, 2 2D, 3 3D */
  209. if (p && *p == '2')
  210. System.fix_type = FIX_TYPE_2D;
  211. else if (p && *p == '3')
  212. System.fix_type = FIX_TYPE_3D;
  213. else
  214. System.fix_type = FIX_TYPE_UNKNOWN;
  215. p = gp_col(str, 16); /* HDOP */
  216. if (p && *p) {
  217. xatof(&p, &hdop);
  218. System.hdop_x100 = (unsigned int)(hdop*100.0 + 0.5);
  219. }
  220. }
  221. /*$PMTK355*31<CR><LF>
  222. Return $PMTK001,355,3,1,0,0*2E “$PMTK001,355,3,GLON_Enable,BEIDOU_Enable,GALILEO_Enable”
  223. The GLONASS search mode is enabled. */
  224. static void pmtk001_parse(const char *str) {
  225. const char *p;
  226. /* check validity */
  227. p = gp_col(str, 1);
  228. if (strcmp_P(p, PSTR("355"))) /* not the PMTK355 reply */
  229. return;
  230. p = gp_col(str, 2);
  231. if (*p == '0' || *p == '1') { /* invalid / unsupported */
  232. System.gps_only = 1;
  233. xputs_P(PSTR("GPS only\r\n"));
  234. } else {
  235. System.gps_only = 0;
  236. xputs_P(PSTR("Multi GNSS\r\n"));
  237. }
  238. gps_initialize();
  239. }
  240. unsigned char nmea_checksum(const char *str) {
  241. unsigned char cs = 0;
  242. while (*str && *str != '*') {
  243. cs ^= *str++; /* NMEA checksum is quite primitive, but still should catch simple bitstream errors or truncated lines */
  244. }
  245. return cs;
  246. }
  247. time_t gps_parse(const char *str) { /* Get all required data from NMEA sentences */
  248. signed int len = strlen(str)-2; /* remove final \r\n */
  249. const char *checksum;
  250. unsigned char calc_checksum, inc_checksum;
  251. signed int i;
  252. char c;
  253. if (len < 4)
  254. return 0; /* each message must contain $.*xx where . is one or more actual data characters and xx is the checksum */
  255. for (i = len-1; i && i >= (len-2); i--) { /* find checksum */
  256. if (str[i] == '*')
  257. break;
  258. }
  259. checksum = str+i+1; /* skip * character */
  260. inc_checksum = 0; /* parse hex string */
  261. while (*checksum && isalnum(*checksum)) {
  262. inc_checksum *= 16;
  263. c = *checksum++;
  264. if (c >= '0' && c <= '9') {
  265. inc_checksum += c - '0';
  266. } else if (c >= 'a' && c <= 'f') {
  267. inc_checksum += c - 'a' + 10;
  268. } else if (c >= 'A' && c <= 'F') {
  269. inc_checksum += c - 'A' + 10;
  270. } else {
  271. xputs_P(PSTR("Invalid NMEA: malformed checksum\r\n"));
  272. return 0; /* invalid checksum character */
  273. }
  274. }
  275. str++; /* drop initial $ */
  276. calc_checksum = nmea_checksum(str);
  277. if (inc_checksum != calc_checksum) {
  278. xputs_P(PSTR("Invalid NMEA checksum received\r\n"));
  279. return 0;
  280. }
  281. if (!gp_comp(str, PSTR("GPRMC")) || !gp_comp(str, PSTR("GNRMC")) || !gp_comp(str, PSTR("BDRMC")) || !gp_comp(str, PSTR("GARMC"))) {
  282. return gp_rmc_parse(str);
  283. }
  284. if (!gp_comp(str, PSTR("GPGGA")) || !gp_comp(str, PSTR("GNGGA")) || !gp_comp(str, PSTR("BDGGA")) || !gp_comp(str, PSTR("GAGGA"))) {
  285. gp_gga_parse(str);
  286. return 0;
  287. }
  288. if (!gp_comp(str, PSTR("GPVTG")) || !gp_comp(str, PSTR("GNVTG"))) {
  289. gp_vtg_parse(str);
  290. return 0;
  291. }
  292. if (!gp_comp(str, PSTR("GPGSA")) || !gp_comp(str, PSTR("GNGSA")) || !gp_comp(str, PSTR("BDGSA")) || !gp_comp(str, PSTR("GAGSA"))) {
  293. gp_gsa_parse(str);
  294. return 0;
  295. }
  296. if (!System.gps_initialized && !gp_comp(str, PSTR("PMTK011"))) {
  297. gps_initialize();
  298. return 0;
  299. }
  300. if (!gp_comp(str, PSTR("PMTK001"))) {
  301. pmtk001_parse(str);
  302. return 0;
  303. }
  304. if (!gp_comp(str, PSTR("PMTK"))) {
  305. /* Any other PMTK reply (e.g. PMTK705, the firmware release query
  306. * response) - log it verbatim, no need for a dedicated parser. */
  307. log_prefix();
  308. xputs_P(PSTR("RX "));
  309. xputs(str);
  310. return 0;
  311. }
  312. return 0;
  313. }
  314. void uart0_put_wrap(int c) {
  315. uart0_put((char)c);
  316. }
  317. void gps_initialize(void) {
  318. /*
  319. * PMTK355: query gnss search mode (will fail if only GPS is supported)
  320. * PMTK353: set gnss search mode (GPS/Galileo/Glonass/Beidou)
  321. * PMTK313: enable SBAS
  322. * PMTK605: query firmware release; the reply (PMTK705) is logged
  323. * verbatim by the generic PMTK passthrough in gps_parse()
  324. */
  325. switch (System.gps_initialized) {
  326. case GPS_INIT_NOT_INITIALIZED:
  327. xfprintf(uart0_put_wrap, PSTR("$PMTK355*31\r\n"));
  328. xputs_P(PSTR("GNSS mode query sent\r\n"));
  329. xfprintf(uart0_put_wrap, PSTR("$PMTK605*31\r\n"));
  330. System.gps_initialized = GPS_INIT_QUERY_SENT;
  331. break;
  332. case GPS_INIT_QUERY_SENT:
  333. if (get_flag(CONFFLAG_ENABLE_SBAS)) {
  334. xfprintf(uart0_put_wrap, PSTR("$PMTK313,1*2E\r\n"));
  335. xputs_P(PSTR("SBAS enable sent\r\n"));
  336. } else {
  337. xfprintf(uart0_put_wrap, PSTR("$PMTK313,0*2F\r\n"));
  338. xputs_P(PSTR("SBAS disable sent\r\n"));
  339. }
  340. if (!System.gps_only) {
  341. switch (System.conf.gnss_mode) {
  342. default:
  343. case GNSS_MODE_GPS_GLONASS_GALILEO:
  344. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,1,1,0,0*2A\r\n"));
  345. break;
  346. case GNSS_MODE_GPS:
  347. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,0*2A\r\n"));
  348. break;
  349. case GNSS_MODE_GPS_GALILEO:
  350. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,1,0,0*2B\r\n"));
  351. break;
  352. case GNSS_MODE_GALILEO:
  353. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,1,0,0*2A\r\n"));
  354. break;
  355. case GNSS_MODE_GPS_BEIDOU:
  356. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,1*2B\r\n"));
  357. break;
  358. case GNSS_MODE_BEIDOU:
  359. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,0,0,1*2A\r\n"));
  360. break;
  361. }
  362. xputs_P(PSTR("GNSS mode setting sent\r\n"));
  363. } else {
  364. System.conf.gnss_mode = GNSS_MODE_GPS;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. void check_min_sat_limit(void) {
  372. 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)) {
  373. /* by geometry, max visible number is 6..12 */
  374. System.conf.min_sats = 6;
  375. }
  376. }