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:
58  virtual ~ISystemReadyHandler() = default;
59 
62  virtual void onSystemReady() = 0;
63 };
64 
69  eCF_80MHz = 80,
70  eCF_125MHz = 125,
71  eCF_133MHz = 133,
72  eCF_160MHz = 160,
73  eCF_240MHz = 240,
74 };
75 
84  4,
85 };
86 
93  eSS_Ready
94 };
95 
99 {
100 public:
101  SystemClass() = default;
102 
108  static bool initialize();
109 
113  bool isReady()
114  {
115  return state == eSS_Ready;
116  }
117 
124  void restart(unsigned deferMillis = 0);
125 
131  {
132  return system_update_cpu_freq(freq);
133  }
134 
139  {
140  return static_cast<CpuFrequency>(system_get_cpu_freq());
141  }
142 
158  bool deepSleep(uint32_t timeMilliseconds, DeepSleepOptions options = eDSO_RF_CAL_BY_INIT_DATA);
159 
164  void onReady(SystemReadyDelegate readyHandler)
165  {
166  queueCallback(readyHandler);
167  }
168 
173  void onReady(ISystemReadyHandler* readyHandler)
174  {
175  if(readyHandler != nullptr) {
176  queueCallback([](void* param) { static_cast<ISystemReadyHandler*>(param)->onSystemReady(); }, readyHandler);
177  }
178  }
179 
190  static bool IRAM_ATTR queueCallback(TaskCallback32 callback, uint32_t param = 0)
191  {
192  return queueCallback(reinterpret_cast<TaskCallback>(callback), reinterpret_cast<void*>(param));
193  }
194 
198  static bool IRAM_ATTR queueCallback(TaskCallback callback, void* param = nullptr);
199 
203  static bool IRAM_ATTR queueCallback(InterruptCallback callback);
204 
213  static bool queueCallback(TaskDelegate callback);
214 
218  static unsigned getTaskCount()
219  {
220 #ifdef ENABLE_TASK_COUNT
221  return taskCount;
222 #else
223  return 255;
224 #endif
225  }
226 
232  static unsigned getMaxTaskCount()
233  {
234 #ifdef ENABLE_TASK_COUNT
235  return maxTaskCount;
236 #else
237  return 255;
238 #endif
239  }
240 
241 private:
242  static void taskHandler(os_event_t* event);
243 
244 private:
245  static SystemState state;
246  static os_event_t taskQueue[];
247 #ifdef ENABLE_TASK_COUNT
248  static volatile uint8_t taskCount;
249  static volatile uint8_t maxTaskCount;
250 #endif
251 };
252 
259 extern SystemClass System;
260 
Interface class implemented by classes to support on-ready callback.
Definition: System.h:56
virtual ~ISystemReadyHandler()=default
virtual void onSystemReady()=0
Handle system ready events.
System class.
Definition: System.h:99
static unsigned getTaskCount()
Get number of tasks currently on queue.
Definition: System.h:218
static bool queueCallback(TaskCallback32 callback, uint32_t param=0)
Queue a deferred callback.
Definition: System.h:190
static unsigned getMaxTaskCount()
Get maximum number of tasks seen on queue at any one time.
Definition: System.h:232
void onReady(SystemReadyDelegate readyHandler)
Set handler for system ready event.
Definition: System.h:164
CpuFrequency getCpuFrequency()
Get the CPU frequency.
Definition: System.h:138
void restart(unsigned deferMillis=0)
Request a restart of the system.
SystemClass()=default
static bool queueCallback(TaskDelegate callback)
Queue a deferred Delegate callback.
bool setCpuFrequency(CpuFrequency freq)
Set the CPU frequency.
Definition: System.h:130
void onReady(ISystemReadyHandler *readyHandler)
Set handler for system ready event.
Definition: System.h:173
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:113
static bool queueCallback(TaskCallback callback, void *param=nullptr)
Queue a deferred callback, with optional void* parameter.
static bool queueCallback(InterruptCallback callback)
Queue a deferred callback with no callback parameter.
static bool initialize()
System initialisation.
void(*)() InterruptCallback
Definition: Interrupts.h:23
CpuFrequency
Common CPU frequencies.
Definition: System.h:68
void(*)(uint32_t param) TaskCallback32
Task callback function type, uint32_t parameter.
Definition: System.h:37
DeepSleepOptions
Deep sleep options.
Definition: System.h:79
SystemClass System
Global instance of system object.
SystemState
System state.
Definition: System.h:90
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:71
@ eCF_125MHz
Definition: System.h:70
@ eCF_160MHz
Definition: System.h:72
@ eCF_80MHz
Definition: System.h:69
@ eCF_240MHz
Definition: System.h:73
@ eDSO_DISABLE_RF
disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
Definition: System.h:83
@ eDSO_RF_CAL_BY_INIT_DATA
RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
Definition: System.h:80
@ eDSO_RF_CAL_ALWAYS
RF_CAL after deep-sleep wake up, there will be large current.
Definition: System.h:81
@ eDSO_RF_CAL_NEVER
no RF_CAL after deep-sleep wake up, there will only be small current.
Definition: System.h:82
@ eSS_Intializing
System initialising.
Definition: System.h:92
@ eSS_Ready
System ready.
Definition: System.h:93
@ eSS_None
System state unknown.
Definition: System.h:91