Arduino_core_stm32: STM32F405RG Clock Not Working

Created on 12 Mar 2021  路  14Comments  路  Source: stm32duino/Arduino_Core_STM32

Describe the bug
Code does not run at all on Generic STM32F405RG target.

To Reproduce

#define LED_PIN PA13
void setup() {
    pinMode(LED_PIN , OUTPUT);
}

void loop() {
    digitalWrite(LED_PIN , LOW);
    delay(500);
    digitalWrite(LED_PIN , HIGH);
    delay(500);
  }
}

Steps to reproduce the behavior:

  1. Upload
  2. Nothing happens. Depending on configuration, the LED might blink once (see additional context)

Expected behavior
The LED blinks.

Desktop (please complete the following information):

  • OS: Windows]
  • Arduino IDE version: 1.8.12
  • STM32 core version: 1.9.0
  • Tools menu settings if not the default:
    image
  • Upload method: DFU

Board (please complete the following information):

Additional context
When I include the following updated timer code from master, the issue gets weirder.
https://github.com/stm32duino/Arduino_Core_STM32/blob/master/variants/Generic_F4x5RG/variant.cpp#L176
Line 176 down to the end of the file was copied into the sketch, and the WEAK modifier on the SystemClock_Config function was removed.

Now, the LED blinks once and then doesn't blink ever again. This is really confusing me - it seems like the MCU crashes or something. I can't easily debug as the USB serial doesn't work when configured (Device Descriptor Request Failed, probably also due to the clock issue). I have tried both wrapping it in another for loop and using while (millis() - start > 500); as an alternative to delay() but it always blinks once, no matter what the delays, and then crashes.

I also really don't understand why duplicating this code fixes the issue...

UPDATE: Updating my whole core to master completely breaks it again! :(

Then I tried defining HSE_VALUE to different values.

#define HSE_VALUE 12000000U results in working code that runs 1.5x too fast (as the clock is actually 80MHz)
#define HSE_VALUE 8000000U results in code that crashes.

Question Waiting feedback

Most helpful comment

Thank you for your time helping with this. I don't know what's going on with PA13's LED. PA14's LED works completely fine and since I don't have a board schematic I'll have to ignore that issue for now.

I probed the board and found that the HSE is 8MHz. After studying the PLL configuration some more (this is my first time programming STM32...) I came up with the following:

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.PLLM = 4; // VCO Input Clock 2MHz
RCC_OscInitStruct.PLL.PLLN = 168; // VCO Output Clock 336MHz
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // Core Clock 168MHz
RCC_OscInitStruct.PLL.PLLQ = 7; // USB Clock 48MHz

This works! :)

All 14 comments

I've been playing with the core trying to get it to work, and I've got to the point where even after reverting the core to 1.9.0, I can't get it to work anymore, even with the strange workarounds I found earlier. I am truly stuck, unfortunately this bug makes this core useless on this chip :(

Hi @harry1453
Generic variant try first to use an HSE then HSE bypass and finally HSI.
My guess is that the HSE or HSE by pass failed.Is your board has an external oscillator?

Note: can be linked to this issue: https://github.com/STMicroelectronics/STM32CubeF4/issues/17

Hi @harry1453,
What I propose you is to check the different steps below:

1) Work with HSI as System clock (No PLL) 16MHz :
in your sketch add an empty SystemClock_Config. This will keep default clock configuration after reset (aka HSI)

extern "C" void SystemClock_Config(void)
{
} 

Check if led is blinking correctly. If ok, then check step2

2) Work with PLL as SystemClock (168MHz) with HSI as source
In your sketch:

extern "C" void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  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_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;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

Check if led is blinking correctly.

@harry1453 Don't forget the extern "C" like I did initially (thanks @fpistm)

Thanks for the response. I've tried both options. Interestingly removing/adding extern "C" makes no difference in either case, which is suspicious to me.

First case (empty clock config) = LED lights very dimly, blinks off, and then doesn't come back on.

Second case = LED blinks, albeit too quickly and very dimly.

@fpistm How can I tell which STM32Cube version I'm using to make sure that I'm on 1.26.0 to avoid that bug?

I'm trying to find out the clock configuration of my board but unfortunately it is a closed source design and I'm struggling to find it... It seems most likely to me that it has an external oscillator, but I'm not sure. All other similar flight controller boards I can find information on all have external 8MHz oscillators, so this seems most likely.

Even if I don't understand, I confirm that with or without extern "C" produce the same result: the SystemClock_Config() function used is the one of the sketch.
Now I am very surprised that with empty clock configuration it doesn't work. Could you try the same (step1 and step2) with another led ? I mean on another gpio pin

@fpistm How can I tell which STM32Cube version I'm using to make sure that I'm on 1.26.0 to avoid that bug?

https://github.com/stm32duino/Arduino_Core_STM32/blob/master/system/Drivers/STM32YYxx_HAL_Driver_version.md

Regarding the board's LED pad, that's for Adafruit NeoPixel LEDs. I'm using the built-in one marked LED1 here (circled):
image
Unfortunately I don't have the schematic for this board so I can't tell if it's just an LED. I only know that it's on PA13.

However...

I changed my program to this:

#include <Arduino.h>

void setup() {
    pinMode(PA10, OUTPUT);
}

void loop() {
    digitalWrite(PA10, LOW);
    delay(500);
    digitalWrite(PA10, HIGH);
    delay(500);
}

extern "C" void SystemClock_Config(void)
{
}

and on PA10 I get the following:
SDS00001
....which is perfect!!!

I have no idea why the LED doesn't work at full brightness... especially when it worked with the Feather F405 variant.. but that seems like a hardware issue perhaps, as LED0 (on pin PA14) works fine.

With the 168MHz PLL clock the code also works correctly.

However, when removing the function entirely the default implementation is still broken - no output is seen on the pin.

Sorry, but I'm now struggling to get USB VCP to work... Could the issue be to do with the timer configuration?

Using PIO,

build_flags =
    -D USBD_USE_CDC
    -D USBCON
    -D HAL_PCD_MODULE_ENABLED

and code:

void setup() {
    SerialUSB.begin();
    delay(10000);
    SerialUSB.println("Hello World!");
}

void loop() {
}

extern "C" void SystemClock_Config(void)
{
  (Code from above)
}

The only result is Device Descriptor Request Failed. I know that this board is good for USB VCP because other firmwares use it on this board. Declaring -D USE_USB_HS causes it to not even be detected as a USB device.

Hi @harry1453,
The "SystemClock_Config(void)" above doesn't configure USB clock properly, thus USB could not work.

Below is the modified code (PLLQ modified).
Please note that USB with HSI based clock is not optimal, it would be better to use HSE (if you succeed to get information of the HSE on your board)

extern "C" void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  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_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;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

By the way, we tested Adafruit Feather F405, and led is blinking properly on PA13.

Thank you for your time helping with this. I don't know what's going on with PA13's LED. PA14's LED works completely fine and since I don't have a board schematic I'll have to ignore that issue for now.

I probed the board and found that the HSE is 8MHz. After studying the PLL configuration some more (this is my first time programming STM32...) I came up with the following:

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.PLLM = 4; // VCO Input Clock 2MHz
RCC_OscInitStruct.PLL.PLLN = 168; // VCO Output Clock 336MHz
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // Core Clock 168MHz
RCC_OscInitStruct.PLL.PLLQ = 7; // USB Clock 48MHz

This works! :)

So if all is fine now. This issue can be closed?

Yes, thank you both very much for your help!

Was this page helpful?
0 / 5 - 0 ratings