@@ -45,6 +45,7 @@ struct UART_Handle {
4545 UART_Bus bus_id ;
4646 CircularBuffer * rx_buffer ;
4747 CircularBuffer * tx_buffer ;
48+ CircularBuffer * original_tx_buffer ; /* Stored during passthrough */
4849 uint32_t volatile rx_dma_head ;
4950 UART_RxCallback rx_callback ;
5051 uint32_t rx_threshold ;
@@ -55,9 +56,6 @@ struct UART_Handle {
5556/* Global array to keep track of active UART handles */
5657static UART_Handle * g_active_handles [UART_BUS_COUNT ] = { nullptr };
5758
58- /* Store disabled TX buffers during passthrough */
59- static CircularBuffer * g_disabled_tx_buffers [2 ] = { nullptr };
60-
6159/**
6260 * @brief Get the number of available UART bus instances.
6361 *
@@ -276,6 +274,7 @@ UART_Handle *UART_init(
276274 handle -> bus_id = bus_id ;
277275 handle -> rx_buffer = rx_buffer ;
278276 handle -> tx_buffer = tx_buffer ;
277+ handle -> original_tx_buffer = nullptr ;
279278 handle -> rx_dma_head = 0 ;
280279 handle -> rx_callback = nullptr ;
281280 handle -> rx_threshold = 0 ;
@@ -516,22 +515,26 @@ static void passthrough_callback(UART_Handle *handle, uint32_t bytes_available)
516515
517516void UART_enable_passthrough (UART_Handle * handle1 , UART_Handle * handle2 )
518517{
519- // Only one passthrough pair is allowed at a time
520- if ((g_disabled_tx_buffers [0 ] != nullptr ) ||
521- (g_disabled_tx_buffers [1 ] != nullptr )) {
522- THROW (ERROR_RESOURCE_BUSY );
523- }
524-
525518 if (!handle1 || !handle1 -> initialized || !handle2 ||
526519 !handle2 -> initialized ) {
527520 THROW (ERROR_DEVICE_NOT_READY );
528521 }
529522
523+ // Check that handles are different
524+ if (handle1 == handle2 ) {
525+ THROW (ERROR_INVALID_ARGUMENT );
526+ }
527+
528+ // Check if either handle is already in passthrough mode
529+ if (handle1 -> passthrough_target || handle2 -> passthrough_target ) {
530+ THROW (ERROR_RESOURCE_BUSY );
531+ }
532+
530533 // Set up shared buffers:
531- // - Normal tx_buffers are temporarily disabled
534+ // - Store normal tx_buffers in each handle
532535 // - Redirect each handle's rx_buffer to the other handle's tx_buffer
533- g_disabled_tx_buffers [ 0 ] = handle1 -> tx_buffer ;
534- g_disabled_tx_buffers [ 1 ] = handle2 -> tx_buffer ;
536+ handle1 -> original_tx_buffer = handle1 -> tx_buffer ;
537+ handle2 -> original_tx_buffer = handle2 -> tx_buffer ;
535538 handle1 -> tx_buffer = handle2 -> rx_buffer ;
536539 handle2 -> tx_buffer = handle1 -> rx_buffer ;
537540
@@ -551,17 +554,17 @@ void UART_disable_passthrough(UART_Handle *handle1, UART_Handle *handle2)
551554 THROW (ERROR_INVALID_ARGUMENT );
552555 }
553556
557+ /* Disable passthrough callbacks */
558+ UART_set_rx_callback (handle1 , nullptr , 0 );
559+ UART_set_rx_callback (handle2 , nullptr , 0 );
560+
554561 /* Restore original TX buffers */
555- handle1 -> tx_buffer = g_disabled_tx_buffers [ 0 ] ;
556- handle2 -> tx_buffer = g_disabled_tx_buffers [ 1 ] ;
557- g_disabled_tx_buffers [ 0 ] = nullptr ;
558- g_disabled_tx_buffers [ 1 ] = nullptr ;
562+ handle1 -> tx_buffer = handle1 -> original_tx_buffer ;
563+ handle2 -> tx_buffer = handle2 -> original_tx_buffer ;
564+ handle1 -> original_tx_buffer = nullptr ;
565+ handle2 -> original_tx_buffer = nullptr ;
559566
560567 /* Clear passthrough targets */
561568 handle1 -> passthrough_target = nullptr ;
562569 handle2 -> passthrough_target = nullptr ;
563-
564- /* Disable RX callbacks */
565- UART_set_rx_callback (handle1 , nullptr , 0 );
566- UART_set_rx_callback (handle2 , nullptr , 0 );
567570}
0 commit comments