aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWez Furlong <wez@fb.com>2016-11-27 22:41:22 -0800
committerWez Furlong <wez@fb.com>2016-11-27 22:58:34 -0800
commitbe4e75423a232d9d328bb23835e0fa5152292c95 (patch)
tree256d1b98c3f71a4150524184d2d880caad2aeb5a
parent1585fc4b616cb28b8d4a418cd31c8ce0dd64f731 (diff)
downloadqmk_firmware-be4e75423a232d9d328bb23835e0fa5152292c95.tar.gz
qmk_firmware-be4e75423a232d9d328bb23835e0fa5152292c95.zip
Tidy up atomicity in timer.c and ring_buffer.h
Adopt the macros for saving/restoring the interrupt state that are provided by the avr gcc environment. Removing intialization of the timer value; this shaves off a few bytes because globals are default initialized to zero.
-rw-r--r--tmk_core/common/avr/timer.c36
-rw-r--r--tmk_core/ring_buffer.h26
2 files changed, 28 insertions, 34 deletions
diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c
index 292b41c3a..84af44488 100644
--- a/tmk_core/common/avr/timer.c
+++ b/tmk_core/common/avr/timer.c
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18#include <avr/io.h> 18#include <avr/io.h>
19#include <avr/interrupt.h> 19#include <avr/interrupt.h>
20#include <util/atomic.h>
20#include <stdint.h> 21#include <stdint.h>
21#include "timer_avr.h" 22#include "timer_avr.h"
22#include "timer.h" 23#include "timer.h"
@@ -24,7 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
24 25
25// counter resolution 1ms 26// counter resolution 1ms
26// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} 27// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
27volatile uint32_t timer_count = 0; 28volatile uint32_t timer_count;
28 29
29void timer_init(void) 30void timer_init(void)
30{ 31{
@@ -52,10 +53,9 @@ void timer_init(void)
52inline 53inline
53void timer_clear(void) 54void timer_clear(void)
54{ 55{
55 uint8_t sreg = SREG; 56 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
56 cli();
57 timer_count = 0; 57 timer_count = 0;
58 SREG = sreg; 58 }
59} 59}
60 60
61inline 61inline
@@ -63,10 +63,9 @@ uint16_t timer_read(void)
63{ 63{
64 uint32_t t; 64 uint32_t t;
65 65
66 uint8_t sreg = SREG; 66 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
67 cli(); 67 t = timer_count;
68 t = timer_count; 68 }
69 SREG = sreg;
70 69
71 return (t & 0xFFFF); 70 return (t & 0xFFFF);
72} 71}
@@ -76,10 +75,9 @@ uint32_t timer_read32(void)
76{ 75{
77 uint32_t t; 76 uint32_t t;
78 77
79 uint8_t sreg = SREG; 78 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
80 cli(); 79 t = timer_count;
81 t = timer_count; 80 }
82 SREG = sreg;
83 81
84 return t; 82 return t;
85} 83}
@@ -89,10 +87,9 @@ uint16_t timer_elapsed(uint16_t last)
89{ 87{
90 uint32_t t; 88 uint32_t t;
91 89
92 uint8_t sreg = SREG; 90 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
93 cli(); 91 t = timer_count;
94 t = timer_count; 92 }
95 SREG = sreg;
96 93
97 return TIMER_DIFF_16((t & 0xFFFF), last); 94 return TIMER_DIFF_16((t & 0xFFFF), last);
98} 95}
@@ -102,10 +99,9 @@ uint32_t timer_elapsed32(uint32_t last)
102{ 99{
103 uint32_t t; 100 uint32_t t;
104 101
105 uint8_t sreg = SREG; 102 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
106 cli(); 103 t = timer_count;
107 t = timer_count; 104 }
108 SREG = sreg;
109 105
110 return TIMER_DIFF_32(t, last); 106 return TIMER_DIFF_32(t, last);
111} 107}
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 */