Skip to content

Commit 24ce323

Browse files
authored
Added functionality to pass custom parameter to HardwareTimer callback (#23)
1 parent 683463d commit 24ce323

File tree

6 files changed

+90
-17
lines changed

6 files changed

+90
-17
lines changed

examples/NonReg/HardwareTimer/HardwareTimer_OutputInput_test/HardwareTimer_OutputInput_test.ino

+13-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
/***************************************
3333
** Defines
3434
***************************************/
35+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
36+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
37+
#endif
38+
39+
3540
#if !defined(ARDUINO_NUCLEO_L476RG)
3641
#error "Sketch is applicable to NUCLEO_L476RG"
3742
#endif
@@ -102,23 +107,23 @@ uint32_t test_Status = PASSED;
102107
** Interrupt callback
103108
***************************************/
104109
/******** Output *****/
105-
void output_Update_IT_callback(HardwareTimer *)
110+
void output_Update_IT_callback(void)
106111
{
107112
Output_Update++;
108113
}
109114

110-
void Output_Compare1_IT_callback(HardwareTimer *)
115+
void Output_Compare1_IT_callback(void)
111116
{
112117
Output_Compare1++;
113118
}
114119

115-
void Output_Compare2_IT_callback(HardwareTimer *)
120+
void Output_Compare2_IT_callback(void)
116121
{
117122
Output_Compare2++;
118123
}
119124

120125
/******** Input 1 *****/
121-
void Input_Capture1_Rising_IT_callback(HardwareTimer *)
126+
void Input_Capture1_Rising_IT_callback(void)
122127
{
123128
Current1_Capture = MyTim_input->getCaptureCompare(Freq1_channelRising);
124129
/* frequency computation */
@@ -138,7 +143,7 @@ void Input_Capture1_Rising_IT_callback(HardwareTimer *)
138143
rolloverCompare1Count = 0;
139144
}
140145

141-
void Input_Capture1_Falling_IT_callback(HardwareTimer *)
146+
void Input_Capture1_Falling_IT_callback(void)
142147
{
143148
/* prepare DutyCycle computation */
144149
Current1_Capture = MyTim_input->getCaptureCompare(Freq1_channelFalling);
@@ -155,7 +160,7 @@ void Input_Capture1_Falling_IT_callback(HardwareTimer *)
155160
}
156161

157162
/******** Input 2 *****/
158-
void Input_Capture2_Rising_IT_callback(HardwareTimer *)
163+
void Input_Capture2_Rising_IT_callback(void)
159164
{
160165
Current2_Capture = MyTim_input->getCaptureCompare(Freq2_channelRising);
161166
/* frequency computation */
@@ -175,7 +180,7 @@ void Input_Capture2_Rising_IT_callback(HardwareTimer *)
175180
rolloverCompare2Count = 0;
176181
}
177182

178-
void Input_Capture2_Falling_IT_callback(HardwareTimer *)
183+
void Input_Capture2_Falling_IT_callback(void)
179184
{
180185
/* prepare DutyCycle computation */
181186
Current2_Capture = MyTim_input->getCaptureCompare(Freq2_channelFalling);
@@ -194,7 +199,7 @@ void Input_Capture2_Falling_IT_callback(HardwareTimer *)
194199
/******** Input rollover *****/
195200
/* In case of timer rollover, frequency is to low to be measured set values to 0
196201
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
197-
void Rollover_IT_callback(HardwareTimer *)
202+
void Rollover_IT_callback(void)
198203
{
199204
rolloverCompare1Count++;
200205
rolloverCompare2Count++;

examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
This is specially true for F1 serie (BluePill, ...)
1414
*/
1515

16+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
17+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
18+
#endif
19+
1620
#define pin D2
1721

1822
uint32_t channelRising, channelFalling;
@@ -25,7 +29,7 @@ HardwareTimer *MyTim;
2529
@brief Input capture interrupt callback : Compute frequency and dutycycle of input signal
2630
2731
*/
28-
void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)
32+
void TIMINPUT_Capture_Rising_IT_callback(void)
2933
{
3034
CurrentCapture = MyTim->getCaptureCompare(channelRising);
3135
/* frequency computation */
@@ -47,7 +51,7 @@ void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)
4751

4852
/* In case of timer rollover, frequency is to low to be measured set values to 0
4953
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
50-
void Rollover_IT_callback(HardwareTimer*)
54+
void Rollover_IT_callback(void)
5155
{
5256
rolloverCompareCount++;
5357

@@ -62,7 +66,7 @@ void Rollover_IT_callback(HardwareTimer*)
6266
@brief Input capture interrupt callback : Compute frequency and dutycycle of input signal
6367
6468
*/
65-
void TIMINPUT_Capture_Falling_IT_callback(HardwareTimer*)
69+
void TIMINPUT_Capture_Falling_IT_callback(void)
6670
{
6771
/* prepare DutyCycle computation */
6872
CurrentCapture = MyTim->getCaptureCompare(channelFalling);

examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
This is specially true for F1 serie (BluePill, ...)
1212
*/
1313

14+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
15+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
16+
#endif
17+
1418
#define pin D2
1519

1620
uint32_t channel;
@@ -19,7 +23,7 @@ uint32_t input_freq = 0;
1923
volatile uint32_t rolloverCompareCount = 0;
2024
HardwareTimer *MyTim;
2125

22-
void InputCapture_IT_callback(HardwareTimer*)
26+
void InputCapture_IT_callback(void)
2327
{
2428
CurrentCapture = MyTim->getCaptureCompare(channel);
2529
/* frequency computation */
@@ -36,7 +40,7 @@ void InputCapture_IT_callback(HardwareTimer*)
3640

3741
/* In case of timer rollover, frequency is to low to be measured set value to 0
3842
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
39-
void Rollover_IT_callback(HardwareTimer*)
43+
void Rollover_IT_callback(void)
4044
{
4145
rolloverCompareCount++;
4246

examples/Peripherals/HardwareTimer/PWM_FullConfiguration/PWM_FullConfiguration.ino

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
This is specially true for F1 serie (BluePill, ...)
1414
*/
1515

16-
// 'pin' PWM will be mangaed automatically by hardware whereas 'pin2' PWM will be managed by software through interrupt callback
16+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
17+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
18+
#endif
19+
20+
// 'pin' PWM will be managed automatically by hardware whereas 'pin2' PWM will be managed by software through interrupt callback
1721
#if defined(LED_BUILTIN)
1822
#define pin LED_BUILTIN
1923

@@ -28,12 +32,12 @@
2832
#define pin2 D3
2933
#endif
3034

31-
void Update_IT_callback(HardwareTimer*)
35+
void Update_IT_callback(void)
3236
{ // Update event correspond to Rising edge of PWM when configured in PWM1 mode
3337
digitalWrite(pin2, LOW); // pin2 will be complementary to pin
3438
}
3539

36-
void Compare_IT_callback(HardwareTimer*)
40+
void Compare_IT_callback(void)
3741
{ // Compare match event correspond to falling edge of PWM when configured in PWM1 mode
3842
digitalWrite(pin2, HIGH);
3943
}

examples/Peripherals/HardwareTimer/Timebase_callback/Timebase_callback.ino

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
Once configured, there is only CPU load for callbacks executions.
66
*/
77

8+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
9+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
10+
#endif
11+
812
#if defined(LED_BUILTIN)
913
#define pin LED_BUILTIN
1014
#else
1115
#define pin D2
1216
#endif
1317

14-
void Update_IT_callback(HardwareTimer*)
18+
void Update_IT_callback(void)
1519
{ // Toggle pin. 10hz toogle --> 5Hz PWM
1620
digitalWrite(pin, !digitalRead(pin));
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Timebase callback
3+
This example shows how to configure HardwareTimer to execute a callback with some parameter at regular interval.
4+
Callback toggles pin.
5+
Once configured, there is only CPU load for callbacks executions.
6+
*/
7+
8+
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
9+
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
10+
#endif
11+
12+
#if defined(LED_BUILTIN)
13+
#define pin LED_BUILTIN
14+
#else
15+
#define pin D2
16+
#endif
17+
18+
19+
uint32_t MyData = 1; // Parameter used for callback is arbitrarily a pointer to uint32_t, it could be of other type.
20+
21+
// Every second, print on serial MyData. And increment it.
22+
void Update_IT_callback(uint32_t* data)
23+
{
24+
Serial.println(*data);
25+
*data = *data + 1;
26+
}
27+
28+
void setup()
29+
{
30+
Serial.begin(9600);
31+
#if defined(TIM1)
32+
TIM_TypeDef *Instance = TIM1;
33+
#else
34+
TIM_TypeDef *Instance = TIM2;
35+
#endif
36+
37+
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
38+
HardwareTimer *MyTim = new HardwareTimer(Instance);
39+
40+
// configure pin in output mode
41+
pinMode(pin, OUTPUT);
42+
43+
MyTim->setOverflow(1, HERTZ_FORMAT); // 1 Hz
44+
MyTim->attachInterrupt(std::bind(Update_IT_callback, &MyData)); // bind argument to callback: When Update_IT_callback is called MyData will be given as argument
45+
MyTim->resume();
46+
}
47+
48+
49+
void loop()
50+
{
51+
/* Nothing to do all is done by hardware. Even no interrupt required. */
52+
}

0 commit comments

Comments
 (0)