Problem with HardwareTimer

Hi all.

Today I updated the platformio version and libraries for stm32 as well.

In this moment, I just unable to use any HardwareTimer utility due to CPU hang even if simple use how it described in manual.

#include <Arduino.h>
#include <HardwareTimer.h>

>> HardwareTimer *timer2 = new HardwareTimer(TIM2); <<
… rest of code here …

So, since I include this line in code, any application, such as tone() (TIM2 use in tone.cpp by default) will hang my application. Trying to use pure timer2->pwm() does nothing, no PWM at pin.

Currently, no even digital output at pin with timer connected which HardwareTimer initialized…

What’s going wrong?

Processing genericSTM32F103C8 (platform: ststm32; board: genericSTM32F103C8; framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/genericSTM32F103C8.html
PLATFORM: ST STM32 (8.1.0) > STM32F103C8 (20k RAM. 64k Flash)
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (blackmagic) External (blackmagic, jlink, stlink)
PACKAGES:
 - framework-arduinoststm32 4.10900.200819 (1.9.0)
 - framework-cmsis 2.50501.200527 (5.5.1)
 - toolchain-gccarmnoneeabi 1.90201.191206 (9.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 11 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <STM32duino FreeRTOS> 10.0.1
Building in release mode
Building .pio\build\genericSTM32F103C8\firmware.bin
Checking size .pio\build\genericSTM32F103C8\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   5.2% (used 1064 bytes from 20480 bytes)
Flash: [==        ]  19.1% (used 12540 bytes from 65536 bytes)
============================================================================================ [SUCCESS] Took 5.38 seconds ============================================================================================

Terminal will be reused by tasks, press any key to close it.

I have a Blue Pill which is also an STM32. Can you post here your code example and the platformio.ini file please? Thanks.

Please post your code wrapped in three (or more) back ticks (not single quotes) at the top and bottom, thanks. That way it formats as the code in your file.

Cheers,
Norm.

Breaking news!

This issue happens ONLY if FreeRTOS library included (id=2093)
Deleting this line from platformio.ini solves it.

attachInterrupt(XX) also not working while FreeRTOS library attached…

But why?

You have to be careful to not use timer used by other parts of STM32-Arduino. Usually, things like millis() is implemented with the SysTick of the ARM Cortex core (so that’s okay), but may also be implemented using a high-resolution timer (TIMx) of the microcontroller.

PWM outputs rely on timers, too. So things like tone() on a certain pin will use a timer (if it’s not software bit-banged…). In STM32, a hardware timer will have some hardcoded PWM output pins associated with it. (That’s also why no hardware-PWM is possible on some pins)

Hm, I don’t see why that would be the case here in particular, since FreeRTOS only uses the SysTick_Handler

And osSystickHandler is run by…

The SysTick_Handler ISR. So no TIMx used.

Can you provide the a complete, minimal example code and platformio.ini with which the issue occurs?

I know about systick.
I also know about RTOS with interrupts.
But… When I moved test code inside RTOS task, it works, nevertheless same code outside hangs. Moreover, simple blink in loop continue working even placed with RTOS included…

Full code will be attached later, now no PC at hand.

By the way, downgrading ststm32 to previous also solves issue, but break RTOS compilation due to hungreds of compiler errors…

This code is working except interrupt outside RTOS task (e.g., using attachInterrupt inside setup causes CPU hang while first one interrupt is fired.

Therefore, problem with pwm and timer released after I apply git version of FreeRTOS, analogWrite and tone() also working, only interrupt is fired incorrectly.

#include <Arduino.h>
#include <defines.h>
#include <STM32FreeRTOS.h>
#include <HardwareTimer.h>

HardwareTimer *t2 = new HardwareTimer(TIM2);
HardwareTimer *t3 = new HardwareTimer(TIM3);

volatile bool _tone = false;

void test(void *p)
{
  static uint32_t a = 10;
  while (1)
  {
    if (_tone)
    {
      _tone = false;
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      t2->setPWM(2, BUZ_PIN, a, 50);
      a = a + 500;
    };
    vTaskDelay(1);
  }
}

void btn()
{
  _tone = true;
}

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUZ_PIN, OUTPUT);
  pinMode(TEST_BTN_PIN, INPUT);
  //tone(BUZ_PIN, NOTE_A3, 1000);
  digitalWrite(LED_BUILTIN, LOW);

  pinMode(PA7, OUTPUT);

  attachInterrupt(
      TEST_BTN_PIN, btn, FALLING);

  digitalWrite(PA7, LOW);
  t2->setPWM(2, BUZ_PIN, 10, 50);
  t3->setPWM(2, PA7, 10, 50);

  xTaskCreate(test, "TEST", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  vTaskStartScheduler();
}

void loop()
{
}

[env:genericSTM32F103C8]

platform = https://github.com/platformio/platform-ststm32 #ststm32

board = genericSTM32F103C8

framework = arduino

upload_protocol = stlink

lib_deps = 

   #2093     # FreeRTOS

   https://github.com/stm32duino/STM32FreeRTOS