gps_uart.c 8.6 KB

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