Skip to content

Commit 0b936e5

Browse files
committed
Added functionality to pass custom parameter to HardwareTimer callback
1 parent 835a014 commit 0b936e5

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

cores/arduino/HardwareTimer.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,9 @@ void HardwareTimer::setInterruptPriority(uint32_t preemptPriority, uint32_t subP
850850
* @param callback: interrupt callback
851851
* @retval None
852852
*/
853-
void HardwareTimer::attachInterrupt(void (*callback)(HardwareTimer *))
853+
void HardwareTimer::attachInterrupt(void (*callback)(HardwareTimer *), void *arg)
854854
{
855+
args[0] = arg;
855856
if (callbacks[0] != NULL) {
856857
// Callback previously configured : do not clear neither enable IT, it is just a change of callback
857858
callbacks[0] = callback;
@@ -875,6 +876,7 @@ void HardwareTimer::detachInterrupt()
875876
// Disable update interrupt and clear callback
876877
__HAL_TIM_DISABLE_IT(&(_timerObj.handle), TIM_IT_UPDATE); // disables the interrupt call to save cpu cycles for useless context switching
877878
callbacks[0] = NULL;
879+
args[0] = NULL;
878880
}
879881

880882
/**
@@ -883,7 +885,7 @@ void HardwareTimer::detachInterrupt()
883885
* @param callback: interrupt callback
884886
* @retval None
885887
*/
886-
void HardwareTimer::attachInterrupt(uint32_t channel, void (*callback)(HardwareTimer *))
888+
void HardwareTimer::attachInterrupt(uint32_t channel, void (*callback)(HardwareTimer *), void *arg)
887889
{
888890
int interrupt = getIT(channel);
889891
if (interrupt == -1) {
@@ -893,7 +895,7 @@ void HardwareTimer::attachInterrupt(uint32_t channel, void (*callback)(HardwareT
893895
if ((channel == 0) || (channel > (TIMER_CHANNELS + 1))) {
894896
Error_Handler(); // only channel 1..4 have an interrupt
895897
}
896-
898+
args[channel] = arg;
897899
if (callbacks[channel] != NULL) {
898900
// Callback previously configured : do not clear neither enable IT, it is just a change of callback
899901
callbacks[channel] = callback;
@@ -927,6 +929,14 @@ void HardwareTimer::detachInterrupt(uint32_t channel)
927929
// Disable interrupt corresponding to channel and clear callback
928930
__HAL_TIM_DISABLE_IT(&(_timerObj.handle), interrupt);
929931
callbacks[channel] = NULL;
932+
args[channel] = NULL;
933+
}
934+
935+
void* HardwareTimer::getArg(uint32_t channel){
936+
if (channel > (TIMER_CHANNELS + 1)) {
937+
Error_Handler();
938+
}
939+
return args[channel];
930940
}
931941

932942
/**

cores/arduino/HardwareTimer.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,16 @@ class HardwareTimer {
126126
void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority
127127

128128
//Add interrupt to period update
129-
void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
129+
void attachInterrupt(void (*handler)(HardwareTimer *), void *arg = NULL); // Attach interrupt callback which will be called upon update event (timer rollover)
130130
void detachInterrupt(); // remove interrupt callback which was attached to update event
131131
bool hasInterrupt(); //returns true if a timer rollover interrupt has already been set
132132
//Add interrupt to capture/compare channel
133-
void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
133+
void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *), void *arg = NULL); // Attach interrupt callback which will be called upon compare match event of specified channel
134134
void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
135135
bool hasInterrupt(uint32_t channel); //returns true if an interrupt has already been set on the channel compare match
136136

137+
void* getArg(uint32_t channel = 0);
138+
137139
void timerHandleDeinit(); // Timer deinitialization
138140

139141
// Refresh() is usefull while timer is running after some registers update
@@ -152,6 +154,8 @@ class HardwareTimer {
152154
TimerModes_t _ChannelMode[TIMER_CHANNELS];
153155
timerObj_t _timerObj;
154156
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
157+
void *args[1 + TIMER_CHANNELS];
158+
155159
int getChannel(uint32_t channel);
156160
int getLLChannel(uint32_t channel);
157161
int getIT(uint32_t channel);

0 commit comments

Comments
 (0)