Skip to content

Available yield #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions hardware/esp8266com/esp8266/cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern "C" {

#include "HardwareSerial.h"

extern "C" uint32_t esp_micros_at_task_start();

#define UART_TX_FIFO_SIZE 0x80

struct uart_ {
Expand Down Expand Up @@ -552,13 +554,20 @@ bool HardwareSerial::isRxEnabled(void) {
}

int HardwareSerial::available(void) {
if(_uart == 0)
return 0;
if(_uart->rxEnabled) {
return static_cast<int>(_rx_buffer->getSize());
} else {
return 0;
int isAvailable;

if (_uart != NULL && _uart->rxEnabled) {
isAvailable = static_cast<int>(_rx_buffer->getSize());
}
else {
isAvailable = 0;
}

if (!isAvailable && (micros() - esp_micros_at_task_start()) > 10000) {
yield();
}

return isAvailable;
}

int HardwareSerial::peek(void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C"
#include "lwip/mem.h"
#include "include/UdpContext.h"

extern "C" uint32_t esp_micros_at_task_start();

template<>
WiFiUDP* SList<WiFiUDP>::_s_first = 0;
Expand Down Expand Up @@ -116,9 +117,13 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
/* 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 isAvailable = (_ctx != NULL) ? static_cast<int>(_ctx->getSize()) : 0

if (!isAvailable && (micros() - esp_micros_at_task_start()) > 10000) {
yield();
}

return isAvailable;
}

/* Release any resources being used by this WiFiUDP instance */
Expand Down
10 changes: 9 additions & 1 deletion hardware/esp8266com/esp8266/libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern "C" {
#include "twi.h"
#include "Wire.h"

extern "C" uint32_t esp_micros_at_task_start();

// Initialize Class Variables //////////////////////////////////////////////////

uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
Expand Down Expand Up @@ -161,7 +163,13 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity){
}

int TwoWire::available(void){
return rxBufferLength - rxBufferIndex;
int isAvailable = rxBufferLength - rxBufferIndex;

if (!isAvailable && (micros() - esp_micros_at_task_start()) > 10000) {
yield();
}

return isAvailable;
}

int TwoWire::read(void){
Expand Down