Author Topic: STM32duino Baud Rate Mismatch  (Read 3192 times)

0 Members and 1 Guest are viewing this topic.

Offline ipek.grgcTopic starter

  • Contributor
  • Posts: 15
  • Country: tr
STM32duino Baud Rate Mismatch
« on: June 30, 2020, 01:09:37 pm »
Hello everyone,

I have designed my own STM32F1 board and now testing it with STM32duino, through Arduino IDE. I believe I am having a baud mismatch problem. My serial communication test code is:

Code: [Select]
/*
Serial test
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // print out the word:
  Serial.println("Stayin' Alive");
  delay(1000);        // a second delay in between prints for stability
}

When the code runs on STM32F1, serial monitor doesn't show meaningful prints at 9600. Instead it shows it correctly on 19200. Also, when I set the delay to one second, it prints twice in a second.

I did some research and find out it might be happening because my board's frequency is twice as fast than the one selected on Arduino IDE. I have a HSE oscillator  of 16MHz and STM32F103C8T6. On Arduino IDE, CPU speed setting of the board is set to 72MHZ.

Is this happening because of the bootloader or something else?

Thank you.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2451
  • Country: us
Re: STM32duino Baud Rate Mismatch
« Reply #1 on: June 30, 2020, 02:40:19 pm »
It means the configuration of the hardware timers in the Arduino library doesn't correctly match the specs of the actual hardware. The actual setup of hardware registers and timers happens in a function called init(), which is called before setup() and loop() in the default main() function (which you can override if you prefer your own). Odds are it's a header file you need to change a constant in, or something along those lines.
 
The following users thanked this post: ipek.grgc

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5809
  • Country: dk
Re: STM32duino Baud Rate Mismatch
« Reply #2 on: June 30, 2020, 02:45:34 pm »
it probably assumes an 8MHz xtal
 
The following users thanked this post: thm_w, ipek.grgc

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5370
  • Country: nl
Re: STM32duino Baud Rate Mismatch
« Reply #3 on: July 01, 2020, 05:51:42 pm »
STM32F100 has a quit complicated routing of clock signals, including internal dividers and PLL to get to 72MHz from a lower frequency crystal.

Usually it makes 72MHz from an 8MHz crystal. Are you sure you are not overklocking your STM32 to 144MHz at the moment?

Checking and fixing this is a bit beyond the target group for "arduino", but if you want to expand your knowledge it's doable and not too complicated.
 
The following users thanked this post: ipek.grgc

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 405
  • Country: au
Re: STM32duino Baud Rate Mismatch
« Reply #4 on: July 01, 2020, 08:59:45 pm »
I'm a bit surprised it would run that fast, but then I've never experimented with overclocking (accidental or deliberate).

If you are using the “official” core written by STM (there are several others), then the clock configuration is set up in variant.cpp. There are many variant.cpp files, and the one it uses depends on what board you select, for example for “BLUEPILL_F103C8” it contains this:

Code: [Select]
/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow :
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 72000000
  *            HCLK(Hz)                       = 72000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 2
  *            APB2 Prescaler                 = 1
  *            PLL_Source                     = HSE
  *            PLL_Mul                        = 9
  *            Flash Latency(WS)              = 2
  *            ADC Prescaler                  = 6
  *            USB Prescaler                  = 1.5
  * @param  None
  * @retval None
  */
WEAK void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    while (1);
  }

  /* 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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
    while (1);
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
    while (1);
  }
}
 

They give an explanation somewhere of how to create your own board. It is probably just a matter of changing  RCC_HSE_PREDIV_DIV1 to  RCC_HSE_PREDIV_DIV2 .

If you are not using the STM core, well, then good luck finding the equivalent code. If you are just starting out with this, and don't have a very good reason to use one of the other cores, I'd suggest trying to use the STM one.
« Last Edit: July 01, 2020, 09:10:27 pm by ozcar »
 
The following users thanked this post: ipek.grgc


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->