2020-05-10 09:53 AM
Hello Guys,
I am trying to setup UART under oscillator bypass mode in STM32f446RE Nucleo Board. I have created a new project in CubeIDE with UART under Asynchronous mode. I have commented out the SystemClock_Config and written my code for oscillator bypass mode even though everything can be done via code generation tool ( Part of learning ). But UART is not responding. Please find the below-attached project.
char data[] = "PK is Working\r\n";
int main(void)
{
HAL_Init();
//SystemClock_Config();
RCC_OscInitTypeDef Osc = {0};
RCC_ClkInitTypeDef Clk = {0};
Osc.OscillatorType = RCC_OSCILLATORTYPE_HSE;
Osc.HSEState = RCC_HSE_BYPASS;
HAL_RCC_OscConfig(&Osc);
Clk.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
Clk.AHBCLKDivider = RCC_SYSCLK_DIV2;
Clk.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
Clk.APB1CLKDivider = RCC_HCLK_DIV2;
Clk.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&Clk, FLASH_ACR_LATENCY_0WS);
__HAL_RCC_HSI_DISABLE();
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
HAL_UART_Transmit(&huart2, (uint8_t*) data, sizeof(data), HAL_MAX_DELAY);
}
}
2020-05-10 10:14 AM
Debug your code. Do all the calls to HAL_* return HAL_OK? Have you observed the TX line with a scope to verify it's not responding?
Your clock is pretty slow. Is your UART baud rate within tolerance?
2020-05-15 09:55 PM
After some debugging, I found that when the function HAL_RCC_ClockConfig(&Clk, FLASH_ACR_LATENCY_0WS) exit, the value of SystemCoreClock changes from 16000000 to 12500000. I am expecting an HCLK of 4Mhz. The UART sometimes respond with random garbage values. I dont understand why this happens? Any solution guys?
2020-05-15 10:17 PM
You need to set properly the constant which determines the HSE frequency - you've copied some project based on a board which has 25MHz crystal at HSE.
I don't know which constant it is, I don't use Cube, but Clive mentioned it here many times in the past in similar cases.
JW
2020-05-16 04:27 AM
As waclawek.jan says, change HSE_VALUE to 8000000.