nmea.c 7.7 KB

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