Sfoglia il codice sorgente

no syntax error; memmanage error raised

frux-c 1 anno fa
parent
commit
583fba3022
8 ha cambiato i file con 68 aggiunte e 76 eliminazioni
  1. 5 5
      scenes/uhf_scene_settings.c
  2. 1 6
      uhf_app.c
  3. 42 47
      uhf_module.c
  4. 4 6
      uhf_module.h
  5. 8 6
      uhf_uart.c
  6. 5 5
      uhf_uart.h
  7. 1 1
      uhf_worker.c
  8. 2 0
      uhf_worker.h

+ 5 - 5
scenes/uhf_scene_settings.c

@@ -2,15 +2,15 @@
 #include "../uhf_module.h"
 
 void uhf_settings_set_module_baudrate(VariableItem* item) {
-    M100Module* uhf_module = variable_item_get_context(item);
+    M100Module* module = variable_item_get_context(item);
     uint8_t index = variable_item_get_current_value_index(item);
     if(index >= BAUD_RATES_COUNT) {
         return;
     }
     uint32_t baudrate = BAUD_RATES[index];
-    m100_set_baudrate(uhf_module, baudrate);
+    m100_set_baudrate(module, baudrate);
     char text_buf[10];
-    snprintf(text_buf, sizeof(text_buf), "%lu", uhf_module->baudrate);
+    snprintf(text_buf, sizeof(text_buf), "%lu", module->uart->baudrate);
     variable_item_set_current_value_text(item, text_buf);
 }
 
@@ -40,7 +40,7 @@ void uhf_settings_set_module_working_region(VariableItem* item) {
 
 uint8_t uhf_settings_get_module_baudrate_index(M100Module* module) {
     for(uint8_t i = 0; i < BAUD_RATES_COUNT; i++) {
-        if(BAUD_RATES[i] == module->baudrate) {
+        if(BAUD_RATES[i] == module->uart->baudrate) {
             return i;
         }
     }
@@ -73,7 +73,7 @@ void uhf_scene_settings_on_enter(void* ctx) {
 
     uint8_t value_index = uhf_settings_get_module_baudrate_index(uhf_module);
     char text_buf[10];
-    snprintf(text_buf, sizeof(text_buf), "%lu", uhf_module->baudrate);
+    snprintf(text_buf, sizeof(text_buf), "%lu", uhf_module->uart->baudrate);
     item = variable_item_list_add(
         variable_item_list,
         "Baudrate:",

+ 1 - 6
uhf_app.c

@@ -195,18 +195,13 @@ void uhf_show_loading_popup(void* ctx, bool show) {
 int32_t uhf_app_main(void* ctx) {
     UNUSED(ctx);
     UHFApp* uhf_app = uhf_alloc();
-
     // enable 5v pin
     furi_hal_power_enable_otg();
-    // init pin a2
-    // furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeOutputPushPull);
-    furi_hal_uart_set_br(FuriHalUartIdUSART1, DEFAULT_BAUDRATE);
+    // enter app
     scene_manager_next_scene(uhf_app->scene_manager, UHFSceneModuleInfo);
     view_dispatcher_run(uhf_app->view_dispatcher);
-
     // disable 5v pin
     furi_hal_power_disable_otg();
-    // furi_hal_gpio_disable_int_callback()
     // exit app
     uhf_free(uhf_app);
     return 0;

+ 42 - 47
uhf_module.c

@@ -6,26 +6,21 @@
 
 volatile uint16_t tick = 0;
 
-void rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
-    UNUSED(event);
-    Buffer* buffer = ctx;
-    if(buffer->closed) return; // buffer closed
-    buffer_append_single(buffer, data); // append data
-    if(data == FRAME_END) buffer_close(buffer); // end of frame
-    tick = WAIT_TICK; // reset tick
-}
+// void rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
+//     UNUSED(event);
+//     Buffer* buffer = ctx;
+//     if(buffer->closed) return; // buffer closed
+//     buffer_append_single(buffer, data); // append data
+//     if(data == FRAME_END) buffer_close(buffer); // end of frame
+//     tick = WAIT_TICK; // reset tick
+// }
 
 static M100ResponseType setup_and_send_rx(M100Module* module, uint8_t* cmd, size_t cmd_length) {
-    buffer_reset(module->buf);
-    tick = WAIT_TICK;
-    furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
-    while(--tick) {
-        furi_delay_us(5);
-    }
-    buffer_close(module->buf);
+    uhf_uart_send_wait(module->uart, cmd, cmd_length);
+    buffer_close(module->uart->buffer);
     // Validation Checks
-    uint8_t* data = buffer_get_data(module->buf);
-    size_t length = buffer_get_size(module->buf);
+    uint8_t* data = buffer_get_data(module->uart->buffer);
+    size_t length = buffer_get_size(module->uart->buffer);
     // check if size > 0
     if(!length) return M100EmptyResponse;
     // check if data is valid
@@ -41,25 +36,24 @@ M100ModuleInfo* m100_module_info_alloc() {
 }
 
 void m100_module_info_free(M100ModuleInfo* module_info) {
-    free(module_info->hw_version);
-    free(module_info->sw_version);
-    free(module_info->manufacturer);
+    if(module_info->hw_version != NULL) free(module_info->hw_version);
+    if(module_info->sw_version != NULL) free(module_info->sw_version);
+    if(module_info->manufacturer != NULL) free(module_info->manufacturer);
     free(module_info);
 }
+
 M100Module* m100_module_alloc() {
     M100Module* module = (M100Module*)malloc(sizeof(M100Module));
-    module->info = m100_module_info_alloc();
-    module->buf = buffer_alloc(MAX_BUFFER_SIZE);
-    module->baudrate = DEFAULT_BAUDRATE;
     module->transmitting_power = DEFAULT_TRANSMITTING_POWER;
     module->region = DEFAULT_WORKING_REGION;
-    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
+    module->info = m100_module_info_alloc();
+    module->uart = uhf_uart_alloc();
     return module;
 }
 
 void m100_module_free(M100Module* module) {
     m100_module_info_free(module->info);
-    buffer_free(module->buf);
+    uhf_uart_free(module->uart);
     free(module);
 }
 
@@ -92,8 +86,8 @@ uint16_t crc16_genibus(const uint8_t* data, size_t length) {
 }
 
 char* _m100_info_helper(M100Module* module, char** info) {
-    if(!buffer_get_size(module->buf)) return NULL;
-    uint8_t* data = buffer_get_data(module->buf);
+    if(!buffer_get_size(module->uart->buffer)) return NULL;
+    uint8_t* data = buffer_get_data(module->uart->buffer);
     uint16_t payload_len = data[3];
     payload_len = (payload_len << 8) + data[4];
     FuriString* temp_str = furi_string_alloc();
@@ -129,8 +123,8 @@ M100ResponseType m100_single_poll(M100Module* module, UHFTag* uhf_tag) {
     M100ResponseType rp_type =
         setup_and_send_rx(module, (uint8_t*)&CMD_SINGLE_POLLING.cmd[0], CMD_SINGLE_POLLING.length);
     if(rp_type != M100SuccessResponse) return rp_type;
-    uint8_t* data = buffer_get_data(module->buf);
-    size_t length = buffer_get_size(module->buf);
+    uint8_t* data = buffer_get_data(module->uart->buffer);
+    size_t length = buffer_get_size(module->uart->buffer);
     uint16_t pc = data[6];
     uint16_t crc = 0;
     // mask out epc length from protocol control
@@ -186,7 +180,7 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
 
     setup_and_send_rx(module, cmd, 12 + mask_length_bytes + 3);
 
-    uint8_t* data = buffer_get_data(module->buf);
+    uint8_t* data = buffer_get_data(module->uart->buffer);
     if(checksum(data + 1, 5) != data[6]) return M100ValidationFail; // error in rx
     if(data[5] != 0x00) return M100ValidationFail; // error if not 0
 
@@ -194,15 +188,15 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
 }
 
 UHFTag* m100_get_select_param(M100Module* module) {
-    buffer_reset(module->buf);
-    furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->buf);
-    furi_hal_uart_tx(
-        FuriHalUartIdUSART1,
-        (uint8_t*)&CMD_GET_SELECT_PARAMETER.cmd,
-        CMD_GET_SELECT_PARAMETER.length);
-    furi_delay_ms(DELAY_MS);
+    buffer_reset(module->uart->buffer);
+    // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->uart->buffer);
+    // furi_hal_uart_tx(
+    //     FuriHalUartIdUSART1,
+    //     (uint8_t*)&CMD_GET_SELECT_PARAMETER.cmd,
+    //     CMD_GET_SELECT_PARAMETER.length);
+    // furi_delay_ms(DELAY_MS);
     // UHFTag* uhf_tag = uhf_tag_alloc();
-    // uint8_t* data = buffer_get_data(module->buf);
+    // uint8_t* data = buffer_get_data(module->uart->buffer);
     // size_t mask_length =
     // uhf_tag_set_epc(uhf_tag, data + 12, )
     // TODO : implement
@@ -238,7 +232,7 @@ M100ResponseType m100_read_label_data_storage(
     M100ResponseType rp_type = setup_and_send_rx(module, cmd, cmd_length);
     if(rp_type != M100SuccessResponse) return rp_type;
 
-    uint8_t* data = buffer_get_data(module->buf);
+    uint8_t* data = buffer_get_data(module->uart->buffer);
 
     uint8_t rtn_command = data[2];
     uint16_t payload_len = data[3];
@@ -318,17 +312,17 @@ M100ResponseType m100_write_label_data_storage(
     cmd[cmd_length - 2] = checksum(cmd + 1, cmd_length - 3);
     cmd[cmd_length - 1] = FRAME_END;
     // send cmd
-    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
+    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->uart->buffer);
     // furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
     // unsigned int delay = DELAY_MS / 2;
     // unsigned int timeout = 15;
-    // while(!buffer_get_size(module->buf)) {
+    // while(!buffer_get_size(module->uart->buffer)) {
     //     furi_delay_ms(delay);
     //     if(!timeout--) break;
     // }
     setup_and_send_rx(module, cmd, cmd_length);
-    uint8_t* buff_data = buffer_get_data(module->buf);
-    size_t buff_length = buffer_get_size(module->buf);
+    uint8_t* buff_data = buffer_get_data(module->uart->buffer);
+    size_t buff_length = buffer_get_size(module->uart->buffer);
     if(buff_data[2] == 0xFF && buff_length == 8)
         return M100NoTagResponse;
     else if(buff_data[2] == 0xFF)
@@ -343,9 +337,10 @@ void m100_set_baudrate(M100Module* module, uint32_t baudrate) {
     cmd[6] = 0xFF & br_mod; // pow LSB
     cmd[5] = 0xFF & (br_mod >> 8); // pow MSB
     cmd[length - 2] = checksum(cmd + 1, length - 3);
-    furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, length);
-    furi_hal_uart_set_br(FuriHalUartIdUSART1, baudrate);
-    module->baudrate = baudrate;
+    // setup_and_send_rx(module, cmd, length);
+    uhf_uart_send_wait(module->uart, cmd, length);
+    uhf_uart_set_baudrate(module->uart, baudrate);
+    module->uart->baudrate = baudrate;
 }
 
 bool m100_set_working_region(M100Module* module, WorkingRegion region) {
@@ -384,5 +379,5 @@ bool m100_set_power(M100Module* module, uint8_t* power) {
 }
 
 uint32_t m100_get_baudrate(M100Module* module) {
-    return module->baudrate;
+    return module->uart->baudrate;
 }

+ 4 - 6
uhf_module.h

@@ -1,15 +1,14 @@
 #pragma once
-
+#include <furi_hal.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
-#include <furi_hal.h>
+#include "uhf_uart.h"
 #include "uhf_tag.h"
 #include "uhf_buffer.h"
-#include "uhf_tag.h"
-#include <furi_hal.h>
 #include "uhf_module_settings.h"
 
+
 #define FRAME_START 0xBB
 #define FRAME_END 0x7E
 #define DEFAULT_BAUDRATE BAUD_RATES[BAUD_RATES_COUNT - 1]
@@ -34,12 +33,11 @@ typedef enum {
 typedef struct {
     M100ModuleInfo* info;
     WorkingRegion region;
-    uint32_t baudrate;
     uint16_t region_frequency;
     uint16_t transmitting_power;
     uint16_t max_transmitting_power;
     bool freq_hopping;
-    Buffer* buf;
+    UHFUart* uart;
 } M100Module;
 
 M100ModuleInfo* m100_module_info_alloc();

+ 8 - 6
uhf_uart.c

@@ -16,19 +16,21 @@ UHFUart* uhf_uart_alloc(){
     uart->bus = FuriHalBusUSART1;
     uart->handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
     uart->init_by_app = !furi_hal_bus_is_enabled(uart->bus);
-    uart->baudrate = UHF_UART_DEFAULT_BAUDRATE;
     uart->tick = UHF_UART_WAIT_TICK;
     if(uart->init_by_app){
         furi_hal_serial_init(uart->handle, uart->baudrate);
     }
-    uart->callback = NULL;
+    uhf_uart_set_baudrate(uart, UHF_UART_DEFAULT_BAUDRATE);
     uart->buffer = buffer_alloc(UHF_UART_RX_BUFFER_SIZE);
     uart->thread = furi_thread_alloc_ex("UHFUartWorker", UHF_UART_WORKER_STACK_SIZE, NULL, uart);
     furi_thread_start(uart->thread);
+    uhf_uart_set_receive_byte_callback(uart, uhf_uart_default_rx_callback, uart, false);
+    return uart;
 }   
 
 void uhf_uart_free(UHFUart* uart){
-    furi_thread_stop(uart->thread);
+    // furi_thread_flags_set(furi_thread_get_id(uart->thread), UHFUartWorkerExitingFlag);
+    furi_thread_join(uart->thread);
     furi_thread_free(uart->thread);
     buffer_free(uart->buffer);
     if(uart->init_by_app){
@@ -38,15 +40,15 @@ void uhf_uart_free(UHFUart* uart){
     free(uart);
 }
 
-void uhf_uart_set_receive_byte_callback(UHFUart* uart, FuriHalSerialAsyncRxCallback callback, bool report_errors, void *ctx){
-    furi_hal_serial_set_async_rx_callback(uart->handle, callback, report_errors, ctx);
+void uhf_uart_set_receive_byte_callback(UHFUart* uart, FuriHalSerialAsyncRxCallback callback, void *ctx, bool report_errors){
+    furi_hal_serial_async_rx_start(uart->handle, callback, ctx, report_errors);
 }
 
 void uhf_uart_send(UHFUart* uart, uint8_t* data, size_t size){
     furi_hal_serial_tx(uart->handle, data, size);
 }
 
-void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, uint16_t size){
+void uhf_uart_send_wait(UHFUart* uart, uint8_t* data, size_t size){
     uhf_uart_send(uart, data, size);
     furi_hal_serial_tx_wait_complete(uart->handle);
 }

+ 5 - 5
uhf_uart.h

@@ -4,8 +4,8 @@
 #include <stdbool.h>
 #include "uhf_buffer.h"
 
-#define UHF_UART_RX_BUFFER_SIZE 2048
-#define UHF_UART_WORKER_STACK_SIZE 1024
+#define UHF_UART_RX_BUFFER_SIZE 100
+#define UHF_UART_WORKER_STACK_SIZE 1 * 1024
 #define UHF_UART_DEFAULT_BAUDRATE 115200
 #define UHF_UART_FRAME_START 0xBB
 #define UHF_UART_FRAME_END 0x7E
@@ -18,7 +18,7 @@ typedef enum{
     UHFUartWorkerExitingFlag
 }UHFUartWorkerEventFlag;
 
-static void uhf_uart_received_byte_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void *ctx);
+// static void uhf_uart_received_byte_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void *ctx);
 
 typedef struct{
     FuriHalBus bus;
@@ -26,7 +26,7 @@ typedef struct{
     FuriThread *thread;
     CallbackFunction callback;
     Buffer *buffer;
-    uint16_t baudrate;
+    uint32_t baudrate;
     bool init_by_app;
     void *ctx;
     volatile int tick;
@@ -36,6 +36,6 @@ UHFUart* uhf_uart_alloc();
 void uhf_uart_free(UHFUart* uart);
 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_set_receive_byte_callback(UHFUart* uart, FuriHalSerialAsyncRxCallback callback, bool report_errors, void *ctx);
+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);
 

+ 1 - 1
uhf_worker.c

@@ -98,7 +98,7 @@ int32_t uhf_worker_task(void* ctx) {
 
 UHFWorker* uhf_worker_alloc() {
     UHFWorker* uhf_worker = (UHFWorker*)malloc(sizeof(UHFWorker));
-    uhf_worker->thread = furi_thread_alloc_ex("UHFWorker", 8 * 1024, uhf_worker_task, uhf_worker);
+    uhf_worker->thread = furi_thread_alloc_ex("UHFWorker", UHF_WORKER_STACK_SIZE, uhf_worker_task, uhf_worker);
     uhf_worker->module = m100_module_alloc();
     uhf_worker->callback = NULL;
     uhf_worker->ctx = NULL;

+ 2 - 0
uhf_worker.h

@@ -4,6 +4,8 @@
 #include <furi_hal.h>
 #include "uhf_module.h"
 
+#define UHF_WORKER_STACK_SIZE 1 * 1024
+
 typedef enum {
     // Init states
     UHFWorkerStateNone,