nmea.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. static void gp_vtg_parse(const char *str) {
  173. const char *p;
  174. double speed;
  175. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  176. p = gp_col(str, 9);
  177. if (*p == 'N') /* Not valid */
  178. return;
  179. p = gp_col(str, 7); /* speed in km/h */
  180. xatof(&p, &speed);
  181. System.speed = speed+0.5;
  182. /* Average speed: fixed-point accumulation of instantaneous samples,
  183. * excluding time spent paused, instead of dividing distance by time. */
  184. if (!paused) {
  185. System.speed_accum_x100 += (unsigned long int)(speed*100.0 + 0.5);
  186. System.speed_sample_count++;
  187. }
  188. }
  189. static void gp_gsa_parse(const char *str) {
  190. const char *p;
  191. double hdop;
  192. p = gp_col(str, 2); /* fix type: 1 no fix, 2 2D, 3 3D */
  193. if (*p == '2')
  194. System.fix_type = FIX_TYPE_2D;
  195. else if (*p == '3')
  196. System.fix_type = FIX_TYPE_3D;
  197. else
  198. System.fix_type = FIX_TYPE_UNKNOWN;
  199. p = gp_col(str, 16); /* HDOP */
  200. if (p && *p) {
  201. xatof(&p, &hdop);
  202. System.hdop_x100 = (unsigned int)(hdop*100.0 + 0.5);
  203. }
  204. }
  205. /*$PMTK355*31<CR><LF>
  206. Return $PMTK001,355,3,1,0,0*2E “$PMTK001,355,3,GLON_Enable,BEIDOU_Enable,GALILEO_Enable”
  207. The GLONASS search mode is enabled. */
  208. static void pmtk001_parse(const char *str) {
  209. const char *p;
  210. /* check validity */
  211. p = gp_col(str, 1);
  212. if (strcmp_P(p, PSTR("355"))) /* not the PMTK355 reply */
  213. return;
  214. p = gp_col(str, 2);
  215. if (*p == '0' || *p == '1') { /* invalid / unsupported */
  216. System.gps_only = 1;
  217. xputs_P(PSTR("GPS only\r\n"));
  218. } else {
  219. System.gps_only = 0;
  220. xputs_P(PSTR("Multi GNSS\r\n"));
  221. }
  222. gps_initialize();
  223. }
  224. unsigned char nmea_checksum(const char *str) {
  225. unsigned char cs = 0;
  226. while (*str && *str != '*') {
  227. cs ^= *str++; /* NMEA checksum is quite primitive, but still should catch simple bitstream errors or truncated lines */
  228. }
  229. return cs;
  230. }
  231. time_t gps_parse(const char *str) { /* Get all required data from NMEA sentences */
  232. signed int len = strlen(str)-2; /* remove final \r\n */
  233. const char *checksum;
  234. unsigned char calc_checksum, inc_checksum;
  235. signed int i;
  236. char c;
  237. if (len < 4)
  238. return 0; /* each message must contain $.*xx where . is one or more actual data characters and xx is the checksum */
  239. for (i = len-1; i && i >= (len-2); i--) { /* find checksum */
  240. if (str[i] == '*')
  241. break;
  242. }
  243. checksum = str+i+1; /* skip * character */
  244. inc_checksum = 0; /* parse hex string */
  245. while (*checksum && isalnum(*checksum)) {
  246. inc_checksum *= 16;
  247. c = *checksum++;
  248. if (c >= '0' && c <= '9') {
  249. inc_checksum += c - '0';
  250. } else if (c >= 'a' && c <= 'f') {
  251. inc_checksum += c - 'a' + 10;
  252. } else if (c >= 'A' && c <= 'F') {
  253. inc_checksum += c - 'A' + 10;
  254. } else {
  255. xputs_P(PSTR("Invalid NMEA: malformed checksum\r\n"));
  256. return 0; /* invalid checksum character */
  257. }
  258. }
  259. str++; /* drop initial $ */
  260. calc_checksum = nmea_checksum(str);
  261. if (inc_checksum != calc_checksum) {
  262. xputs_P(PSTR("Invalid NMEA checksum received\r\n"));
  263. return 0;
  264. }
  265. if (!gp_comp(str, PSTR("GPRMC")) || !gp_comp(str, PSTR("GNRMC")) || !gp_comp(str, PSTR("BDRMC")) || !gp_comp(str, PSTR("GARMC"))) {
  266. return gp_rmc_parse(str);
  267. }
  268. if (!gp_comp(str, PSTR("GPGGA")) || !gp_comp(str, PSTR("GNGGA")) || !gp_comp(str, PSTR("BDGGA")) || !gp_comp(str, PSTR("GAGGA"))) {
  269. gp_gga_parse(str);
  270. return 0;
  271. }
  272. if (!gp_comp(str, PSTR("GPVTG")) || !gp_comp(str, PSTR("GNVTG"))) {
  273. gp_vtg_parse(str);
  274. return 0;
  275. }
  276. if (!gp_comp(str, PSTR("GPGSA")) || !gp_comp(str, PSTR("GNGSA")) || !gp_comp(str, PSTR("BDGSA")) || !gp_comp(str, PSTR("GAGSA"))) {
  277. gp_gsa_parse(str);
  278. return 0;
  279. }
  280. if (!System.gps_initialized && !gp_comp(str, PSTR("PMTK011"))) {
  281. gps_initialize();
  282. return 0;
  283. }
  284. if (!gp_comp(str, PSTR("PMTK001"))) {
  285. pmtk001_parse(str);
  286. return 0;
  287. }
  288. return 0;
  289. }
  290. void uart0_put_wrap(int c) {
  291. uart0_put((char)c);
  292. }
  293. void gps_initialize(void) {
  294. /*
  295. * PMTK355: query gnss search mode (will fail if only GPS is supported)
  296. * PMTK353: set gnss search mode (GPS/Galileo/Glonass/Beidou)
  297. * PMTK313: enable SBAS
  298. */
  299. switch (System.gps_initialized) {
  300. case GPS_INIT_NOT_INITIALIZED:
  301. xfprintf(uart0_put_wrap, PSTR("$PMTK355*31\r\n"));
  302. xputs_P(PSTR("GNSS mode query sent\r\n"));
  303. System.gps_initialized = GPS_INIT_QUERY_SENT;
  304. break;
  305. case GPS_INIT_QUERY_SENT:
  306. if (get_flag(CONFFLAG_ENABLE_SBAS)) {
  307. xfprintf(uart0_put_wrap, PSTR("$PMTK313,1*2E\r\n"));
  308. xputs_P(PSTR("SBAS enable sent\r\n"));
  309. } else {
  310. xfprintf(uart0_put_wrap, PSTR("$PMTK313,0*2F\r\n"));
  311. xputs_P(PSTR("SBAS disable sent\r\n"));
  312. }
  313. if (!System.gps_only) {
  314. switch (System.conf.gnss_mode) {
  315. default:
  316. case GNSS_MODE_GPS_GLONASS_GALILEO:
  317. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,1,1,0,0*2A\r\n"));
  318. break;
  319. case GNSS_MODE_GPS:
  320. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,0*2A\r\n"));
  321. break;
  322. case GNSS_MODE_GPS_GALILEO:
  323. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,1,0,0*2B\r\n"));
  324. break;
  325. case GNSS_MODE_GALILEO:
  326. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,1,0,0*2A\r\n"));
  327. break;
  328. case GNSS_MODE_GPS_BEIDOU:
  329. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,1*2B\r\n"));
  330. break;
  331. case GNSS_MODE_BEIDOU:
  332. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,0,0,1*2A\r\n"));
  333. break;
  334. }
  335. xputs_P(PSTR("GNSS mode setting sent\r\n"));
  336. } else {
  337. System.conf.gnss_mode = GNSS_MODE_GPS;
  338. }
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. void check_min_sat_limit(void) {
  345. 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)) {
  346. /* by geometry, max visible number is 6..12 */
  347. System.conf.min_sats = 6;
  348. }
  349. }