cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F7 UART problem

pedro
Associate II
Posted on July 16, 2015 at 03:57

I'm using the HAL drivers for the first time and I'm trying to implement the UART protocol. I used the stm32f74g... examples for the UART and copied part of the uart-polling example.

The problem I get is that I set the baudrate to 9600 and I get (I see on my computer) the right data at 19200 baudrate, otherwise I see trash. I checked the HSE_VALUE and HSI_VALUE in ''stm32f7xx_hal_config.h'' and they are correct at 25Mhz and 16 Mhz respectivly. Since I'm using USART6 I also invoked this functionHAL_RCC_GetPCLK2Freq(); to see the frequency the uart was using to configure de BRR and it was also right at 100 Mhz (/2 prescler from HCLK at 200Mhz). I ran out of ideas to solve the problem... I'm leaving the code I have so far in the hope you have already encontered the same problem. Thanks

#include ''stm32746g_discovery.h''
#include ''stm32f7xx.h''
#define USARTx_CLK_ENABLE() __USART6_CLK_ENABLE()
#define USARTx_RX_GPIO_CLK_ENABLE() __GPIOC_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __GPIOC_CLK_ENABLE()
#define USARTx_FORCE_RESET() __USART6_FORCE_RESET()
#define USARTx_RELEASE_RESET() __USART6_RELEASE_RESET()
#define USARTx_TX_PIN GPIO_PIN_6
#define USARTx_TX_GPIO_PORT GPIOC
#define USARTx_TX_AF GPIO_AF8_USART6
#define USARTx_RX_PIN GPIO_PIN_7
#define USARTx_RX_GPIO_PORT GPIOC
#define USARTx_RX_AF GPIO_AF8_USART6
void SystemClock_Config(void);
static void CPU_CACHE_Enable(void);
/* Buffer used for transmission */
uint8_t aTxBuffer[] = '' **** UART_TwoBoards_ComPolling **** **** UART_TwoBoards_ComPolling **** **** UART_TwoBoards_ComPolling **** '';
/* Buffer used for reception */
uint8_t aRxBuffer[100];
int main(void)
{
unsigned int i = 0;
UART_HandleTypeDef UartHandle;
CPU_CACHE_Enable();
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Off(LED1);
UartHandle.Instance = USART6;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
{
BSP_LED_On(LED1);
}
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
BSP_LED_On(LED1);
}
i = HAL_RCC_GetPCLK2Freq();
if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer,20,5000) != HAL_OK) {
BSP_LED_On(LED1);
}
while(1){
HAL_Delay(1000);
//BSP_LED_Toggle(LED1);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
HAL_PWREx_ActivateOverDrive();
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 200;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
}
/**
* @brief CPU L1-Cache enable.
* @param None
* @retval None
*/
static void CPU_CACHE_Enable(void)
{
/* Enable I-Cache */
SCB_EnableICache();
/* Enable D-Cache */
SCB_EnableDCache();
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/* Enable USARTx clock */
USARTx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
}
/**
* @brief UART MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO configuration to their default state
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
/*##-1- Reset peripherals ##################################################*/
USARTx_FORCE_RESET();
USARTx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks #################################*/
/* Configure USART6 Tx as alternate function */
HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
/* Configure USART6 Rx as alternate function */
HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}

1 REPLY 1
pedro
Associate II
Posted on July 16, 2015 at 04:10

I found out the problem, and I will post the solution for others.

In the UART examples, the OverSampling is not set and by default ( when not initizalized ) is at UART_OVERSAMPLING_16. 

To solve it I just change the OverSampling parameter to UART_OVERSAMPLING_8.