aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/ring_buffer.h
diff options
context:
space:
mode:
authorZay950 <Zay950@users.noreply.github.com>2017-03-29 12:00:38 -0700
committerGitHub <noreply@github.com>2017-03-29 12:00:38 -0700
commit2366ebfbbdeb6ec29cc9a0facda44d666305dd6e (patch)
tree883efed0b7260f3143f5a2a879bc3844a8255e0b /tmk_core/ring_buffer.h
parent80c5ada3394c5ad8087df00ef878eb2cbcd87d70 (diff)
parent942f2ccee44bdb2e251553e9730cd8d59307d8b2 (diff)
downloadqmk_firmware-2366ebfbbdeb6ec29cc9a0facda44d666305dd6e.tar.gz
qmk_firmware-2366ebfbbdeb6ec29cc9a0facda44d666305dd6e.zip
Merge branch 'master' into to_push
Diffstat (limited to 'tmk_core/ring_buffer.h')
-rw-r--r--tmk_core/ring_buffer.h26
1 files changed, 12 insertions, 14 deletions
diff --git a/tmk_core/ring_buffer.h b/tmk_core/ring_buffer.h
index 7bdebbcf3..005d1be61 100644
--- a/tmk_core/ring_buffer.h
+++ b/tmk_core/ring_buffer.h
@@ -4,13 +4,13 @@
4 * Ring buffer to store scan codes from keyboard 4 * Ring buffer to store scan codes from keyboard
5 *------------------------------------------------------------------*/ 5 *------------------------------------------------------------------*/
6#define RBUF_SIZE 32 6#define RBUF_SIZE 32
7#include <util/atomic.h>
7static uint8_t rbuf[RBUF_SIZE]; 8static uint8_t rbuf[RBUF_SIZE];
8static uint8_t rbuf_head = 0; 9static uint8_t rbuf_head = 0;
9static uint8_t rbuf_tail = 0; 10static uint8_t rbuf_tail = 0;
10static inline void rbuf_enqueue(uint8_t data) 11static inline void rbuf_enqueue(uint8_t data)
11{ 12{
12 uint8_t sreg = SREG; 13 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
13 cli();
14 uint8_t next = (rbuf_head + 1) % RBUF_SIZE; 14 uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
15 if (next != rbuf_tail) { 15 if (next != rbuf_tail) {
16 rbuf[rbuf_head] = data; 16 rbuf[rbuf_head] = data;
@@ -18,36 +18,34 @@ static inline void rbuf_enqueue(uint8_t data)
18 } else { 18 } else {
19 print("rbuf: full\n"); 19 print("rbuf: full\n");
20 } 20 }
21 SREG = sreg; 21 }
22} 22}
23static inline uint8_t rbuf_dequeue(void) 23static inline uint8_t rbuf_dequeue(void)
24{ 24{
25 uint8_t val = 0; 25 uint8_t val = 0;
26 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
26 27
27 uint8_t sreg = SREG;
28 cli();
29 if (rbuf_head != rbuf_tail) { 28 if (rbuf_head != rbuf_tail) {
30 val = rbuf[rbuf_tail]; 29 val = rbuf[rbuf_tail];
31 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; 30 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
32 } 31 }
33 SREG = sreg; 32 }
34 33
35 return val; 34 return val;
36} 35}
37static inline bool rbuf_has_data(void) 36static inline bool rbuf_has_data(void)
38{ 37{
39 uint8_t sreg = SREG; 38 bool has_data;
40 cli(); 39 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
41 bool has_data = (rbuf_head != rbuf_tail); 40 has_data = (rbuf_head != rbuf_tail);
42 SREG = sreg; 41 }
43 return has_data; 42 return has_data;
44} 43}
45static inline void rbuf_clear(void) 44static inline void rbuf_clear(void)
46{ 45{
47 uint8_t sreg = SREG; 46 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
48 cli();
49 rbuf_head = rbuf_tail = 0; 47 rbuf_head = rbuf_tail = 0;
50 SREG = sreg; 48 }
51} 49}
52 50
53#endif /* RING_BUFFER_H */ 51#endif /* RING_BUFFER_H */