From 822dfe61f1a20d904f0cb503f9af77f881e26b73 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Sat, 20 Jan 2024 00:30:51 +0300 Subject: [PATCH] merge fixes pt1 by Willy-JL --- .../views/camera_suite_view_camera.c | 50 ++++++++-------- .../views/camera_suite_view_camera.h | 7 ++- base_pack/dap_link/dap_link.c | 60 +++++++++---------- base_pack/dap_link/usb/dap_v2_usb.c | 26 ++++---- base_pack/esp8266_deauth/esp8266_deauth.c | 28 +++++---- 5 files changed, 88 insertions(+), 83 deletions(-) diff --git a/base_pack/camera_suite/views/camera_suite_view_camera.c b/base_pack/camera_suite/views/camera_suite_view_camera.c index bf26b6b2085..df94808693f 100644 --- a/base_pack/camera_suite/views/camera_suite_view_camera.c +++ b/base_pack/camera_suite/views/camera_suite_view_camera.c @@ -196,7 +196,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { UNUSED(model); // Stop camera stream. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'s'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'s'}, 1); furi_delay_ms(50); // Go back to the main menu. @@ -217,13 +217,13 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { if(model->is_inverted) { // Camera: Set invert to false on the ESP32-CAM. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'i'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'i'}, 1); furi_delay_ms(50); model->is_inverted = false; } else { // Camera: Set invert to true on the ESP32-CAM. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'I'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'I'}, 1); furi_delay_ms(50); model->is_inverted = true; @@ -246,13 +246,13 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { if(model->is_dithering_enabled) { // Camera: Disable dithering. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'d'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'d'}, 1); furi_delay_ms(50); model->is_dithering_enabled = false; } else { // Camera: Enable dithering. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'D'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'D'}, 1); furi_delay_ms(50); model->is_dithering_enabled = true; @@ -276,7 +276,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { camera_suite_led_set_rgb(instance->context, 0, 0, 255); // Camera: Increase contrast. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'C'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'C'}, 1); furi_delay_ms(50); instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context); @@ -297,7 +297,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { camera_suite_led_set_rgb(instance->context, 0, 0, 255); // Camera: Reduce contrast. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'c'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'c'}, 1); furi_delay_ms(50); instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context); @@ -316,7 +316,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) { camera_suite_led_set_rgb(instance->context, 0, 0, 255); // @todo - Save picture directly to ESP32-CAM. - // furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'P'}, 1); + // furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'P'}, 1); // Save currently displayed image to the Flipper Zero SD card. save_image_to_flipper_sd_card(model); @@ -349,22 +349,22 @@ static void camera_suite_view_camera_enter(void* context) { CameraSuite* instance_context = instance->context; // Start camera stream. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'S'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'S'}, 1); furi_delay_ms(50); // Get/set dither type. uint8_t dither_type = instance_context->dither; - furi_hal_uart_tx(FuriHalUartIdUSART1, &dither_type, 1); + furi_hal_serial_tx(instance->serial_handle, &dither_type, 1); furi_delay_ms(50); // Make sure the camera is not inverted. - furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'i'}, 1); + furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'i'}, 1); furi_delay_ms(50); // Toggle flash on or off based on the current state. If the user has this // on the flash will stay on the entire time the user is in the camera view. uint8_t flash_state = instance_context->flash ? 'F' : 'f'; - furi_hal_uart_tx(FuriHalUartIdUSART1, &flash_state, 1); + furi_hal_serial_tx(instance->serial_handle, &flash_state, 1); furi_delay_ms(50); with_view_model( @@ -374,9 +374,9 @@ static void camera_suite_view_camera_enter(void* context) { true); } -static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) { - furi_assert(uartIrqEvent); - furi_assert(data); +static void + camera_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) { + furi_assert(handle); furi_assert(context); // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`. @@ -384,7 +384,8 @@ static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* cont // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the // `rx_stream` and set the `WorkerEventRx` flag. - if(uartIrqEvent == UartIrqEventRXNE) { + if(event == FuriHalSerialRxEventData) { + uint8_t data = furi_hal_serial_async_rx(handle); furi_stream_buffer_send(instance->rx_stream, &data, 1, 0); furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx); } @@ -512,14 +513,13 @@ CameraSuiteViewCamera* camera_suite_view_camera_alloc() { instance->worker_thread = thread; furi_thread_start(instance->worker_thread); - // Enable uart listener - furi_hal_console_disable(); - // 115200 is the default baud rate for the ESP32-CAM. - furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400); + instance->serial_handle = furi_hal_serial_control_acquire(UART_CH); + furi_check(instance->serial_handle); + furi_hal_serial_init(instance->serial_handle, 230400); // Enable UART1 and set the IRQ callback. - furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance); + furi_hal_serial_async_rx_start(instance->serial_handle, camera_on_irq_cb, instance, false); return instance; } @@ -527,17 +527,17 @@ CameraSuiteViewCamera* camera_suite_view_camera_alloc() { void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) { furi_assert(instance); - // Remove the IRQ callback. - furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL); - // Free the worker thread. + furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventStop); + furi_thread_join(instance->worker_thread); furi_thread_free(instance->worker_thread); // Free the allocated stream buffer. furi_stream_buffer_free(instance->rx_stream); // Re-enable the console. - // furi_hal_console_enable(); + furi_hal_serial_deinit(instance->serial_handle); + furi_hal_serial_control_release(instance->serial_handle); with_view_model( instance->view, UartDumpModel * model, { UNUSED(model); }, true); diff --git a/base_pack/camera_suite/views/camera_suite_view_camera.h b/base_pack/camera_suite/views/camera_suite_view_camera.h index 6efbbb8129e..ec781175c82 100644 --- a/base_pack/camera_suite/views/camera_suite_view_camera.h +++ b/base_pack/camera_suite/views/camera_suite_view_camera.h @@ -2,8 +2,8 @@ #include #include -#include -#include +#include +#include #include #include #include @@ -19,6 +19,8 @@ #include +#define UART_CH (FuriHalSerialIdUsart) + #define BITMAP_HEADER_LENGTH 62 #define FRAME_BIT_DEPTH 1 #define FRAME_BUFFER_LENGTH 1024 @@ -49,6 +51,7 @@ typedef void (*CameraSuiteViewCameraCallback)(CameraSuiteCustomEvent event, void typedef struct CameraSuiteViewCamera { CameraSuiteViewCameraCallback callback; FuriStreamBuffer* rx_stream; + FuriHalSerialHandle* serial_handle; FuriThread* worker_thread; NotificationApp* notification; View* view; diff --git a/base_pack/dap_link/dap_link.c b/base_pack/dap_link/dap_link.c index c55ebad4ff9..b20100cccf4 100644 --- a/base_pack/dap_link/dap_link.c +++ b/base_pack/dap_link/dap_link.c @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -275,14 +275,15 @@ typedef enum { typedef struct { FuriStreamBuffer* rx_stream; FuriThreadId thread_id; - FuriHalUartId uart_id; + FuriHalSerialHandle* serial_handle; struct usb_cdc_line_coding line_coding; } CDCProcess; -static void cdc_uart_irq_cb(UartIrqEvent ev, uint8_t data, void* ctx) { +static void cdc_uart_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* ctx) { CDCProcess* app = ctx; - if(ev == UartIrqEventRXNE) { + if(event == FuriHalSerialRxEventData) { + uint8_t data = furi_hal_serial_async_rx(handle); furi_stream_buffer_send(app->rx_stream, &data, 1, 0); furi_thread_flags_set(app->thread_id, CdcThreadEventUartRx); } @@ -316,54 +317,52 @@ static void cdc_usb_config_callback(struct usb_cdc_line_coding* config, void* co furi_thread_flags_set(app->thread_id, CdcThreadEventCdcConfig); } -static FuriHalUartId cdc_init_uart( +static void cdc_init_uart( + CDCProcess* app, DapUartType type, DapUartTXRX swap, uint32_t baudrate, - void (*cb)(UartIrqEvent ev, uint8_t data, void* ctx), + void (*cb)(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* ctx), void* ctx) { - FuriHalUartId uart_id = FuriHalUartIdUSART1; if(baudrate == 0) baudrate = 115200; switch(type) { case DapUartTypeUSART1: - uart_id = FuriHalUartIdUSART1; - furi_hal_console_disable(); - furi_hal_uart_deinit(uart_id); + app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart); + furi_check(app->serial_handle); if(swap == DapUartTXRXSwap) { LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_SWAPPED); } else { LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD); } - furi_hal_uart_init(uart_id, baudrate); - furi_hal_uart_set_irq_cb(uart_id, cb, ctx); + furi_hal_serial_init(app->serial_handle, baudrate); + furi_hal_serial_async_rx_start(app->serial_handle, cb, ctx, false); break; case DapUartTypeLPUART1: - uart_id = FuriHalUartIdLPUART1; - furi_hal_uart_deinit(uart_id); + app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdLpuart); + furi_check(app->serial_handle); if(swap == DapUartTXRXSwap) { LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_SWAPPED); } else { LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD); } - furi_hal_uart_init(uart_id, baudrate); - furi_hal_uart_set_irq_cb(uart_id, cb, ctx); + furi_hal_serial_init(app->serial_handle, baudrate); + furi_hal_serial_async_rx_start(app->serial_handle, cb, ctx, false); break; } - - return uart_id; } -static void cdc_deinit_uart(DapUartType type) { +static void cdc_deinit_uart(CDCProcess* app, DapUartType type) { switch(type) { case DapUartTypeUSART1: - furi_hal_uart_deinit(FuriHalUartIdUSART1); + furi_hal_serial_deinit(app->serial_handle); LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD); - furi_hal_console_init(); + furi_hal_serial_control_release(app->serial_handle); break; case DapUartTypeLPUART1: - furi_hal_uart_deinit(FuriHalUartIdLPUART1); + furi_hal_serial_deinit(app->serial_handle); LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD); + furi_hal_serial_control_release(app->serial_handle); break; } } @@ -385,8 +384,8 @@ static int32_t dap_cdc_process(void* p) { const uint8_t rx_buffer_size = 64; uint8_t* rx_buffer = malloc(rx_buffer_size); - app->uart_id = cdc_init_uart( - uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, cdc_uart_irq_cb, app); + cdc_init_uart( + app, uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, cdc_uart_irq_cb, app); dap_cdc_usb_set_context(app); dap_cdc_usb_set_rx_callback(cdc_usb_rx_callback); @@ -405,7 +404,7 @@ static int32_t dap_cdc_process(void* p) { if(dap_state->cdc_baudrate != app->line_coding.dwDTERate) { dap_state->cdc_baudrate = app->line_coding.dwDTERate; if(dap_state->cdc_baudrate > 0) { - furi_hal_uart_set_br(app->uart_id, dap_state->cdc_baudrate); + furi_hal_serial_set_br(app->serial_handle, dap_state->cdc_baudrate); } } } @@ -424,7 +423,7 @@ static int32_t dap_cdc_process(void* p) { if(events & CdcThreadEventCdcRx) { size_t len = dap_cdc_usb_rx(rx_buffer, rx_buffer_size); if(len > 0) { - furi_hal_uart_tx(app->uart_id, rx_buffer, len); + furi_hal_serial_tx(app->serial_handle, rx_buffer, len); } dap_state->cdc_tx_counter += len; } @@ -432,10 +431,11 @@ static int32_t dap_cdc_process(void* p) { if(events & CdcThreadEventApplyConfig) { if(uart_pins_prev != dap_app->config.uart_pins || uart_swap_prev != dap_app->config.uart_swap) { - cdc_deinit_uart(uart_pins_prev); + cdc_deinit_uart(app, uart_pins_prev); uart_pins_prev = dap_app->config.uart_pins; uart_swap_prev = dap_app->config.uart_swap; - app->uart_id = cdc_init_uart( + cdc_init_uart( + app, uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, @@ -456,7 +456,7 @@ static int32_t dap_cdc_process(void* p) { } } - cdc_deinit_uart(uart_pins_prev); + cdc_deinit_uart(app, uart_pins_prev); free(rx_buffer); furi_stream_buffer_free(app->rx_stream); free(app); diff --git a/base_pack/dap_link/usb/dap_v2_usb.c b/base_pack/dap_link/usb/dap_v2_usb.c index f25ede6f0b2..74bc3bbc8b4 100644 --- a/base_pack/dap_link/usb/dap_v2_usb.c +++ b/base_pack/dap_link/usb/dap_v2_usb.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include "dap_v2_usb.h" @@ -484,8 +484,8 @@ void furi_console_log_printf(const char* format, ...) { va_start(args, format); vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); - furi_hal_console_puts(buffer); - furi_hal_console_puts("\r\n"); + furi_log_puts(buffer); + furi_log_puts("\r\n"); UNUSED(format); } #else @@ -800,41 +800,41 @@ static usbd_respond hid_ep_config(usbd_device* dev, uint8_t cfg) { static void dump_request_type(uint8_t type) { switch(type & USB_REQ_DIRECTION) { case USB_REQ_HOSTTODEV: - furi_hal_console_puts("host to dev, "); + furi_log_puts("host to dev, "); break; case USB_REQ_DEVTOHOST: - furi_hal_console_puts("dev to host, "); + furi_log_puts("dev to host, "); break; } switch(type & USB_REQ_TYPE) { case USB_REQ_STANDARD: - furi_hal_console_puts("standard, "); + furi_log_puts("standard, "); break; case USB_REQ_CLASS: - furi_hal_console_puts("class, "); + furi_log_puts("class, "); break; case USB_REQ_VENDOR: - furi_hal_console_puts("vendor, "); + furi_log_puts("vendor, "); break; } switch(type & USB_REQ_RECIPIENT) { case USB_REQ_DEVICE: - furi_hal_console_puts("device"); + furi_log_puts("device"); break; case USB_REQ_INTERFACE: - furi_hal_console_puts("interface"); + furi_log_puts("interface"); break; case USB_REQ_ENDPOINT: - furi_hal_console_puts("endpoint"); + furi_log_puts("endpoint"); break; case USB_REQ_OTHER: - furi_hal_console_puts("other"); + furi_log_puts("other"); break; } - furi_hal_console_puts("\r\n"); + furi_log_puts("\r\n"); } #else #define dump_request_type(...) diff --git a/base_pack/esp8266_deauth/esp8266_deauth.c b/base_pack/esp8266_deauth/esp8266_deauth.c index 15b86584526..48b9a79d858 100644 --- a/base_pack/esp8266_deauth/esp8266_deauth.c +++ b/base_pack/esp8266_deauth/esp8266_deauth.c @@ -1,8 +1,8 @@ #include -#include #include #include -#include +#include +#include #include #include #include @@ -13,6 +13,8 @@ #include "FlipperZeroWiFiDeauthModuleDefines.h" +#define UART_CH (FuriHalSerialIdUsart) + #define DEAUTH_APP_DEBUG 0 #if DEAUTH_APP_DEBUG @@ -26,7 +28,6 @@ #define DEAUTH_APP_LOG_E(format, ...) #endif // WIFI_APP_DEBUG -#define DISABLE_CONSOLE !DEAUTH_APP_DEBUG #define ENABLE_MODULE_POWER 1 #define ENABLE_MODULE_DETECTION 1 @@ -65,6 +66,7 @@ typedef struct SWiFiDeauthApp { FuriThread* m_worker_thread; //NotificationApp* m_notification; FuriStreamBuffer* m_rx_stream; + FuriHalSerialHandle* serial_handle; SGpioButtons m_GpioButtons; bool m_wifiDeauthModuleInitialized; @@ -209,14 +211,16 @@ static void furi_message_queue_put(event_queue, &event, FuriWaitForever); } -static void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) { +static void + uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) { furi_assert(context); SWiFiDeauthApp* app = context; DEAUTH_APP_LOG_I("uart_echo_on_irq_cb"); - if(ev == UartIrqEventRXNE) { + if(event == FuriHalSerialRxEventData) { + uint8_t data = furi_hal_serial_async_rx(handle); DEAUTH_APP_LOG_I("ev == UartIrqEventRXNE"); furi_stream_buffer_send(app->m_rx_stream, &data, 1, 0); furi_thread_flags_set(furi_thread_get_id(app->m_worker_thread), WorkerEventRx); @@ -387,11 +391,10 @@ int32_t esp8266_deauth_app(void* p) { DEAUTH_APP_LOG_I("UART thread allocated"); // Enable uart listener -#if DISABLE_CONSOLE - furi_hal_console_disable(); -#endif - furi_hal_uart_set_br(FuriHalUartIdUSART1, FLIPPERZERO_SERIAL_BAUD); - furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_on_irq_cb, app); + app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart); + furi_check(app->serial_handle); + furi_hal_serial_init(app->serial_handle, FLIPPERZERO_SERIAL_BAUD); + furi_hal_serial_async_rx_start(app->serial_handle, uart_on_irq_cb, app, false); DEAUTH_APP_LOG_I("UART Listener created"); SPluginEvent event; @@ -505,9 +508,8 @@ int32_t esp8266_deauth_app(void* p) { furi_hal_gpio_init(&gpio_ext_pb3, GpioModeAnalog, GpioPullNo, GpioSpeedLow); furi_hal_gpio_init(&gpio_ext_pa4, GpioModeAnalog, GpioPullNo, GpioSpeedLow); -#if DISABLE_CONSOLE - furi_hal_console_enable(); -#endif + furi_hal_serial_deinit(app->serial_handle); + furi_hal_serial_control_release(app->serial_handle); //*app->m_originalBufferLocation = app->m_originalBuffer;