diff options
Diffstat (limited to 'tmk_core/ring_buffer.h')
| -rw-r--r-- | tmk_core/ring_buffer.h | 26 |
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> | ||
| 7 | static uint8_t rbuf[RBUF_SIZE]; | 8 | static uint8_t rbuf[RBUF_SIZE]; |
| 8 | static uint8_t rbuf_head = 0; | 9 | static uint8_t rbuf_head = 0; |
| 9 | static uint8_t rbuf_tail = 0; | 10 | static uint8_t rbuf_tail = 0; |
| 10 | static inline void rbuf_enqueue(uint8_t data) | 11 | static 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 | } |
| 23 | static inline uint8_t rbuf_dequeue(void) | 23 | static 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 | } |
| 37 | static inline bool rbuf_has_data(void) | 36 | static 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 | } |
| 45 | static inline void rbuf_clear(void) | 44 | static 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 */ |
