diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 7c9e82f7e3..09f8f4f4f0 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -220,7 +220,7 @@ void HardwareSerial::begin(unsigned long baud, byte config) _serial.baudrate = (uint32_t)baud; - // Manage databitshardware/arduino/avr/cores/arduino/HardwareSerial.cpp + // Manage databits switch(config & 0x07) { case 0x02: databits = 6; diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index dd9929abec..10f6acf5d6 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -58,11 +58,18 @@ // linked to PIN_SERIAL_TX #if !defined(DEBUG_UART) #if defined(PIN_SERIAL_TX) -#define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX) +#define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX) +#define DEBUG_PINNAME_TX digitalPinToPinName(PIN_SERIAL_TX) #else -#define DEBUG_UART NP +// No debug UART defined +#define DEBUG_UART NP +#define DEBUG_PINNAME_TX NC #endif #endif +#if !defined(DEBUG_UART_BAUDRATE) +#define DEBUG_UART_BAUDRATE 9600 +#endif + // @brief uart caracteristics #if defined(STM32F4xx) #define UART_NUM (10) @@ -83,6 +90,8 @@ static serial_t *rx_callback_obj[UART_NUM]; static int (*tx_callback[UART_NUM])(serial_t*); static serial_t *tx_callback_obj[UART_NUM]; +static serial_t serial_debug = { .uart=NP, .index=UART_NUM }; + /** * @brief Function called to initialize the uart interface * @param obj : pointer to serial_t structure @@ -97,6 +106,7 @@ void uart_init(serial_t *obj) UART_HandleTypeDef *huart = &(obj->handle); GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; + uint32_t function = (uint32_t)NC; // Determine the UART to use (UART_1, UART_2, ...) USART_TypeDef *uart_tx = pinmap_peripheral(obj->pin_tx, PinMap_UART_TX); @@ -238,27 +248,29 @@ void uart_init(serial_t *obj) //Configure GPIOs //RX port = set_GPIO_Port_Clock(STM_PORT(obj->pin_rx)); + function = pinmap_function(obj->pin_rx, PinMap_UART_RX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_rx); - GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_rx,PinMap_UART_RX)); + GPIO_InitStruct.Mode = STM_PIN_MODE(function); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_rx,PinMap_UART_RX)); + GPIO_InitStruct.Pull = STM_PIN_PUPD(function); #ifdef STM32F1xx - pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_rx,PinMap_UART_RX))); + pin_SetF1AFPin(STM_PIN_AFNUM(function)); #else - GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_rx,PinMap_UART_RX)); + GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); //TX port = set_GPIO_Port_Clock(STM_PORT(obj->pin_tx)); + function = pinmap_function(obj->pin_tx, PinMap_UART_TX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_tx); - GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_tx,PinMap_UART_TX)); + GPIO_InitStruct.Mode = STM_PIN_MODE(function); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_tx,PinMap_UART_TX)); + GPIO_InitStruct.Pull = STM_PIN_PUPD(function); #ifdef STM32F1xx - pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_tx,PinMap_UART_TX))); + pin_SetF1AFPin(STM_PIN_AFNUM(function)); #else - GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_tx,PinMap_UART_TX)); + GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); @@ -399,9 +411,33 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size) } } +/** + * @brief Function called to initialize the debug uart interface + * @note Call only if debug U(S)ART peripheral is not already initialized + * by a Serial instance + * Default config: 8N1 + * @retval None + */ +void uart_debug_init(void) +{ + if ( DEBUG_UART != NP) { + serial_debug.pin_rx = pinmap_pin(DEBUG_UART, PinMap_UART_RX); +#if defined(DEBUG_PINNAME_TX) + serial_debug.pin_tx = DEBUG_PINNAME_TX; +#else + serial_debug.pin_tx = pinmap_pin(DEBUG_UART, PinMap_UART_TX); +#endif + serial_debug.baudrate = DEBUG_UART_BAUDRATE; + serial_debug.parity = UART_PARITY_NONE; + serial_debug.databits = UART_WORDLENGTH_8B; + serial_debug.stopbits = UART_STOPBITS_1; + + uart_init(&serial_debug); + } +} + /** * @brief write the data on the uart: used by printf for debug only (syscalls) - * @param obj : pointer to serial_t structure * @param data : bytes to write * @param size : number of data to write * @retval The number of bytes written @@ -409,18 +445,29 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size) size_t uart_debug_write(uint8_t *data, uint32_t size) { uint8_t index = 0; - USART_TypeDef* dbg_uart = DEBUG_UART; uint32_t tickstart = HAL_GetTick(); + + if (DEBUG_UART == NP) { + return 0; + } + /* Search if DEBUG_UART already initialized */ for(index = 0; index < UART_NUM; index++) { if(uart_handlers[index] != NULL) { - if(dbg_uart == uart_handlers[index]->Instance) { + if(DEBUG_UART == uart_handlers[index]->Instance) { break; } } } if(index >= UART_NUM) { - return 0; + /* DEBUG_UART not initialized */ + if( serial_debug.index >= UART_NUM ) { + uart_debug_init(); + if( serial_debug.index >= UART_NUM ) { + return 0; + } + } + index = serial_debug.index; } while(HAL_UART_Transmit(uart_handlers[index], data, size, TX_TIMEOUT) != HAL_OK) { diff --git a/variants/board_template/variant.h b/variants/board_template/variant.h index 560b6b4111..06255ed24d 100644 --- a/variants/board_template/variant.h +++ b/variants/board_template/variant.h @@ -109,6 +109,10 @@ enum { #define SERIAL_UART_INSTANCE x //ex: 2 for Serial2 (USART2) // 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 x +// 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 // UART Emulation (uncomment if needed, required TIM1) //#define UART_EMUL_RX PX_n // PinName used for RX