comparison main.c @ 20:878be5e353a0

Untested - calculate crc in uart_putchar
author Matt Johnston <matt@ucc.asn.au>
date Tue, 22 May 2012 23:23:38 +0800
parents f1016b151689
children 885532437100
comparison
equal deleted inserted replaced
19:f1016b151689 20:878be5e353a0
57 int uart_putchar(char c, FILE *stream); 57 int uart_putchar(char c, FILE *stream);
58 static void long_delay(int ms); 58 static void long_delay(int ms);
59 59
60 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 60 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
61 _FDEV_SETUP_WRITE); 61 _FDEV_SETUP_WRITE);
62
63 uint16_t crc_out;
64 static FILE _crc_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
65 _FDEV_SETUP_WRITE);
66 // convenience
67 static FILE *crc_stdout = &_crc_stdout;
62 68
63 static uint16_t n_measurements; 69 static uint16_t n_measurements;
64 // stored as decidegrees 70 // stored as decidegrees
65 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; 71 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS];
66 72
178 } 184 }
179 185
180 int 186 int
181 uart_putchar(char c, FILE *stream) 187 uart_putchar(char c, FILE *stream)
182 { 188 {
183 // XXX should sleep in the loop for power. 189 // XXX could perhaps sleep in the loop for power.
184 if (c == '\n') 190 if (c == '\n')
185 { 191 {
186 loop_until_bit_is_set(UCSR0A, UDRE0); 192 loop_until_bit_is_set(UCSR0A, UDRE0);
187 UDR0 = '\r';; 193 UDR0 = '\r';
188 } 194 }
189 loop_until_bit_is_set(UCSR0A, UDRE0); 195 loop_until_bit_is_set(UCSR0A, UDRE0);
190 UDR0 = c; 196 UDR0 = c;
197 if (stream == crc_stdout)
198 {
199 crc_out = _crc_ccitt_update(crc_out, '\n');
200 }
191 if (c == '\r') 201 if (c == '\r')
192 { 202 {
193 loop_until_bit_is_set(UCSR0A, UDRE0); 203 loop_until_bit_is_set(UCSR0A, UDRE0);
194 UDR0 = '\n';; 204 UDR0 = '\n';
205 if (stream == crc_stdout)
206 {
207 crc_out = _crc_ccitt_update(crc_out, '\n');
208 }
195 } 209 }
196 return 0; 210 return 0;
197 } 211 }
198 212
199 static void 213 static void
200 update_crc(uint16_t *crc, const void *data, uint8_t len)
201 {
202 for (uint8_t i = 0; i < len; i++)
203 {
204 *crc = _crc_ccitt_update(*crc, ((const uint8_t*)data)[i]);
205 }
206 }
207
208 static void
209 cmd_fetch() 214 cmd_fetch()
210 { 215 {
211 uint16_t crc = 0; 216 crc_out = 0;
212 uint8_t n_sensors; 217 uint8_t n_sensors;
213 eeprom_read(n_sensors, n_sensors); 218 eeprom_read(n_sensors, n_sensors);
214 219
215 printf_P(PSTR("Time %lu\n"), clock_epoch); 220 fprintf_P(crc_stdout, PSTR("START\n"));
216 update_crc(&crc, &clock_epoch, sizeof(clock_epoch)); 221 fprintf_P(crc_stdout, PSTR("time=%lu\n"), clock_epoch);
217 printf_P(PSTR("%d sensors\n"), n_measurements); 222 fprintf_P(crc_stdout, PSTR("sensors=%d\n"), n_measurements);
218 for (uint8_t s = 0; s < n_sensors; s++) 223 for (uint8_t s = 0; s < n_sensors; s++)
219 { 224 {
220 uint8_t id[ID_LEN]; 225 uint8_t id[ID_LEN];
221 printf_P(PSTR("%d : "), s); 226 fprintf_P(crc_stdout, PSTR("sensor_id%d="), s);
222 eeprom_read_to(id, sensor_id[s], ID_LEN); 227 eeprom_read_to(id, sensor_id[s], ID_LEN);
223 printhex(id, ID_LEN); 228 printhex(id, ID_LEN, crc_stdout);
224 putchar('\n'); 229 fputc('\n', crc_stdout);
225 update_crc(&crc, id, ID_LEN); 230 }
226 } 231 fprintf_P(crc_stdout, PSTR("measurements=%d\n"), n_measurements);
227 printf_P(PSTR("%d measurements\n"), n_measurements);
228 for (uint16_t n = 0; n < n_measurements; n++) 232 for (uint16_t n = 0; n < n_measurements; n++)
229 { 233 {
230 printf_P(PSTR("%3d :"), n); 234 fprintf_P(crc_stdout, PSTR("meas%3d="), n);
231 for (uint8_t s = 0; s < n_sensors; s++) 235 for (uint8_t s = 0; s < n_sensors; s++)
232 { 236 {
233 printf_P(PSTR(" %6d"), measurements[n][s]); 237 fprintf_P(crc_stdout, PSTR(" %6d"), measurements[n][s]);
234 update_crc(&crc, &measurements[n][s], sizeof(measurements[n][s])); 238 }
235 } 239 fputc('\n', crc_stdout);
236 putchar('\n'); 240 }
237 } 241 fprintf_P(crc_stdout, PSTR("END\n"));
238 printf_P(PSTR("CRC : %d\n"), crc); 242 fprintf_P(stdout, PSTR("CRC=%d\n"), crc_out);
239 } 243 }
240 244
241 static void 245 static void
242 cmd_clear() 246 cmd_clear()
243 { 247 {
336 eeprom_write_from(id, sensor_id[n], ID_LEN); 340 eeprom_write_from(id, sensor_id[n], ID_LEN);
337 n++; 341 n++;
338 eeprom_write(n, n_sensors); 342 eeprom_write(n, n_sensors);
339 sei(); 343 sei();
340 printf_P(PSTR("Added sensor %d : "), n); 344 printf_P(PSTR("Added sensor %d : "), n);
341 printhex(id, ID_LEN); 345 printhex(id, ID_LEN, stdout);
342 putchar('\n'); 346 putchar('\n');
343 } 347 }
344 else 348 else
345 { 349 {
346 printf_P(PSTR("Too many sensors\n")); 350 printf_P(PSTR("Too many sensors\n"));
446 { 450 {
447 printf_P(PSTR("Bad command\n")); 451 printf_P(PSTR("Bad command\n"));
448 } 452 }
449 } 453 }
450 454
451 ISR(INT0_vwct) 455 ISR(INT0_vect)
452 { 456 {
453 need_comms = 1; 457 need_comms = 1;
454 } 458 }
455 459
456 460