Skip to content

Commit f918bec

Browse files
committed
Enhance DEBUG_UART feature (used by printf)
If the debug UART is not initialised by a Serial instance, init it at 9600 (8N1) by default. Those values could be redefined in the variant.h DEBUG_UART could be redefined to print on another instance than 'Serial' #define DEBUG_UART ((USART_TypeDef *) U(S)ARTX) // ex: USART3 DEBUG_UART baudrate, default: 9600 if not defined #define DEBUG_UART_BAUDRATE 115200 DEBUG_UART Tx pin name, default: the first one found in PinMap_UART_TX for DEBUG_UART #define DEBUG_PINNAME_TX PX_n // PinName used for TX Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 81bc8a0 commit f918bec

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

cores/arduino/stm32/uart.c

+50-6
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@
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];
8390
static int (*tx_callback[UART_NUM])(serial_t*);
8491
static 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
@@ -402,28 +411,63 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
402411
}
403412
}
404413

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+
405439
/**
406440
* @brief write the data on the uart: used by printf for debug only (syscalls)
407-
* @param obj : pointer to serial_t structure
408441
* @param data : bytes to write
409442
* @param size : number of data to write
410443
* @retval The number of bytes written
411444
*/
412445
size_t uart_debug_write(uint8_t *data, uint32_t size)
413446
{
414447
uint8_t index = 0;
415-
USART_TypeDef* dbg_uart = DEBUG_UART;
416448
uint32_t tickstart = HAL_GetTick();
449+
450+
if (DEBUG_UART == NP) {
451+
return 0;
452+
}
453+
/* Search if DEBUG_UART already initialized */
417454
for(index = 0; index < UART_NUM; index++) {
418455
if(uart_handlers[index] != NULL) {
419-
if(dbg_uart == uart_handlers[index]->Instance) {
456+
if(DEBUG_UART == uart_handlers[index]->Instance) {
420457
break;
421458
}
422459
}
423460
}
424461

425462
if(index >= UART_NUM) {
426-
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;
427471
}
428472

429473
while(HAL_UART_Transmit(uart_handlers[index], data, size, TX_TIMEOUT) != HAL_OK) {

variants/board_template/variant.h

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ enum {
109109
#define SERIAL_UART_INSTANCE x //ex: 2 for Serial2 (USART2)
110110
// DEBUG_UART could be redefined to print on another instance than 'Serial'
111111
//#define DEBUG_UART ((USART_TypeDef *) U(S)ARTX) // ex: USART3
112+
// DEBUG_UART baudrate, default: 9600 if not defined
113+
//#define DEBUG_UART_BAUDRATE x
114+
// DEBUG_UART Tx pin name, default: the first one found in PinMap_UART_TX for DEBUG_UART
115+
//#define DEBUG_PINNAME_TX PX_n // PinName used for TX
112116

113117
// UART Emulation (uncomment if needed, required TIM1)
114118
//#define UART_EMUL_RX PX_n // PinName used for RX

0 commit comments

Comments
 (0)