Skip to content

Enhance DEBUG_UART feature (used by printf) #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
@@ -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;
75 changes: 61 additions & 14 deletions cores/arduino/stm32/uart.c
Original file line number Diff line number Diff line change
@@ -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,28 +411,63 @@ 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
*/
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) {
4 changes: 4 additions & 0 deletions variants/board_template/variant.h
Original file line number Diff line number Diff line change
@@ -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