gps_uart.c 8.4 KB

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