nmea_parser.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <math.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "nmea_parser.h"
  14. /**
  15. * @brief NMEA Parser runtime buffer size
  16. *
  17. */
  18. #define NMEA_PARSER_RUNTIME_BUFFER_SIZE (CONFIG_NMEA_PARSER_RING_BUFFER_SIZE / 2)
  19. #define NMEA_MAX_STATEMENT_ITEM_LENGTH (16)
  20. #define NMEA_EVENT_LOOP_QUEUE_SIZE (16)
  21. /**
  22. * @brief Define of NMEA Parser Event base
  23. *
  24. */
  25. ESP_EVENT_DEFINE_BASE(ESP_NMEA_EVENT);
  26. static const char *GPS_TAG = "nmea_parser";
  27. /**
  28. * @brief GPS parser library runtime structure
  29. *
  30. */
  31. typedef struct {
  32. uint8_t item_pos; /*!< Current position in item */
  33. uint8_t item_num; /*!< Current item number */
  34. uint8_t asterisk; /*!< Asterisk detected flag */
  35. uint8_t crc; /*!< Calculated CRC value */
  36. uint8_t parsed_statement; /*!< OR'd of statements that have been parsed */
  37. uint8_t sat_num; /*!< Satellite number */
  38. uint8_t sat_count; /*!< Satellite count */
  39. uint8_t cur_statement; /*!< Current statement ID */
  40. uint32_t all_statements; /*!< All statements mask */
  41. char item_str[NMEA_MAX_STATEMENT_ITEM_LENGTH]; /*!< Current item */
  42. gps_t parent; /*!< Parent class */
  43. uart_port_t uart_port; /*!< Uart port number */
  44. uint8_t *buffer; /*!< Runtime buffer */
  45. esp_event_loop_handle_t event_loop_hdl; /*!< Event loop handle */
  46. TaskHandle_t tsk_hdl; /*!< NMEA Parser task handle */
  47. QueueHandle_t event_queue; /*!< UART event queue handle */
  48. } esp_gps_t;
  49. /**
  50. * @brief parse latitude or longitude
  51. * format of latitude in NMEA is ddmm.sss and longitude is dddmm.sss
  52. * @param esp_gps esp_gps_t type object
  53. * @return float Latitude or Longitude value (unit: degree)
  54. */
  55. static float parse_lat_long(esp_gps_t *esp_gps)
  56. {
  57. float ll = strtof(esp_gps->item_str, NULL);
  58. int deg = ((int)ll) / 100;
  59. float min = ll - (deg * 100);
  60. ll = deg + min / 60.0f;
  61. return ll;
  62. }
  63. /**
  64. * @brief Converter two continuous numeric character into a uint8_t number
  65. *
  66. * @param digit_char numeric character
  67. * @return uint8_t result of converting
  68. */
  69. static inline uint8_t convert_two_digit2number(const char *digit_char)
  70. {
  71. return 10 * (digit_char[0] - '0') + (digit_char[1] - '0');
  72. }
  73. /**
  74. * @brief Parse UTC time in GPS statements
  75. *
  76. * @param esp_gps esp_gps_t type object
  77. */
  78. static void parse_utc_time(esp_gps_t *esp_gps)
  79. {
  80. esp_gps->parent.tim.hour = convert_two_digit2number(esp_gps->item_str + 0);
  81. esp_gps->parent.tim.minute = convert_two_digit2number(esp_gps->item_str + 2);
  82. esp_gps->parent.tim.second = convert_two_digit2number(esp_gps->item_str + 4);
  83. if (esp_gps->item_str[6] == '.') {
  84. uint16_t tmp = 0;
  85. uint8_t i = 7;
  86. while (esp_gps->item_str[i]) {
  87. tmp = 10 * tmp + esp_gps->item_str[i] - '0';
  88. i++;
  89. }
  90. esp_gps->parent.tim.thousand = tmp;
  91. }
  92. }
  93. #if CONFIG_NMEA_STATEMENT_GGA
  94. /**
  95. * @brief Parse GGA statements
  96. *
  97. * @param esp_gps esp_gps_t type object
  98. */
  99. static void parse_gga(esp_gps_t *esp_gps)
  100. {
  101. /* Process GGA statement */
  102. switch (esp_gps->item_num) {
  103. case 1: /* Process UTC time */
  104. parse_utc_time(esp_gps);
  105. break;
  106. case 2: /* Latitude */
  107. esp_gps->parent.latitude = parse_lat_long(esp_gps);
  108. break;
  109. case 3: /* Latitude north(1)/south(-1) information */
  110. if (esp_gps->item_str[0] == 'S' || esp_gps->item_str[0] == 's') {
  111. esp_gps->parent.latitude *= -1;
  112. }
  113. break;
  114. case 4: /* Longitude */
  115. esp_gps->parent.longitude = parse_lat_long(esp_gps);
  116. break;
  117. case 5: /* Longitude east(1)/west(-1) information */
  118. if (esp_gps->item_str[0] == 'W' || esp_gps->item_str[0] == 'w') {
  119. esp_gps->parent.longitude *= -1;
  120. }
  121. break;
  122. case 6: /* Fix status */
  123. esp_gps->parent.fix = (gps_fix_t)strtol(esp_gps->item_str, NULL, 10);
  124. break;
  125. case 7: /* Satellites in use */
  126. esp_gps->parent.sats_in_use = (uint8_t)strtol(esp_gps->item_str, NULL, 10);
  127. break;
  128. case 8: /* HDOP */
  129. esp_gps->parent.dop_h = strtof(esp_gps->item_str, NULL);
  130. break;
  131. case 9: /* Altitude */
  132. esp_gps->parent.altitude = strtof(esp_gps->item_str, NULL);
  133. break;
  134. case 11: /* Altitude above ellipsoid */
  135. esp_gps->parent.altitude += strtof(esp_gps->item_str, NULL);
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. #endif
  142. #if CONFIG_NMEA_STATEMENT_GSA
  143. /**
  144. * @brief Parse GSA statements
  145. *
  146. * @param esp_gps esp_gps_t type object
  147. */
  148. static void parse_gsa(esp_gps_t *esp_gps)
  149. {
  150. /* Process GSA statement */
  151. switch (esp_gps->item_num) {
  152. case 2: /* Process fix mode */
  153. esp_gps->parent.fix_mode = (gps_fix_mode_t)strtol(esp_gps->item_str, NULL, 10);
  154. break;
  155. case 15: /* Process PDOP */
  156. esp_gps->parent.dop_p = strtof(esp_gps->item_str, NULL);
  157. break;
  158. case 16: /* Process HDOP */
  159. esp_gps->parent.dop_h = strtof(esp_gps->item_str, NULL);
  160. break;
  161. case 17: /* Process VDOP */
  162. esp_gps->parent.dop_v = strtof(esp_gps->item_str, NULL);
  163. break;
  164. default:
  165. /* Parse satellite IDs */
  166. if (esp_gps->item_num >= 3 && esp_gps->item_num <= 14) {
  167. esp_gps->parent.sats_id_in_use[esp_gps->item_num - 3] = (uint8_t)strtol(esp_gps->item_str, NULL, 10);
  168. }
  169. break;
  170. }
  171. }
  172. #endif
  173. #if CONFIG_NMEA_STATEMENT_GSV
  174. /**
  175. * @brief Parse GSV statements
  176. *
  177. * @param esp_gps esp_gps_t type object
  178. */
  179. static void parse_gsv(esp_gps_t *esp_gps)
  180. {
  181. /* Process GSV statement */
  182. switch (esp_gps->item_num) {
  183. case 1: /* total GSV numbers */
  184. esp_gps->sat_count = (uint8_t)strtol(esp_gps->item_str, NULL, 10);
  185. break;
  186. case 2: /* Current GSV statement number */
  187. esp_gps->sat_num = (uint8_t)strtol(esp_gps->item_str, NULL, 10);
  188. break;
  189. case 3: /* Process satellites in view */
  190. esp_gps->parent.sats_in_view = (uint8_t)strtol(esp_gps->item_str, NULL, 10);
  191. break;
  192. default:
  193. if (esp_gps->item_num >= 4 && esp_gps->item_num <= 19) {
  194. uint8_t item_num = esp_gps->item_num - 4; /* Normalize item number from 4-19 to 0-15 */
  195. uint8_t index;
  196. uint32_t value;
  197. index = 4 * (esp_gps->sat_num - 1) + item_num / 4; /* Get array index */
  198. if (index < GPS_MAX_SATELLITES_IN_VIEW) {
  199. value = strtol(esp_gps->item_str, NULL, 10);
  200. switch (item_num % 4) {
  201. case 0:
  202. esp_gps->parent.sats_desc_in_view[index].num = (uint8_t)value;
  203. break;
  204. case 1:
  205. esp_gps->parent.sats_desc_in_view[index].elevation = (uint8_t)value;
  206. break;
  207. case 2:
  208. esp_gps->parent.sats_desc_in_view[index].azimuth = (uint16_t)value;
  209. break;
  210. case 3:
  211. esp_gps->parent.sats_desc_in_view[index].snr = (uint8_t)value;
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. }
  218. break;
  219. }
  220. }
  221. #endif
  222. #if CONFIG_NMEA_STATEMENT_RMC
  223. /**
  224. * @brief Parse RMC statements
  225. *
  226. * @param esp_gps esp_gps_t type object
  227. */
  228. static void parse_rmc(esp_gps_t *esp_gps)
  229. {
  230. /* Process GPRMC statement */
  231. switch (esp_gps->item_num) {
  232. case 1:/* Process UTC time */
  233. parse_utc_time(esp_gps);
  234. break;
  235. case 2: /* Process valid status */
  236. esp_gps->parent.valid = (esp_gps->item_str[0] == 'A');
  237. break;
  238. case 3:/* Latitude */
  239. esp_gps->parent.latitude = parse_lat_long(esp_gps);
  240. break;
  241. case 4: /* Latitude north(1)/south(-1) information */
  242. if (esp_gps->item_str[0] == 'S' || esp_gps->item_str[0] == 's') {
  243. esp_gps->parent.latitude *= -1;
  244. }
  245. break;
  246. case 5: /* Longitude */
  247. esp_gps->parent.longitude = parse_lat_long(esp_gps);
  248. break;
  249. case 6: /* Longitude east(1)/west(-1) information */
  250. if (esp_gps->item_str[0] == 'W' || esp_gps->item_str[0] == 'w') {
  251. esp_gps->parent.longitude *= -1;
  252. }
  253. break;
  254. case 7: /* Process ground speed in unit m/s */
  255. esp_gps->parent.speed = strtof(esp_gps->item_str, NULL) * 1.852;
  256. break;
  257. case 8: /* Process true course over ground */
  258. esp_gps->parent.cog = strtof(esp_gps->item_str, NULL);
  259. break;
  260. case 9: /* Process date */
  261. esp_gps->parent.date.day = convert_two_digit2number(esp_gps->item_str + 0);
  262. esp_gps->parent.date.month = convert_two_digit2number(esp_gps->item_str + 2);
  263. esp_gps->parent.date.year = convert_two_digit2number(esp_gps->item_str + 4);
  264. break;
  265. case 10: /* Process magnetic variation */
  266. esp_gps->parent.variation = strtof(esp_gps->item_str, NULL);
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. #endif
  273. #if CONFIG_NMEA_STATEMENT_GLL
  274. /**
  275. * @brief Parse GLL statements
  276. *
  277. * @param esp_gps esp_gps_t type object
  278. */
  279. static void parse_gll(esp_gps_t *esp_gps)
  280. {
  281. /* Process GPGLL statement */
  282. switch (esp_gps->item_num) {
  283. case 1:/* Latitude */
  284. esp_gps->parent.latitude = parse_lat_long(esp_gps);
  285. break;
  286. case 2: /* Latitude north(1)/south(-1) information */
  287. if (esp_gps->item_str[0] == 'S' || esp_gps->item_str[0] == 's') {
  288. esp_gps->parent.latitude *= -1;
  289. }
  290. break;
  291. case 3: /* Longitude */
  292. esp_gps->parent.longitude = parse_lat_long(esp_gps);
  293. break;
  294. case 4: /* Longitude east(1)/west(-1) information */
  295. if (esp_gps->item_str[0] == 'W' || esp_gps->item_str[0] == 'w') {
  296. esp_gps->parent.longitude *= -1;
  297. }
  298. break;
  299. case 5:/* Process UTC time */
  300. parse_utc_time(esp_gps);
  301. break;
  302. case 6: /* Process valid status */
  303. esp_gps->parent.valid = (esp_gps->item_str[0] == 'A');
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. #endif
  310. #if CONFIG_NMEA_STATEMENT_VTG
  311. /**
  312. * @brief Parse VTG statements
  313. *
  314. * @param esp_gps esp_gps_t type object
  315. */
  316. static void parse_vtg(esp_gps_t *esp_gps)
  317. {
  318. /* Process GPVGT statement */
  319. switch (esp_gps->item_num) {
  320. case 1: /* Process true course over ground */
  321. esp_gps->parent.cog = strtof(esp_gps->item_str, NULL);
  322. break;
  323. case 3:/* Process magnetic variation */
  324. esp_gps->parent.variation = strtof(esp_gps->item_str, NULL);
  325. break;
  326. case 5:/* Process ground speed in unit m/s */
  327. esp_gps->parent.speed = strtof(esp_gps->item_str, NULL) * 1.852;//knots to m/s
  328. break;
  329. case 7:/* Process ground speed in unit m/s */
  330. esp_gps->parent.speed = strtof(esp_gps->item_str, NULL) / 3.6;//km/h to m/s
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. #endif
  337. /**
  338. * @brief Parse received item
  339. *
  340. * @param esp_gps esp_gps_t type object
  341. * @return esp_err_t ESP_OK on success, ESP_FAIL on error
  342. */
  343. static esp_err_t parse_item(esp_gps_t *esp_gps)
  344. {
  345. esp_err_t err = ESP_OK;
  346. /* start of a statement */
  347. if (esp_gps->item_num == 0 && esp_gps->item_str[0] == '$') {
  348. if (0) {
  349. }
  350. #if CONFIG_NMEA_STATEMENT_GGA
  351. else if (strstr(esp_gps->item_str, "GGA")) {
  352. esp_gps->cur_statement = STATEMENT_GGA;
  353. }
  354. #endif
  355. #if CONFIG_NMEA_STATEMENT_GSA
  356. else if (strstr(esp_gps->item_str, "GSA")) {
  357. esp_gps->cur_statement = STATEMENT_GSA;
  358. }
  359. #endif
  360. #if CONFIG_NMEA_STATEMENT_RMC
  361. else if (strstr(esp_gps->item_str, "RMC")) {
  362. esp_gps->cur_statement = STATEMENT_RMC;
  363. }
  364. #endif
  365. #if CONFIG_NMEA_STATEMENT_GSV
  366. else if (strstr(esp_gps->item_str, "GSV")) {
  367. esp_gps->cur_statement = STATEMENT_GSV;
  368. }
  369. #endif
  370. #if CONFIG_NMEA_STATEMENT_GLL
  371. else if (strstr(esp_gps->item_str, "GLL")) {
  372. esp_gps->cur_statement = STATEMENT_GLL;
  373. }
  374. #endif
  375. #if CONFIG_NMEA_STATEMENT_VTG
  376. else if (strstr(esp_gps->item_str, "VTG")) {
  377. esp_gps->cur_statement = STATEMENT_VTG;
  378. }
  379. #endif
  380. else {
  381. esp_gps->cur_statement = STATEMENT_UNKNOWN;
  382. }
  383. goto out;
  384. }
  385. /* Parse each item, depend on the type of the statement */
  386. if (esp_gps->cur_statement == STATEMENT_UNKNOWN) {
  387. goto out;
  388. }
  389. #if CONFIG_NMEA_STATEMENT_GGA
  390. else if (esp_gps->cur_statement == STATEMENT_GGA) {
  391. parse_gga(esp_gps);
  392. }
  393. #endif
  394. #if CONFIG_NMEA_STATEMENT_GSA
  395. else if (esp_gps->cur_statement == STATEMENT_GSA) {
  396. parse_gsa(esp_gps);
  397. }
  398. #endif
  399. #if CONFIG_NMEA_STATEMENT_GSV
  400. else if (esp_gps->cur_statement == STATEMENT_GSV) {
  401. parse_gsv(esp_gps);
  402. }
  403. #endif
  404. #if CONFIG_NMEA_STATEMENT_RMC
  405. else if (esp_gps->cur_statement == STATEMENT_RMC) {
  406. parse_rmc(esp_gps);
  407. }
  408. #endif
  409. #if CONFIG_NMEA_STATEMENT_GLL
  410. else if (esp_gps->cur_statement == STATEMENT_GLL) {
  411. parse_gll(esp_gps);
  412. }
  413. #endif
  414. #if CONFIG_NMEA_STATEMENT_VTG
  415. else if (esp_gps->cur_statement == STATEMENT_VTG) {
  416. parse_vtg(esp_gps);
  417. }
  418. #endif
  419. else {
  420. err = ESP_FAIL;
  421. }
  422. out:
  423. return err;
  424. }
  425. /**
  426. * @brief Parse NMEA statements from GPS receiver
  427. *
  428. * @param esp_gps esp_gps_t type object
  429. * @param len number of bytes to decode
  430. * @return esp_err_t ESP_OK on success, ESP_FAIL on error
  431. */
  432. static esp_err_t gps_decode(esp_gps_t *esp_gps, size_t len)
  433. {
  434. const uint8_t *d = esp_gps->buffer;
  435. while (*d) {
  436. /* Start of a statement */
  437. if (*d == '$') {
  438. /* Reset runtime information */
  439. esp_gps->asterisk = 0;
  440. esp_gps->item_num = 0;
  441. esp_gps->item_pos = 0;
  442. esp_gps->cur_statement = 0;
  443. esp_gps->crc = 0;
  444. esp_gps->sat_count = 0;
  445. esp_gps->sat_num = 0;
  446. /* Add character to item */
  447. esp_gps->item_str[esp_gps->item_pos++] = *d;
  448. esp_gps->item_str[esp_gps->item_pos] = '\0';
  449. }
  450. /* Detect item separator character */
  451. else if (*d == ',') {
  452. /* Parse current item */
  453. parse_item(esp_gps);
  454. /* Add character to CRC computation */
  455. esp_gps->crc ^= (uint8_t)(*d);
  456. /* Start with next item */
  457. esp_gps->item_pos = 0;
  458. esp_gps->item_str[0] = '\0';
  459. esp_gps->item_num++;
  460. }
  461. /* End of CRC computation */
  462. else if (*d == '*') {
  463. /* Parse current item */
  464. parse_item(esp_gps);
  465. /* Asterisk detected */
  466. esp_gps->asterisk = 1;
  467. /* Start with next item */
  468. esp_gps->item_pos = 0;
  469. esp_gps->item_str[0] = '\0';
  470. esp_gps->item_num++;
  471. }
  472. /* End of statement */
  473. else if (*d == '\r') {
  474. /* Convert received CRC from string (hex) to number */
  475. uint8_t crc = (uint8_t)strtol(esp_gps->item_str, NULL, 16);
  476. /* CRC passed */
  477. if (esp_gps->crc == crc) {
  478. switch (esp_gps->cur_statement) {
  479. #if CONFIG_NMEA_STATEMENT_GGA
  480. case STATEMENT_GGA:
  481. esp_gps->parsed_statement |= 1 << STATEMENT_GGA;
  482. break;
  483. #endif
  484. #if CONFIG_NMEA_STATEMENT_GSA
  485. case STATEMENT_GSA:
  486. esp_gps->parsed_statement |= 1 << STATEMENT_GSA;
  487. break;
  488. #endif
  489. #if CONFIG_NMEA_STATEMENT_RMC
  490. case STATEMENT_RMC:
  491. esp_gps->parsed_statement |= 1 << STATEMENT_RMC;
  492. break;
  493. #endif
  494. #if CONFIG_NMEA_STATEMENT_GSV
  495. case STATEMENT_GSV:
  496. if (esp_gps->sat_num == esp_gps->sat_count) {
  497. esp_gps->parsed_statement |= 1 << STATEMENT_GSV;
  498. }
  499. break;
  500. #endif
  501. #if CONFIG_NMEA_STATEMENT_GLL
  502. case STATEMENT_GLL:
  503. esp_gps->parsed_statement |= 1 << STATEMENT_GLL;
  504. break;
  505. #endif
  506. #if CONFIG_NMEA_STATEMENT_VTG
  507. case STATEMENT_VTG:
  508. esp_gps->parsed_statement |= 1 << STATEMENT_VTG;
  509. break;
  510. #endif
  511. default:
  512. break;
  513. }
  514. /* Check if all statements have been parsed */
  515. if (((esp_gps->parsed_statement) & esp_gps->all_statements) == esp_gps->all_statements) {
  516. esp_gps->parsed_statement = 0;
  517. /* Send signal to notify that GPS information has been updated */
  518. esp_event_post_to(esp_gps->event_loop_hdl, ESP_NMEA_EVENT, GPS_UPDATE,
  519. &(esp_gps->parent), sizeof(gps_t), 100 / portTICK_PERIOD_MS);
  520. }
  521. } else {
  522. ESP_LOGD(GPS_TAG, "CRC Error for statement:%s", esp_gps->buffer);
  523. }
  524. if (esp_gps->cur_statement == STATEMENT_UNKNOWN) {
  525. /* Send signal to notify that one unknown statement has been met */
  526. esp_event_post_to(esp_gps->event_loop_hdl, ESP_NMEA_EVENT, GPS_UNKNOWN,
  527. esp_gps->buffer, len, 100 / portTICK_PERIOD_MS);
  528. }
  529. }
  530. /* Other non-space character */
  531. else {
  532. if (!(esp_gps->asterisk)) {
  533. /* Add to CRC */
  534. esp_gps->crc ^= (uint8_t)(*d);
  535. }
  536. /* Add character to item */
  537. esp_gps->item_str[esp_gps->item_pos++] = *d;
  538. esp_gps->item_str[esp_gps->item_pos] = '\0';
  539. }
  540. /* Process next character */
  541. d++;
  542. }
  543. return ESP_OK;
  544. }
  545. /**
  546. * @brief Handle when a pattern has been detected by uart
  547. *
  548. * @param esp_gps esp_gps_t type object
  549. */
  550. static void esp_handle_uart_pattern(esp_gps_t *esp_gps)
  551. {
  552. int pos = uart_pattern_pop_pos(esp_gps->uart_port);
  553. if (pos != -1) {
  554. /* read one line(include '\n') */
  555. int read_len = uart_read_bytes(esp_gps->uart_port, esp_gps->buffer, pos + 1, 100 / portTICK_PERIOD_MS);
  556. /* make sure the line is a standard string */
  557. esp_gps->buffer[read_len] = '\0';
  558. /* Send new line to handle */
  559. if (gps_decode(esp_gps, read_len + 1) != ESP_OK) {
  560. ESP_LOGW(GPS_TAG, "GPS decode line failed");
  561. }
  562. } else {
  563. ESP_LOGW(GPS_TAG, "Pattern Queue Size too small");
  564. uart_flush_input(esp_gps->uart_port);
  565. }
  566. }
  567. /**
  568. * @brief NMEA Parser Task Entry
  569. *
  570. * @param arg argument
  571. */
  572. static void nmea_parser_task_entry(void *arg)
  573. {
  574. esp_gps_t *esp_gps = (esp_gps_t *)arg;
  575. uart_event_t event;
  576. while (1) {
  577. if (xQueueReceive(esp_gps->event_queue, &event, pdMS_TO_TICKS(200))) {
  578. switch (event.type) {
  579. case UART_DATA:
  580. break;
  581. case UART_FIFO_OVF:
  582. ESP_LOGW(GPS_TAG, "HW FIFO Overflow");
  583. uart_flush(esp_gps->uart_port);
  584. xQueueReset(esp_gps->event_queue);
  585. break;
  586. case UART_BUFFER_FULL:
  587. ESP_LOGW(GPS_TAG, "Ring Buffer Full");
  588. uart_flush(esp_gps->uart_port);
  589. xQueueReset(esp_gps->event_queue);
  590. break;
  591. case UART_BREAK:
  592. ESP_LOGW(GPS_TAG, "Rx Break");
  593. break;
  594. case UART_PARITY_ERR:
  595. ESP_LOGE(GPS_TAG, "Parity Error");
  596. break;
  597. case UART_FRAME_ERR:
  598. ESP_LOGE(GPS_TAG, "Frame Error");
  599. break;
  600. case UART_PATTERN_DET:
  601. esp_handle_uart_pattern(esp_gps);
  602. break;
  603. default:
  604. ESP_LOGW(GPS_TAG, "unknown uart event type: %d", event.type);
  605. break;
  606. }
  607. }
  608. /* Drive the event loop */
  609. esp_event_loop_run(esp_gps->event_loop_hdl, pdMS_TO_TICKS(50));
  610. }
  611. vTaskDelete(NULL);
  612. }
  613. /**
  614. * @brief Init NMEA Parser
  615. *
  616. * @param config Configuration of NMEA Parser
  617. * @return nmea_parser_handle_t handle of nmea_parser
  618. */
  619. nmea_parser_handle_t nmea_parser_init(const nmea_parser_config_t *config)
  620. {
  621. esp_gps_t *esp_gps = calloc(1, sizeof(esp_gps_t));
  622. if (!esp_gps) {
  623. ESP_LOGE(GPS_TAG, "calloc memory for esp_fps failed");
  624. goto err_gps;
  625. }
  626. esp_gps->buffer = calloc(1, NMEA_PARSER_RUNTIME_BUFFER_SIZE);
  627. if (!esp_gps->buffer) {
  628. ESP_LOGE(GPS_TAG, "calloc memory for runtime buffer failed");
  629. goto err_buffer;
  630. }
  631. #if CONFIG_NMEA_STATEMENT_GSA
  632. esp_gps->all_statements |= (1 << STATEMENT_GSA);
  633. #endif
  634. #if CONFIG_NMEA_STATEMENT_GSV
  635. esp_gps->all_statements |= (1 << STATEMENT_GSV);
  636. #endif
  637. #if CONFIG_NMEA_STATEMENT_GGA
  638. esp_gps->all_statements |= (1 << STATEMENT_GGA);
  639. #endif
  640. #if CONFIG_NMEA_STATEMENT_RMC
  641. esp_gps->all_statements |= (1 << STATEMENT_RMC);
  642. #endif
  643. #if CONFIG_NMEA_STATEMENT_GLL
  644. esp_gps->all_statements |= (1 << STATEMENT_GLL);
  645. #endif
  646. #if CONFIG_NMEA_STATEMENT_VTG
  647. esp_gps->all_statements |= (1 << STATEMENT_VTG);
  648. #endif
  649. /* Set attributes */
  650. esp_gps->uart_port = config->uart.uart_port;
  651. esp_gps->all_statements &= 0xFE;
  652. /* Install UART friver */
  653. uart_config_t uart_config = {
  654. .baud_rate = config->uart.baud_rate,
  655. .data_bits = config->uart.data_bits,
  656. .parity = config->uart.parity,
  657. .stop_bits = config->uart.stop_bits,
  658. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  659. .source_clk = UART_SCLK_DEFAULT,
  660. };
  661. if (uart_driver_install(esp_gps->uart_port, CONFIG_NMEA_PARSER_RING_BUFFER_SIZE, 0,
  662. config->uart.event_queue_size, &esp_gps->event_queue, 0) != ESP_OK) {
  663. ESP_LOGE(GPS_TAG, "install uart driver failed");
  664. goto err_uart_install;
  665. }
  666. if (uart_param_config(esp_gps->uart_port, &uart_config) != ESP_OK) {
  667. ESP_LOGE(GPS_TAG, "config uart parameter failed");
  668. goto err_uart_config;
  669. }
  670. if (uart_set_pin(esp_gps->uart_port, UART_PIN_NO_CHANGE, config->uart.rx_pin,
  671. UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK) {
  672. ESP_LOGE(GPS_TAG, "config uart gpio failed");
  673. goto err_uart_config;
  674. }
  675. /* Set pattern interrupt, used to detect the end of a line */
  676. uart_enable_pattern_det_baud_intr(esp_gps->uart_port, '\n', 1, 9, 0, 0);
  677. /* Set pattern queue size */
  678. uart_pattern_queue_reset(esp_gps->uart_port, config->uart.event_queue_size);
  679. uart_flush(esp_gps->uart_port);
  680. /* Create Event loop */
  681. esp_event_loop_args_t loop_args = {
  682. .queue_size = NMEA_EVENT_LOOP_QUEUE_SIZE,
  683. .task_name = NULL
  684. };
  685. if (esp_event_loop_create(&loop_args, &esp_gps->event_loop_hdl) != ESP_OK) {
  686. ESP_LOGE(GPS_TAG, "create event loop faild");
  687. goto err_eloop;
  688. }
  689. /* Create NMEA Parser task */
  690. BaseType_t err = xTaskCreate(
  691. nmea_parser_task_entry,
  692. "nmea_parser",
  693. CONFIG_NMEA_PARSER_TASK_STACK_SIZE,
  694. esp_gps,
  695. CONFIG_NMEA_PARSER_TASK_PRIORITY,
  696. &esp_gps->tsk_hdl);
  697. if (err != pdTRUE) {
  698. ESP_LOGE(GPS_TAG, "create NMEA Parser task failed");
  699. goto err_task_create;
  700. }
  701. ESP_LOGI(GPS_TAG, "NMEA Parser init OK");
  702. return esp_gps;
  703. /*Error Handling*/
  704. err_task_create:
  705. esp_event_loop_delete(esp_gps->event_loop_hdl);
  706. err_eloop:
  707. err_uart_install:
  708. uart_driver_delete(esp_gps->uart_port);
  709. err_uart_config:
  710. err_buffer:
  711. free(esp_gps->buffer);
  712. err_gps:
  713. free(esp_gps);
  714. return NULL;
  715. }
  716. /**
  717. * @brief Deinit NMEA Parser
  718. *
  719. * @param nmea_hdl handle of NMEA parser
  720. * @return esp_err_t ESP_OK on success,ESP_FAIL on error
  721. */
  722. esp_err_t nmea_parser_deinit(nmea_parser_handle_t nmea_hdl)
  723. {
  724. esp_gps_t *esp_gps = (esp_gps_t *)nmea_hdl;
  725. vTaskDelete(esp_gps->tsk_hdl);
  726. esp_event_loop_delete(esp_gps->event_loop_hdl);
  727. esp_err_t err = uart_driver_delete(esp_gps->uart_port);
  728. free(esp_gps->buffer);
  729. free(esp_gps);
  730. return err;
  731. }
  732. /**
  733. * @brief Add user defined handler for NMEA parser
  734. *
  735. * @param nmea_hdl handle of NMEA parser
  736. * @param event_handler user defined event handler
  737. * @param handler_args handler specific arguments
  738. * @return esp_err_t
  739. * - ESP_OK: Success
  740. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  741. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  742. * - Others: Fail
  743. */
  744. esp_err_t nmea_parser_add_handler(nmea_parser_handle_t nmea_hdl, esp_event_handler_t event_handler, void *handler_args)
  745. {
  746. esp_gps_t *esp_gps = (esp_gps_t *)nmea_hdl;
  747. return esp_event_handler_register_with(esp_gps->event_loop_hdl, ESP_NMEA_EVENT, ESP_EVENT_ANY_ID,
  748. event_handler, handler_args);
  749. }
  750. /**
  751. * @brief Remove user defined handler for NMEA parser
  752. *
  753. * @param nmea_hdl handle of NMEA parser
  754. * @param event_handler user defined event handler
  755. * @return esp_err_t
  756. * - ESP_OK: Success
  757. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  758. * - Others: Fail
  759. */
  760. esp_err_t nmea_parser_remove_handler(nmea_parser_handle_t nmea_hdl, esp_event_handler_t event_handler)
  761. {
  762. esp_gps_t *esp_gps = (esp_gps_t *)nmea_hdl;
  763. return esp_event_handler_unregister_with(esp_gps->event_loop_hdl, ESP_NMEA_EVENT, ESP_EVENT_ANY_ID, event_handler);
  764. }