Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
HardwareTimer.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HardwareTimer.h
8  *
9  * Created 23.11.2015 by johndoe
10  *
11  ****/
12 
18 #ifndef _SMING_CORE_HARDWARE_TIMER_H_
19 #define _SMING_CORE_HARDWARE_TIMER_H_
20 
21 #include "Interrupts.h"
22 #include "Delegate.h"
23 
24 #define MAX_HW_TIMER_INTERVAL_US 0x7fffff
25 #define MIN_HW_TIMER_INTERVAL_US 0x32
26 
27 // Hardware Timer operating mode
28 enum HardwareTimerMode {
29  eHWT_Maskable,
30  eHWT_NonMaskable,
31 };
32 
36 uint32_t IRAM_ATTR usToTimerTicks(uint32_t us);
37 
41 uint32_t IRAM_ATTR timerTicksToUs(uint32_t ticks);
42 
45 {
46 public:
54  HardwareTimer(HardwareTimerMode mode = eHWT_NonMaskable);
55  ~HardwareTimer();
56 
62  HardwareTimer& IRAM_ATTR initializeUs(uint32_t microseconds,
63  InterruptCallback callback = nullptr); // Init in Microseconds.
64 
70  HardwareTimer& IRAM_ATTR initializeMs(uint32_t milliseconds,
71  InterruptCallback callback = nullptr); // Init in Milliseconds.
72 
77  bool IRAM_ATTR start(bool repeating = true);
78 
83  __forceinline bool IRAM_ATTR startOnce()
84  {
85  return start(false);
86  }
87 
90  void IRAM_ATTR stop();
91 
96  bool IRAM_ATTR restart();
97 
101  bool isStarted()
102  {
103  return started;
104  }
105 
109  uint32_t getIntervalUs()
110  {
111  return interval;
112  }
113 
117  uint32_t getIntervalMs()
118  {
119  return getIntervalUs() / 1000;
120  }
121 
125  bool IRAM_ATTR setIntervalUs(uint32_t microseconds);
126 
130  __forceinline bool IRAM_ATTR setIntervalMs(uint32_t milliseconds)
131  {
132  return setIntervalUs(milliseconds * 1000);
133  }
134 
138  void IRAM_ATTR setCallback(InterruptCallback callback);
139 
143  __forceinline void IRAM_ATTR call()
144  {
145  if(callback != nullptr) {
146  callback();
147  }
148  }
149 
150 private:
151  uint32_t interval = 0;
152  InterruptCallback callback = nullptr;
153  bool repeating = false;
154  bool started = false;
155 };
156 
161 {
162 };
163 
165 #endif /* _SMING_CORE_HARDWARE_TIMER_H_ */
uint32_t IRAM_ATTR usToTimerTicks(uint32_t us)
Convert microseconds into timer ticks.
Definition: HardwareTimer.h:160
__forceinline bool IRAM_ATTR setIntervalMs(uint32_t milliseconds)
Set timer interval.
Definition: HardwareTimer.h:130
HardwareTimer(HardwareTimerMode mode=eHWT_NonMaskable)
Hardware timer.
bool IRAM_ATTR start(bool repeating=true)
Start timer running.
Hardware timer class.
Definition: HardwareTimer.h:44
uint32_t getIntervalMs()
Get timer interval.
Definition: HardwareTimer.h:117
bool IRAM_ATTR restart()
Restart timer.
uint32_t getIntervalUs()
Get timer interval.
Definition: HardwareTimer.h:109
HardwareTimer &IRAM_ATTR initializeMs(uint32_t milliseconds, InterruptCallback callback=nullptr)
Initialise hardware timer.
void IRAM_ATTR stop()
Stops timer.
__forceinline void IRAM_ATTR call()
Call timer callback.
Definition: HardwareTimer.h:143
void IRAM_ATTR setCallback(InterruptCallback callback)
Set timer trigger callback.
bool isStarted()
Check if timer is started.
Definition: HardwareTimer.h:101
HardwareTimer &IRAM_ATTR initializeUs(uint32_t microseconds, InterruptCallback callback=nullptr)
Initialise hardware timer.
__forceinline bool IRAM_ATTR startOnce()
Start one-shot timer.
Definition: HardwareTimer.h:83
uint32_t IRAM_ATTR timerTicksToUs(uint32_t ticks)
Convert timer ticks into microseconds.
bool IRAM_ATTR setIntervalUs(uint32_t microseconds)
Set timer interval.