2022-07-29 17:48:51 +03:00
|
|
|
#include "wifi_marauder_app_i.h"
|
|
|
|
#include "wifi_marauder_uart.h"
|
|
|
|
|
|
|
|
#define UART_CH (FuriHalUartIdUSART1)
|
2023-03-16 00:00:10 +03:00
|
|
|
#define LP_UART_CH (FuriHalUartIdLPUART1)
|
2022-07-29 17:48:51 +03:00
|
|
|
#define BAUDRATE (115200)
|
|
|
|
|
|
|
|
struct WifiMarauderUart {
|
|
|
|
WifiMarauderApp* app;
|
2023-03-16 00:00:10 +03:00
|
|
|
FuriHalUartId channel;
|
2022-07-29 17:48:51 +03:00
|
|
|
FuriThread* rx_thread;
|
2022-10-07 21:05:16 +03:00
|
|
|
FuriStreamBuffer* rx_stream;
|
2022-07-29 19:34:41 +03:00
|
|
|
uint8_t rx_buf[RX_BUF_SIZE + 1];
|
|
|
|
void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
|
2022-07-29 17:48:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
WorkerEvtStop = (1 << 0),
|
|
|
|
WorkerEvtRxDone = (1 << 1),
|
|
|
|
} WorkerEvtFlags;
|
|
|
|
|
2022-07-29 19:34:41 +03:00
|
|
|
void wifi_marauder_uart_set_handle_rx_data_cb(
|
|
|
|
WifiMarauderUart* uart,
|
|
|
|
void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
|
2022-07-29 17:48:51 +03:00
|
|
|
furi_assert(uart);
|
|
|
|
uart->handle_rx_data_cb = handle_rx_data_cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
|
|
|
|
|
|
|
|
void wifi_marauder_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
|
|
|
|
WifiMarauderUart* uart = (WifiMarauderUart*)context;
|
|
|
|
|
|
|
|
if(ev == UartIrqEventRXNE) {
|
2022-10-07 21:05:16 +03:00
|
|
|
furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
|
2022-07-29 17:48:51 +03:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t uart_worker(void* context) {
|
|
|
|
WifiMarauderUart* uart = (void*)context;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
uint32_t events =
|
|
|
|
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
furi_check((events & FuriFlagError) == 0);
|
|
|
|
if(events & WorkerEvtStop) break;
|
|
|
|
if(events & WorkerEvtRxDone) {
|
2022-10-07 21:05:16 +03:00
|
|
|
size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
|
2022-07-29 17:48:51 +03:00
|
|
|
if(len > 0) {
|
2022-07-29 19:34:41 +03:00
|
|
|
if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
|
2022-07-29 17:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 21:05:16 +03:00
|
|
|
furi_stream_buffer_free(uart->rx_stream);
|
2022-07-29 17:48:51 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-29 19:34:41 +03:00
|
|
|
void wifi_marauder_uart_tx(uint8_t* data, size_t len) {
|
2022-07-29 17:48:51 +03:00
|
|
|
furi_hal_uart_tx(UART_CH, data, len);
|
|
|
|
}
|
|
|
|
|
2023-03-16 00:00:10 +03:00
|
|
|
void wifi_marauder_lp_uart_tx(uint8_t* data, size_t len) {
|
|
|
|
furi_hal_uart_tx(LP_UART_CH, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiMarauderUart*
|
|
|
|
wifi_marauder_uart_init(WifiMarauderApp* app, FuriHalUartId channel, const char* thread_name) {
|
2022-07-29 19:34:41 +03:00
|
|
|
WifiMarauderUart* uart = malloc(sizeof(WifiMarauderUart));
|
2022-07-29 17:48:51 +03:00
|
|
|
|
|
|
|
uart->app = app;
|
2023-03-16 00:00:10 +03:00
|
|
|
uart->channel = channel;
|
2022-11-24 02:23:40 +03:00
|
|
|
uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
|
2022-07-29 17:48:51 +03:00
|
|
|
uart->rx_thread = furi_thread_alloc();
|
2023-03-16 00:00:10 +03:00
|
|
|
furi_thread_set_name(uart->rx_thread, thread_name);
|
2022-07-29 17:48:51 +03:00
|
|
|
furi_thread_set_stack_size(uart->rx_thread, 1024);
|
|
|
|
furi_thread_set_context(uart->rx_thread, uart);
|
|
|
|
furi_thread_set_callback(uart->rx_thread, uart_worker);
|
|
|
|
furi_thread_start(uart->rx_thread);
|
2023-03-16 00:00:10 +03:00
|
|
|
if(channel == FuriHalUartIdUSART1) {
|
|
|
|
furi_hal_console_disable();
|
|
|
|
} else if(channel == FuriHalUartIdLPUART1) {
|
|
|
|
furi_hal_uart_init(channel, BAUDRATE);
|
|
|
|
}
|
|
|
|
furi_hal_uart_set_br(channel, BAUDRATE);
|
|
|
|
furi_hal_uart_set_irq_cb(channel, wifi_marauder_uart_on_irq_cb, uart);
|
2022-11-24 02:23:40 +03:00
|
|
|
|
2022-07-29 17:48:51 +03:00
|
|
|
return uart;
|
|
|
|
}
|
|
|
|
|
2023-03-16 00:00:10 +03:00
|
|
|
WifiMarauderUart* wifi_marauder_usart_init(WifiMarauderApp* app) {
|
|
|
|
return wifi_marauder_uart_init(app, UART_CH, "WifiMarauderUartRxThread");
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiMarauderUart* wifi_marauder_lp_uart_init(WifiMarauderApp* app) {
|
|
|
|
return wifi_marauder_uart_init(app, LP_UART_CH, "WifiMarauderLPUartRxThread");
|
|
|
|
}
|
|
|
|
|
2022-07-29 17:48:51 +03:00
|
|
|
void wifi_marauder_uart_free(WifiMarauderUart* uart) {
|
|
|
|
furi_assert(uart);
|
|
|
|
|
|
|
|
furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
|
|
|
|
furi_thread_join(uart->rx_thread);
|
|
|
|
furi_thread_free(uart->rx_thread);
|
|
|
|
|
2023-03-16 00:00:10 +03:00
|
|
|
furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL);
|
2022-07-29 17:48:51 +03:00
|
|
|
furi_hal_console_enable();
|
|
|
|
|
|
|
|
free(uart);
|
|
|
|
}
|