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 
47 
51 
56 {
57 public:
59  {
60  }
61 
64  virtual void onSystemReady() = 0;
65 };
66 
71  eCF_80MHz = 80,
72  eCF_160MHz = 160,
73  eCF_240MHz = 240,
74 };
75 
84  4,
85 };
86 
94 };
95 
99 {
100 public:
102  {
103  }
104 
110  static bool initialize();
111 
115  bool isReady()
116  {
117  return state == eSS_Ready;
118  }
119 
126  void restart(unsigned deferMillis = 0);
127 
132  {
133  system_update_cpu_freq(freq);
134  }
135 
140  {
141  return static_cast<CpuFrequency>(system_get_cpu_freq());
142  }
143 
148  bool deepSleep(uint32_t timeMilliseconds, DeepSleepOptions options = eDSO_RF_CAL_BY_INIT_DATA);
149 
154  void onReady(SystemReadyDelegate readyHandler)
155  {
156  queueCallback(readyHandler);
157  }
158 
163  void onReady(ISystemReadyHandler* readyHandler)
164  {
165  if(readyHandler != nullptr) {
166  queueCallback([](void* param) { static_cast<ISystemReadyHandler*>(param)->onSystemReady(); }, readyHandler);
167  }
168  }
169 
180  static bool IRAM_ATTR queueCallback(TaskCallback32 callback, uint32_t param = 0);
181 
185  __forceinline static bool IRAM_ATTR queueCallback(TaskCallback callback, void* param = nullptr)
186  {
187  return queueCallback(reinterpret_cast<TaskCallback32>(callback), reinterpret_cast<uint32_t>(param));
188  }
189 
193  __forceinline static bool IRAM_ATTR queueCallback(InterruptCallback callback)
194  {
195  return queueCallback(reinterpret_cast<TaskCallback>(callback));
196  }
197 
206  static bool queueCallback(TaskDelegate callback);
207 
211  static unsigned getTaskCount()
212  {
213 #ifdef ENABLE_TASK_COUNT
214  return taskCount;
215 #else
216  return 255;
217 #endif
218  }
219 
225  static unsigned getMaxTaskCount()
226  {
227 #ifdef ENABLE_TASK_COUNT
228  return maxTaskCount;
229 #else
230  return 255;
231 #endif
232  }
233 
234 private:
235  static void taskHandler(os_event_t* event);
236 
237 private:
238  static SystemState state;
239  static os_event_t taskQueue[];
240 #ifdef ENABLE_TASK_COUNT
241  static volatile uint8_t taskCount;
242  static volatile uint8_t maxTaskCount;
243 #endif
244 };
245 
252 extern SystemClass System;
253 
System initialising.
Definition: System.h:92
#define __forceinline
Definition: sming_attr.h:13
void onReady(SystemReadyDelegate readyHandler)
Set handler for system ready event.
Definition: System.h:154
DeepSleepOptions
Deep sleep options.
Definition: System.h:79
disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current...
Definition: System.h:83
CPU 80MHz.
Definition: System.h:71
SystemClass()
Definition: System.h:101
void(*)(uint32_t param) TaskCallback32
Task callback function type, uint32_t parameter.
Definition: System.h:37
CpuFrequency getCpuFrequency()
Get the CPU frequency.
Definition: System.h:139
CpuFrequency
CPU Frequency.
Definition: System.h:70
System ready.
Definition: System.h:93
SystemClass System
Global instance of system object.
RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
Definition: System.h:80
void(*)() InterruptCallback
Definition: Interrupts.h:23
virtual void onSystemReady()=0
Handle system ready events.
CPU 160MHz.
Definition: System.h:72
static unsigned getMaxTaskCount()
Get maximum number of tasks seen on queue at any one time.
Definition: System.h:225
bool isReady()
Check if system ready.
Definition: System.h:115
RF_CAL after deep-sleep wake up, there will be large current.
Definition: System.h:81
Delegate< void()> TaskDelegate
Task Delegate callback type.
Definition: System.h:46
CPU 240MHz.
Definition: System.h:73
static unsigned getTaskCount()
Get number of tasks currently on queue.
Definition: System.h:211
static bool queueCallback(InterruptCallback callback)
Queue a deferred callback with no callback parameter.
Definition: System.h:193
virtual ~ISystemReadyHandler()
Definition: System.h:58
System state unknown.
Definition: System.h:91
static bool queueCallback(TaskCallback callback, void *param=nullptr)
Queue a deferred callback, with optional void* parameter.
Definition: System.h:185
System class.
Definition: System.h:98
void initialize()
Called early in the startup phase.
void onReady(ISystemReadyHandler *readyHandler)
Set handler for system ready event.
Definition: System.h:163
void(*)(void *param) TaskCallback
Task callback function type, void* parameter.
Definition: System.h:42
void setCpuFrequency(CpuFrequency freq)
Set the CPU frequency.
Definition: System.h:131
no RF_CAL after deep-sleep wake up, there will only be small current.
Definition: System.h:82
Interface class implemented by classes to support on-ready callback.
Definition: System.h:55
SystemState
System state.
Definition: System.h:90