annotate main.c @ 307:e50091063890

Some counter2 bits
author Matt Johnston <matt@ucc.asn.au>
date Mon, 07 May 2012 00:14:53 +0800
parents 848b0fe159d2
children ab0e30c4b344
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* Name: main.c
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Author: <insert your name here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * Copyright: <insert your copyright message here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * License: <insert your license reference here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 */
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 #include <stdio.h>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #include <string.h>
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
9 #include <avr/io.h>
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
10 #include <avr/interrupt.h>
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
11 #include <avr/sleep.h>
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include <util/crc16.h>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 // configuration params
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 // - measurement interval
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 // - transmit interval
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 // - bluetooth params
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 // - number of sensors (and range?)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
20 // 1 second. we have 1024 prescaler, 32768 crystal.
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
21 static const uint8_t CNT2_COMPARE = 32;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
22 static const int SECONDS_WAKE = 60;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
23
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 static int uart_putchar(char c, FILE *stream);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 _FDEV_SETUP_WRITE);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 static uint8_t n_measurements;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 static uint8_t measurements[500];
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
31 static uint8_t need_measurement;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
32
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 static uint8_t readpos;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 static char readbuf[30];
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
36 static uint8_t sec_count;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
37
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 uart_init(unsigned int baud)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 // baud rate
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 UBRRH = (unsigned char)(baud >> 8);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 UBRRL = (unsigned char)baud;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 UCSRB = (1<<RXEN)|(1<<TXEN);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 //8N1
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 UCSRC = (1<<URSEL)|(3<<UCSZ0);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 static int
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 uart_putchar(char c, FILE *stream)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 // XXX should sleep in the loop for power.
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 loop_until_bit_is_set(UCSRA, UDRE);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 UDR = c;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 return 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 cmd_fetch()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 uint16_t crc = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 int i;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 printf("%d measurements\n", n_measurements);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 for (i = 0; i < n_measurements; i++)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 printf("%3d : %d\n", i, measurements[i]);
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
67 crc = _crc_ccitt_update(crc, measurements[i]);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 }
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
69 printf("CRC : %d\n", crc);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 cmd_clear()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 cmd_btoff()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 read_handler()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 if (strcmp(readbuf, "fetch") == 0)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 cmd_fetch();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 else if (strcmp(readbuf, "clear") == 0)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 cmd_clear();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 else if (strcmp(readbuf, "btoff") == 0)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 cmd_btoff();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 else
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 printf("Bad command\n");
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
103 ISR(USART_RXC_vect)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 char c = UDR;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 if (c == '\n')
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 readbuf[readpos] = '\0';
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 read_handler();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 readpos = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 else
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 readbuf[readpos] = c;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 readpos++;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 if (readpos >= sizeof(readbuf))
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 readpos = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
123 ISR(TIMER2_COMP_vect)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
124 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
125 sec_count ++;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
126 if (sec_count == SECONDS_WAKE)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
127 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
128 sec_count = 0;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
129 need_measurement = 1;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
130 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
131 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
132
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
133 static void
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
134 deep_sleep()
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
135 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
136 // p119 of manual
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
137 OCR2 = CNT2_COMPARE;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
138 loop_until_bit_is_clear(ASSR, OCR2UB);
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
139
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
140 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
141 sleep_mode();
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
142 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
143
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
144 static void
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
145 do_measurement()
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
146 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
147 need_measurement = 0;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
148 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
149
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 int main(void)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 uart_init(9600);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 fprintf(&mystdout, "hello %d\n", 12);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
156 // set up counter2.
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
157 // COM21 COM20 Set OC2 on Compare Match (p116)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
158 // WGM21 Clear counter on compare
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
159 // CS22 CS21 CS20 clk/1024
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
160 TCCR2 = _BV(COM21) | _BV(COM20) | _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20);
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
161 // set async mode
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
162 ASSR |= _BV(AS2);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 for(;;){
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 /* insert your main loop code here */
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
166 if (need_measurement)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
167 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
168 do_measurement();
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
169 }
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 return 0; /* never reached */
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 }