stm32 hardware timer function with parameter

Hi, I am playing with stm32 black pill (STM32F411CeU6) and I have tried out HardwareTimer example from stm32duino github:
here

In example is shown how to use callback with parameter, but if I change MyData type from uint32_t to int or boolean it fails to compile.

This is an error message:
no instance of overloaded function "HardwareTimer::attachInterrupt" matches the argument list -- argument types are: (std::_Bind<void (*(int *))(uint32_t *data)>) -- object type is: HardwareTimer

It is bug or is it just not implemented or something? Can I make it work?

Let's see your code.

Sorry, my mistake it is not due to type but because of volatile keyword which I add before. But I need this variable to be volatile. It is possible to make it work with volatile variable?

Here is the code :

#include <Arduino.h>
/*
  Timebase callback
  This example shows how to configure HardwareTimer to execute a callback with some parameter at regular interval.
  Callback toggles pin.
  Once configured, there is only CPU load for callbacks executions.
*/

#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION  < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION  >= 0x01090000"
#endif

#if defined(LED_BUILTIN)
#define pin  LED_BUILTIN
#else
#define pin  D2
#endif


volatile uint32_t MyData = 1; // Parameter used for callback is arbitrarily a pointer to uint32_t, it could be of other type.

// Every second, print on serial MyData. And increment it.
void Update_IT_callback(uint32_t* data)
{
  Serial.println(*data);
  *data = *data + 1;
}

void setup()
{
  Serial.begin(9600); 
#if defined(TIM1)
  TIM_TypeDef *Instance = TIM1;
#else
  TIM_TypeDef *Instance = TIM2;
#endif

  // Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
  HardwareTimer *MyTim = new HardwareTimer(Instance);

  // configure pin in output mode
  pinMode(pin, OUTPUT);

  MyTim->setOverflow(1, HERTZ_FORMAT); // 1 Hz
  MyTim->attachInterrupt(std::bind(Update_IT_callback, &MyData)); // bind argument to callback: When Update_IT_callback is called MyData will be given as argument
  MyTim->resume();
}


void loop()
{
  /* Nothing to do all is done by hardware. Even no interrupt required. */
}

andz:
But I need this variable to be volatile. It is possible to make it work with volatile variable?

Which variable? Show the code that doesn't work.

Never mind, I think I see. Perhaps this?

void Update_IT_callback(volatile uint32_t* data) {

Yes, it is compiling now. Thank you! But why should I use it there? I was never adding volatile keywords to any function parameter. I guess it is due to black magic of that bind function which I don't understand.

No, nothing to do with the bind function. If you're going to pass your Update_IT_callback() function a pointer to a volatile uint32_t, then that's what you have to put in the function definition and prototype.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.