From a110c0bbeba98dac1503a2c58c59a469ed7ccd0f Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 18 Dec 2020 09:46:58 +0100 Subject: [PATCH] Provide irq safe implementation of RingBuffer Alternative way to fix https://github.com/arduino/ArduinoCore-samd/issues/580 without restoring the buggy RingBuffer implementation from API --- cores/arduino/SafeRingBuffer.h | 56 ++++++++++++++++++++++++++++++++++ cores/arduino/Uart.h | 6 ++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 cores/arduino/SafeRingBuffer.h 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;