11#include "uart.h"
22
3- #define UART_CH (FuriHalUartIdUSART1 )
4- #define LP_UART_CH (FuriHalUartIdLPUART1 )
5- #define BAUDRATE (115200 )
3+ #define UART_CH (FuriHalSerialIdUsart )
4+ #define LP_UART_CH (FuriHalSerialIdLpuart )
5+ #define BAUDRATE (115200UL )
66
77struct Uart {
88 void * app ;
9- FuriHalUartId channel ;
109 FuriThread * rx_thread ;
10+ FuriHalSerialHandle * serial_handle ;
11+ FuriHalSerialId channel ;
12+ FuriThread * worker_thread ;
1113 FuriStreamBuffer * rx_stream ;
1214 uint8_t rx_buf [RX_BUF_SIZE + 1 ];
1315 void (* handle_rx_data_cb )(uint8_t * buf , size_t len , void * context );
1416};
1517
16-
1718typedef enum {
1819 WorkerEvtStop = (1 << 0 ),
1920 WorkerEvtRxDone = (1 << 1 ),
20- } WorkerEvtFlags ;
21+ } WorkerEventFlags ;
22+
23+ #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
24+
25+
2126
2227void uart_set_handle_rx_data_cb (
2328 Uart * uart ,
@@ -26,17 +31,29 @@ void uart_set_handle_rx_data_cb(
2631 uart -> handle_rx_data_cb = handle_rx_data_cb ;
2732}
2833
29- #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
30-
31- void uart_on_irq_cb (UartIrqEvent ev , uint8_t data , void * context ) {
34+ static void wifi_marauder_uart_on_irq_cb (
35+ FuriHalSerialHandle * handle ,
36+ FuriHalSerialRxEvent event ,
37+ void * context ) {
3238 Uart * uart = (Uart * )context ;
3339
34- if (ev == UartIrqEventRXNE ) {
40+ if (event == FuriHalSerialRxEventData ) {
41+ uint8_t data = furi_hal_serial_async_rx (handle );
3542 furi_stream_buffer_send (uart -> rx_stream , & data , 1 , 0 );
3643 furi_thread_flags_set (furi_thread_get_id (uart -> rx_thread ), WorkerEvtRxDone );
3744 }
3845}
3946
47+ static void uart_on_irq_cb (FuriHalSerialHandle * handle , FuriHalSerialRxEvent event , void * context ) {
48+ Uart * uart = (Uart * )context ;
49+ UNUSED (handle );
50+
51+ if (event & (FuriHalSerialRxEventData | FuriHalSerialRxEventIdle )) {
52+ uint8_t data = furi_hal_serial_async_rx (handle );
53+ furi_stream_buffer_send (uart -> rx_stream , & data , 1 , 0 );
54+ furi_thread_flags_set (furi_thread_get_id (uart -> rx_thread ), WorkerEvtRxDone );
55+ }
56+ }
4057
4158// Define una constante para el prefijo que estamos buscando
4259#define JSON_PREFIX "JSON:"
@@ -50,26 +67,25 @@ static bool json_capture_active = false;
5067// Prototipo de la función
5168// static void process_json_buffer();
5269
53-
5470static void process_json_buffer (void * context ) {
5571 Uart * uart = (Uart * )context ;
5672 // Agregamos el terminador nulo al final del buffer
5773 json_buffer [json_buffer_index ] = '\0' ;
58- if (uart -> handle_rx_data_cb ) {
59- uart -> handle_rx_data_cb ((uint8_t * )json_buffer , json_buffer_index , uart -> app );
74+ if (uart -> handle_rx_data_cb ) {
75+ uart -> handle_rx_data_cb ((uint8_t * )json_buffer , json_buffer_index , uart -> app );
6076 memset (json_buffer , 0 , sizeof (json_buffer ));
6177 }
62-
78+
6379 // Reiniciamos el buffer
6480 json_buffer_index = 0 ;
6581}
6682
6783static void uart_echo_push_to_list (void * context , uint8_t data ) {
6884 Uart * uart = (Uart * )context ;
69- if (!json_capture_active ) {
70- if (data == JSON_PREFIX [json_buffer_index ]) {
85+ if (!json_capture_active ) {
86+ if (data == JSON_PREFIX [json_buffer_index ]) {
7187 json_buffer [json_buffer_index ++ ] = data ; // Agregar el carácter al buffer
72- if (json_buffer_index == strlen (JSON_PREFIX )) {
88+ if (json_buffer_index == strlen (JSON_PREFIX )) {
7389 // Encontramos el prefijo, comenzamos a capturar
7490 json_buffer_index = 0 ;
7591 json_capture_active = true;
@@ -81,7 +97,7 @@ static void uart_echo_push_to_list(void* context, uint8_t data) {
8197 } else {
8298 // Capturamos caracteres hasta encontrar '\n'
8399 json_buffer [json_buffer_index ++ ] = data ;
84- if (data == '\n' ) {
100+ if (data == '\n' ) {
85101 // Terminamos de capturar la línea, procesamos el buffer
86102 json_capture_active = false;
87103 process_json_buffer (uart );
@@ -93,7 +109,8 @@ static int32_t uart_worker(void* context) {
93109 Uart * uart = (Uart * )context ;
94110
95111 while (1 ) {
96- uint32_t events = furi_thread_flags_wait (WORKER_ALL_RX_EVENTS , FuriFlagWaitAny , FuriWaitForever );
112+ uint32_t events =
113+ furi_thread_flags_wait (WORKER_ALL_RX_EVENTS , FuriFlagWaitAny , FuriWaitForever );
97114 furi_check ((events & FuriFlagError ) == 0 );
98115
99116 if (events & WorkerEvtStop ) break ;
@@ -103,17 +120,20 @@ static int32_t uart_worker(void* context) {
103120 do {
104121 uint8_t data [64 ];
105122 length = furi_stream_buffer_receive (uart -> rx_stream , data , 64 , 0 );
106- // FURI_LOG_I("UART", "[in]: %s", (char*)data);
123+
107124 if (length > 0 ) {
108125 for (size_t i = 0 ; i < length ; i ++ ) {
109126 uart_echo_push_to_list (uart , data [i ]);
127+ // FURI_LOG_I("UART", "[in]: %c - %d", (const char)data[i], data[i]);
110128 }
111129 }
112130 } while (length > 0 );
113131 } else if (uart -> channel == LP_UART_CH ) {
114- size_t len = furi_stream_buffer_receive (uart -> rx_stream , uart -> rx_buf , RX_BUF_SIZE , 0 );
132+ size_t len =
133+ furi_stream_buffer_receive (uart -> rx_stream , uart -> rx_buf , RX_BUF_SIZE , 0 );
115134 if (len > 0 ) {
116- if (uart -> handle_rx_data_cb ) uart -> handle_rx_data_cb (uart -> rx_buf , len , uart -> app );
135+ if (uart -> handle_rx_data_cb )
136+ uart -> handle_rx_data_cb (uart -> rx_buf , len , uart -> app );
117137 }
118138 }
119139 }
@@ -122,19 +142,13 @@ static int32_t uart_worker(void* context) {
122142
123143 return 0 ;
124144}
125-
126- void uart_tx ( uint8_t * data , size_t len ) {
127- furi_hal_uart_tx ( UART_CH , data , len );
145+ void uart_tx ( void * app , uint8_t * data , size_t len ) {
146+ Uart * uart = ( Uart * ) app ;
147+ furi_hal_serial_tx ( uart -> serial_handle , data , len );
128148}
129149
130- void lp_uart_tx (uint8_t * data , size_t len ) {
131- furi_hal_uart_tx (LP_UART_CH , data , len );
132- }
133-
134- Uart *
135- _uart_init (void * app , FuriHalUartId channel , const char * thread_name ) {
150+ Uart * _uart_init (void * app , FuriHalSerialId channel , const char * thread_name ) {
136151 Uart * uart = (Uart * )malloc (sizeof (Uart ));
137-
138152 uart -> app = app ;
139153 uart -> channel = channel ;
140154 uart -> rx_stream = furi_stream_buffer_alloc (RX_BUF_SIZE , 1 );
@@ -144,13 +158,18 @@ Uart*
144158 furi_thread_set_context (uart -> rx_thread , uart );
145159 furi_thread_set_callback (uart -> rx_thread , uart_worker );
146160 furi_thread_start (uart -> rx_thread );
147- if (channel == FuriHalUartIdUSART1 ) {
148- furi_hal_console_disable ();
149- } else if (channel == FuriHalUartIdLPUART1 ) {
150- furi_hal_uart_init (channel , BAUDRATE );
161+ uart -> serial_handle = furi_hal_serial_control_acquire (channel );
162+ if (!uart -> serial_handle ) {
163+ furi_delay_ms (5000 );
151164 }
152- furi_hal_uart_set_br (channel , BAUDRATE );
153- furi_hal_uart_set_irq_cb (channel , uart_on_irq_cb , uart );
165+ furi_check (uart -> serial_handle );
166+ furi_hal_serial_init (uart -> serial_handle , BAUDRATE );
167+ furi_hal_serial_async_rx_start (
168+ uart -> serial_handle ,
169+ channel == FuriHalSerialIdUsart ? uart_on_irq_cb : wifi_marauder_uart_on_irq_cb ,
170+ uart ,
171+ false);
172+
154173
155174 return uart ;
156175}
@@ -166,15 +185,12 @@ Uart* lp_uart_init(void* app) {
166185void uart_free (Uart * uart ) {
167186 furi_assert (uart );
168187
169- furi_thread_flags_set (furi_thread_get_id (uart -> rx_thread ), WorkerEvtStop );
188+ furi_thread_flags_set (furi_thread_get_id (uart -> rx_thread ), WorkerEvtStop );
170189 furi_thread_join (uart -> rx_thread );
171190 furi_thread_free (uart -> rx_thread );
172191
173- furi_hal_uart_set_irq_cb (uart -> channel , NULL , NULL );
174- if (uart -> channel == FuriHalUartIdLPUART1 ) {
175- furi_hal_uart_deinit (uart -> channel );
176- }
177- furi_hal_console_enable ();
192+ furi_hal_serial_deinit (uart -> serial_handle );
193+ furi_hal_serial_control_release (uart -> serial_handle );
178194
179195 free (uart );
180196}
0 commit comments