root / scout_avr / src / Atmega128rfa1.cpp @ 88fb3a79
History | View | Annotate | Download (1.28 KB)
1 |
#include "Atmega128rfa1.h" |
---|---|
2 |
|
3 |
extern "C" |
4 |
{ |
5 |
#include <avr/io.h> |
6 |
#include <avr/interrupt.h> |
7 |
} |
8 |
|
9 |
unsigned long millis; |
10 |
|
11 |
Atmega128rfa1::Atmega128rfa1() |
12 |
{ |
13 |
} |
14 |
|
15 |
ISR(TIMER0_COMPA_vect) |
16 |
{ |
17 |
millis++; |
18 |
} |
19 |
|
20 |
void Atmega128rfa1::init()
|
21 |
{ |
22 |
// === init serial ===
|
23 |
// for 16 MHz clock, 76800 baud
|
24 |
uint16_t baud = 12;
|
25 |
UBRR0H = baud >> 8;
|
26 |
UBRR0L = baud; |
27 |
// UMSEL0 = 0, asynchronous usart
|
28 |
// UPM0 = 0, parity check disabled
|
29 |
// USBS0 = 0, 1 stop bit
|
30 |
// UCSZ0 = 3, 8-bit
|
31 |
UCSR0B = _BV(RXEN0) | _BV(TXEN0); |
32 |
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
33 |
|
34 |
// === init time ===
|
35 |
// COM0x = 0, pin OC0x not used
|
36 |
// WGM0 = 2, clear timer on compare match, TOP = OCRA
|
37 |
// CS0 = 3, 64 prescaler
|
38 |
TCCR0A = _BV(WGM01); |
39 |
TCCR0B = _BV(CS01) | _BV(CS00); |
40 |
// enable interrupt on compare match A
|
41 |
TIMSK0 = _BV(OCIE0A); |
42 |
// (1 ms) * 16 MHz / 64 prescaler = 250
|
43 |
OCR0A = 250;
|
44 |
millis = 0;
|
45 |
} |
46 |
|
47 |
int Atmega128rfa1::read()
|
48 |
{ |
49 |
// TODO make a rx buffer
|
50 |
if (UCSR0A & _BV(RXC0)) return UDR0; |
51 |
else return -1; |
52 |
} |
53 |
|
54 |
void Atmega128rfa1::write(uint8_t* data, int length) |
55 |
{ |
56 |
// TODO make this non-blocking with a tx buffer
|
57 |
int i;
|
58 |
for (i = 0; i < length; i++) |
59 |
{ |
60 |
while (!(UCSR0A & _BV(UDRE0)));
|
61 |
UDR0 = data[i]; |
62 |
} |
63 |
} |
64 |
|
65 |
unsigned long Atmega128rfa1::time() |
66 |
{ |
67 |
unsigned long ret; |
68 |
cli(); |
69 |
ret = millis; |
70 |
sei(); |
71 |
return ret;
|
72 |
} |