-
Notifications
You must be signed in to change notification settings - Fork 55
Added functionality to pass custom parameter to HardwareTimer callback #23
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
Merged
fpistm
merged 2 commits into
stm32duino:master
from
ABOSTM:HARDWARETIMER_ADD_CALLBACK_PARAMETER
Apr 17, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
examples/Peripherals/HardwareTimer/Timebase_callback_with_parameter/Timebase_callback.ino
This file contains hidden or 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,52 @@ | ||
/* | ||
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 | ||
|
||
|
||
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. */ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.