Mercurial > templog
changeset 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 | 2029633912c2 |
files | main.c simple_ds18b20.c simple_ds18b20.h |
diffstat | 3 files changed, 44 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/main.c Tue May 22 21:32:52 2012 +0800 +++ b/main.c Tue May 22 23:23:38 2012 +0800 @@ -60,6 +60,12 @@ static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); +uint16_t crc_out; +static FILE _crc_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, + _FDEV_SETUP_WRITE); +// convenience +static FILE *crc_stdout = &_crc_stdout; + static uint16_t n_measurements; // stored as decidegrees static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; @@ -180,62 +186,60 @@ int uart_putchar(char c, FILE *stream) { - // XXX should sleep in the loop for power. + // XXX could perhaps sleep in the loop for power. if (c == '\n') { loop_until_bit_is_set(UCSR0A, UDRE0); - UDR0 = '\r';; + UDR0 = '\r'; } loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; + if (stream == crc_stdout) + { + crc_out = _crc_ccitt_update(crc_out, '\n'); + } if (c == '\r') { loop_until_bit_is_set(UCSR0A, UDRE0); - UDR0 = '\n';; + UDR0 = '\n'; + if (stream == crc_stdout) + { + crc_out = _crc_ccitt_update(crc_out, '\n'); + } } return 0; } static void -update_crc(uint16_t *crc, const void *data, uint8_t len) -{ - for (uint8_t i = 0; i < len; i++) - { - *crc = _crc_ccitt_update(*crc, ((const uint8_t*)data)[i]); - } -} - -static void cmd_fetch() { - uint16_t crc = 0; + crc_out = 0; uint8_t n_sensors; eeprom_read(n_sensors, n_sensors); - printf_P(PSTR("Time %lu\n"), clock_epoch); - update_crc(&crc, &clock_epoch, sizeof(clock_epoch)); - printf_P(PSTR("%d sensors\n"), n_measurements); + fprintf_P(crc_stdout, PSTR("START\n")); + fprintf_P(crc_stdout, PSTR("time=%lu\n"), clock_epoch); + fprintf_P(crc_stdout, PSTR("sensors=%d\n"), n_measurements); for (uint8_t s = 0; s < n_sensors; s++) { uint8_t id[ID_LEN]; - printf_P(PSTR("%d : "), s); + fprintf_P(crc_stdout, PSTR("sensor_id%d="), s); eeprom_read_to(id, sensor_id[s], ID_LEN); - printhex(id, ID_LEN); - putchar('\n'); - update_crc(&crc, id, ID_LEN); + printhex(id, ID_LEN, crc_stdout); + fputc('\n', crc_stdout); } - printf_P(PSTR("%d measurements\n"), n_measurements); + fprintf_P(crc_stdout, PSTR("measurements=%d\n"), n_measurements); for (uint16_t n = 0; n < n_measurements; n++) { - printf_P(PSTR("%3d :"), n); + fprintf_P(crc_stdout, PSTR("meas%3d="), n); for (uint8_t s = 0; s < n_sensors; s++) { - printf_P(PSTR(" %6d"), measurements[n][s]); - update_crc(&crc, &measurements[n][s], sizeof(measurements[n][s])); + fprintf_P(crc_stdout, PSTR(" %6d"), measurements[n][s]); } - putchar('\n'); + fputc('\n', crc_stdout); } - printf_P(PSTR("CRC : %d\n"), crc); + fprintf_P(crc_stdout, PSTR("END\n")); + fprintf_P(stdout, PSTR("CRC=%d\n"), crc_out); } static void @@ -338,7 +342,7 @@ eeprom_write(n, n_sensors); sei(); printf_P(PSTR("Added sensor %d : "), n); - printhex(id, ID_LEN); + printhex(id, ID_LEN, stdout); putchar('\n'); } else @@ -448,7 +452,7 @@ } } -ISR(INT0_vwct) +ISR(INT0_vect) { need_comms = 1; }
--- a/simple_ds18b20.c Tue May 22 21:32:52 2012 +0800 +++ b/simple_ds18b20.c Tue May 22 23:23:38 2012 +0800 @@ -126,7 +126,7 @@ } static void -printhex_nibble(const unsigned char b) +printhex_nibble(const unsigned char b, FILE *stream) { unsigned char c = b & 0x0f; if ( c > 9 ) { @@ -135,26 +135,26 @@ else { c += '0'; } - putchar(c); + fputc(c, stream); } void -printhex_byte( const unsigned char b ) +printhex_byte(const unsigned char b, FILE *stream) { - printhex_nibble( b >> 4 ); - printhex_nibble( b ); + printhex_nibble( b >> 4, stream); + printhex_nibble( b, stream); } void -printhex(uint8_t *id, uint8_t n) +printhex(uint8_t *id, uint8_t n, FILE *stream) { for (uint8_t i = 0; i < n; i++) { if (i > 0) { - putchar(' '); + fputc(' ', stream); } - printhex_byte(id[i]); + printhex_byte(id[i], stream); } } @@ -190,7 +190,7 @@ { printf_P(PSTR("CRC fail")); } - printhex(id, OW_ROMCODE_SIZE); + printhex(id, OW_ROMCODE_SIZE, stdout); printf_P(PSTR(" %d.%d ÂșC\n"), decicelsius/10, decicelsius % 10); } printf_P(PSTR("Done sensors\n"));
--- a/simple_ds18b20.h Tue May 22 21:32:52 2012 +0800 +++ b/simple_ds18b20.h Tue May 22 23:23:38 2012 +0800 @@ -1,12 +1,13 @@ #ifndef SIMPLE_DS18B20_H_ #define SIMPLE_DS18B20_H_ #include <stdint.h> +#include <stdio.h> #include "ds18x20.h" uint8_t simple_ds18b20_start_meas(uint8_t id[]); -void printhex(uint8_t *id, uint8_t n); -void printhex_byte( const unsigned char b ); +void printhex(uint8_t *id, uint8_t n, FILE *stream); +void printhex_byte( const unsigned char b, FILE *stream ); uint8_t simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius ); uint8_t simple_ds18b20_read_all();