@@ -78,8 +78,14 @@ typedef enum {
7878} uart_index_t ;
7979
8080static UART_HandleTypeDef * uart_handlers [UART_NUM ] = {NULL };
81-
82- static serial_t serial_debug = { .uart = NP , .index = UART_NUM };
81+ static serial_t serial_debug = {
82+ .uart = NP ,
83+ .pin_tx = NC ,
84+ .pin_rx = NC ,
85+ .pin_rts = NC ,
86+ .pin_cts = NC ,
87+ .index = UART_NUM
88+ };
8389
8490/* Aim of the function is to get serial_s pointer using huart pointer */
8591/* Highly inspired from magical linux kernel's "container_of" */
@@ -115,22 +121,30 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
115121
116122 /* Pin Tx must not be NP */
117123 if (uart_tx == NP ) {
118- core_debug ("ERROR: [U(S)ART] Tx pin has no peripheral!\n" );
124+ if (obj != & serial_debug ) {
125+ core_debug ("ERROR: [U(S)ART] Tx pin has no peripheral!\n" );
126+ }
119127 return ;
120128 }
121129 /* Pin Rx must not be NP if not half-duplex */
122130 if ((obj -> pin_rx != NC ) && (uart_rx == NP )) {
123- core_debug ("ERROR: [U(S)ART] Rx pin has no peripheral!\n" );
131+ if (obj != & serial_debug ) {
132+ core_debug ("ERROR: [U(S)ART] Rx pin has no peripheral!\n" );
133+ }
124134 return ;
125135 }
126136 /* Pin RTS must not be NP if flow control is enabled */
127137 if ((obj -> pin_rts != NC ) && (uart_rts == NP )) {
128- core_debug ("ERROR: [U(S)ART] RTS pin has no peripheral!\n" );
138+ if (obj != & serial_debug ) {
139+ core_debug ("ERROR: [U(S)ART] RTS pin has no peripheral!\n" );
140+ }
129141 return ;
130142 }
131143 /* Pin CTS must not be NP if flow control is enabled */
132144 if ((obj -> pin_cts != NC ) && (uart_cts == NP )) {
133- core_debug ("ERROR: [U(S)ART] CTS pin has no peripheral!\n" );
145+ if (obj != & serial_debug ) {
146+ core_debug ("ERROR: [U(S)ART] CTS pin has no peripheral!\n" );
147+ }
134148 return ;
135149 }
136150
@@ -144,7 +158,9 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
144158 obj -> uart = pinmap_merge_peripheral (obj -> uart , uart_cts );
145159
146160 if (obj -> uart == NP ) {
147- core_debug ("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n" );
161+ if (obj != & serial_debug ) {
162+ core_debug ("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n" );
163+ }
148164 return ;
149165 }
150166
@@ -660,22 +676,6 @@ void uart_config_lowpower(serial_t *obj)
660676}
661677#endif
662678
663- /**
664- * @brief write the data on the uart
665- * @param obj : pointer to serial_t structure
666- * @param data : byte to write
667- * @param size : number of data to write
668- * @retval The number of bytes written
669- */
670- size_t uart_write (serial_t * obj , uint8_t data , uint16_t size )
671- {
672- if (HAL_UART_Transmit (uart_handlers [obj -> index ], & data , size , TX_TIMEOUT ) == HAL_OK ) {
673- return size ;
674- } else {
675- return 0 ;
676- }
677- }
678-
679679/**
680680 * @brief Function called to initialize the debug uart interface
681681 * @note Call only if debug U(S)ART peripheral is not already initialized
@@ -686,13 +686,12 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
686686void uart_debug_init (void )
687687{
688688 if (DEBUG_UART != NP ) {
689- serial_debug .pin_rx = pinmap_pin (DEBUG_UART , PinMap_UART_RX );
690689#if defined(DEBUG_PINNAME_TX )
691690 serial_debug .pin_tx = DEBUG_PINNAME_TX ;
692691#else
693692 serial_debug .pin_tx = pinmap_pin (DEBUG_UART , PinMap_UART_TX );
694693#endif
695-
694+ /* serial_debug.pin_rx set by default to NC to configure in half duplex mode */
696695 uart_init (& serial_debug , DEBUG_UART_BAUDRATE , UART_WORDLENGTH_8B , UART_PARITY_NONE , UART_STOPBITS_1 );
697696 }
698697}
@@ -706,11 +705,13 @@ void uart_debug_init(void)
706705size_t uart_debug_write (uint8_t * data , uint32_t size )
707706{
708707 uint32_t tickstart = HAL_GetTick ();
708+ serial_t * obj = NULL ;
709709
710- if (DEBUG_UART == NP ) {
711- return 0 ;
712- }
713710 if (serial_debug .index >= UART_NUM ) {
711+ if (DEBUG_UART == NP ) {
712+ return 0 ;
713+ }
714+
714715 /* Search if DEBUG_UART already initialized */
715716 for (serial_debug .index = 0 ; serial_debug .index < UART_NUM ; serial_debug .index ++ ) {
716717 if (uart_handlers [serial_debug .index ] != NULL ) {
@@ -726,24 +727,22 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
726727 if (serial_debug .index >= UART_NUM ) {
727728 return 0 ;
728729 }
729- } else {
730- serial_t * obj = get_serial_obj (uart_handlers [serial_debug .index ]);
731- if (obj ) {
732- serial_debug .irq = obj -> irq ;
733- }
734730 }
735731 }
732+ obj = get_serial_obj (uart_handlers [serial_debug .index ]);
733+ if (!obj ) {
734+ return 0 ;
735+ }
736736
737- HAL_NVIC_DisableIRQ (serial_debug .irq );
738-
739- while (HAL_UART_Transmit (uart_handlers [serial_debug .index ], data , size , TX_TIMEOUT ) != HAL_OK ) {
740- if ((HAL_GetTick () - tickstart ) >= TX_TIMEOUT ) {
741- size = 0 ;
742- break ;
737+ while (serial_tx_active (obj )) {
738+ if ((HAL_GetTick () - tickstart ) >= TX_TIMEOUT ) {
739+ return 0 ;
743740 }
744741 }
745742
746- HAL_NVIC_EnableIRQ (serial_debug .irq );
743+ if (HAL_UART_Transmit (& (obj -> handle ), data , size , TX_TIMEOUT ) != HAL_OK ) {
744+ size = 0 ;
745+ }
747746
748747 return size ;
749748}
0 commit comments