initial commit
This commit is contained in:
87
io/Timer0.h
Normal file
87
io/Timer0.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#ifndef TIMER0_H
|
||||
#define TIMER0_H
|
||||
|
||||
#include "ESP.h"
|
||||
|
||||
#define TIMER0_VARIABLES() \
|
||||
volatile timercallback Timer0::callback = nullptr; \
|
||||
volatile uint32_t Timer0::tickIncrement; \
|
||||
|
||||
typedef void(*timercallback)(void);
|
||||
|
||||
|
||||
//extern "C" {
|
||||
// void timer0_detachInterrupt();
|
||||
// void timer0_attachInterrupt(timercallback userFunc);
|
||||
// void timer0_isr_init();
|
||||
//}
|
||||
|
||||
class Timer0 {
|
||||
|
||||
private:
|
||||
|
||||
static volatile timercallback callback;
|
||||
|
||||
static volatile uint32_t tickIncrement;
|
||||
|
||||
public:
|
||||
|
||||
/** creates a one-time interrupt after the given delay */
|
||||
void fireOnce(const uint32_t tickDelay) {
|
||||
ETS_CCOMPARE0_DISABLE();
|
||||
ETS_CCOMPARE0_INTR_ATTACH(isrOnce, NULL);
|
||||
setDelay(tickDelay);
|
||||
}
|
||||
|
||||
/** creates a periodic interrupt with the given delay-interval */
|
||||
void fireLoop(const uint32_t tickInterval) {
|
||||
ETS_CCOMPARE0_DISABLE();
|
||||
tickIncrement = tickInterval;
|
||||
ETS_CCOMPARE0_INTR_ATTACH(isrLoop, NULL);
|
||||
setDelay(1000);
|
||||
}
|
||||
|
||||
void fireLoopHz(const uint32_t hz) {
|
||||
const uint32_t ticks = (80*1000*1000) / hz;
|
||||
fireLoop(ticks);
|
||||
}
|
||||
|
||||
void attachInterrupt(timercallback userFunc) {
|
||||
callback = userFunc;
|
||||
}
|
||||
|
||||
// void detachInterrupt() {
|
||||
// timer0_user_cb = NULL;
|
||||
// ETS_CCOMPARE0_DISABLE();
|
||||
// }
|
||||
|
||||
#define timer0_interrupted() (ETS_INTR_PENDING() & (_BV(ETS_COMPARE0_INUM)))
|
||||
#define timer0_read() ((__extension__({uint32_t count;__asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count));count;})))
|
||||
#define timer0_write(count) __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory")
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void setDelay(const uint32_t ticks) {
|
||||
timer0_write(ESP::getCycleCount() + ticks);
|
||||
ETS_CCOMPARE0_ENABLE();
|
||||
}
|
||||
|
||||
/** fire once, hereafter disable the timer */
|
||||
static void isrOnce(void* para) {
|
||||
(void) para;
|
||||
ETS_CCOMPARE0_DISABLE();
|
||||
if (callback) {callback();}
|
||||
}
|
||||
|
||||
/** fire periodically */
|
||||
static void isrLoop(void* para) {
|
||||
(void) para;
|
||||
timer0_write(ESP::getCycleCount() + tickIncrement);
|
||||
if (callback) {callback();}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // TIMER0_H
|
||||
Reference in New Issue
Block a user