Skip to content

Commit

Permalink
rewrite buffer for new uart stream
Browse files Browse the repository at this point in the history
  • Loading branch information
frux-c committed Mar 6, 2024
1 parent 0e78c7f commit 8c729ba
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion uhf_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ volatile uint16_t tick = 0;
// }

static M100ResponseType setup_and_send_rx(M100Module* module, uint8_t* cmd, size_t cmd_length) {
uhf_uart_send_wait(module->uart, cmd, cmd_length);
uhf_uart_send(module->uart, cmd, cmd_length);
buffer_close(module->uart->buffer);
// Validation Checks
uint8_t* data = buffer_get_data(module->uart->buffer);
Expand Down
64 changes: 48 additions & 16 deletions uhf_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,59 @@


int32_t uhf_uart_worker_callback(void *ctx){
// UHFUart* uart = (UHFUart*)ctx;
UNUSED(ctx);
// while(true){
// if(uart->tick > 0){
// furi_thread_sleep(uart->tick);
// uart->tick--;
// }
// }
UHFUart* uart = (UHFUart*)ctx;
// FuriString* line = furi_string_alloc();
uint32_t events;
events = furi_thread_flags_wait(
UHFUartWorkerWaitingDataFlag | UHFUartWorkerExitingFlag, FuriFlagWaitAny, FuriWaitForever
);
FURI_LOG_E("UHF_UART_WK_CB", "WAITING DATA");
if(events & UHFUartWorkerWaitingDataFlag){
FURI_LOG_E("UHF_UART_WK_CB", "STARTED");
size_t length_read = 0;
do{

uint8_t read_buffer[1];
length_read = furi_stream_buffer_receive(uart->rx_buff_stream, read_buffer, 1, 0);
if(length_read > 0){
// int i = 0;
FURI_LOG_E("UHF_UART_WK_CB", "FRAME START");
do{
FURI_LOG_E("UHF_UART_WK_CB", "UHF UART RX: %02X", read_buffer[0]);
length_read = furi_stream_buffer_receive(uart->rx_buff_stream, read_buffer, 1, 0);
}while(read_buffer[0] != UHF_UART_FRAME_END && length_read > 0);
FURI_LOG_E("UHF_UART_WK_CB", "UHF UART RX: %02X", read_buffer[0]);
furi_stream_buffer_reset(uart->rx_buff_stream);
FURI_LOG_E("UHF_UART_WK_CB", "FRAME END");
}
}while((events & UHFUartWorkerExitingFlag) != UHFUartWorkerExitingFlag);
FURI_LOG_E("UHF_UART_WK_CB", "EXITING");
}
return 0;
}
void uhf_uart_default_rx_callback(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void* ctx) {
UHFUart* uart = (UHFUart*)ctx;
Buffer* buffer = (Buffer*)uart->buffer;
if(buffer->closed) return; // buffer closed
if(event != FuriHalSerialRxEventData) return; // not byte received
uint8_t data = furi_hal_serial_async_rx(handle); // read data
buffer_append_single(buffer, data); // append data
if(data == UHF_UART_FRAME_END) buffer_close(buffer); // end of frame
uart->tick = UHF_UART_WAIT_TICK; // reset tick
if(event == FuriHalSerialRxEventData){
uint8_t data = furi_hal_serial_async_rx(handle);
// FURI_LOG_E("UHF_UART_RB_CB", "UHF UART RX: %02X", data);
furi_stream_buffer_send(uart->rx_buff_stream, (void*)&data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerWaitingDataFlag);
}
// Buffer* buffer = (Buffer*)uart->buffer;
// if(buffer->closed) return; // buffer closed
// if(event != FuriHalSerialRxEventData) return; // not byte received
// uint8_t data = furi_hal_serial_async_rx(handle); // read data
// FURI_LOG_E("UHF_UART_CB", "UHF UART RX: %02X", data);
// buffer_append_single(buffer, data); // append data
// if(data == UHF_UART_FRAME_END) buffer_close(buffer); // end of frame
// uart->tick = UHF_UART_WAIT_TICK; // reset tick
}

UHFUart* uhf_uart_alloc(){
UHFUart *uart = (UHFUart*)malloc(sizeof(UHFUart));
uart->bus = FuriHalBusUSART1;
uart->handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
uart->rx_buff_stream = furi_stream_buffer_alloc(UHF_UART_RX_BUFFER_SIZE, 1);
uart->init_by_app = !furi_hal_bus_is_enabled(uart->bus);
uart->tick = UHF_UART_WAIT_TICK;
if(uart->init_by_app){
Expand All @@ -41,9 +69,13 @@ UHFUart* uhf_uart_alloc(){
}

void uhf_uart_free(UHFUart* uart){
// furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerExitingFlag);
furi_assert(uart);
furi_assert(uart->thread);

furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerExitingFlag);
furi_thread_join(uart->thread);
furi_thread_free(uart->thread);
furi_stream_buffer_free(uart->rx_buff_stream);
buffer_free(uart->buffer);
if(uart->init_by_app){
furi_hal_serial_deinit(uart->handle);
Expand Down
5 changes: 3 additions & 2 deletions uhf_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ typedef void (*CallbackFunction)(uint8_t *data, void *ctx);


typedef enum{
UHFUartWorkerWaitingFlag,
UHFUartWorkerExitingFlag
UHFUartWorkerWaitingDataFlag = 1 << 0,
UHFUartWorkerExitingFlag = 1 << 1,
}UHFUartWorkerEventFlag;

// static void uhf_uart_received_byte_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void *ctx);

typedef struct{
FuriHalBus bus;
FuriHalSerialHandle *handle;
FuriStreamBuffer *rx_buff_stream;
FuriThread *thread;
CallbackFunction callback;
Buffer *buffer;
Expand Down

0 comments on commit 8c729ba

Please sign in to comment.