Skip to content

Timebase_callback example does not work with STMduino core #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Bodmer opened this issue Jan 24, 2020 · 2 comments
Closed

Timebase_callback example does not work with STMduino core #19

Bodmer opened this issue Jan 24, 2020 · 2 comments

Comments

@Bodmer
Copy link

Bodmer commented Jan 24, 2020

Could not get the example to work or find a way using the HardwareTimer support library so resorted to HAL. This works:

/*
  Timer interrupt callback
  This example shows how to configure HardwareTimer to execute a callback at a regular interval.
  Callback toggles pin.
*/

#include "HardwareTimer.h"

#if defined(TIM1)
  #define TIMER TIM1
#else
  #define TIMER TIM2
#endif

#define TICK_HZ 1 // Set interrupt frequency

// Create an instance of the timer class
HardwareTimer tick = HardwareTimer(TIMER);

void timerCallback(HardwareTimer*) {
  // Toggle the LED pin state
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

void setup() {
  // Set LED pin to output
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  // Attach interrupt to the sketch callback
  tick.attachInterrupt(timerCallback);

  // Configure the timer using low level HAL calls
  TIM_HandleTypeDef timerHal;
  timerHal.Instance = TIMER;
  timerHal.Init.Prescaler = tick.getTimerClkFreq() / 4000;
  timerHal.Init.Period = (4000 / TICK_HZ) - 1; // Tick frequency
  timerHal.Init.CounterMode = TIM_COUNTERMODE_UP;
  timerHal.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  timerHal.Init.RepetitionCounter = 0;
  HAL_TIM_Base_Init(&timerHal);
  HAL_TIM_Base_Start(&timerHal);
}

void loop() {
  // Timer interrupt toggles LED as a background interrupt task
}
@fpistm
Copy link
Member

fpistm commented Jan 25, 2020

I guess you used the master example example instead of the latest release one.
See 60f8b8c#diff-496a1315886bd1b0c808440193033adeL34

So add the setMode to the example

@Bodmer
Copy link
Author

Bodmer commented Jan 25, 2020

You guessed right. All working now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants