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. |
|
21 static const uint8_t CNT2_COMPARE = 32; |
|
22 static const int SECONDS_WAKE = 60; |
|
23 |
0
|
24 static int uart_putchar(char c, FILE *stream); |
|
25 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
|
26 _FDEV_SETUP_WRITE); |
|
27 |
|
28 static uint8_t n_measurements; |
|
29 static uint8_t measurements[500]; |
|
30 |
1
|
31 static uint8_t need_measurement; |
|
32 |
0
|
33 static uint8_t readpos; |
|
34 static char readbuf[30]; |
|
35 |
1
|
36 static uint8_t sec_count; |
|
37 |
0
|
38 static void |
|
39 uart_init(unsigned int baud) |
|
40 { |
|
41 // baud rate |
|
42 UBRRH = (unsigned char)(baud >> 8); |
|
43 UBRRL = (unsigned char)baud; |
|
44 UCSRB = (1<<RXEN)|(1<<TXEN); |
|
45 //8N1 |
|
46 UCSRC = (1<<URSEL)|(3<<UCSZ0); |
|
47 } |
|
48 |
|
49 static int |
|
50 uart_putchar(char c, FILE *stream) |
|
51 { |
|
52 // XXX should sleep in the loop for power. |
|
53 loop_until_bit_is_set(UCSRA, UDRE); |
|
54 UDR = c; |
|
55 return 0; |
|
56 } |
|
57 |
|
58 static void |
|
59 cmd_fetch() |
|
60 { |
|
61 uint16_t crc = 0; |
|
62 int i; |
|
63 printf("%d measurements\n", n_measurements); |
|
64 for (i = 0; i < n_measurements; i++) |
|
65 { |
|
66 printf("%3d : %d\n", i, measurements[i]); |
1
|
67 crc = _crc_ccitt_update(crc, measurements[i]); |
0
|
68 } |
1
|
69 printf("CRC : %d\n", crc); |
0
|
70 } |
|
71 |
|
72 static void |
|
73 cmd_clear() |
|
74 { |
|
75 } |
|
76 |
|
77 static void |
|
78 cmd_btoff() |
|
79 { |
|
80 } |
|
81 |
|
82 static void |
|
83 read_handler() |
|
84 { |
|
85 if (strcmp(readbuf, "fetch") == 0) |
|
86 { |
|
87 cmd_fetch(); |
|
88 } |
|
89 else if (strcmp(readbuf, "clear") == 0) |
|
90 { |
|
91 cmd_clear(); |
|
92 } |
|
93 else if (strcmp(readbuf, "btoff") == 0) |
|
94 { |
|
95 cmd_btoff(); |
|
96 } |
|
97 else |
|
98 { |
|
99 printf("Bad command\n"); |
|
100 } |
|
101 } |
|
102 |
1
|
103 ISR(USART_RXC_vect) |
0
|
104 { |
|
105 char c = UDR; |
|
106 if (c == '\n') |
|
107 { |
|
108 readbuf[readpos] = '\0'; |
|
109 read_handler(); |
|
110 readpos = 0; |
|
111 } |
|
112 else |
|
113 { |
|
114 readbuf[readpos] = c; |
|
115 readpos++; |
|
116 if (readpos >= sizeof(readbuf)) |
|
117 { |
|
118 readpos = 0; |
|
119 } |
|
120 } |
|
121 } |
|
122 |
1
|
123 ISR(TIMER2_COMP_vect) |
|
124 { |
|
125 sec_count ++; |
|
126 if (sec_count == SECONDS_WAKE) |
|
127 { |
|
128 sec_count = 0; |
|
129 need_measurement = 1; |
|
130 } |
|
131 } |
|
132 |
|
133 static void |
|
134 deep_sleep() |
|
135 { |
|
136 // p119 of manual |
|
137 OCR2 = CNT2_COMPARE; |
|
138 loop_until_bit_is_clear(ASSR, OCR2UB); |
|
139 |
|
140 set_sleep_mode(SLEEP_MODE_PWR_SAVE); |
|
141 sleep_mode(); |
|
142 } |
|
143 |
|
144 static void |
|
145 do_measurement() |
|
146 { |
|
147 need_measurement = 0; |
|
148 } |
|
149 |
0
|
150 int main(void) |
|
151 { |
|
152 uart_init(9600); |
|
153 |
|
154 fprintf(&mystdout, "hello %d\n", 12); |
|
155 |
1
|
156 // set up counter2. |
|
157 // COM21 COM20 Set OC2 on Compare Match (p116) |
|
158 // WGM21 Clear counter on compare |
|
159 // CS22 CS21 CS20 clk/1024 |
|
160 TCCR2 = _BV(COM21) | _BV(COM20) | _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20); |
|
161 // set async mode |
|
162 ASSR |= _BV(AS2); |
0
|
163 |
|
164 for(;;){ |
|
165 /* insert your main loop code here */ |
1
|
166 if (need_measurement) |
|
167 { |
|
168 do_measurement(); |
|
169 } |
0
|
170 } |
|
171 return 0; /* never reached */ |
|
172 } |