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