aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/avr/timer.c
diff options
context:
space:
mode:
authorJoshua T <joshua@sonofone.net>2017-01-07 14:01:21 -0600
committerJoshua T <joshua@sonofone.net>2017-01-07 14:01:21 -0600
commit8f8d10475956a17953f6db4db3f3b50bd795c0d8 (patch)
tree90c80474baa080e5743ee745ec98254e1a2faadd /tmk_core/common/avr/timer.c
parentb7b44dc481430438552d91b7069d5e37a5e3a649 (diff)
parente7df488a92da56cf160ac64c8cc7302ab717e145 (diff)
downloadqmk_firmware-8f8d10475956a17953f6db4db3f3b50bd795c0d8.tar.gz
qmk_firmware-8f8d10475956a17953f6db4db3f3b50bd795c0d8.zip
Merged from upstream
Diffstat (limited to 'tmk_core/common/avr/timer.c')
-rw-r--r--tmk_core/common/avr/timer.c36
1 files changed, 16 insertions, 20 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}