1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

optimistic_yield()

this introduces optimistic_yield() used for when standard library
methods are normally used in tight loops waiting for something to
happen, like available().
This commit is contained in:
Makuna 2015-07-13 13:47:13 -07:00
parent dece240830
commit d815c36753
7 changed files with 58 additions and 31 deletions

View File

@ -218,6 +218,7 @@ Libraries that don't rely on low-level access to AVR registers should work well.
- [PubSubClient](https://github.com/Imroy/pubsubclient) MQTT library by @Imroy.
- [RTC](https://github.com/Makuna/Rtc) - Arduino Library for Ds1307 & Ds3231 compatible with esp8266.
- [Souliss, Smart Home](https://github.com/souliss/souliss) - Framework for Smart Home based on Arduino, Android and openHAB.
- [ST7735](https://github.com/nzmichaelh/Adafruit-ST7735-Library) - Adafruit's ST7735 library modified to be compatible with esp8266. Just make sure to modify the pins in the examples as they are still AVR specific.
#### Upload via serial port ####
Pick the correct serial port.

View File

@ -551,14 +551,20 @@ bool HardwareSerial::isRxEnabled(void) {
return _uart->rxEnabled;
}
extern "C" void optimistic_yield();
int HardwareSerial::available(void) {
if(_uart == 0)
return 0;
if(_uart->rxEnabled) {
return static_cast<int>(_rx_buffer->getSize());
} else {
return 0;
int result = 0;
if (_uart != NULL && _uart->rxEnabled) {
result = static_cast<int>(_rx_buffer->getSize());
}
if (!result) {
optimistic_yield();
}
return result;
}
int HardwareSerial::peek(void) {

View File

@ -34,6 +34,8 @@ extern "C" {
#define LOOP_TASK_PRIORITY 0
#define LOOP_QUEUE_SIZE 1
#define OPTIMISTIC_YIELD_TIME_US 16000
struct rst_info resetInfo;
int atexit(void (*func)()) {
@ -62,11 +64,8 @@ extern void (*__init_array_end)(void);
cont_t g_cont __attribute__ ((aligned (16)));
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
static uint32_t g_micros_at_task_start;
static uint32_t g_micros_at_last_task_yield;
extern "C" uint32_t esp_micros_at_task_start() {
return g_micros_at_task_start;
}
extern "C" void abort() {
while(1) {
@ -74,6 +73,7 @@ extern "C" void abort() {
}
extern "C" void esp_yield() {
g_micros_at_last_task_yield = system_get_time();
cont_yield(&g_cont);
}
@ -87,6 +87,13 @@ extern "C" void __yield() {
}
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
extern "C" void optimistic_yield() {
if (system_get_time() - g_micros_at_last_task_yield > OPTIMISTIC_YIELD_TIME_US)
{
__yield();
}
}
static void loop_wrapper() {
static bool setup_done = false;
if(!setup_done) {
@ -99,7 +106,7 @@ static void loop_wrapper() {
}
static void loop_task(os_event_t *events) {
g_micros_at_task_start = system_get_time();
g_micros_at_last_task_yield = system_get_time();
cont_run(&g_cont, &loop_wrapper);
if(cont_check(&g_cont) != 0) {
ets_printf("\r\nheap collided with sketch stack\r\n");

View File

@ -177,20 +177,19 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
return _client->write(reinterpret_cast<const char*>(buf), size);
}
extern "C" uint32_t esp_micros_at_task_start();
extern "C" void optimistic_yield();
int WiFiClient::available()
{
static uint32_t lastPollTime = 0;
if (!_client)
return 0;
int result = 0;
if (lastPollTime > esp_micros_at_task_start())
yield();
if (_client) {
result = _client->getSize();
}
lastPollTime = micros();
int result = _client->getSize();
if (!result) {
optimistic_yield();
}
return result;
}

View File

@ -84,17 +84,15 @@ bool WiFiServer::getNoDelay(){
return tcp_nagle_disabled(_pcb);
}
extern "C" uint32_t esp_micros_at_task_start();
bool WiFiServer::hasClient(){
if (_unclaimed) return true;
return false;
}
extern "C" void optimistic_yield();
WiFiClient WiFiServer::available(byte* status)
{
static uint32_t lastPollTime = 0;
if (_unclaimed)
{
WiFiClient result(_unclaimed);
@ -103,9 +101,7 @@ WiFiClient WiFiServer::available(byte* status)
return result;
}
if (lastPollTime > esp_micros_at_task_start())
yield();
lastPollTime = micros();
optimistic_yield();
return WiFiClient();
}

View File

@ -113,12 +113,22 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
return 1;
}
extern "C" void optimistic_yield();
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiUDP::available() {
if (!_ctx)
return 0;
return static_cast<int>(_ctx->getSize());
int result = 0;
if (_ctx) {
result = static_cast<int>(_ctx->getSize());
}
if (!result) {
optimistic_yield();
}
return result;
}
/* Release any resources being used by this WiFiUDP instance */

View File

@ -160,8 +160,16 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity){
return quantity;
}
extern "C" void optimistic_yield();
int TwoWire::available(void){
return rxBufferLength - rxBufferIndex;
int result = rxBufferLength - rxBufferIndex;
if (!result) {
optimistic_yield();
}
return result;
}
int TwoWire::read(void){