Skip to main content
antonius
Associate III
April 7, 2019
Question

HSI clock and baudrate ?

  • April 7, 2019
  • 29 replies
  • 3658 views

Dear Members,

I changed my clock from MSI to HSI but my SPI and UART baudrate becoming wrong,

What do I miss here ?

thanks

This topic has been closed for replies.

29 replies

S.Ma
Principal
April 7, 2019

Only you can answer a question with so little details, except Sherlock maybe?

antonius
antoniusAuthor
Associate III
April 7, 2019

Not good for me, it's not supported for STM32LX series :(

Tesla DeLorean
Guru
April 7, 2019

If you change clocks on ​the fly, you'll have to change dividers dependent on them also.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
April 7, 2019

0690X000008A1dtQAC.jpgyes what value for divider ?

the setting for UART3

:

void MX_USART3_UART_Init(void)

{

 huart3.Instance = USART3;

 huart3.Init.BaudRate = 115200;

 huart3.Init.WordLength = UART_WORDLENGTH_8B;

 huart3.Init.StopBits = UART_STOPBITS_1;

 huart3.Init.Parity = UART_PARITY_NONE;

 huart3.Init.Mode = UART_MODE_TX_RX;

 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart3.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart3) != HAL_OK)

 {

   Error_Handler();

 }

}

antonius
antoniusAuthor
Associate III
April 7, 2019

clock

0690X000008A1dyQAC.jpg

S.Ma
Principal
April 7, 2019

What do you want to do? Use HSI through PLL and generate the same SYSCLK 2.097 MHz default MSI frequency?

antonius
antoniusAuthor
Associate III
April 7, 2019

I want to use 16MHz HSI, but SPI2 and UART3 are misconfigured , working on 2.097MHz but not on HSI 16MHz, by the time I have 16MHz

any clues or same experiences ? thanks

antonius
antoniusAuthor
Associate III
April 7, 2019

I put on 32MHz, only USART3 can not work properly

I make :

   USART3->BRR = 32000000/115200;

Correct me ?0690X000008A1g4QAC.jpg

///

void MX_USART3_UART_Init(void)

{

 // Configure BRR by deviding the bus clock with the baud rate

 huart3.Instance = USART3;

 huart3.Init.BaudRate = 115200;

 huart3.Init.WordLength = UART_WORDLENGTH_8B;

 huart3.Init.StopBits = UART_STOPBITS_1;

 huart3.Init.Parity = UART_PARITY_NONE;

 huart3.Init.Mode = UART_MODE_TX_RX;

 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart3.Init.OverSampling = UART_OVERSAMPLING_16;

   USART3->BRR = 32000000/115200;

 if (HAL_UART_Init(&huart3) != HAL_OK)

 {

   Error_Handler();

 }

}

antonius
antoniusAuthor
Associate III
April 8, 2019

Please correct my configuration ?

====

void MX_USART3_UART_Init(void)

{

 huart3.Instance = USART3;

   huart3.Instance->BRR=34;

 huart3.Init.BaudRate = 115200;

 huart3.Init.WordLength = UART_WORDLENGTH_8B;

 huart3.Init.StopBits = UART_STOPBITS_1;

 huart3.Init.Parity = UART_PARITY_NONE;

 huart3.Init.Mode = UART_MODE_TX_RX;

 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart3.Init.OverSampling = UART_OVERSAMPLING_8;

 if (HAL_UART_Init(&huart3) != HAL_OK)

 {

   Error_Handler();

 }

}

=====

I read from :

0690X000008A25sQAC.jpg

Tesla DeLorean
Guru
April 8, 2019

I would decompose the clocks as seen of the AHB/APB. Print them out a USART that is working. Use the SWV channel if available.

HAL_UART_Init() is going to overwrite the value you put in BBR, and BBR might well be locked when the USART is enabled.

What bit rate do you see if you send 0x55 data patterns? You see anything at the pins?

Assuming the L152, any board we might be familiar with?

Drops into Error_Handler() ?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
April 8, 2019

Assuming the L152, any board we might be familiar with?

Nucleo STM32L152RE

Drops into Error_Handler() ?

printf on my Error_Handler ? It's working with uart2, which is talking with GPS, but uart3 not working on a proper speed ??

0x55 pattern to uart3 ? and ping back ?

thanks

antonius
antoniusAuthor
Associate III
April 8, 2019

decomposing ? sounds advance for me, trying to figure it out on implementing it ...

Thanks

Tesla DeLorean
Guru
April 8, 2019

Starts by doing simple stuff

Like

 printf("Core=%d, %d MHz\n", SystemCoreClock, SystemCoreClock / 1000000);

 printf("HCLK=%d\n", HAL_RCC_GetHCLKFreq());

 printf("APB1=%d\n", HAL_RCC_GetPCLK1Freq());

 printf("APB2=%d\n", HAL_RCC_GetPCLK2Freq());

Outputting internal clocks via PA8 MCO and scoping them.

And then looking at RCC and peripheral settings in debugger, finding settings in the chain that look/are wrong.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
April 9, 2019

On 2.097MHz :

0690X000008A65gQAC.jpg

antonius
antoniusAuthor
Associate III
April 8, 2019

On STM32CubeMX, it said, UART3 has a conflict, does it matter ?

0690X000008A3otQAC.jpg

Tesla DeLorean
Guru
April 8, 2019

Sorry I don't use CubeMX, what does the Reference Manual suggest are the connectivity modes here, and what pin conflicts are there. As presented you're showing with work with clock source X and failing with clock source Y

So I'd focus on the clock trees and sources, and determine is the clock routing is broken, for example you've selected a source that isn't present, or is at a different frequency.

Outputting a stream of 'U' characters (0x55) gives you something to measure on a scope. ie does something come out of the pin? is it the right frequency?

Try doubling or halving the baud rate you program whilst leaving the terminal at 115200. See data then?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
April 14, 2019

the stable setting only :

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 /**Configure the main internal regulator output voltage 
 */
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 /**Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
 RCC_OscInitStruct.MSICalibrationValue = 0;
 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /**Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
}