-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Clock framework and Polled Timers (#1821)
* Define Clocks and Polled Timers `NanoTime` provides time calculation support and base template classes for Clocks and Time Sources. `Platform/Clocks.h` parameterises all clock sources `PolledTimer.h` provides template class for implementing polled (elapse and timeout) timers `Platform/Timers.h` defines available timer types Add implementation of `esp_get_count()' to `esp_clk.h`, together with `ets_get_cpu_frequency` declaration. * Update documentation * Simplify samples and framework code using polled timers * Add clocks module to HostTests * Add section on timer range checking to docs. and add PolledTimer::checkTime() method. * Fix NanoTime::Ticks methods * Revise hosttests display of clock limit values
- Loading branch information
Showing
20 changed files
with
1,997 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <sming_attr.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// system_get_cpu_frequency is just a wrapper for this ROM function. | ||
uint8_t ets_get_cpu_frequency(void); | ||
|
||
__forceinline static uint32_t esp_get_ccount() | ||
{ | ||
uint32_t ccount; | ||
__asm__ __volatile__("rsr %0, ccount\n" : "=a"(ccount) : : "memory"); | ||
return ccount; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
#include "include/esp_clk.h" | ||
#include "include/esp_system.h" | ||
|
||
// The current CPU frequency | ||
static uint8_t __cpu_frequency = 80; | ||
// The current CPU frequency in MHz (ticks per us) | ||
static uint8_t cpu_frequency = SYS_CPU_80MHZ; | ||
|
||
bool system_update_cpu_freq(uint8 freq) | ||
{ | ||
if(freq == 80 || freq == 160) { | ||
__cpu_frequency = freq; | ||
if(freq == SYS_CPU_80MHZ || freq == SYS_CPU_160MHZ) { | ||
cpu_frequency = freq; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
uint8_t ets_get_cpu_frequency(void) | ||
{ | ||
return cpu_frequency; | ||
} | ||
|
||
uint8 system_get_cpu_freq(void) | ||
{ | ||
return __cpu_frequency; | ||
return ets_get_cpu_frequency(); | ||
} | ||
|
||
/* | ||
* The 'correct' conversion is actually: | ||
* | ||
* `os_get_nanoseconds() / (1000UL * cpu_frequency)` | ||
* | ||
* However, in use this just ends up returning 0 all the time which is | ||
* not particularly useful. | ||
* | ||
* On my dev. system a straight nanosecond count gives quite useful | ||
* values when evaluating code paths. Try :sample:`Basic_Delegates`. | ||
* | ||
*/ | ||
uint32_t esp_get_ccount() | ||
{ | ||
return os_get_nanoseconds(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* NanoTime.cpp | ||
* | ||
* @author mikee47 <mike@sillyhouse.net> | ||
* | ||
****/ | ||
|
||
#include "NanoTime.h" | ||
|
||
#include <WString.h> | ||
|
||
namespace NanoTime | ||
{ | ||
const char* unitToString(Unit unit) | ||
{ | ||
switch(unit) { | ||
case Nanoseconds: | ||
return "ns"; | ||
case Microseconds: | ||
return "us"; | ||
case Milliseconds: | ||
return "ms"; | ||
case Seconds: | ||
return "s"; | ||
case Minutes: | ||
return "m"; | ||
case Hours: | ||
return "h"; | ||
case Days: | ||
return "d"; | ||
default: | ||
return "?s"; | ||
} | ||
} | ||
|
||
const char* unitToLongString(Unit unit) | ||
{ | ||
switch(unit) { | ||
case Nanoseconds: | ||
return "nanoseconds"; | ||
case Microseconds: | ||
return "microseconds"; | ||
case Milliseconds: | ||
return "milliseconds"; | ||
case Seconds: | ||
return "seconds"; | ||
case Minutes: | ||
return "minutes"; | ||
case Hours: | ||
return "hours"; | ||
case Days: | ||
return "days"; | ||
default: | ||
return "?s"; | ||
} | ||
} | ||
|
||
String Frequency::toString() const | ||
{ | ||
auto freq = frequency; | ||
unsigned div = 0; | ||
while(freq % 1000 == 0) { | ||
freq /= 1000; | ||
++div; | ||
} | ||
String s(freq); | ||
if(div == 1) { | ||
s += 'K'; | ||
} else if(div == 2) { | ||
s += 'M'; | ||
} else if(div == 3) { | ||
s += 'G'; | ||
} | ||
s += "Hz"; | ||
return s; | ||
} | ||
|
||
template <unsigned BufSize> class FormatBuffer | ||
{ | ||
public: | ||
FormatBuffer() | ||
{ | ||
buffer[0] = '\0'; | ||
} | ||
|
||
void add(unsigned value, unsigned digits) | ||
{ | ||
pos += strlen(ultoa_wp(value, &buffer[pos], 10, digits, '0')); | ||
} | ||
|
||
void add(char c) | ||
{ | ||
buffer[pos++] = c; | ||
} | ||
|
||
operator String() const | ||
{ | ||
return String(buffer, pos); | ||
} | ||
|
||
private: | ||
char buffer[BufSize]; | ||
unsigned pos = 0; | ||
}; | ||
|
||
String TimeValue::toString() const | ||
{ | ||
if(overflow) { | ||
return "(OVF)"; | ||
} | ||
|
||
FormatBuffer<64> buf; | ||
|
||
if(days != 0) { | ||
buf.add(days, 0); | ||
buf.add('d'); | ||
buf.add(' '); | ||
} | ||
|
||
buf.add(hours, 2); | ||
buf.add(':'); | ||
buf.add(minutes, 2); | ||
buf.add(':'); | ||
buf.add(seconds, 2); | ||
|
||
if(unit < NanoTime::Seconds) { | ||
buf.add('.'); | ||
buf.add(milliseconds, 3); | ||
if(microseconds != 0 || nanoseconds != 0) { | ||
buf.add(microseconds, 3); | ||
if(nanoseconds != 0) { | ||
buf.add(nanoseconds, 3); | ||
} | ||
} | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
} // namespace NanoTime |
Oops, something went wrong.