Skip to content

Commit a110c0b

Browse files
committed
Provide irq safe implementation of RingBuffer
Alternative way to fix #580 without restoring the buggy RingBuffer implementation from API
1 parent d5d336c commit a110c0b

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

cores/arduino/SafeRingBuffer.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright (c) 2020 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifdef __cplusplus
20+
21+
#ifndef _SAFE_RING_BUFFER_
22+
#define _SAFE_RING_BUFFER_
23+
24+
#include <api/RingBuffer.h>
25+
#include "sync.h"
26+
27+
namespace arduino {
28+
29+
template <int N>
30+
class SafeRingBufferN : public RingBufferN<N>
31+
{
32+
public:
33+
int read_char();
34+
void store_char( uint8_t c ) ;
35+
};
36+
37+
typedef SafeRingBufferN<SERIAL_BUFFER_SIZE> SafeRingBuffer;
38+
39+
template <int N>
40+
int SafeRingBufferN<N>::read_char() {
41+
synchronized {
42+
return RingBufferN<N>::read_char();
43+
}
44+
}
45+
46+
template <int N>
47+
void SafeRingBufferN<N>::store_char(uint8_t c) {
48+
synchronized {
49+
RingBufferN<N>::store_char(c);
50+
}
51+
}
52+
53+
}
54+
55+
#endif /* _SAFE_RING_BUFFER_ */
56+
#endif /* __cplusplus */

cores/arduino/Uart.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include "api/HardwareSerial.h"
2222
#include "SERCOM.h"
23-
#include "api/RingBuffer.h"
23+
#include "SafeRingBuffer.h"
2424

2525
#define SERIAL_BUFFER_SIZE 64
2626

@@ -46,8 +46,8 @@ class Uart : public HardwareSerial
4646

4747
private:
4848
SERCOM *sercom;
49-
RingBuffer rxBuffer;
50-
RingBuffer txBuffer;
49+
SafeRingBuffer rxBuffer;
50+
SafeRingBuffer txBuffer;
5151

5252
uint8_t uc_pinRX;
5353
uint8_t uc_pinTX;

0 commit comments

Comments
 (0)