STM32F401CC - incorrect operation of timers and delays

Hi. I am having a very strange problem.
Recently bought STM32F401CCu6

and ran into incorrect work of timers in a test sketch.

Code

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PC13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
The test diode does not work correctly in time.

I understood the behavior of the lack of power supply to the external quartz, I realized that it was a matter of the clock

changed generic_clock.c according to cubeMX generation

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_SCALE2);
    / ** 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_NONE;
    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_HSE;
    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 ();
}
}

The situation has changed, but the problem remains.

And with what platformio.ini is that?

Have you tried

[env:genericSTM32F401CC]
platform = ststm32
board = genericSTM32F401CC
framework = arduino
build_flags = -DHSE_VALUE=25000000L

while not modifying framework-internal files like generic_clock.c, but instead adding them in your sketch (src\main.cpp) as

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_SCALE2);
    /** 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_NONE;
    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_HSE;
    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 ();
   }
}

Yes, after reading the recommendations, I tried this option. But it didn’t work.

My Platformio.ini
[env:genericSTM32F401CC]
platform = ststm32
board = genericSTM32F401CC
framework = arduino
upload_protocol = stlink

Can you link to which exact board you bought?

Yes. https://aliexpress.ru/item/32792513237.html?_ga=2.248718023.196515565.1636631544-2081023384.1629635013&item_id=32792513237&sku_id=12000017141150498&spm=a2g39.orderlist.0.0.36154aa6M9v7aY

Version STM32F401.

Does the crystal oscillator read “25.000” on top or a different number?

Does it make a different if you use the standard

[env:blackpill_f401cc]
platform = ststm32
board = blackpill_f401cc
framework = arduino

platformio.ini?

Does it fail in the same way in the Arduino IDE?

Yes I tried the same

[env: blackpill_f401cc]
platform = ststm32
board = blackpill_f401cc
framework = arduino

platformio.ini

In the Arduino IDE, follow the instructions Program STM32 Black Pill (STM32F401 / F411) with Arduino IDE

Works correctly. BUT does not work after reboot. Hmm…

After full earse the chip in ST-linkUtility and uploading the Arduino IDE sketch, there is data in the chip.

Does it matter that I removed Option Bytes and erased the chip completely on first power-up? He did not overwrite it without removing Option Bytes. There was a write protection error.

I did this to check the correctness of saving the memory of the chip.


Option Bytes

Solved a problem! I put the Option Bytes like my old STM32F103 and it all worked!

WDG_SW ON
nRST_stop ON
nRST_STDBY ON

The generic_clock.c file is left modified with an external clock enabled. Additionally, CubeMX included quartz on the RTC.

1 Like

I just did not decide with what frequency to work 25 or less

Huh? Your board has an external crystal of fixed frequency, you should be using that for the HSE (or HSE+PLL to get to higher clockspeeds) per