System.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * System.h
8  *
9  * @author: 14/8/2018 - mikee47 <mike@sillyhouse.net>
10  *
11  * When SystemClass::onReady method called, the callback is only queued if the
12  * system is not actually ready; there is otherwise no point in queueing the
13  * callback as it would never get invoked. To avoid unpredictable behaviour and
14  * simplify application code, the callback is invoked immediately in this situation.
15  *
16  * Global task queue added to class, initialised at system startup.
17  *
18  */
19 
20 #pragma once
21 
22 #include <esp_systemapi.h>
23 #include <Delegate.h>
24 #include <Interrupts.h>
25 
37 using TaskCallback32 = void (*)(uint32_t param);
38 
42 using TaskCallback = void (*)(void* param);
43 
46 using TaskDelegate = Delegate<void()>;
47 
51 
56 {
57 public:
59  {
60  }
61 
64  virtual void onSystemReady() = 0;
65 };
66 
71  eCF_80MHz = 80,
72  eCF_125MHz = 125,
73  eCF_133MHz = 133,
74  eCF_160MHz = 160,
75  eCF_240MHz = 240,
76 };
77 
86  4,
87 };
88 
95  eSS_Ready
96 };
97 
101 {
102 public:
104  {
105  }
106 
112  static bool initialize();
113 
117  bool isReady()
118  {
119  return state == eSS_Ready;
120  }
121 
128  void restart(unsigned deferMillis = 0);
129 
135  {
136  return system_update_cpu_freq(freq);
137  }
138 
143  {
144  return static_cast<CpuFrequency>(system_get_cpu_freq());
145  }
146 
162  bool deepSleep(uint32_t timeMilliseconds, DeepSleepOptions options = eDSO_RF_CAL_BY_INIT_DATA);
163 
168  void onReady(SystemReadyDelegate readyHandler)
169  {
170  queueCallback(readyHandler);
171  }
172 
177  void onReady(ISystemReadyHandler* readyHandler)
178  {
179  if(readyHandler != nullptr) {
180  queueCallback([](void* param) { static_cast<ISystemReadyHandler*>(param)->onSystemReady(); }, readyHandler);
181  }
182  }
183 
194  static bool IRAM_ATTR queueCallback(TaskCallback32 callback, uint32_t param = 0);
195 
199  __forceinline static bool IRAM_ATTR queueCallback(TaskCallback callback, void* param = nullptr)
200  {
201  return queueCallback(reinterpret_cast<TaskCallback32>(callback), reinterpret_cast<uint32_t>(param));
202  }
203 
207  __forceinline static bool IRAM_ATTR queueCallback(InterruptCallback callback)
208  {
209  return queueCallback(reinterpret_cast<TaskCallback>(callback));
210  }
211 
220  static bool queueCallback(TaskDelegate callback);
221 
225  static unsigned getTaskCount()
226  {
227 #ifdef ENABLE_TASK_COUNT
228  return taskCount;
229 #else
230  return 255;
231 #endif
232  }
233 
239  static unsigned getMaxTaskCount()
240  {
241 #ifdef ENABLE_TASK_COUNT
242  return maxTaskCount;
243 #else
244  return 255;
245 #endif
246  }
247 
248 private:
249  static void taskHandler(os_event_t* event);
250 
251 private:
252  static SystemState state;
253  static os_event_t taskQueue[];
254 #ifdef ENABLE_TASK_COUNT
255  static volatile uint8_t taskCount;
256  static volatile uint8_t maxTaskCount;
257 #endif
258 };
259 
266 extern SystemClass System;
267 
Interface class implemented by classes to support on-ready callback.
Definition: System.h:56
virtual ~ISystemReadyHandler()
Definition: System.h:58
virtual void onSystemReady()=0
Handle system ready events.
System class.
Definition: System.h:101
static unsigned getTaskCount()
Get number of tasks currently on queue.
Definition: System.h:225
static bool queueCallback(TaskCallback32 callback, uint32_t param=0)
Queue a deferred callback.
static unsigned getMaxTaskCount()
Get maximum number of tasks seen on queue at any one time.
Definition: System.h:239
void onReady(SystemReadyDelegate readyHandler)
Set handler for system ready event.
Definition: System.h:168
CpuFrequency getCpuFrequency()
Get the CPU frequency.
Definition: System.h:142
void restart(unsigned deferMillis=0)
Request a restart of the system.
static bool queueCallback(TaskDelegate callback)
Queue a deferred Delegate callback.
bool setCpuFrequency(CpuFrequency freq)
Set the CPU frequency.
Definition: System.h:134
void onReady(ISystemReadyHandler *readyHandler)
Set handler for system ready event.
Definition: System.h:177
bool deepSleep(uint32_t timeMilliseconds, DeepSleepOptions options=eDSO_RF_CAL_BY_INIT_DATA)
Enter deep sleep mode. Deep sleep turns off processor and keeps only the RTC memory active.
bool isReady()
Check if system ready.
Definition: System.h:117
static bool queueCallback(TaskCallback callback, void *param=nullptr)
Queue a deferred callback, with optional void* parameter.
Definition: System.h:199
SystemClass()
Definition: System.h:103
static bool queueCallback(InterruptCallback callback)
Queue a deferred callback with no callback parameter.
Definition: System.h:207
static bool initialize()
System initialisation.
void(*)() InterruptCallback
Definition: Interrupts.h:23
CpuFrequency
Common CPU frequencies.
Definition: System.h:70
void(*)(uint32_t param) TaskCallback32
Task callback function type, uint32_t parameter.
Definition: System.h:37
DeepSleepOptions
Deep sleep options.
Definition: System.h:81
SystemClass System
Global instance of system object.
SystemState
System state.
Definition: System.h:92
Delegate< void()> TaskDelegate
Task Delegate callback type.
Definition: System.h:46
void(*)(void *param) TaskCallback
Task callback function type, void* parameter.
Definition: System.h:42
@ eCF_133MHz
Definition: System.h:73
@ eCF_125MHz
Definition: System.h:72
@ eCF_160MHz
Definition: System.h:74
@ eCF_80MHz
Definition: System.h:71
@ eCF_240MHz
Definition: System.h:75
@ eDSO_DISABLE_RF
disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
Definition: System.h:85
@ eDSO_RF_CAL_BY_INIT_DATA
RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
Definition: System.h:82
@ eDSO_RF_CAL_ALWAYS
RF_CAL after deep-sleep wake up, there will be large current.
Definition: System.h:83
@ eDSO_RF_CAL_NEVER
no RF_CAL after deep-sleep wake up, there will only be small current.
Definition: System.h:84
@ eSS_Intializing
System initialising.
Definition: System.h:94
@ eSS_Ready
System ready.
Definition: System.h:95
@ eSS_None
System state unknown.
Definition: System.h:93