wifi_marauder_uart.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "wifi_marauder_app_i.h"
  2. #include "wifi_marauder_uart.h"
  3. #include <xtreme.h>
  4. #define XTREME_UART_CH \
  5. (xtreme_settings.uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)
  6. bool xtreme_uart = false;
  7. #define UART_CH (FuriHalUartIdUSART1)
  8. #define LP_UART_CH (FuriHalUartIdLPUART1)
  9. #define BAUDRATE (115200)
  10. struct WifiMarauderUart {
  11. WifiMarauderApp* app;
  12. FuriHalUartId channel;
  13. FuriThread* rx_thread;
  14. FuriStreamBuffer* rx_stream;
  15. uint8_t rx_buf[RX_BUF_SIZE + 1];
  16. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
  17. };
  18. typedef enum {
  19. WorkerEvtStop = (1 << 0),
  20. WorkerEvtRxDone = (1 << 1),
  21. } WorkerEvtFlags;
  22. void wifi_marauder_uart_set_handle_rx_data_cb(
  23. WifiMarauderUart* uart,
  24. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
  25. furi_assert(uart);
  26. uart->handle_rx_data_cb = handle_rx_data_cb;
  27. }
  28. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  29. void wifi_marauder_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  30. WifiMarauderUart* uart = (WifiMarauderUart*)context;
  31. if(ev == UartIrqEventRXNE) {
  32. furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
  33. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  34. }
  35. }
  36. static int32_t uart_worker(void* context) {
  37. WifiMarauderUart* uart = (void*)context;
  38. while(1) {
  39. uint32_t events =
  40. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  41. furi_check((events & FuriFlagError) == 0);
  42. if(events & WorkerEvtStop) break;
  43. if(events & WorkerEvtRxDone) {
  44. size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  45. if(len > 0) {
  46. if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
  47. }
  48. }
  49. }
  50. furi_stream_buffer_free(uart->rx_stream);
  51. return 0;
  52. }
  53. // Will switch appropriately based on whether usart_init or xtreme_uart_init was called
  54. void wifi_marauder_uart_tx(uint8_t* data, size_t len) {
  55. if(xtreme_uart) {
  56. furi_hal_uart_tx(XTREME_UART_CH, data, len);
  57. } else {
  58. furi_hal_uart_tx(UART_CH, data, len);
  59. }
  60. }
  61. void wifi_marauder_lp_uart_tx(uint8_t* data, size_t len) {
  62. furi_hal_uart_tx(LP_UART_CH, data, len);
  63. }
  64. WifiMarauderUart*
  65. wifi_marauder_uart_init(WifiMarauderApp* app, FuriHalUartId channel, const char* thread_name) {
  66. WifiMarauderUart* uart = malloc(sizeof(WifiMarauderUart));
  67. uart->app = app;
  68. uart->channel = channel;
  69. uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  70. uart->rx_thread = furi_thread_alloc();
  71. furi_thread_set_name(uart->rx_thread, thread_name);
  72. furi_thread_set_stack_size(uart->rx_thread, 1024);
  73. furi_thread_set_context(uart->rx_thread, uart);
  74. furi_thread_set_callback(uart->rx_thread, uart_worker);
  75. furi_thread_start(uart->rx_thread);
  76. if(channel == FuriHalUartIdUSART1) {
  77. furi_hal_console_disable();
  78. } else if(channel == FuriHalUartIdLPUART1) {
  79. furi_hal_uart_init(channel, BAUDRATE);
  80. }
  81. furi_hal_uart_set_br(channel, BAUDRATE);
  82. furi_hal_uart_set_irq_cb(channel, wifi_marauder_uart_on_irq_cb, uart);
  83. return uart;
  84. }
  85. WifiMarauderUart* wifi_marauder_xtreme_uart_init(WifiMarauderApp* app) {
  86. xtreme_uart = true;
  87. return wifi_marauder_uart_init(app, XTREME_UART_CH, "WifiMarauderUartRxThread");
  88. }
  89. WifiMarauderUart* wifi_marauder_usart_init(WifiMarauderApp* app) {
  90. xtreme_uart = false;
  91. return wifi_marauder_uart_init(app, UART_CH, "WifiMarauderUartRxThread");
  92. }
  93. WifiMarauderUart* wifi_marauder_lp_uart_init(WifiMarauderApp* app) {
  94. return wifi_marauder_uart_init(app, LP_UART_CH, "WifiMarauderLPUartRxThread");
  95. }
  96. void wifi_marauder_uart_free(WifiMarauderUart* uart) {
  97. furi_assert(uart);
  98. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
  99. furi_thread_join(uart->rx_thread);
  100. furi_thread_free(uart->rx_thread);
  101. furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL);
  102. if(uart->channel == FuriHalUartIdLPUART1) {
  103. furi_hal_uart_deinit(uart->channel);
  104. } else {
  105. furi_hal_console_enable();
  106. }
  107. free(uart);
  108. }