root / scout_avr / src / Atmega128rfa1.cpp @ 807483bf
History | View | Annotate | Download (1.86 KB)
1 | 88fb3a79 | Tom Mullins | #include "Atmega128rfa1.h" |
---|---|---|---|
2 | |||
3 | extern "C" |
||
4 | { |
||
5 | #include <avr/io.h> |
||
6 | #include <avr/interrupt.h> |
||
7 | 49090532 | Tom Mullins | void __cxa_pure_virtual(void) {} |
8 | 88fb3a79 | Tom Mullins | } |
9 | |||
10 | unsigned long millis; |
||
11 | |||
12 | 20f94878 | Tom Mullins | int rx_start = 0, rx_end = 0; |
13 | char rx_buffer[RX_BUFFER_SIZE];
|
||
14 | |||
15 | 88fb3a79 | Tom Mullins | Atmega128rfa1::Atmega128rfa1() |
16 | { |
||
17 | } |
||
18 | |||
19 | ISR(TIMER0_COMPA_vect) |
||
20 | { |
||
21 | millis++; |
||
22 | } |
||
23 | |||
24 | 807483bf | Tom Mullins | ISR(USART0_RX_vect) |
25 | 20f94878 | Tom Mullins | { |
26 | char data = UDR0;
|
||
27 | cf115e3d | Tom Mullins | if (rx_end == rx_start-1 || (rx_start == 0 && rx_end == RX_BUFFER_SIZE-1)) |
28 | 20f94878 | Tom Mullins | { |
29 | // TODO warn of buffer overflow?
|
||
30 | } |
||
31 | else
|
||
32 | { |
||
33 | rx_buffer[rx_end] = data; |
||
34 | rx_end++; |
||
35 | if (rx_end == RX_BUFFER_SIZE)
|
||
36 | rx_end = 0;
|
||
37 | } |
||
38 | } |
||
39 | |||
40 | 88fb3a79 | Tom Mullins | void Atmega128rfa1::init()
|
41 | { |
||
42 | // === init serial ===
|
||
43 | 49090532 | Tom Mullins | // baud = F_CPU / (16 (UBRR + 1))
|
44 | 1c3c96ce | Tom Mullins | uint16_t ubrr = F_CPU / 16 / BAUD_RATE - 1; |
45 | 49090532 | Tom Mullins | UBRR0H = ubrr >> 8;
|
46 | UBRR0L = ubrr; |
||
47 | 88fb3a79 | Tom Mullins | // UMSEL0 = 0, asynchronous usart
|
48 | // UPM0 = 0, parity check disabled
|
||
49 | // USBS0 = 0, 1 stop bit
|
||
50 | // UCSZ0 = 3, 8-bit
|
||
51 | 20f94878 | Tom Mullins | UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
52 | 88fb3a79 | Tom Mullins | UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
53 | |||
54 | // === init time ===
|
||
55 | // COM0x = 0, pin OC0x not used
|
||
56 | // WGM0 = 2, clear timer on compare match, TOP = OCRA
|
||
57 | // CS0 = 3, 64 prescaler
|
||
58 | TCCR0A = _BV(WGM01); |
||
59 | TCCR0B = _BV(CS01) | _BV(CS00); |
||
60 | // enable interrupt on compare match A
|
||
61 | TIMSK0 = _BV(OCIE0A); |
||
62 | // (1 ms) * 16 MHz / 64 prescaler = 250
|
||
63 | 49090532 | Tom Mullins | //OCR0A = 250;
|
64 | // (1 ms) * 8 MHz / 64 prescaler = 125
|
||
65 | OCR0A = 125;
|
||
66 | 88fb3a79 | Tom Mullins | millis = 0;
|
67 | 20f94878 | Tom Mullins | |
68 | sei(); |
||
69 | 88fb3a79 | Tom Mullins | } |
70 | |||
71 | int Atmega128rfa1::read()
|
||
72 | { |
||
73 | cf115e3d | Tom Mullins | cli(); |
74 | 20f94878 | Tom Mullins | if (rx_start == rx_end)
|
75 | return -1; |
||
76 | else
|
||
77 | { |
||
78 | int ret = rx_buffer[rx_start];
|
||
79 | rx_start++; |
||
80 | if (rx_start == RX_BUFFER_SIZE)
|
||
81 | rx_start = 0;
|
||
82 | return ret;
|
||
83 | } |
||
84 | cf115e3d | Tom Mullins | sei(); |
85 | 88fb3a79 | Tom Mullins | } |
86 | |||
87 | void Atmega128rfa1::write(uint8_t* data, int length) |
||
88 | { |
||
89 | // TODO make this non-blocking with a tx buffer
|
||
90 | int i;
|
||
91 | for (i = 0; i < length; i++) |
||
92 | { |
||
93 | while (!(UCSR0A & _BV(UDRE0)));
|
||
94 | UDR0 = data[i]; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | unsigned long Atmega128rfa1::time() |
||
99 | { |
||
100 | unsigned long ret; |
||
101 | cli(); |
||
102 | ret = millis; |
||
103 | sei(); |
||
104 | return ret;
|
||
105 | } |