Skip to content

Commit

Permalink
IntervalTimer use inplace_function for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Apr 5, 2023
1 parent a9d7ece commit 673a4c0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions teensy3/IntervalTimer.cpp
Expand Up @@ -34,16 +34,16 @@ static void dummy_funct(void);

#if defined(KINETISK)
#define NUM_CHANNELS 4
static void (*funct_table[4])(void) = {dummy_funct, dummy_funct, dummy_funct, dummy_funct};
static IntervalTimer::callback funct_table[4] = {dummy_funct, dummy_funct, dummy_funct, dummy_funct};

#elif defined(KINETISL)
#define NUM_CHANNELS 2
static void (*funct_table[2])(void) = {dummy_funct, dummy_funct};
static IntervalTimer::callback funct_table[2] = {dummy_funct, dummy_funct};
uint8_t IntervalTimer::nvic_priorites[2] = {255, 255};
#endif


bool IntervalTimer::beginCycles(void (*funct)(), uint32_t cycles)
bool IntervalTimer::beginCycles(callback funct, uint32_t cycles)
{
if (channel) {
channel->TCTRL = 0;
Expand Down
16 changes: 9 additions & 7 deletions teensy3/IntervalTimer.h
Expand Up @@ -32,6 +32,7 @@
#define __INTERVALTIMER_H__

#include "kinetis.h"
#include "inplace_function.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -48,29 +49,30 @@ class IntervalTimer {
~IntervalTimer() {
end();
}
bool begin(void (*funct)(), unsigned int microseconds) {
using callback = stdext::inplace_function<void(void), 16>;
bool begin(callback funct, unsigned int microseconds) {
if (microseconds == 0 || microseconds > MAX_PERIOD) return false;
uint32_t cycles = (F_BUS / 1000000) * microseconds - 1;
if (cycles < 36) return false;
return beginCycles(funct, cycles);
}
bool begin(void (*funct)(), int microseconds) {
bool begin(callback funct, int microseconds) {
if (microseconds < 0) return false;
return begin(funct, (unsigned int)microseconds);
}
bool begin(void (*funct)(), unsigned long microseconds) {
bool begin(callback funct, unsigned long microseconds) {
return begin(funct, (unsigned int)microseconds);
}
bool begin(void (*funct)(), long microseconds) {
bool begin(callback funct, long microseconds) {
return begin(funct, (int)microseconds);
}
bool begin(void (*funct)(), float microseconds) {
bool begin(callback funct, float microseconds) {
if (microseconds <= 0 || microseconds > MAX_PERIOD) return false;
uint32_t cycles = (float)(F_BUS / 1000000) * microseconds - 0.5;
if (cycles < 36) return false;
return beginCycles(funct, cycles);
}
bool begin(void (*funct)(), double microseconds) {
bool begin(callback funct, double microseconds) {
return begin(funct, (float)microseconds);
}
void update(unsigned int microseconds) {
Expand Down Expand Up @@ -135,7 +137,7 @@ class IntervalTimer {
#if defined(KINETISL)
static uint8_t nvic_priorites[2];
#endif
bool beginCycles(void (*funct)(), uint32_t cycles);
bool beginCycles(callback funct, uint32_t cycles);

};

Expand Down
4 changes: 2 additions & 2 deletions teensy4/IntervalTimer.cpp
Expand Up @@ -34,11 +34,11 @@
static void pit_isr(void);

#define NUM_CHANNELS 4
static void (*funct_table[4])(void) __attribute((aligned(32))) = {nullptr, nullptr, nullptr, nullptr};
static IntervalTimer::callback funct_table[4] __attribute((aligned(32))) = {nullptr, nullptr, nullptr, nullptr};
uint8_t IntervalTimer::nvic_priorites[4] = {255, 255, 255, 255};


bool IntervalTimer::beginCycles(void (*funct)(), uint32_t cycles)
bool IntervalTimer::beginCycles(callback funct, uint32_t cycles)
{
printf("beginCycles %u\n", cycles);
if (channel) {
Expand Down
16 changes: 9 additions & 7 deletions teensy4/IntervalTimer.h
Expand Up @@ -33,6 +33,7 @@

#include <stddef.h>
#include "imxrt.h"
#include "inplace_function.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -51,10 +52,11 @@ class IntervalTimer {
~IntervalTimer() {
end();
}
using callback = stdext::inplace_function<void(void), 16>;
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), unsigned int microseconds) {
bool begin(callback funct, unsigned int microseconds) {
if (microseconds == 0 || microseconds > MAX_PERIOD) return false;
uint32_t cycles = (24000000 / 1000000) * microseconds - 1;
if (cycles < 17) return false;
Expand All @@ -63,26 +65,26 @@ class IntervalTimer {
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), int microseconds) {
bool begin(callback funct, int microseconds) {
if (microseconds < 0) return false;
return begin(funct, (unsigned int)microseconds);
}
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), unsigned long microseconds) {
bool begin(callback funct, unsigned long microseconds) {
return begin(funct, (unsigned int)microseconds);
}
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), long microseconds) {
bool begin(callback funct, long microseconds) {
return begin(funct, (int)microseconds);
}
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), float microseconds) {
bool begin(callback funct, float microseconds) {
if (microseconds <= 0 || microseconds > MAX_PERIOD) return false;
uint32_t cycles = (float)(24000000 / 1000000) * microseconds - 0.5f;
if (cycles < 17) return false;
Expand All @@ -91,7 +93,7 @@ class IntervalTimer {
// Start the hardware timer and begin calling the function. The
// interval is specified in microseconds. Returns true is sucessful,
// or false if all hardware timers are already in use.
bool begin(void (*funct)(), double microseconds) {
bool begin(callback funct, double microseconds) {
return begin(funct, (float)microseconds);
}
// Change the timer's interval. The current interval is completed
Expand Down Expand Up @@ -168,7 +170,7 @@ class IntervalTimer {
IMXRT_PIT_CHANNEL_t *channel = nullptr;
uint8_t nvic_priority = 128;
static uint8_t nvic_priorites[4];
bool beginCycles(void (*funct)(), uint32_t cycles);
bool beginCycles(callback funct, uint32_t cycles);

};

Expand Down

0 comments on commit 673a4c0

Please sign in to comment.