]> git.donarmstrong.com Git - tmk_firmware.git/blob - timer.c
integrate V-USB support into ps2_usb
[tmk_firmware.git] / timer.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <stdint.h>
4 #include "timer.h"
5
6 volatile uint16_t timer_count = 0;
7
8 // Configure timer 0 to generate a timer overflow interrupt every
9 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
10 // This demonstrates how to use interrupts to implement a simple
11 // inactivity timeout.
12 void timer_init(void)
13 {
14     TCCR0A = 0x00;
15     TCCR0B = 0x05;
16     TIMSK0 = (1<<TOIE0);
17 }
18
19 inline
20 void timer_clear(void)
21 {
22     uint8_t sreg = SREG;
23     cli();
24     timer_count = 0;
25     SREG = sreg;
26 }
27
28 inline
29 uint16_t timer_read(void)
30 {
31     uint16_t t;
32
33     uint8_t sreg = SREG;
34     cli();
35     t = timer_count;
36     SREG = sreg;
37
38     return t;
39 }
40
41 inline
42 uint16_t timer_elapsed(uint16_t last)
43 {
44     uint16_t t;
45
46     uint8_t sreg = SREG;
47     cli();
48     t = timer_count;
49     SREG = sreg;
50
51     return TIMER_DIFF(t, last);
52 }
53
54 // This interrupt routine is run approx 61 times per second.
55 // A very simple inactivity timeout is implemented, where we
56 // will send a space character and print a message to the
57 // hid_listen debug message window.
58 ISR(TIMER0_OVF_vect)
59 {
60     timer_count++;
61 }