Skip to content

Commit

Permalink
Added functionality to pass custom parameter to HardwareTimer callback
Browse files Browse the repository at this point in the history
Update example following implementation of stm32duino/Arduino_Core_STM32#892
And add a new examplek to show how to use parameter.
  • Loading branch information
ABOSTM committed Mar 10, 2020
1 parent 683463d commit 776acd9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ uint32_t test_Status = PASSED;
** Interrupt callback
***************************************/
/******** Output *****/
void output_Update_IT_callback(HardwareTimer *)
void output_Update_IT_callback(void)
{
Output_Update++;
}

void Output_Compare1_IT_callback(HardwareTimer *)
void Output_Compare1_IT_callback(void)
{
Output_Compare1++;
}

void Output_Compare2_IT_callback(HardwareTimer *)
void Output_Compare2_IT_callback(void)
{
Output_Compare2++;
}

/******** Input 1 *****/
void Input_Capture1_Rising_IT_callback(HardwareTimer *)
void Input_Capture1_Rising_IT_callback(void)
{
Current1_Capture = MyTim_input->getCaptureCompare(Freq1_channelRising);
/* frequency computation */
Expand All @@ -138,7 +138,7 @@ void Input_Capture1_Rising_IT_callback(HardwareTimer *)
rolloverCompare1Count = 0;
}

void Input_Capture1_Falling_IT_callback(HardwareTimer *)
void Input_Capture1_Falling_IT_callback(void)
{
/* prepare DutyCycle computation */
Current1_Capture = MyTim_input->getCaptureCompare(Freq1_channelFalling);
Expand All @@ -155,7 +155,7 @@ void Input_Capture1_Falling_IT_callback(HardwareTimer *)
}

/******** Input 2 *****/
void Input_Capture2_Rising_IT_callback(HardwareTimer *)
void Input_Capture2_Rising_IT_callback(void)
{
Current2_Capture = MyTim_input->getCaptureCompare(Freq2_channelRising);
/* frequency computation */
Expand All @@ -175,7 +175,7 @@ void Input_Capture2_Rising_IT_callback(HardwareTimer *)
rolloverCompare2Count = 0;
}

void Input_Capture2_Falling_IT_callback(HardwareTimer *)
void Input_Capture2_Falling_IT_callback(void)
{
/* prepare DutyCycle computation */
Current2_Capture = MyTim_input->getCaptureCompare(Freq2_channelFalling);
Expand All @@ -194,7 +194,7 @@ void Input_Capture2_Falling_IT_callback(HardwareTimer *)
/******** Input rollover *****/
/* In case of timer rollover, frequency is to low to be measured set values to 0
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
void Rollover_IT_callback(HardwareTimer *)
void Rollover_IT_callback(void)
{
rolloverCompare1Count++;
rolloverCompare2Count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ HardwareTimer *MyTim;
@brief Input capture interrupt callback : Compute frequency and dutycycle of input signal
*/
void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)
void TIMINPUT_Capture_Rising_IT_callback(void)
{
CurrentCapture = MyTim->getCaptureCompare(channelRising);
/* frequency computation */
Expand All @@ -47,7 +47,7 @@ void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)

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

Expand All @@ -62,7 +62,7 @@ void Rollover_IT_callback(HardwareTimer*)
@brief Input capture interrupt callback : Compute frequency and dutycycle of input signal
*/
void TIMINPUT_Capture_Falling_IT_callback(HardwareTimer*)
void TIMINPUT_Capture_Falling_IT_callback(void)
{
/* prepare DutyCycle computation */
CurrentCapture = MyTim->getCaptureCompare(channelFalling);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ uint32_t input_freq = 0;
volatile uint32_t rolloverCompareCount = 0;
HardwareTimer *MyTim;

void InputCapture_IT_callback(HardwareTimer*)
void InputCapture_IT_callback(void)
{
CurrentCapture = MyTim->getCaptureCompare(channel);
/* frequency computation */
Expand All @@ -36,7 +36,7 @@ void InputCapture_IT_callback(HardwareTimer*)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#define pin2 D3
#endif

void Update_IT_callback(HardwareTimer*)
void Update_IT_callback(void)
{ // Update event correspond to Rising edge of PWM when configured in PWM1 mode
digitalWrite(pin2, LOW); // pin2 will be complementary to pin
}

void Compare_IT_callback(HardwareTimer*)
void Compare_IT_callback(void)
{ // Compare match event correspond to falling edge of PWM when configured in PWM1 mode
digitalWrite(pin2, HIGH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define pin D2
#endif

void Update_IT_callback(HardwareTimer*)
void Update_IT_callback(void)
{ // Toggle pin. 10hz toogle --> 5Hz PWM
digitalWrite(pin, !digitalRead(pin));
}
Expand Down
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. */
}

0 comments on commit 776acd9

Please sign in to comment.