Skip to content

Commit 7309602

Browse files
committed
Merge branch 'develop'
2 parents e82ed02 + bba5ae6 commit 7309602

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

src/m5_utility/compatibility_feature.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,46 @@
88
@brief Maintain compatibility with Arduino API, etc.
99
*/
1010
#include "compatibility_feature.hpp"
11-
#include <ctime>
12-
#include <chrono>
13-
#include <thread>
11+
#include <freertos/task.h>
12+
#include <esp_cpu.h>
1413

1514
namespace {
16-
using clock = std::chrono::high_resolution_clock;
17-
const clock::time_point start_at = clock::now();
1815

1916
} // namespace
2017

2118
namespace m5 {
2219
namespace utility {
2320

24-
unsigned long millis()
25-
{
26-
return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - ::start_at).count();
27-
}
28-
unsigned long micros()
29-
{
30-
return std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - ::start_at).count();
31-
}
3221
void delay(const unsigned long ms)
3322
{
34-
#if 0
35-
auto abst = clock::now() + std::chrono::milliseconds(ms);
36-
std::this_thread::sleep_until(abst);
37-
#else
38-
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
39-
#endif
23+
if (ms) {
24+
if (xPortInIsrContext()) {
25+
// Using busy-wait in ISR
26+
const uint64_t target = esp_timer_get_time() + static_cast<uint64_t>(ms) * 1000ULL;
27+
while (esp_timer_get_time() < target) { /* nop */
28+
}
29+
} else {
30+
vTaskDelay(pdMS_TO_TICKS(ms));
31+
}
32+
}
4033
}
4134

4235
void delayMicroseconds(const unsigned int us)
4336
{
44-
#if 0
45-
auto abst = clock::now() + std::chrono::microseconds(us);
46-
std::this_thread::sleep_until(abst);
47-
#else
48-
std::this_thread::sleep_for(std::chrono::microseconds(us));
49-
#endif
37+
if (us) {
38+
// Using esp_rom_delay if less than 1ms
39+
if (us < 1000 || xPortInIsrContext()) {
40+
esp_rom_delay_us(us);
41+
return;
42+
}
43+
44+
// vTaskDelay + esp_rom_delay
45+
vTaskDelay(pdMS_TO_TICKS(us / 1000));
46+
const uint32_t us_rem = us % 1000;
47+
if (us_rem) {
48+
esp_rom_delay_us(us_rem);
49+
}
50+
}
5051
}
5152

5253
} // namespace utility

src/m5_utility/compatibility_feature.hpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,38 @@
1010
#ifndef M5_UTILITY_COMPATIBILITY_FEATURE_HPP
1111
#define M5_UTILITY_COMPATIBILITY_FEATURE_HPP
1212

13+
#include <freertos/FreeRTOS.h>
14+
#include <esp_timer.h>
15+
1316
namespace m5 {
1417
namespace utility {
1518

1619
///@name Arduino API
1720
///@{
1821
/*!
19-
@brief Returns the number of milliseconds passed since the Arduino board began
20-
running the current program
22+
@brief Returns the number of milliseconds passed since the Arduino board began running the current program
2123
*/
22-
unsigned long millis();
24+
inline IRAM_ATTR unsigned long millis()
25+
{
26+
return static_cast<unsigned long>(esp_timer_get_time() / 1000ULL);
27+
}
2328
/*!
24-
@brief Returns the number of microseconds since the Arduino board began
25-
running the current program
29+
@brief Returns the number of microseconds since the Arduino board began running the current program
2630
*/
27-
unsigned long micros();
31+
inline IRAM_ATTR unsigned long micros()
32+
{
33+
return static_cast<unsigned long>(esp_timer_get_time());
34+
}
2835
/*!
29-
@brief Pauses the program for the amount of time (in milliseconds) specified
30-
as parameter.
36+
@brief Pauses the program for the amount of time (in milliseconds) specified as parameter
37+
@param ms delay time (ms)
3138
@warning Accuracy varies depending on the environment.
3239
*/
3340
void delay(const unsigned long ms);
41+
3442
/*!
35-
@brief Pauses the program for the amount of time (in microseconds) specified
36-
by the parameter.
43+
@brief Pauses the program for the amount of time (in microseconds) specified by the parameter
44+
@param us delay time (us)
3745
@warning Accuracy varies depending on the environment.
3846
*/
3947
void delayMicroseconds(const unsigned int us);

src/m5_utility/crc.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class CRC8 {
9999
return do_finalize ? finalize(crc, refOut, xorout) : crc;
100100
}
101101

102+
inline void clear()
103+
{
104+
_crc = _init;
105+
}
106+
102107
protected:
103108
static inline uint8_t finalize(const uint8_t value, const bool refOut, const uint8_t xorout)
104109
{
@@ -192,6 +197,11 @@ class CRC16 {
192197
return do_finalize ? finalize(crc, refOut, xorout) : crc;
193198
}
194199

200+
inline void clear()
201+
{
202+
_crc = _init;
203+
}
204+
195205
protected:
196206
static inline uint16_t finalize(const uint16_t value, const bool refOut, const uint16_t xorout)
197207
{

0 commit comments

Comments
 (0)