gps_uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.latitude = minmea_tocoord(&frame.latitude);
  41. gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
  42. }
  43. } break;
  44. default:
  45. break;
  46. }
  47. }
  48. static int32_t gps_uart_worker(void* context)
  49. {
  50. GpsUart* gps_uart = (GpsUart*)context;
  51. gps_uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
  52. size_t rx_offset = 0;
  53. gps_uart_serial_init(gps_uart);
  54. while (1)
  55. {
  56. uint32_t events =
  57. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  58. furi_check((events & FuriFlagError) == 0);
  59. if (events & WorkerEvtStop)
  60. {
  61. break;
  62. }
  63. if (events & WorkerEvtRxDone)
  64. {
  65. size_t len = 0;
  66. do
  67. {
  68. len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset,
  69. 0);
  70. if (len > 0)
  71. {
  72. rx_offset += len;
  73. gps_uart->rx_buf[rx_offset] = '\0';
  74. char * line_current = (char *)gps_uart->rx_buf;
  75. while (1)
  76. {
  77. while (*line_current == '\0' && line_current < (char *)gps_uart->rx_buf + rx_offset - 1)
  78. {
  79. line_current++;
  80. }
  81. char * newline = strchr(line_current, '\n');
  82. if (newline)
  83. {
  84. *newline = '\0';
  85. gps_uart_parse_nmea(gps_uart, line_current);
  86. line_current = newline + 1;
  87. }
  88. else
  89. {
  90. if (line_current > (char *)gps_uart->rx_buf)
  91. {
  92. rx_offset = 0;
  93. while (*line_current)
  94. {
  95. gps_uart->rx_buf[rx_offset++] = *(line_current++);
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. while (len > 0);
  104. }
  105. }
  106. gps_uart_serial_deinit(gps_uart);
  107. furi_stream_buffer_free(gps_uart->rx_stream);
  108. return 0;
  109. }
  110. GpsUart* gps_uart_enable()
  111. {
  112. GpsUart* gps_uart = malloc(sizeof(GpsUart));
  113. gps_uart->status.latitude = 0.0;
  114. gps_uart->status.longitude = 0.0;
  115. gps_uart->thread = furi_thread_alloc();
  116. furi_thread_set_name(gps_uart->thread, "GpsUartWorker");
  117. furi_thread_set_stack_size(gps_uart->thread, 1024);
  118. furi_thread_set_context(gps_uart->thread, gps_uart);
  119. furi_thread_set_callback(gps_uart->thread, gps_uart_worker);
  120. furi_thread_start(gps_uart->thread);
  121. return gps_uart;
  122. }
  123. void gps_uart_disable(GpsUart* gps_uart)
  124. {
  125. furi_assert(gps_uart);
  126. furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtStop);
  127. furi_thread_join(gps_uart->thread);
  128. furi_thread_free(gps_uart->thread);
  129. free(gps_uart);
  130. }