Skip to content

Interrupt safety fixes for HardwareSerial #1213

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

Merged
merged 2 commits into from
Dec 14, 2015
Merged
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
40 changes: 29 additions & 11 deletions cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <inttypes.h>
#include "Arduino.h"
#include "cbuf.h"
#include "interrupts.h"

extern "C" {
#include "osapi.h"
Expand Down Expand Up @@ -556,6 +557,7 @@ int HardwareSerial::available(void) {
int result = 0;

if (_uart != NULL && _uart->rxEnabled) {
InterruptLock il;
result = static_cast<int>(_rx_buffer->getSize());
}

Expand All @@ -570,6 +572,7 @@ int HardwareSerial::peek(void) {
if(_uart == 0)
return -1;
if(_uart->rxEnabled) {
InterruptLock il;
return _rx_buffer->peek();
} else {
return -1;
Expand All @@ -580,6 +583,7 @@ int HardwareSerial::read(void) {
if(_uart == 0)
return -1;
if(_uart->rxEnabled) {
InterruptLock il;
return _rx_buffer->read();
} else {
return -1;
Expand All @@ -590,6 +594,7 @@ int HardwareSerial::availableForWrite(void) {
if(_uart == 0)
return 0;
if(_uart->txEnabled) {
InterruptLock il;
return static_cast<int>(_tx_buffer->room());
} else {
return 0;
Expand All @@ -604,28 +609,41 @@ void HardwareSerial::flush() {
if(!_written)
return;

while(_tx_buffer->getSize() || uart_get_tx_fifo_room(_uart) < UART_TX_FIFO_SIZE)
while(true) {
{
InterruptLock il;
if(_tx_buffer->getSize() == 0 &&
uart_get_tx_fifo_room(_uart) >= UART_TX_FIFO_SIZE) {
break;
}
}
yield();

}
_written = false;
}

size_t HardwareSerial::write(uint8_t c) {
if(_uart == 0 || !_uart->txEnabled)
return 0;
_written = true;
size_t room = uart_get_tx_fifo_room(_uart);
if(room > 0 && _tx_buffer->empty()) {
uart_transmit_char(_uart, c);
return 1;
}

while(_tx_buffer->room() == 0) {
while(true) {
{
InterruptLock il;
if(_tx_buffer->empty()) {
if(uart_get_tx_fifo_room(_uart) > 0) {
uart_transmit_char(_uart, c);
} else {
_tx_buffer->write(c);
uart_arm_tx_interrupt(_uart);
}
break;
} else if(_tx_buffer->write(c)) {
break;
}
}
yield();
}

_tx_buffer->write(c);
uart_arm_tx_interrupt(_uart);
return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions cores/esp8266/cbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class cbuf {
return (ptr == _bufend) ? _buf : ptr;
}

size_t _size;
const size_t _size;
char* _buf;
char* _bufend;
const char* const _bufend;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two time const?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a const pointer to chars which are const, so _bufend is const and *_bufend is const as well.

char* _begin;
char* _end;
};
Expand Down