Skip to content

Instantly share code, notes, and snippets.

@amrithHN
Created April 26, 2023 11:24
Show Gist options
  • Save amrithHN/def122ad276935e2cba0e93a64caadfb to your computer and use it in GitHub Desktop.
Save amrithHN/def122ad276935e2cba0e93a64caadfb to your computer and use it in GitHub Desktop.
UART printf redirection stm32
/*# 1- Identify the UART interface, GPIO pins and Alternate Function #########*/
/* For example:
* B-L475E-IOT01A:
* PB6 ------> USART1_TX
* PB7 ------> USART1_RX
*
* NUCLEO-L476RG:
* PA2 ------> USART2_TX
* PA3 ------> USART2_RX
*
* STM32L4R9-DISCO:
* PA2 ------> USART2_TX
* PA3 ------> USART2_RX
*
* STM32H747I-DISCO:
* PA9 ------> USART1_TX
* PA10 ------> USART1_RX
*
*/
/*# 2- Enable UART MODULE in Inc/stm32l4xx_hal_conf.h ########################*/
#define HAL_UART_MODULE_ENABLED
/*# 3- From Drivers/STM32L4xx_HAL_Driver/Src/ add the HAL UART C sources
to your project: stm32xxxx_hal_uart.c stm32xxxx_hal_uart_ex.c ############*/
/*# 4- Create Global UART Handle in Src/main.c ###############################*/
UART_HandleTypeDef huart1;
/*# 5- Configure the UART interface in Src/main.c ############################*/
/**
* @brief UART interface configuration
*/
void UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/*# 6- Configure the UART GPIOs in Src/stm32l4xx_hal_msp.c ##################*/
/**
* @brief UART MSP Initialization
* @param huart: UART handle pointer
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if (huart->Instance == USART1)
{
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
/**
* USART1 GPIO Configuration
* PB6 ------> USART1_TX
* PB7 ------> USART1_RX
*/
/* Peripheral clock enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
/*# 7- Retarget printf to UART (std library and toolchain dependent) #########*/
#if defined(__GNUC__)
int _write(int fd, char * ptr, int len)
{
HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
return len;
}
#elif defined (__ICCARM__)
#include "LowLevelIOInterface.h"
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
HAL_UART_Transmit(&huart1, (uint8_t *) buffer, size, HAL_MAX_DELAY);
return size;
}
#elif defined (__CC_ARM)
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif
// OR:
// Add syscalls.c with GCC
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment