uart.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "uart.h"
  2. #define UART_CH (FuriHalUartIdUSART1)
  3. #define LP_UART_CH (FuriHalUartIdLPUART1)
  4. #define BAUDRATE (115200)
  5. struct Uart {
  6. void* app;
  7. FuriHalUartId channel;
  8. FuriThread* rx_thread;
  9. FuriStreamBuffer* rx_stream;
  10. uint8_t rx_buf[RX_BUF_SIZE + 1];
  11. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
  12. };
  13. typedef enum {
  14. WorkerEvtStop = (1 << 0),
  15. WorkerEvtRxDone = (1 << 1),
  16. } WorkerEvtFlags;
  17. void uart_set_handle_rx_data_cb(
  18. Uart* uart,
  19. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
  20. furi_assert(uart);
  21. uart->handle_rx_data_cb = handle_rx_data_cb;
  22. }
  23. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  24. void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  25. Uart* uart = (Uart*)context;
  26. if(ev == UartIrqEventRXNE) {
  27. furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
  28. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  29. }
  30. }
  31. // Define una constante para el prefijo que estamos buscando
  32. #define JSON_PREFIX "JSON:"
  33. // Variables globales
  34. static char json_buffer[2048]; // Ajusta el tamaño según tus necesidades
  35. static size_t json_buffer_index = 0;
  36. static bool json_capture_active = false;
  37. // static bool json_finded = false;
  38. // Prototipo de la función
  39. // static void process_json_buffer();
  40. static void process_json_buffer(void* context) {
  41. Uart* uart = (Uart*)context;
  42. // Agregamos el terminador nulo al final del buffer
  43. json_buffer[json_buffer_index] = '\0';
  44. if (uart->handle_rx_data_cb) {
  45. uart->handle_rx_data_cb((uint8_t *)json_buffer, json_buffer_index, uart->app);
  46. memset(json_buffer, 0, sizeof(json_buffer));
  47. }
  48. // Reiniciamos el buffer
  49. json_buffer_index = 0;
  50. }
  51. static void uart_echo_push_to_list(void* context, uint8_t data) {
  52. Uart* uart = (Uart*)context;
  53. if (!json_capture_active) {
  54. if (data == JSON_PREFIX[json_buffer_index]) {
  55. json_buffer[json_buffer_index++] = data; // Agregar el carácter al buffer
  56. if (json_buffer_index == strlen(JSON_PREFIX)) {
  57. // Encontramos el prefijo, comenzamos a capturar
  58. json_buffer_index = 0;
  59. json_capture_active = true;
  60. }
  61. } else {
  62. // Reiniciamos el índice si no coincide con el prefijo
  63. json_buffer_index = 0;
  64. }
  65. } else {
  66. // Capturamos caracteres hasta encontrar '\n'
  67. json_buffer[json_buffer_index++] = data;
  68. if (data == '\n') {
  69. // Terminamos de capturar la línea, procesamos el buffer
  70. json_capture_active = false;
  71. process_json_buffer(uart);
  72. }
  73. }
  74. }
  75. static int32_t uart_worker(void* context) {
  76. furi_assert(context);
  77. Uart* uart = (Uart*)context;
  78. while(1) {
  79. uint32_t events = furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  80. furi_check((events & FuriFlagError) == 0);
  81. if(events & WorkerEvtStop) break;
  82. if(events & WorkerEvtRxDone) {
  83. if(uart->channel == UART_CH) {
  84. size_t length = 0;
  85. do {
  86. uint8_t data[64];
  87. length = furi_stream_buffer_receive(uart->rx_stream, data, 64, 0);
  88. // FURI_LOG_I("UART", "[in]: %s", (char*)data);
  89. if(length > 0) {
  90. for(size_t i = 0; i < length; i++) {
  91. uart_echo_push_to_list(uart, data[i]);
  92. }
  93. }
  94. } while(length > 0);
  95. } else if(uart->channel == LP_UART_CH) {
  96. size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  97. if(len > 0) {
  98. if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
  99. }
  100. }
  101. }
  102. }
  103. furi_stream_buffer_free(uart->rx_stream);
  104. return 0;
  105. }
  106. void uart_tx(uint8_t* data, size_t len) {
  107. furi_hal_uart_tx(UART_CH, data, len);
  108. }
  109. void lp_uart_tx(uint8_t* data, size_t len) {
  110. furi_hal_uart_tx(LP_UART_CH, data, len);
  111. }
  112. Uart*
  113. _uart_init(void* app, FuriHalUartId channel, const char* thread_name) {
  114. Uart* uart = (Uart*)malloc(sizeof(Uart));
  115. uart->app = app;
  116. uart->channel = channel;
  117. uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  118. uart->rx_thread = furi_thread_alloc();
  119. furi_thread_set_name(uart->rx_thread, thread_name);
  120. furi_thread_set_stack_size(uart->rx_thread, 1024);
  121. furi_thread_set_context(uart->rx_thread, uart);
  122. furi_thread_set_callback(uart->rx_thread, uart_worker);
  123. furi_thread_start(uart->rx_thread);
  124. if(channel == FuriHalUartIdUSART1) {
  125. furi_hal_console_disable();
  126. } else if(channel == FuriHalUartIdLPUART1) {
  127. furi_hal_uart_init(channel, BAUDRATE);
  128. }
  129. furi_hal_uart_set_br(channel, BAUDRATE);
  130. furi_hal_uart_set_irq_cb(channel, uart_on_irq_cb, uart);
  131. return uart;
  132. }
  133. Uart* usart_init(void* app) {
  134. return _uart_init(app, UART_CH, "UartRxThread");
  135. }
  136. Uart* lp_uart_init(void* app) {
  137. return _uart_init(app, LP_UART_CH, "LPUartRxThread");
  138. }
  139. void uart_free(Uart* uart) {
  140. furi_assert(uart);
  141. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
  142. furi_thread_join(uart->rx_thread);
  143. furi_thread_free(uart->rx_thread);
  144. furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL);
  145. if(uart->channel == FuriHalUartIdLPUART1) {
  146. furi_hal_uart_deinit(uart->channel);
  147. }
  148. furi_hal_console_enable();
  149. free(uart);
  150. }