diff --git a/cores/arduino/SafeRingBuffer.h b/cores/arduino/SafeRingBuffer.h new file mode 100644 index 000000000..7526ae54a --- /dev/null +++ b/cores/arduino/SafeRingBuffer.h @@ -0,0 +1,56 @@ +/* + Copyright (c) 2020 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifdef __cplusplus + +#ifndef _SAFE_RING_BUFFER_ +#define _SAFE_RING_BUFFER_ + +#include +#include "sync.h" + +namespace arduino { + +template +class SafeRingBufferN : public RingBufferN +{ + public: + int read_char(); + void store_char( uint8_t c ) ; +}; + +typedef SafeRingBufferN SafeRingBuffer; + +template +int SafeRingBufferN::read_char() { + synchronized { + return RingBufferN::read_char(); + } +} + +template +void SafeRingBufferN::store_char(uint8_t c) { + synchronized { + RingBufferN::store_char(c); + } +} + +} + +#endif /* _SAFE_RING_BUFFER_ */ +#endif /* __cplusplus */ \ No newline at end of file diff --git a/cores/arduino/Uart.h b/cores/arduino/Uart.h index 62ed3bbb8..d02600b3f 100644 --- a/cores/arduino/Uart.h +++ b/cores/arduino/Uart.h @@ -20,7 +20,7 @@ #include "api/HardwareSerial.h" #include "SERCOM.h" -#include "api/RingBuffer.h" +#include "SafeRingBuffer.h" #define SERIAL_BUFFER_SIZE 64 @@ -46,8 +46,8 @@ class Uart : public HardwareSerial private: SERCOM *sercom; - RingBuffer rxBuffer; - RingBuffer txBuffer; + SafeRingBuffer rxBuffer; + SafeRingBuffer txBuffer; uint8_t uc_pinRX; uint8_t uc_pinTX;