Преглед изворни кода

change buffer to use UHF_ prefix due to method conflicts with built-in functions

frux-c пре 1 година
родитељ
комит
6e9d1ba947
5 измењених фајлова са 84 додато и 57 уклоњено
  1. 12 8
      uhf_buffer.c
  2. 10 8
      uhf_buffer.h
  3. 22 14
      uhf_module.c
  4. 36 23
      uhf_uart.c
  5. 4 4
      uhf_uart.h

+ 12 - 8
uhf_buffer.c

@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
 
 
-Buffer* buffer_alloc(size_t initial_capacity) {
+Buffer* uhf_buffer_alloc(size_t initial_capacity) {
     Buffer* buf = (Buffer*)malloc(sizeof(Buffer));
     Buffer* buf = (Buffer*)malloc(sizeof(Buffer));
     buf->data = (uint8_t*)malloc(sizeof(uint8_t) * initial_capacity);
     buf->data = (uint8_t*)malloc(sizeof(uint8_t) * initial_capacity);
     if(!buf->data) {
     if(!buf->data) {
@@ -14,7 +14,7 @@ Buffer* buffer_alloc(size_t initial_capacity) {
     return buf;
     return buf;
 }
 }
 
 
-bool buffer_append_single(Buffer* buf, uint8_t data) {
+bool uhf_buffer_append_single(Buffer* buf, uint8_t data) {
     if(buf->closed) return false;
     if(buf->closed) return false;
     if(buf->size + 1 > buf->capacity) {
     if(buf->size + 1 > buf->capacity) {
         size_t new_capacity = buf->capacity * 2;
         size_t new_capacity = buf->capacity * 2;
@@ -27,7 +27,7 @@ bool buffer_append_single(Buffer* buf, uint8_t data) {
     return true;
     return true;
 }
 }
 
 
-bool buffer_append(Buffer* buf, uint8_t* data, size_t data_size) {
+bool uhf_buffer_append(Buffer* buf, uint8_t* data, size_t data_size) {
     if(buf->closed) return false;
     if(buf->closed) return false;
     if(buf->size + data_size > buf->capacity) {
     if(buf->size + data_size > buf->capacity) {
         size_t new_capacity = buf->capacity * 2;
         size_t new_capacity = buf->capacity * 2;
@@ -43,19 +43,23 @@ bool buffer_append(Buffer* buf, uint8_t* data, size_t data_size) {
     return true;
     return true;
 }
 }
 
 
-uint8_t* buffer_get_data(Buffer* buf) {
+uint8_t* uhf_buffer_get_data(Buffer* buf) {
     return buf->data;
     return buf->data;
 }
 }
 
 
-size_t buffer_get_size(Buffer* buf) {
+size_t uhf_buffer_get_size(Buffer* buf) {
     return buf->size;
     return buf->size;
 }
 }
 
 
-void buffer_close(Buffer* buf) {
+bool uhf_is_buffer_closed(Buffer* buf) {
+    return buf->closed;
+}
+
+void uhf_buffer_close(Buffer* buf) {
     buf->closed = true;
     buf->closed = true;
 }
 }
 
 
-void buffer_reset(Buffer* buf) {
+void uhf_buffer_reset(Buffer* buf) {
     for(size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
     for(size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
         buf->data[i] = 0;
         buf->data[i] = 0;
     }
     }
@@ -63,7 +67,7 @@ void buffer_reset(Buffer* buf) {
     buf->closed = false;
     buf->closed = false;
 }
 }
 
 
-void buffer_free(Buffer* buf) {
+void uhf_buffer_free(Buffer* buf) {
     free(buf->data);
     free(buf->data);
     free(buf);
     free(buf);
 }
 }

+ 10 - 8
uhf_buffer.h

@@ -12,11 +12,13 @@ typedef struct Buffer {
     bool closed;
     bool closed;
 } Buffer;
 } Buffer;
 
 
-Buffer* buffer_alloc(size_t inital_capacity);
-bool buffer_append_single(Buffer* buf, uint8_t value);
-bool buffer_append(Buffer* buf, uint8_t* data, size_t size);
-uint8_t* buffer_get_data(Buffer* buf);
-size_t buffer_get_size(Buffer* buf);
-void buffer_close(Buffer* buf);
-void buffer_reset(Buffer* buf);
-void buffer_free(Buffer* buf);
+Buffer* uhf_buffer_alloc(size_t inital_capacity);
+bool uhf_buffer_append_single(Buffer* buf, uint8_t value);
+bool uhf_buffer_append(Buffer* buf, uint8_t* data, size_t size);
+
+uint8_t* uhf_buffer_get_data(Buffer* buf);
+size_t uhf_buffer_get_size(Buffer* buf);
+bool uhf_is_buffer_closed(Buffer* buf);
+void uhf_buffer_close(Buffer* buf);
+void uhf_buffer_reset(Buffer* buf);
+void uhf_buffer_free(Buffer* buf);

+ 22 - 14
uhf_module.c

@@ -2,7 +2,7 @@
 #include "uhf_module_cmd.h"
 #include "uhf_module_cmd.h"
 
 
 #define DELAY_MS 100
 #define DELAY_MS 100
-#define WAIT_TICK 8000 // max wait time in between each byte
+#define WAIT_TICK 4000 // max wait time in between each byte
 
 
 volatile uint16_t tick = 0;
 volatile uint16_t tick = 0;
 
 
@@ -16,11 +16,19 @@ volatile uint16_t tick = 0;
 // }
 // }
 
 
 static M100ResponseType setup_and_send_rx(M100Module* module, uint8_t* cmd, size_t cmd_length) {
 static M100ResponseType setup_and_send_rx(M100Module* module, uint8_t* cmd, size_t cmd_length) {
-    uhf_uart_send(module->uart, cmd, cmd_length);
-    buffer_close(module->uart->buffer);
+    UHFUart* uart = module->uart;
+    Buffer* buffer = uart->buffer;
+    // clear buffer
+    uhf_buffer_reset(buffer);
+    // send cmd
+    uhf_uart_send_wait(uart, cmd, cmd_length);
+    // wait for response
+    while(!uhf_is_buffer_closed(buffer) && !uhf_uart_tick(uart)) {}
+    // reset tick
+    uhf_uart_tick_reset(uart);
     // Validation Checks
     // Validation Checks
-    uint8_t* data = buffer_get_data(module->uart->buffer);
-    size_t length = buffer_get_size(module->uart->buffer);
+    uint8_t* data = uhf_buffer_get_data(buffer);
+    size_t length = uhf_buffer_get_size(buffer);
     // check if size > 0
     // check if size > 0
     if(!length) return M100EmptyResponse;
     if(!length) return M100EmptyResponse;
     // check if data is valid
     // check if data is valid
@@ -86,8 +94,8 @@ uint16_t crc16_genibus(const uint8_t* data, size_t length) {
 }
 }
 
 
 char* _m100_info_helper(M100Module* module, char** info) {
 char* _m100_info_helper(M100Module* module, char** info) {
-    if(!buffer_get_size(module->uart->buffer)) return NULL;
-    uint8_t* data = buffer_get_data(module->uart->buffer);
+    if(!uhf_buffer_get_size(module->uart->buffer)) return NULL;
+    uint8_t* data = uhf_buffer_get_data(module->uart->buffer);
     uint16_t payload_len = data[3];
     uint16_t payload_len = data[3];
     payload_len = (payload_len << 8) + data[4];
     payload_len = (payload_len << 8) + data[4];
     FuriString* temp_str = furi_string_alloc();
     FuriString* temp_str = furi_string_alloc();
@@ -123,8 +131,8 @@ M100ResponseType m100_single_poll(M100Module* module, UHFTag* uhf_tag) {
     M100ResponseType rp_type =
     M100ResponseType rp_type =
         setup_and_send_rx(module, (uint8_t*)&CMD_SINGLE_POLLING.cmd[0], CMD_SINGLE_POLLING.length);
         setup_and_send_rx(module, (uint8_t*)&CMD_SINGLE_POLLING.cmd[0], CMD_SINGLE_POLLING.length);
     if(rp_type != M100SuccessResponse) return rp_type;
     if(rp_type != M100SuccessResponse) return rp_type;
-    uint8_t* data = buffer_get_data(module->uart->buffer);
-    size_t length = buffer_get_size(module->uart->buffer);
+    uint8_t* data = uhf_buffer_get_data(module->uart->buffer);
+    size_t length = uhf_buffer_get_size(module->uart->buffer);
     uint16_t pc = data[6];
     uint16_t pc = data[6];
     uint16_t crc = 0;
     uint16_t crc = 0;
     // mask out epc length from protocol control
     // mask out epc length from protocol control
@@ -180,7 +188,7 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
 
 
     setup_and_send_rx(module, cmd, 12 + mask_length_bytes + 3);
     setup_and_send_rx(module, cmd, 12 + mask_length_bytes + 3);
 
 
-    uint8_t* data = buffer_get_data(module->uart->buffer);
+    uint8_t* data = uhf_buffer_get_data(module->uart->buffer);
     if(checksum(data + 1, 5) != data[6]) return M100ValidationFail; // error in rx
     if(checksum(data + 1, 5) != data[6]) return M100ValidationFail; // error in rx
     if(data[5] != 0x00) return M100ValidationFail; // error if not 0
     if(data[5] != 0x00) return M100ValidationFail; // error if not 0
 
 
@@ -188,7 +196,7 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
 }
 }
 
 
 UHFTag* m100_get_select_param(M100Module* module) {
 UHFTag* m100_get_select_param(M100Module* module) {
-    buffer_reset(module->uart->buffer);
+    uhf_buffer_reset(module->uart->buffer);
     // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->uart->buffer);
     // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->uart->buffer);
     // furi_hal_uart_tx(
     // furi_hal_uart_tx(
     //     FuriHalUartIdUSART1,
     //     FuriHalUartIdUSART1,
@@ -232,7 +240,7 @@ M100ResponseType m100_read_label_data_storage(
     M100ResponseType rp_type = setup_and_send_rx(module, cmd, cmd_length);
     M100ResponseType rp_type = setup_and_send_rx(module, cmd, cmd_length);
     if(rp_type != M100SuccessResponse) return rp_type;
     if(rp_type != M100SuccessResponse) return rp_type;
 
 
-    uint8_t* data = buffer_get_data(module->uart->buffer);
+    uint8_t* data = uhf_buffer_get_data(module->uart->buffer);
 
 
     uint8_t rtn_command = data[2];
     uint8_t rtn_command = data[2];
     uint16_t payload_len = data[3];
     uint16_t payload_len = data[3];
@@ -321,8 +329,8 @@ M100ResponseType m100_write_label_data_storage(
     //     if(!timeout--) break;
     //     if(!timeout--) break;
     // }
     // }
     setup_and_send_rx(module, cmd, cmd_length);
     setup_and_send_rx(module, cmd, cmd_length);
-    uint8_t* buff_data = buffer_get_data(module->uart->buffer);
-    size_t buff_length = buffer_get_size(module->uart->buffer);
+    uint8_t* buff_data = uhf_buffer_get_data(module->uart->buffer);
+    size_t buff_length = uhf_buffer_get_size(module->uart->buffer);
     if(buff_data[2] == 0xFF && buff_length == 8)
     if(buff_data[2] == 0xFF && buff_length == 8)
         return M100NoTagResponse;
         return M100NoTagResponse;
     else if(buff_data[2] == 0xFF)
     else if(buff_data[2] == 0xFF)

+ 36 - 23
uhf_uart.c

@@ -3,40 +3,37 @@
 
 
 int32_t uhf_uart_worker_callback(void *ctx){
 int32_t uhf_uart_worker_callback(void *ctx){
     UHFUart* uart = (UHFUart*)ctx;
     UHFUart* uart = (UHFUart*)ctx;
-    // FuriString* line = furi_string_alloc();
+    Buffer* buffer = (Buffer*)uart->buffer;
     uint32_t events;
     uint32_t events;
-    events = furi_thread_flags_wait(
+    size_t length_read = 0;
+    uint8_t read_buffer[1];
+    FURI_LOG_E("UHF_UART_WORKER", "UHF UART WORKER STARTED");
+    do{
+        events = furi_thread_flags_wait(
             UHFUartWorkerWaitingDataFlag | UHFUartWorkerExitingFlag, FuriFlagWaitAny, FuriWaitForever
             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];
+        FURI_LOG_E("UHF_UART_WORKER", "events = %lu", events);
+        if(events & UHFUartWorkerWaitingDataFlag){
+            FURI_LOG_E("UHF_UART_WORKER", "Waiting data...");
             length_read = furi_stream_buffer_receive(uart->rx_buff_stream, read_buffer, 1, 0);
             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");
+            if(length_read){
                 do{
                 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);
                     length_read = furi_stream_buffer_receive(uart->rx_buff_stream, read_buffer, 1, 0);
+                    uhf_buffer_append_single(buffer, read_buffer[0]);
+                    uhf_uart_tick_reset(uart);
                 }while(read_buffer[0] != UHF_UART_FRAME_END && length_read > 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_LOG_E("UHF_UART_WORKER", "UHF Total length read = %u", uhf_buffer_get_size(buffer));
+                uhf_buffer_close(buffer);
                 furi_stream_buffer_reset(uart->rx_buff_stream);
                 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");
-    }
+        }
+    }while((events & UHFUartWorkerExitingFlag) != UHFUartWorkerExitingFlag);
     return 0;
     return 0;
 }
 }
 void uhf_uart_default_rx_callback(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void* ctx) {
 void uhf_uart_default_rx_callback(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void* ctx) {
     UHFUart* uart = (UHFUart*)ctx;
     UHFUart* uart = (UHFUart*)ctx;
     if(event == FuriHalSerialRxEventData){
     if(event == FuriHalSerialRxEventData){
         uint8_t data = furi_hal_serial_async_rx(handle);
         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_stream_buffer_send(uart->rx_buff_stream, (void*)&data, 1, 0);
         furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerWaitingDataFlag);
         furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerWaitingDataFlag);
     }
     }
@@ -57,14 +54,18 @@ UHFUart* uhf_uart_alloc(){
     uart->rx_buff_stream = furi_stream_buffer_alloc(UHF_UART_RX_BUFFER_SIZE, 1);
     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->init_by_app = !furi_hal_bus_is_enabled(uart->bus);
     uart->tick = UHF_UART_WAIT_TICK;
     uart->tick = UHF_UART_WAIT_TICK;
+    uart->baudrate = UHF_UART_DEFAULT_BAUDRATE;
     if(uart->init_by_app){
     if(uart->init_by_app){
+        FURI_LOG_E("UHF_UART", "UHF UART INIT BY APP");
         furi_hal_serial_init(uart->handle, uart->baudrate);
         furi_hal_serial_init(uart->handle, uart->baudrate);
     }
     }
-    uhf_uart_set_baudrate(uart, UHF_UART_DEFAULT_BAUDRATE);
-    uart->buffer = buffer_alloc(UHF_UART_RX_BUFFER_SIZE);
+    else{
+        FURI_LOG_E("UHF_UART", "UHF UART INIT BY HAL");
+    }
+    uart->buffer = uhf_buffer_alloc(UHF_UART_RX_BUFFER_SIZE);
     uart->thread = furi_thread_alloc_ex("UHFUartWorker", UHF_UART_WORKER_STACK_SIZE, uhf_uart_worker_callback, uart);
     uart->thread = furi_thread_alloc_ex("UHFUartWorker", UHF_UART_WORKER_STACK_SIZE, uhf_uart_worker_callback, uart);
     furi_thread_start(uart->thread);
     furi_thread_start(uart->thread);
-    uhf_uart_set_receive_byte_callback(uart, uhf_uart_default_rx_callback, uart, false);
+    furi_hal_serial_async_rx_start(uart->handle, uhf_uart_default_rx_callback, uart, false);
     return uart;
     return uart;
 }   
 }   
 
 
@@ -76,7 +77,7 @@ void uhf_uart_free(UHFUart* uart){
     furi_thread_join(uart->thread);
     furi_thread_join(uart->thread);
     furi_thread_free(uart->thread);
     furi_thread_free(uart->thread);
     furi_stream_buffer_free(uart->rx_buff_stream);
     furi_stream_buffer_free(uart->rx_buff_stream);
-    buffer_free(uart->buffer);
+    uhf_buffer_free(uart->buffer);
     if(uart->init_by_app){
     if(uart->init_by_app){
         furi_hal_serial_deinit(uart->handle);
         furi_hal_serial_deinit(uart->handle);
     }
     }
@@ -95,9 +96,21 @@ void uhf_uart_send(UHFUart* uart, uint8_t* data, size_t size){
 void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, size_t size){
 void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, size_t size){
     uhf_uart_send(uart, data, size);
     uhf_uart_send(uart, data, size);
     furi_hal_serial_tx_wait_complete(uart->handle);
     furi_hal_serial_tx_wait_complete(uart->handle);
+    furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerWaitingDataFlag);
 }
 }
 
 
 void uhf_uart_set_baudrate(UHFUart* uart, uint32_t baudrate){
 void uhf_uart_set_baudrate(UHFUart* uart, uint32_t baudrate){
     furi_hal_serial_set_br(uart->handle, baudrate);
     furi_hal_serial_set_br(uart->handle, baudrate);
     uart->baudrate = baudrate;
     uart->baudrate = baudrate;
+}
+
+bool uhf_uart_tick(UHFUart* uart){
+    if(uart->tick > 0){
+        uart->tick--;
+    }
+    return uart->tick == 0;
+}
+
+void uhf_uart_tick_reset(UHFUart* uart){
+    uart->tick = UHF_UART_WAIT_TICK;
 }
 }

+ 4 - 4
uhf_uart.h

@@ -4,7 +4,7 @@
 #include <stdbool.h>
 #include <stdbool.h>
 #include "uhf_buffer.h"
 #include "uhf_buffer.h"
 
 
-#define UHF_UART_RX_BUFFER_SIZE 100
+#define UHF_UART_RX_BUFFER_SIZE 250
 #define UHF_UART_WORKER_STACK_SIZE 1 * 1024
 #define UHF_UART_WORKER_STACK_SIZE 1 * 1024
 #define UHF_UART_DEFAULT_BAUDRATE 115200
 #define UHF_UART_DEFAULT_BAUDRATE 115200
 #define UHF_UART_FRAME_START 0xBB
 #define UHF_UART_FRAME_START 0xBB
@@ -16,11 +16,9 @@ typedef void (*CallbackFunction)(uint8_t *data, void *ctx);
 
 
 typedef enum{
 typedef enum{
     UHFUartWorkerWaitingDataFlag = 1 << 0,
     UHFUartWorkerWaitingDataFlag = 1 << 0,
-    UHFUartWorkerExitingFlag = 1 << 1,
+    UHFUartWorkerExitingFlag = 1 << 2,
 }UHFUartWorkerEventFlag;
 }UHFUartWorkerEventFlag;
 
 
-// static void uhf_uart_received_byte_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void *ctx);
-
 typedef struct{
 typedef struct{
     FuriHalBus bus;
     FuriHalBus bus;
     FuriHalSerialHandle *handle;
     FuriHalSerialHandle *handle;
@@ -42,4 +40,6 @@ void uhf_uart_send(UHFUart* uart, uint8_t* data, size_t size);
 void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, size_t size);
 void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, size_t size);
 void uhf_uart_set_receive_byte_callback(UHFUart* uart, FuriHalSerialAsyncRxCallback callback, void *ctx, bool report_errors);
 void uhf_uart_set_receive_byte_callback(UHFUart* uart, FuriHalSerialAsyncRxCallback callback, void *ctx, bool report_errors);
 void uhf_uart_set_baudrate(UHFUart* uart, uint32_t baudrate);
 void uhf_uart_set_baudrate(UHFUart* uart, uint32_t baudrate);
+bool uhf_uart_tick(UHFUart* uart);
+void uhf_uart_tick_reset(UHFUart* uart);