5858// linked to PIN_SERIAL_TX
5959#if !defined(DEBUG_UART )
6060#if defined(PIN_SERIAL_TX )
61- #define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
61+ #define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
62+ #define DEBUG_PINNAME_TX digitalPinToPinName(PIN_SERIAL_TX)
6263#else
63- #define DEBUG_UART NP
64+ // No debug UART defined
65+ #define DEBUG_UART NP
66+ #define DEBUG_PINNAME_TX NC
6467#endif
6568#endif
69+ #if !defined(DEBUG_UART_BAUDRATE )
70+ #define DEBUG_UART_BAUDRATE 9600
71+ #endif
72+
6673// @brief uart caracteristics
6774#if defined(STM32F4xx )
6875#define UART_NUM (10)
@@ -83,6 +90,8 @@ static serial_t *rx_callback_obj[UART_NUM];
8390static int (* tx_callback [UART_NUM ])(serial_t * );
8491static serial_t * tx_callback_obj [UART_NUM ];
8592
93+ static serial_t serial_debug = { .uart = NP , .index = UART_NUM };
94+
8695/**
8796 * @brief Function called to initialize the uart interface
8897 * @param obj : pointer to serial_t structure
@@ -97,6 +106,7 @@ void uart_init(serial_t *obj)
97106 UART_HandleTypeDef * huart = & (obj -> handle );
98107 GPIO_InitTypeDef GPIO_InitStruct ;
99108 GPIO_TypeDef * port ;
109+ uint32_t function = (uint32_t )NC ;
100110
101111 // Determine the UART to use (UART_1, UART_2, ...)
102112 USART_TypeDef * uart_tx = pinmap_peripheral (obj -> pin_tx , PinMap_UART_TX );
@@ -238,27 +248,29 @@ void uart_init(serial_t *obj)
238248 //Configure GPIOs
239249 //RX
240250 port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_rx ));
251+ function = pinmap_function (obj -> pin_rx , PinMap_UART_RX );
241252 GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_rx );
242- GPIO_InitStruct .Mode = STM_PIN_MODE (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
253+ GPIO_InitStruct .Mode = STM_PIN_MODE (function );
243254 GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_HIGH ;
244- GPIO_InitStruct .Pull = STM_PIN_PUPD (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
255+ GPIO_InitStruct .Pull = STM_PIN_PUPD (function );
245256#ifdef STM32F1xx
246- pin_SetF1AFPin (STM_PIN_AFNUM (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) ));
257+ pin_SetF1AFPin (STM_PIN_AFNUM (function ));
247258#else
248- GPIO_InitStruct .Alternate = STM_PIN_AFNUM (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
259+ GPIO_InitStruct .Alternate = STM_PIN_AFNUM (function );
249260#endif /* STM32F1xx */
250261 HAL_GPIO_Init (port , & GPIO_InitStruct );
251262
252263 //TX
253264 port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_tx ));
265+ function = pinmap_function (obj -> pin_tx , PinMap_UART_TX );
254266 GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_tx );
255- GPIO_InitStruct .Mode = STM_PIN_MODE (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
267+ GPIO_InitStruct .Mode = STM_PIN_MODE (function );
256268 GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_HIGH ;
257- GPIO_InitStruct .Pull = STM_PIN_PUPD (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
269+ GPIO_InitStruct .Pull = STM_PIN_PUPD (function );
258270#ifdef STM32F1xx
259- pin_SetF1AFPin (STM_PIN_AFNUM (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) ));
271+ pin_SetF1AFPin (STM_PIN_AFNUM (function ));
260272#else
261- GPIO_InitStruct .Alternate = STM_PIN_AFNUM (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
273+ GPIO_InitStruct .Alternate = STM_PIN_AFNUM (function );
262274#endif /* STM32F1xx */
263275 HAL_GPIO_Init (port , & GPIO_InitStruct );
264276
@@ -399,28 +411,63 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
399411 }
400412}
401413
414+ /**
415+ * @brief Function called to initialize the debug uart interface
416+ * @note Call only if debug U(S)ART peripheral is not already initialized
417+ * by a Serial instance
418+ * Default config: 8N1
419+ * @retval None
420+ */
421+ void uart_debug_init (void )
422+ {
423+ if ( DEBUG_UART != NP ) {
424+ serial_debug .pin_rx = pinmap_pin (DEBUG_UART , PinMap_UART_RX );
425+ #if defined(DEBUG_PINNAME_TX )
426+ serial_debug .pin_tx = DEBUG_PINNAME_TX ;
427+ #else
428+ serial_debug .pin_tx = pinmap_pin (DEBUG_UART , PinMap_UART_TX );
429+ #endif
430+ serial_debug .baudrate = DEBUG_UART_BAUDRATE ;
431+ serial_debug .parity = UART_PARITY_NONE ;
432+ serial_debug .databits = UART_WORDLENGTH_8B ;
433+ serial_debug .stopbits = UART_STOPBITS_1 ;
434+
435+ uart_init (& serial_debug );
436+ }
437+ }
438+
402439/**
403440 * @brief write the data on the uart: used by printf for debug only (syscalls)
404- * @param obj : pointer to serial_t structure
405441 * @param data : bytes to write
406442 * @param size : number of data to write
407443 * @retval The number of bytes written
408444 */
409445size_t uart_debug_write (uint8_t * data , uint32_t size )
410446{
411447 uint8_t index = 0 ;
412- USART_TypeDef * dbg_uart = DEBUG_UART ;
413448 uint32_t tickstart = HAL_GetTick ();
449+
450+ if (DEBUG_UART == NP ) {
451+ return 0 ;
452+ }
453+ /* Search if DEBUG_UART already initialized */
414454 for (index = 0 ; index < UART_NUM ; index ++ ) {
415455 if (uart_handlers [index ] != NULL ) {
416- if (dbg_uart == uart_handlers [index ]-> Instance ) {
456+ if (DEBUG_UART == uart_handlers [index ]-> Instance ) {
417457 break ;
418458 }
419459 }
420460 }
421461
422462 if (index >= UART_NUM ) {
423- return 0 ;
463+ /* DEBUG_UART not initialized */
464+ if ( serial_debug .index >= UART_NUM ) {
465+ uart_debug_init ();
466+ if ( serial_debug .index >= UART_NUM ) {
467+ return 0 ;
468+ }
469+ }
470+ index = serial_debug .index ;
424471 }
425472
426473 while (HAL_UART_Transmit (uart_handlers [index ], data , size , TX_TIMEOUT ) != HAL_OK ) {
0 commit comments