gps_uart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <string.h>
  2. #include "minmea.h"
  3. #include "gps_uart.h"
  4. typedef enum
  5. {
  6. WorkerEvtStop = (1 << 0),
  7. WorkerEvtRxDone = (1 << 1),
  8. } WorkerEvtFlags;
  9. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  10. static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context)
  11. {
  12. GpsUart* gps_uart = (GpsUart*)context;
  13. if (ev == UartIrqEventRXNE)
  14. {
  15. furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
  16. furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
  17. }
  18. }
  19. static void gps_uart_serial_init(GpsUart* gps_uart)
  20. {
  21. furi_hal_console_disable();
  22. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
  23. furi_hal_uart_set_br(FuriHalUartIdUSART1, GPS_BAUDRATE);
  24. }
  25. static void gps_uart_serial_deinit(GpsUart* gps_uart)
  26. {
  27. UNUSED(gps_uart);
  28. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  29. furi_hal_console_enable();
  30. }
  31. static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line)
  32. {
  33. switch (minmea_sentence_id(line, false))
  34. {
  35. case MINMEA_SENTENCE_RMC:
  36. {
  37. struct minmea_sentence_rmc frame;
  38. if (minmea_parse_rmc(&frame, line))
  39. {
  40. gps_uart->status.valid = frame.valid;
  41. gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
  42. gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
  43. gps_uart->status.speed = minmea_tofloat(&frame.speed);
  44. gps_uart->status.course = minmea_tofloat(&frame.course);
  45. gps_uart->status.time_hours = frame.time.hours;
  46. gps_uart->status.time_minutes = frame.time.minutes;
  47. gps_uart->status.time_seconds = frame.time.seconds;
  48. notification_message_block(gps_uart->notifications, &sequence_blink_green_10);
  49. }
  50. } break;
  51. case MINMEA_SENTENCE_GGA:
  52. {
  53. struct minmea_sentence_gga frame;
  54. if (minmea_parse_gga(&frame, line))
  55. {
  56. gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
  57. gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
  58. gps_uart->status.altitude = minmea_tofloat(&frame.altitude);
  59. gps_uart->status.altitude_units = frame.altitude_units;
  60. gps_uart->status.fix_quality = frame.fix_quality;
  61. gps_uart->status.satellites_tracked = frame.satellites_tracked;
  62. gps_uart->status.time_hours = frame.time.hours;
  63. gps_uart->status.time_minutes = frame.time.minutes;
  64. gps_uart->status.time_seconds = frame.time.seconds;
  65. notification_message_block(gps_uart->notifications, &sequence_blink_magenta_10);
  66. }
  67. } break;
  68. case MINMEA_SENTENCE_GLL:
  69. {
  70. struct minmea_sentence_gll frame;
  71. if (minmea_parse_gll(&frame, line))
  72. {
  73. gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
  74. gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
  75. gps_uart->status.time_hours = frame.time.hours;
  76. gps_uart->status.time_minutes = frame.time.minutes;
  77. gps_uart->status.time_seconds = frame.time.seconds;
  78. notification_message_block(gps_uart->notifications, &sequence_blink_red_10);
  79. }
  80. } break;
  81. default:
  82. break;
  83. }
  84. }
  85. static int32_t gps_uart_worker(void* context)
  86. {
  87. GpsUart* gps_uart = (GpsUart*)context;
  88. gps_uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
  89. size_t rx_offset = 0;
  90. gps_uart_serial_init(gps_uart);
  91. while (1)
  92. {
  93. uint32_t events =
  94. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  95. furi_check((events & FuriFlagError) == 0);
  96. if (events & WorkerEvtStop)
  97. {
  98. break;
  99. }
  100. if (events & WorkerEvtRxDone)
  101. {
  102. size_t len = 0;
  103. do
  104. {
  105. // receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
  106. // the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
  107. len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset,
  108. 0);
  109. if (len > 0)
  110. {
  111. // increase rx_offset by the number of bytes received, and null-terminate rx_buf
  112. rx_offset += len;
  113. gps_uart->rx_buf[rx_offset] = '\0';
  114. // look for strings ending in newlines, starting at the start of rx_buf
  115. char * line_current = (char *)gps_uart->rx_buf;
  116. while (1)
  117. {
  118. // skip null characters
  119. while (*line_current == '\0' && line_current < (char *)gps_uart->rx_buf + rx_offset - 1)
  120. {
  121. line_current++;
  122. }
  123. // find the next newline
  124. char * newline = strchr(line_current, '\n');
  125. if (newline) // newline found
  126. {
  127. // put a null terminator in place of the newline, to delimit the line string
  128. *newline = '\0';
  129. // attempt to parse the line as a NMEA sentence
  130. gps_uart_parse_nmea(gps_uart, line_current);
  131. // move the cursor to the character after the newline
  132. line_current = newline + 1;
  133. }
  134. else // no more newlines found
  135. {
  136. if (line_current > (char *)gps_uart->rx_buf) // at least one line was found
  137. {
  138. // clear parsed lines, and move any leftover bytes to the start of rx_buf
  139. rx_offset = 0;
  140. while (*line_current) // stop when the original rx_offset terminator is reached
  141. {
  142. gps_uart->rx_buf[rx_offset++] = *(line_current++);
  143. }
  144. }
  145. break; // go back to receiving bytes from the serial stream
  146. }
  147. }
  148. }
  149. }
  150. while (len > 0);
  151. }
  152. }
  153. gps_uart_serial_deinit(gps_uart);
  154. furi_stream_buffer_free(gps_uart->rx_stream);
  155. return 0;
  156. }
  157. GpsUart* gps_uart_enable()
  158. {
  159. GpsUart* gps_uart = malloc(sizeof(GpsUart));
  160. gps_uart->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  161. if (!gps_uart->mutex)
  162. {
  163. FURI_LOG_E("GPS", "cannot create mutex\r\n");
  164. free(gps_uart);
  165. return NULL;
  166. }
  167. gps_uart->status.valid = false;
  168. gps_uart->status.latitude = 0.0;
  169. gps_uart->status.longitude = 0.0;
  170. gps_uart->status.speed = 0.0;
  171. gps_uart->status.course = 0.0;
  172. gps_uart->status.altitude = 0.0;
  173. gps_uart->status.altitude_units = ' ';
  174. gps_uart->status.fix_quality = 0;
  175. gps_uart->status.satellites_tracked = 0;
  176. gps_uart->status.time_hours = 0;
  177. gps_uart->status.time_minutes = 0;
  178. gps_uart->status.time_seconds = 0;
  179. gps_uart->notifications = furi_record_open(RECORD_NOTIFICATION);
  180. gps_uart->thread = furi_thread_alloc();
  181. furi_thread_set_name(gps_uart->thread, "GpsUartWorker");
  182. furi_thread_set_stack_size(gps_uart->thread, 1024);
  183. furi_thread_set_context(gps_uart->thread, gps_uart);
  184. furi_thread_set_callback(gps_uart->thread, gps_uart_worker);
  185. furi_thread_start(gps_uart->thread);
  186. return gps_uart;
  187. }
  188. void gps_uart_disable(GpsUart* gps_uart)
  189. {
  190. furi_assert(gps_uart);
  191. furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtStop);
  192. furi_thread_join(gps_uart->thread);
  193. furi_thread_free(gps_uart->thread);
  194. furi_record_close(RECORD_NOTIFICATION);
  195. free(gps_uart);
  196. }