0
|
1 /* Name: main.c |
|
2 * Author: <insert your name here> |
|
3 * Copyright: <insert your copyright message here> |
|
4 * License: <insert your license reference here> |
|
5 */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <string.h> |
1
|
9 #include <avr/io.h> |
|
10 #include <avr/interrupt.h> |
|
11 #include <avr/sleep.h> |
0
|
12 #include <util/crc16.h> |
|
13 |
|
14 // configuration params |
|
15 // - measurement interval |
|
16 // - transmit interval |
|
17 // - bluetooth params |
|
18 // - number of sensors (and range?) |
|
19 |
1
|
20 // 1 second. we have 1024 prescaler, 32768 crystal. |
2
|
21 #define SLEEP_COMPARE 32 |
|
22 #define SECONDS_WAKE 60 |
|
23 |
|
24 #define BAUD 9600 |
|
25 #define UBRR ((F_CPU)/16/(BAUD)-1) |
1
|
26 |
0
|
27 static int uart_putchar(char c, FILE *stream); |
|
28 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
|
29 _FDEV_SETUP_WRITE); |
|
30 |
|
31 static uint8_t n_measurements; |
|
32 static uint8_t measurements[500]; |
|
33 |
1
|
34 static uint8_t need_measurement; |
|
35 |
0
|
36 static uint8_t readpos; |
|
37 static char readbuf[30]; |
|
38 |
1
|
39 static uint8_t sec_count; |
|
40 |
0
|
41 static void |
2
|
42 uart_init(unsigned int ubrr) |
0
|
43 { |
|
44 // baud rate |
2
|
45 UBRR0H = (unsigned char)(ubrr >> 8); |
|
46 UBRR0L = (unsigned char)ubrr; |
|
47 UCSR0B = (1<<RXEN0)|(1<<TXEN0); |
0
|
48 //8N1 |
2
|
49 UCSR0C = (1<<UMSEL00)|(3<<UCSZ00); |
0
|
50 } |
|
51 |
|
52 static int |
|
53 uart_putchar(char c, FILE *stream) |
|
54 { |
|
55 // XXX should sleep in the loop for power. |
2
|
56 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
57 UDR0 = c; |
0
|
58 return 0; |
|
59 } |
|
60 |
|
61 static void |
|
62 cmd_fetch() |
|
63 { |
|
64 uint16_t crc = 0; |
|
65 int i; |
|
66 printf("%d measurements\n", n_measurements); |
|
67 for (i = 0; i < n_measurements; i++) |
|
68 { |
|
69 printf("%3d : %d\n", i, measurements[i]); |
1
|
70 crc = _crc_ccitt_update(crc, measurements[i]); |
0
|
71 } |
1
|
72 printf("CRC : %d\n", crc); |
0
|
73 } |
|
74 |
|
75 static void |
|
76 cmd_clear() |
|
77 { |
|
78 } |
|
79 |
|
80 static void |
|
81 cmd_btoff() |
|
82 { |
|
83 } |
|
84 |
|
85 static void |
|
86 read_handler() |
|
87 { |
|
88 if (strcmp(readbuf, "fetch") == 0) |
|
89 { |
|
90 cmd_fetch(); |
|
91 } |
|
92 else if (strcmp(readbuf, "clear") == 0) |
|
93 { |
|
94 cmd_clear(); |
|
95 } |
|
96 else if (strcmp(readbuf, "btoff") == 0) |
|
97 { |
|
98 cmd_btoff(); |
|
99 } |
|
100 else |
|
101 { |
|
102 printf("Bad command\n"); |
|
103 } |
|
104 } |
|
105 |
2
|
106 ISR(USART_RX_vect) |
0
|
107 { |
2
|
108 char c = UDR0; |
0
|
109 if (c == '\n') |
|
110 { |
|
111 readbuf[readpos] = '\0'; |
|
112 read_handler(); |
|
113 readpos = 0; |
|
114 } |
|
115 else |
|
116 { |
|
117 readbuf[readpos] = c; |
|
118 readpos++; |
|
119 if (readpos >= sizeof(readbuf)) |
|
120 { |
|
121 readpos = 0; |
|
122 } |
|
123 } |
|
124 } |
|
125 |
2
|
126 ISR(TIMER2_COMPA_vect) |
1
|
127 { |
|
128 sec_count ++; |
|
129 if (sec_count == SECONDS_WAKE) |
|
130 { |
|
131 sec_count = 0; |
|
132 need_measurement = 1; |
|
133 } |
|
134 } |
|
135 |
|
136 static void |
|
137 deep_sleep() |
|
138 { |
|
139 // p119 of manual |
2
|
140 OCR2A = SLEEP_COMPARE; |
|
141 loop_until_bit_is_clear(ASSR, OCR2AUB); |
1
|
142 |
|
143 set_sleep_mode(SLEEP_MODE_PWR_SAVE); |
|
144 sleep_mode(); |
|
145 } |
|
146 |
|
147 static void |
|
148 do_measurement() |
|
149 { |
|
150 need_measurement = 0; |
|
151 } |
|
152 |
0
|
153 int main(void) |
|
154 { |
2
|
155 uart_init(UBRR); |
0
|
156 |
|
157 fprintf(&mystdout, "hello %d\n", 12); |
|
158 |
1
|
159 // set up counter2. |
|
160 // COM21 COM20 Set OC2 on Compare Match (p116) |
|
161 // WGM21 Clear counter on compare |
|
162 // CS22 CS21 CS20 clk/1024 |
2
|
163 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
|
164 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
1
|
165 // set async mode |
|
166 ASSR |= _BV(AS2); |
0
|
167 |
|
168 for(;;){ |
|
169 /* insert your main loop code here */ |
1
|
170 if (need_measurement) |
|
171 { |
|
172 do_measurement(); |
|
173 } |
0
|
174 } |
|
175 return 0; /* never reached */ |
|
176 } |