@@ -78,8 +78,14 @@ typedef enum {
78
78
} uart_index_t ;
79
79
80
80
static 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
+ };
83
89
84
90
/* Aim of the function is to get serial_s pointer using huart pointer */
85
91
/* 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
115
121
116
122
/* Pin Tx must not be NP */
117
123
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
+ }
119
127
return ;
120
128
}
121
129
/* Pin Rx must not be NP if not half-duplex */
122
130
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
+ }
124
134
return ;
125
135
}
126
136
/* Pin RTS must not be NP if flow control is enabled */
127
137
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
+ }
129
141
return ;
130
142
}
131
143
/* Pin CTS must not be NP if flow control is enabled */
132
144
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
+ }
134
148
return ;
135
149
}
136
150
@@ -144,7 +158,9 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
144
158
obj -> uart = pinmap_merge_peripheral (obj -> uart , uart_cts );
145
159
146
160
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
+ }
148
164
return ;
149
165
}
150
166
@@ -660,22 +676,6 @@ void uart_config_lowpower(serial_t *obj)
660
676
}
661
677
#endif
662
678
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
-
679
679
/**
680
680
* @brief Function called to initialize the debug uart interface
681
681
* @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)
686
686
void uart_debug_init (void )
687
687
{
688
688
if (DEBUG_UART != NP ) {
689
- serial_debug .pin_rx = pinmap_pin (DEBUG_UART , PinMap_UART_RX );
690
689
#if defined(DEBUG_PINNAME_TX )
691
690
serial_debug .pin_tx = DEBUG_PINNAME_TX ;
692
691
#else
693
692
serial_debug .pin_tx = pinmap_pin (DEBUG_UART , PinMap_UART_TX );
694
693
#endif
695
-
694
+ /* serial_debug.pin_rx set by default to NC to configure in half duplex mode */
696
695
uart_init (& serial_debug , DEBUG_UART_BAUDRATE , UART_WORDLENGTH_8B , UART_PARITY_NONE , UART_STOPBITS_1 );
697
696
}
698
697
}
@@ -706,11 +705,13 @@ void uart_debug_init(void)
706
705
size_t uart_debug_write (uint8_t * data , uint32_t size )
707
706
{
708
707
uint32_t tickstart = HAL_GetTick ();
708
+ serial_t * obj = NULL ;
709
709
710
- if (DEBUG_UART == NP ) {
711
- return 0 ;
712
- }
713
710
if (serial_debug .index >= UART_NUM ) {
711
+ if (DEBUG_UART == NP ) {
712
+ return 0 ;
713
+ }
714
+
714
715
/* Search if DEBUG_UART already initialized */
715
716
for (serial_debug .index = 0 ; serial_debug .index < UART_NUM ; serial_debug .index ++ ) {
716
717
if (uart_handlers [serial_debug .index ] != NULL ) {
@@ -726,24 +727,22 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
726
727
if (serial_debug .index >= UART_NUM ) {
727
728
return 0 ;
728
729
}
729
- } else {
730
- serial_t * obj = get_serial_obj (uart_handlers [serial_debug .index ]);
731
- if (obj ) {
732
- serial_debug .irq = obj -> irq ;
733
- }
734
730
}
735
731
}
732
+ obj = get_serial_obj (uart_handlers [serial_debug .index ]);
733
+ if (!obj ) {
734
+ return 0 ;
735
+ }
736
736
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 ;
743
740
}
744
741
}
745
742
746
- HAL_NVIC_EnableIRQ (serial_debug .irq );
743
+ if (HAL_UART_Transmit (& (obj -> handle ), data , size , TX_TIMEOUT ) != HAL_OK ) {
744
+ size = 0 ;
745
+ }
747
746
748
747
return size ;
749
748
}
0 commit comments