Skip to content

Commit 6a98725

Browse files
patricklaffpistm
authored andcommitted
chore(uart): harden init
Signed-off-by: patricklaf <[email protected]>
1 parent fddfdeb commit 6a98725

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

cores/arduino/HardwareSerial.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,17 @@ void HardwareSerial::begin(unsigned long baud, byte config)
446446
break;
447447
}
448448

449-
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits, _rx_invert, _tx_invert, _data_invert);
450-
enableHalfDuplexRx();
451-
uart_attach_rx_callback(&_serial, _rx_complete_irq);
449+
_ready = uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits, _rx_invert, _tx_invert, _data_invert);
450+
if (_ready) {
451+
enableHalfDuplexRx();
452+
uart_attach_rx_callback(&_serial, _rx_complete_irq);
453+
}
452454
}
453455

454456
void HardwareSerial::end()
455457
{
458+
_ready = false;
459+
456460
// wait for transmission of outgoing data
457461
flush(TX_TIMEOUT);
458462

cores/arduino/HardwareSerial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class HardwareSerial : public Stream {
146146
using Print::write; // pull in write(str) from Print
147147
operator bool()
148148
{
149-
return true;
149+
return _ready;
150150
}
151151

152152
void setRx(uint32_t _rx);
@@ -189,6 +189,7 @@ class HardwareSerial : public Stream {
189189
#endif // HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY
190190

191191
private:
192+
bool _ready;
192193
bool _rx_enabled;
193194
uint8_t _config;
194195
unsigned long _baud;

libraries/SrcWrapper/inc/uart.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ struct serial_s {
255255

256256
/* Exported macro ------------------------------------------------------------*/
257257
/* Exported functions ------------------------------------------------------- */
258-
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, bool rx_invert, bool tx_invert, bool data_invert);
258+
bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, bool rx_invert, bool tx_invert, bool data_invert);
259259
void uart_deinit(serial_t *obj);
260260
#if defined(HAL_PWR_MODULE_ENABLED) && (defined(UART_IT_WUF) || defined(LPUART1_BASE))
261261
void uart_config_lowpower(serial_t *obj);

libraries/SrcWrapper/src/stm32/uart.c

+31-23
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ serial_t *get_serial_obj(UART_HandleTypeDef *huart)
113113
/**
114114
* @brief Function called to initialize the uart interface
115115
* @param obj : pointer to serial_t structure
116-
* @retval None
116+
* @retval boolean status
117117
*/
118-
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, bool rx_invert, bool tx_invert, bool data_invert)
118+
bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, bool rx_invert, bool tx_invert, bool data_invert)
119119
{
120120
if (obj == NULL) {
121-
return;
121+
return false;
122122
}
123123

124124
UART_HandleTypeDef *huart = &(obj->handle);
@@ -143,28 +143,28 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
143143
if (obj != &serial_debug) {
144144
core_debug("ERROR: [U(S)ART] Tx pin has no peripheral!\n");
145145
}
146-
return;
146+
return false;
147147
}
148148
/* Pin Rx must not be NP if not half-duplex */
149149
if ((obj->pin_rx != NC) && (uart_rx == NP) && (uart_rx_swap == NP)) {
150150
if (obj != &serial_debug) {
151151
core_debug("ERROR: [U(S)ART] Rx pin has no peripheral!\n");
152152
}
153-
return;
153+
return false;
154154
}
155155
/* Pin RTS must not be NP if flow control is enabled */
156156
if ((obj->pin_rts != NC) && (uart_rts == NP)) {
157157
if (obj != &serial_debug) {
158158
core_debug("ERROR: [U(S)ART] RTS pin has no peripheral!\n");
159159
}
160-
return;
160+
return false;
161161
}
162162
/* Pin CTS must not be NP if flow control is enabled */
163163
if ((obj->pin_cts != NC) && (uart_cts == NP)) {
164164
if (obj != &serial_debug) {
165165
core_debug("ERROR: [U(S)ART] CTS pin has no peripheral!\n");
166166
}
167-
return;
167+
return false;
168168
}
169169

170170
/*
@@ -184,7 +184,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
184184
if (obj != &serial_debug) {
185185
core_debug("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n");
186186
}
187-
return;
187+
return false;
188188
}
189189

190190
/* Enable USART clock */
@@ -364,6 +364,12 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
364364
obj->irq = UART12_IRQn;
365365
}
366366
#endif
367+
else {
368+
if (obj != &serial_debug) {
369+
core_debug("ERROR: [U(S)ART] Peripheral not supported!\n");
370+
}
371+
return false;
372+
}
367373
/* Configure UART GPIO pins */
368374
#if defined(UART_ADVFEATURE_SWAP_INIT)
369375
uint32_t pin_swap = UART_ADVFEATURE_SWAP_DISABLE;
@@ -468,10 +474,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
468474
/* Trying default LPUART clock source */
469475
if ((uart_rx == NP) && (uart_rx_swap == NP)) {
470476
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
471-
return;
477+
return true;
472478
}
473479
} else if (HAL_UART_Init(huart) == HAL_OK) {
474-
return;
480+
return true;
475481
}
476482
/* Trying to change LPUART clock source */
477483
/* If baudrate is lower than or equal to 9600 try to change to LSE */
@@ -494,10 +500,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
494500
#endif
495501
if ((uart_rx == NP) && (uart_rx_swap == NP)) {
496502
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
497-
return;
503+
return true;
498504
}
499505
} else if (HAL_UART_Init(huart) == HAL_OK) {
500-
return;
506+
return true;
501507
}
502508
}
503509
}
@@ -517,10 +523,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
517523
#endif
518524
if ((uart_rx == NP) && (uart_rx_swap == NP)) {
519525
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
520-
return;
526+
return true;
521527
}
522528
} else if (HAL_UART_Init(huart) == HAL_OK) {
523-
return;
529+
return true;
524530
}
525531
}
526532
if (obj->uart == LPUART1) {
@@ -544,10 +550,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
544550
#endif
545551
if ((uart_rx == NP) && (uart_rx_swap == NP)) {
546552
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
547-
return;
553+
return true;
548554
}
549555
} else if (HAL_UART_Init(huart) == HAL_OK) {
550-
return;
556+
return true;
551557
}
552558
#if defined(RCC_LPUART1CLKSOURCE_SYSCLK)
553559
if (obj->uart == LPUART1) {
@@ -569,11 +575,12 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
569575

570576
if ((uart_rx == NP) && (uart_rx_swap == NP)) {
571577
if (HAL_HalfDuplex_Init(huart) != HAL_OK) {
572-
return;
578+
return false;
573579
}
574580
} else if (HAL_UART_Init(huart) != HAL_OK) {
575-
return;
581+
return false;
576582
}
583+
return true;
577584
}
578585

579586
/**
@@ -821,19 +828,21 @@ void uart_config_lowpower(serial_t *obj)
821828
* @note Call only if debug U(S)ART peripheral is not already initialized
822829
* by a Serial instance
823830
* Default config: 8N1
824-
* @retval None
831+
* @retval boolean status
825832
*/
826-
void uart_debug_init(void)
833+
bool uart_debug_init(void)
827834
{
835+
bool status = false;
828836
if (DEBUG_UART != NP) {
829837
#if defined(DEBUG_PINNAME_TX)
830838
serial_debug.pin_tx = DEBUG_PINNAME_TX;
831839
#else
832840
serial_debug.pin_tx = pinmap_pin(DEBUG_UART, PinMap_UART_TX);
833841
#endif
834842
/* serial_debug.pin_rx set by default to NC to configure in half duplex mode */
835-
uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, false, false, false);
843+
status = uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, false, false, false);
836844
}
845+
return status;
837846
}
838847

839848
/**
@@ -863,8 +872,7 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
863872

864873
if (serial_debug.index >= UART_NUM) {
865874
/* DEBUG_UART not initialized */
866-
uart_debug_init();
867-
if (serial_debug.index >= UART_NUM) {
875+
if (!uart_debug_init()) {
868876
return 0;
869877
}
870878
}

0 commit comments

Comments
 (0)