@@ -41,6 +41,7 @@ struct UART_Handle {
4141 UART_Bus bus_id ;
4242 CircularBuffer * rx_buffer ;
4343 CircularBuffer * tx_buffer ;
44+ CircularBuffer * original_tx_buffer ; /* Stored during passthrough */
4445 uint32_t volatile rx_dma_head ;
4546 UART_RxCallback rx_callback ;
4647 uint32_t rx_threshold ;
@@ -51,9 +52,6 @@ struct UART_Handle {
5152/* Global array to keep track of active UART handles */
5253static UART_Handle * g_active_handles [UART_BUS_COUNT ] = { nullptr };
5354
54- /* Store disabled TX buffers during passthrough */
55- static CircularBuffer * g_disabled_tx_buffers [2 ] = { nullptr };
56-
5755/**
5856 * @brief Get the number of available UART bus instances.
5957 *
@@ -264,6 +262,7 @@ UART_Handle *UART_init(
264262 handle -> bus_id = bus_id ;
265263 handle -> rx_buffer = rx_buffer ;
266264 handle -> tx_buffer = tx_buffer ;
265+ handle -> original_tx_buffer = nullptr ;
267266 handle -> rx_dma_head = 0 ;
268267 handle -> rx_callback = nullptr ;
269268 handle -> rx_threshold = 0 ;
@@ -504,22 +503,26 @@ static void passthrough_callback(UART_Handle *handle, uint32_t bytes_available)
504503
505504void UART_enable_passthrough (UART_Handle * handle1 , UART_Handle * handle2 )
506505{
507- // Only one passthrough pair is allowed at a time
508- if ((g_disabled_tx_buffers [0 ] != nullptr ) ||
509- (g_disabled_tx_buffers [1 ] != nullptr )) {
510- THROW (ERROR_RESOURCE_BUSY );
511- }
512-
513506 if (!handle1 || !handle1 -> initialized || !handle2 ||
514507 !handle2 -> initialized ) {
515508 THROW (ERROR_DEVICE_NOT_READY );
516509 }
517510
511+ // Check that handles are different
512+ if (handle1 == handle2 ) {
513+ THROW (ERROR_INVALID_ARGUMENT );
514+ }
515+
516+ // Check if either handle is already in passthrough mode
517+ if (handle1 -> passthrough_target || handle2 -> passthrough_target ) {
518+ THROW (ERROR_RESOURCE_BUSY );
519+ }
520+
518521 // Set up shared buffers:
519- // - Normal tx_buffers are temporarily disabled
522+ // - Store normal tx_buffers in each handle
520523 // - Redirect each handle's rx_buffer to the other handle's tx_buffer
521- g_disabled_tx_buffers [ 0 ] = handle1 -> tx_buffer ;
522- g_disabled_tx_buffers [ 1 ] = handle2 -> tx_buffer ;
524+ handle1 -> original_tx_buffer = handle1 -> tx_buffer ;
525+ handle2 -> original_tx_buffer = handle2 -> tx_buffer ;
523526 handle1 -> tx_buffer = handle2 -> rx_buffer ;
524527 handle2 -> tx_buffer = handle1 -> rx_buffer ;
525528
@@ -539,17 +542,17 @@ void UART_disable_passthrough(UART_Handle *handle1, UART_Handle *handle2)
539542 THROW (ERROR_INVALID_ARGUMENT );
540543 }
541544
545+ /* Disable passthrough callbacks */
546+ UART_set_rx_callback (handle1 , nullptr , 0 );
547+ UART_set_rx_callback (handle2 , nullptr , 0 );
548+
542549 /* Restore original TX buffers */
543- handle1 -> tx_buffer = g_disabled_tx_buffers [ 0 ] ;
544- handle2 -> tx_buffer = g_disabled_tx_buffers [ 1 ] ;
545- g_disabled_tx_buffers [ 0 ] = nullptr ;
546- g_disabled_tx_buffers [ 1 ] = nullptr ;
550+ handle1 -> tx_buffer = handle1 -> original_tx_buffer ;
551+ handle2 -> tx_buffer = handle2 -> original_tx_buffer ;
552+ handle1 -> original_tx_buffer = nullptr ;
553+ handle2 -> original_tx_buffer = nullptr ;
547554
548555 /* Clear passthrough targets */
549556 handle1 -> passthrough_target = nullptr ;
550557 handle2 -> passthrough_target = nullptr ;
551-
552- /* Disable RX callbacks */
553- UART_set_rx_callback (handle1 , nullptr , 0 );
554- UART_set_rx_callback (handle2 , nullptr , 0 );
555558}
0 commit comments