Skip to content
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

Added functionality to pass custom parameter to HardwareTimer callback #892

Merged
merged 9 commits into from
Mar 11, 2020
15 changes: 7 additions & 8 deletions cores/arduino/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ uint32_t HardwareTimer::getCaptureCompare(uint32_t channel, TimerCompareFormat_
* HERTZ_FORMAT: return value is the frequency in hertz for Capture/Compare value
* @retval None
*/
void HardwareTimer::setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *), void (*CompareCallback)(HardwareTimer *))
void HardwareTimer::setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback, callback_function_t CompareCallback)
{
setPWM(channel, digitalPinToPinName(pin), frequency, dutycycle, PeriodCallback, CompareCallback);
}
Expand All @@ -876,7 +876,7 @@ void HardwareTimer::setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, u
* HERTZ_FORMAT: return value is the frequency in hertz for Capture/Compare value
* @retval None
*/
void HardwareTimer::setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *), void (*CompareCallback)(HardwareTimer *))
void HardwareTimer::setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback, callback_function_t CompareCallback)
{
setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
setOverflow(frequency, HERTZ_FORMAT);
Expand Down Expand Up @@ -908,9 +908,9 @@ void HardwareTimer::setInterruptPriority(uint32_t preemptPriority, uint32_t subP
* @param callback: interrupt callback
* @retval None
fpistm marked this conversation as resolved.
Show resolved Hide resolved
fpistm marked this conversation as resolved.
Show resolved Hide resolved
*/
void HardwareTimer::attachInterrupt(void (*callback)(HardwareTimer *))
void HardwareTimer::attachInterrupt(callback_function_t callback)
{
if (callbacks[0] != NULL) {
if (callbacks[0]) {
// Callback previously configured : do not clear neither enable IT, it is just a change of callback
callbacks[0] = callback;
} else {
Expand Down Expand Up @@ -941,7 +941,7 @@ void HardwareTimer::detachInterrupt()
* @param callback: interrupt callback
* @retval None
fpistm marked this conversation as resolved.
Show resolved Hide resolved
fpistm marked this conversation as resolved.
Show resolved Hide resolved
*/
void HardwareTimer::attachInterrupt(uint32_t channel, void (*callback)(HardwareTimer *))
void HardwareTimer::attachInterrupt(uint32_t channel, callback_function_t callback)
{
int interrupt = getIT(channel);
if (interrupt == -1) {
Expand All @@ -951,7 +951,6 @@ void HardwareTimer::attachInterrupt(uint32_t channel, void (*callback)(HardwareT
if ((channel == 0) || (channel > (TIMER_CHANNELS + 1))) {
Error_Handler(); // only channel 1..4 have an interrupt
}

if (callbacks[channel] != NULL) {
// Callback previously configured : do not clear neither enable IT, it is just a change of callback
callbacks[channel] = callback;
Expand Down Expand Up @@ -1047,7 +1046,7 @@ void HardwareTimer::updateCallback(TIM_HandleTypeDef *htim)
HardwareTimer *HT = (HardwareTimer *)(obj->__this);

if (HT->callbacks[0] != NULL) {
HT->callbacks[0](HT);
HT->callbacks[0]();
}
}

Expand Down Expand Up @@ -1088,7 +1087,7 @@ void HardwareTimer::captureCompareCallback(TIM_HandleTypeDef *htim)
HardwareTimer *HT = (HardwareTimer *)(obj->__this);

if (HT->callbacks[channel] != NULL) {
HT->callbacks[channel](HT);
HT->callbacks[channel]();
}
}

Expand Down
14 changes: 8 additions & 6 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ typedef enum {

#ifdef __cplusplus

#include <functional>
using callback_function_t = std::function<void(void)>;

/* Class --------------------------------------------------------*/
class HardwareTimer {
public:
Expand All @@ -109,8 +112,8 @@ class HardwareTimer {
void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided

void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL); // Set all in one command freq in HZ, Duty in percentage. Including both interrup.
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL);
void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr); // Set all in one command freq in HZ, Duty in percentage. Including both interrup.
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr);

void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided
Expand All @@ -128,14 +131,13 @@ class HardwareTimer {
void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority

//Add interrupt to period update
void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
void attachInterrupt(callback_function_t callback); // Attach interrupt callback which will be called upon update event (timer rollover)
void detachInterrupt(); // remove interrupt callback which was attached to update event
bool hasInterrupt(); //returns true if a timer rollover interrupt has already been set
//Add interrupt to capture/compare channel
void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
void attachInterrupt(uint32_t channel, callback_function_t callback); // Attach interrupt callback which will be called upon compare match event of specified channel
void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
bool hasInterrupt(uint32_t channel); //returns true if an interrupt has already been set on the channel compare match

void timerHandleDeinit(); // Timer deinitialization

// Refresh() is usefull while timer is running after some registers update
Expand All @@ -158,7 +160,7 @@ class HardwareTimer {
private:
TimerModes_t _ChannelMode[TIMER_CHANNELS];
timerObj_t _timerObj;
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
callback_function_t callbacks[1 + TIMER_CHANNELS]; //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
};

extern timerObj_t *HardwareTimer_Handle[TIMER_NUM];
Expand Down
5 changes: 2 additions & 3 deletions cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct {
} timerPinInfo_t;

static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
static void tonePeriodElapsedCallback(HardwareTimer *HT);
static void tonePeriodElapsedCallback();
static timerPinInfo_t TimerTone_pinInfo = {NC, 0};
static HardwareTimer *TimerTone = NULL;

Expand All @@ -41,9 +41,8 @@ static HardwareTimer *TimerTone = NULL;
* @param htim : timer handle
* @retval None
*/
static void tonePeriodElapsedCallback(HardwareTimer *HT)
static void tonePeriodElapsedCallback()
{
UNUSED(HT);
GPIO_TypeDef *port = get_GPIO_Port(STM_PORT(TimerTone_pinInfo.pin));

if (port != NULL) {
Expand Down
3 changes: 1 addition & 2 deletions libraries/Servo/src/stm32/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ uint8_t ServoCount = 0; // the total number o
/************ static functions common to all instances ***********************/

volatile uint32_t CumulativeCountSinceRefresh = 0;
static void Servo_PeriodElapsedCallback(HardwareTimer *HT)
static void Servo_PeriodElapsedCallback()
{
UNUSED(HT);
// Only 1 timer used
timer16_Sequence_t timer_id = _timer1;

Expand Down
3 changes: 1 addition & 2 deletions libraries/SoftwareSerial/src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,8 @@ inline void SoftwareSerial::recv()
//

/* static */
inline void SoftwareSerial::handleInterrupt(HardwareTimer *timer)
inline void SoftwareSerial::handleInterrupt()
{
UNUSED(timer);
if (active_in) {
active_in->recv();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/SoftwareSerial/src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SoftwareSerial : public Stream {
void setRX();
void setSpeed(uint32_t speed);
void setRXTX(bool input);
static void handleInterrupt(HardwareTimer *timer);
static void handleInterrupt();

public:
// public methods
Expand Down