stm32duino / Arduino_Core_STM32

STM32 core support for Arduino

Home Page:https://github.com/stm32duino/Arduino_Core_STM32/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SystemClock_Config(void) unexpected behavior

AirchipTeknoloji opened this issue · comments

Desktop:

OS: macOS 14.3.1
Arduino IDE version: 2.3.1
STM32 core version: 2.7.1
Upload method: STM32 Cube Programmer (Serial)

Board:

Name: STM32F0 based

Board PART NUMBER:

Name: STM32F030C8TX

Description:

I have custom STM32F030C8T6 based board and when I use HSI or nothing change at sketch level everything works as expected.
But when I want to use HSE and use SystemClock_Config(void) at sketch level, the problem arises. Everything goes up to 2x faster. If I set usart to 9600 baud rate, I can read it correctly at 19200 baud rate. If I make a led on and off for 10 secs, It goes for 5 sec.

Here my very basic skech to reproduce error:

HardwareSerial Serial1(PB7, PB6);
extern "C" void SystemClock_Config(void);
extern "C" void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
  RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                | RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
    Error_Handler();
  }
}
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial1.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  Serial1.println("HEY");  // turn the LED on (HIGH is the voltage level)
  delay(5000);                      // wait for a second
                    // wait for a second
}

I use 16mhz external btw.
Nothing changes when I change RCC_PLL_MUL3 to anything or RCC_PREDIV_DIV1 to anything. Using Arduino IDE 2.3.1 and latest version of (2.7.1) STM32 Based Boards. I tried so many things but can't make it work. Is this issue related to me or core related? Any help will be appreciated.

Thanks in advance.
Aykut.

For anybody facing this issue:

It requires also to redefine the default HSE_VALUE using the build_opt.h:
-DHSE_VALUE=16000000U