@@ -113,12 +113,12 @@ serial_t *get_serial_obj(UART_HandleTypeDef *huart)
113
113
/**
114
114
* @brief Function called to initialize the uart interface
115
115
* @param obj : pointer to serial_t structure
116
- * @retval None
116
+ * @retval boolean status
117
117
*/
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 )
119
119
{
120
120
if (obj == NULL ) {
121
- return ;
121
+ return false ;
122
122
}
123
123
124
124
UART_HandleTypeDef * huart = & (obj -> handle );
@@ -143,28 +143,28 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
143
143
if (obj != & serial_debug ) {
144
144
core_debug ("ERROR: [U(S)ART] Tx pin has no peripheral!\n" );
145
145
}
146
- return ;
146
+ return false ;
147
147
}
148
148
/* Pin Rx must not be NP if not half-duplex */
149
149
if ((obj -> pin_rx != NC ) && (uart_rx == NP ) && (uart_rx_swap == NP )) {
150
150
if (obj != & serial_debug ) {
151
151
core_debug ("ERROR: [U(S)ART] Rx pin has no peripheral!\n" );
152
152
}
153
- return ;
153
+ return false ;
154
154
}
155
155
/* Pin RTS must not be NP if flow control is enabled */
156
156
if ((obj -> pin_rts != NC ) && (uart_rts == NP )) {
157
157
if (obj != & serial_debug ) {
158
158
core_debug ("ERROR: [U(S)ART] RTS pin has no peripheral!\n" );
159
159
}
160
- return ;
160
+ return false ;
161
161
}
162
162
/* Pin CTS must not be NP if flow control is enabled */
163
163
if ((obj -> pin_cts != NC ) && (uart_cts == NP )) {
164
164
if (obj != & serial_debug ) {
165
165
core_debug ("ERROR: [U(S)ART] CTS pin has no peripheral!\n" );
166
166
}
167
- return ;
167
+ return false ;
168
168
}
169
169
170
170
/*
@@ -184,7 +184,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
184
184
if (obj != & serial_debug ) {
185
185
core_debug ("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n" );
186
186
}
187
- return ;
187
+ return false ;
188
188
}
189
189
190
190
/* Enable USART clock */
@@ -364,6 +364,12 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
364
364
obj -> irq = UART12_IRQn ;
365
365
}
366
366
#endif
367
+ else {
368
+ if (obj != & serial_debug ) {
369
+ core_debug ("ERROR: [U(S)ART] Peripheral not supported!\n" );
370
+ }
371
+ return false;
372
+ }
367
373
/* Configure UART GPIO pins */
368
374
#if defined(UART_ADVFEATURE_SWAP_INIT )
369
375
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
468
474
/* Trying default LPUART clock source */
469
475
if ((uart_rx == NP ) && (uart_rx_swap == NP )) {
470
476
if (HAL_HalfDuplex_Init (huart ) == HAL_OK ) {
471
- return ;
477
+ return true ;
472
478
}
473
479
} else if (HAL_UART_Init (huart ) == HAL_OK ) {
474
- return ;
480
+ return true ;
475
481
}
476
482
/* Trying to change LPUART clock source */
477
483
/* 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
494
500
#endif
495
501
if ((uart_rx == NP ) && (uart_rx_swap == NP )) {
496
502
if (HAL_HalfDuplex_Init (huart ) == HAL_OK ) {
497
- return ;
503
+ return true ;
498
504
}
499
505
} else if (HAL_UART_Init (huart ) == HAL_OK ) {
500
- return ;
506
+ return true ;
501
507
}
502
508
}
503
509
}
@@ -517,10 +523,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
517
523
#endif
518
524
if ((uart_rx == NP ) && (uart_rx_swap == NP )) {
519
525
if (HAL_HalfDuplex_Init (huart ) == HAL_OK ) {
520
- return ;
526
+ return true ;
521
527
}
522
528
} else if (HAL_UART_Init (huart ) == HAL_OK ) {
523
- return ;
529
+ return true ;
524
530
}
525
531
}
526
532
if (obj -> uart == LPUART1 ) {
@@ -544,10 +550,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
544
550
#endif
545
551
if ((uart_rx == NP ) && (uart_rx_swap == NP )) {
546
552
if (HAL_HalfDuplex_Init (huart ) == HAL_OK ) {
547
- return ;
553
+ return true ;
548
554
}
549
555
} else if (HAL_UART_Init (huart ) == HAL_OK ) {
550
- return ;
556
+ return true ;
551
557
}
552
558
#if defined(RCC_LPUART1CLKSOURCE_SYSCLK )
553
559
if (obj -> uart == LPUART1 ) {
@@ -569,11 +575,12 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
569
575
570
576
if ((uart_rx == NP ) && (uart_rx_swap == NP )) {
571
577
if (HAL_HalfDuplex_Init (huart ) != HAL_OK ) {
572
- return ;
578
+ return false ;
573
579
}
574
580
} else if (HAL_UART_Init (huart ) != HAL_OK ) {
575
- return ;
581
+ return false ;
576
582
}
583
+ return true;
577
584
}
578
585
579
586
/**
@@ -821,19 +828,21 @@ void uart_config_lowpower(serial_t *obj)
821
828
* @note Call only if debug U(S)ART peripheral is not already initialized
822
829
* by a Serial instance
823
830
* Default config: 8N1
824
- * @retval None
831
+ * @retval boolean status
825
832
*/
826
- void uart_debug_init (void )
833
+ bool uart_debug_init (void )
827
834
{
835
+ bool status = false;
828
836
if (DEBUG_UART != NP ) {
829
837
#if defined(DEBUG_PINNAME_TX )
830
838
serial_debug .pin_tx = DEBUG_PINNAME_TX ;
831
839
#else
832
840
serial_debug .pin_tx = pinmap_pin (DEBUG_UART , PinMap_UART_TX );
833
841
#endif
834
842
/* 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);
836
844
}
845
+ return status ;
837
846
}
838
847
839
848
/**
@@ -863,8 +872,7 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
863
872
864
873
if (serial_debug .index >= UART_NUM ) {
865
874
/* DEBUG_UART not initialized */
866
- uart_debug_init ();
867
- if (serial_debug .index >= UART_NUM ) {
875
+ if (!uart_debug_init ()) {
868
876
return 0 ;
869
877
}
870
878
}
0 commit comments