Skip to content

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
merged 2 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
/***************************************
** Defines
***************************************/
#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(ARDUINO_NUCLEO_L476RG)
#error "Sketch is applicable to NUCLEO_L476RG"
#endif
Expand Down Expand Up @@ -102,23 +107,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 +143,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 +160,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 +180,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 +199,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 @@ -13,6 +13,10 @@
This is specially true for F1 serie (BluePill, ...)
*/

#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif

#define pin D2

uint32_t channelRising, channelFalling;
Expand All @@ -25,7 +29,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 +51,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 +66,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 @@ -11,6 +11,10 @@
This is specially true for F1 serie (BluePill, ...)
*/

#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif

#define pin D2

uint32_t channel;
Expand All @@ -19,7 +23,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 +40,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 @@ -13,7 +13,11 @@
This is specially true for F1 serie (BluePill, ...)
*/

// 'pin' PWM will be mangaed automatically by hardware whereas 'pin2' PWM will be managed by software through interrupt callback
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif

// 'pin' PWM will be managed automatically by hardware whereas 'pin2' PWM will be managed by software through interrupt callback
#if defined(LED_BUILTIN)
#define pin LED_BUILTIN

Expand All @@ -28,12 +32,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 @@ -5,13 +5,17 @@
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

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,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. */
}