gps_uart.c 8.1 KB

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