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 typedef void (*TaskCallback32)(uint32_t param);
38 
42 typedef void (*TaskCallback)(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 };
74 
83  4,
84 };
85 
93 };
94 
98 {
99 public:
101  {
102  }
103 
109  static bool initialize();
110 
114  bool isReady()
115  {
116  return state == eSS_Ready;
117  }
118 
125  void restart(unsigned deferMillis = 0);
126 
131  {
132  system_update_cpu_freq(freq);
133  }
134 
139  {
140  return static_cast<CpuFrequency>(system_get_cpu_freq());
141  }
142 
147  bool deepSleep(uint32_t timeMilliseconds, DeepSleepOptions options = eDSO_RF_CAL_BY_INIT_DATA);
148 
153  void onReady(SystemReadyDelegate readyHandler)
154  {
155  queueCallback(readyHandler);
156  }
157 
162  void onReady(ISystemReadyHandler* readyHandler)
163  {
164  if(readyHandler != nullptr) {
165  queueCallback([](void* param) { static_cast<ISystemReadyHandler*>(param)->onSystemReady(); }, readyHandler);
166  }
167  }
168 
179  static bool IRAM_ATTR queueCallback(TaskCallback32 callback, uint32_t param = 0);
180 
184  __forceinline static bool IRAM_ATTR queueCallback(TaskCallback callback, void* param = nullptr)
185  {
186  return queueCallback(reinterpret_cast<TaskCallback32>(callback), reinterpret_cast<uint32_t>(param));
187  }
188 
192  __forceinline static bool IRAM_ATTR queueCallback(InterruptCallback callback)
193  {
194  return queueCallback(reinterpret_cast<TaskCallback>(callback));
195  }
196 
205  static bool queueCallback(TaskDelegate callback);
206 
210  static unsigned getTaskCount()
211  {
212 #ifdef ENABLE_TASK_COUNT
213  return taskCount;
214 #else
215  return 255;
216 #endif
217  }
218 
224  static unsigned getMaxTaskCount()
225  {
226 #ifdef ENABLE_TASK_COUNT
227  return maxTaskCount;
228 #else
229  return 255;
230 #endif
231  }
232 
233 private:
234  static void taskHandler(os_event_t* event);
235 
236 private:
237  static SystemState state;
238  static os_event_t taskQueue[];
239 #ifdef ENABLE_TASK_COUNT
240  static volatile uint8_t taskCount;
241  static volatile uint8_t maxTaskCount;
242 #endif
243 };
244 
251 extern SystemClass System;
252 
System initialising.
Definition: System.h:91
void(* TaskCallback)(void *param)
Task callback function type, void* parameter.
Definition: System.h:42
#define __forceinline
Definition: sming_attr.h:13
void onReady(SystemReadyDelegate readyHandler)
Set handler for system ready event.
Definition: System.h:153
DeepSleepOptions
Deep sleep options.
Definition: System.h:78
disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current...
Definition: System.h:82
CPU 80MHz.
Definition: System.h:71
void(* InterruptCallback)()
Definition: Interrupts.h:25
SystemClass()
Definition: System.h:100
CpuFrequency getCpuFrequency()
Get the CPU frequency.
Definition: System.h:138
CpuFrequency
CPU Frequency.
Definition: System.h:70
TaskDelegate SystemReadyDelegate
Handler function for system ready.
Definition: System.h:50
System ready.
Definition: System.h:92
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:79
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:224
bool isReady()
Check if system ready.
Definition: System.h:114
RF_CAL after deep-sleep wake up, there will be large current.
Definition: System.h:80
static unsigned getTaskCount()
Get number of tasks currently on queue.
Definition: System.h:210
static bool queueCallback(InterruptCallback callback)
Queue a deferred callback with no callback parameter.
Definition: System.h:192
virtual ~ISystemReadyHandler()
Definition: System.h:58
System state unknown.
Definition: System.h:90
static bool queueCallback(TaskCallback callback, void *param=nullptr)
Queue a deferred callback, with optional void* parameter.
Definition: System.h:184
System class.
Definition: System.h:97
void onReady(ISystemReadyHandler *readyHandler)
Set handler for system ready event.
Definition: System.h:162
Delegate< void()> TaskDelegate
Task Delegate callback type.
Definition: System.h:46
void setCpuFrequency(CpuFrequency freq)
Set the CPU frequency.
Definition: System.h:130
no RF_CAL after deep-sleep wake up, there will only be small current.
Definition: System.h:81
Interface class implented by classes to support on-ready callback.
Definition: System.h:55
SystemState
System state.
Definition: System.h:89
void(* TaskCallback32)(uint32_t param)
Task callback function type, uint32_t parameter.
Definition: System.h:37