forked from stm32duino/STM32Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to pass custom parameter to HardwareTimer callback
Update example following implementation of stm32duino/Arduino_Core_STM32#892 And add a new examplek to show how to use parameter.
- Loading branch information
Showing
6 changed files
with
64 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
examples/Peripherals/HardwareTimer/Timebase_callback_with_parameter/Timebase_callback.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
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(LED_BUILTIN) | ||
#define pin LED_BUILTIN | ||
#else | ||
#define pin D2 | ||
#endif | ||
|
||
|
||
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. */ | ||
} |