From fc2e5beb9173663e1e37a9b5a7b6eea1046222f7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 14 Dec 2012 11:11:39 -0800 Subject: altos: Log baro readings for MicroPeak This logs barometric data every 192ms (more or less) to the 504 remaining bytes of internal EEPROM storage in the ATtiny85. This provides 48.192 seconds of logging. Signed-off-by: Keith Packard --- src/micropeak/ao_log_micro.c | 65 ++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 35 deletions(-) (limited to 'src/micropeak/ao_log_micro.c') diff --git a/src/micropeak/ao_log_micro.c b/src/micropeak/ao_log_micro.c index eda0d1d2..40a7a35d 100644 --- a/src/micropeak/ao_log_micro.c +++ b/src/micropeak/ao_log_micro.c @@ -16,58 +16,53 @@ */ #include +#include #include #include -#if HAS_EEPROM - -ao_pos_t ao_log_micro_pos; +static uint16_t ao_log_offset = STARTING_LOG_OFFSET; void -ao_log_micro_data(uint32_t data) +ao_log_micro_save(void) { - ao_storage_write(ao_log_micro_pos, &data, sizeof (data)); - ao_log_micro_pos += sizeof (data); + uint16_t n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t); + ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground)); + ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min)); + ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples)); } -uint32_t ao_log_last_ground; -uint32_t ao_log_last_done; - -uint8_t -ao_log_micro_scan(void) +void +ao_log_micro_restore(void) { - uint32_t data; - ao_pos_t pos; + ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground)); + ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min)); +} - ao_storage_read(0, &data, sizeof (data)); - if ((data & AO_LOG_MICRO_MASK) != AO_LOG_MICRO_GROUND) - return 0; +void +ao_log_micro_data(void) +{ + uint16_t low_bits = pa; - ao_log_last_ground = data & ~(AO_LOG_MICRO_MASK); - for (pos = 4; pos < ao_storage_total; pos += 4) { - ao_storage_read(pos, &data, sizeof (data)); - if ((data & AO_LOG_MICRO_MASK) == AO_LOG_MICRO_GROUND) { - ao_log_last_done = data & ~(AO_LOG_MICRO_MASK); - return 1; - } + if (ao_log_offset < MAX_LOG_OFFSET) { + ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits)); + ao_log_offset += sizeof (low_bits); } - return 0; } void ao_log_micro_dump(void) { - ao_pos_t pos; - uint8_t data[4]; - uint8_t i; + uint16_t n_samples; + uint16_t nbytes; + uint8_t byte; + uint16_t b; - for (pos = 0; pos < ao_storage_total; pos += 4) { - ao_storage_read(pos, data, 4); - for (i = 0; i < 4; i++) - ao_async_byte(data[i]); - if (data[3] == (uint8_t) (AO_LOG_MICRO_GROUND >> 24)) - break; + ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples)); + nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples; + ao_async_byte('M'); + ao_async_byte('P'); + for (b = 0; b < nbytes; b++) { + ao_eeprom_read(b, &byte, 1); + ao_async_byte(byte); } } - -#endif -- cgit v1.2.3 From 23dc9a63ae8bc982d9352cfb7a3f508d8a08c374 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 17 Dec 2012 22:58:49 -0800 Subject: altos: Make micropeak 'serial' interface work I prototyped the mpserial interface on a breadboard and tuned the circuit to register the LED correctly. Then adjusted the serial code to send bits at the right speed and format. The logging contents are now in hexdecimal with a CCITT CRC-16 computed to verify correct reception. Signed-off-by: Keith Packard --- src/micropeak/Makefile | 2 +- src/micropeak/ao_async.c | 43 +++++++++++++++++++++++++++++----- src/micropeak/ao_async.h | 6 +++++ src/micropeak/ao_log_micro.c | 55 +++++++++++++++++++++++++++++++++++++++++++- src/micropeak/ao_micropeak.c | 1 + 5 files changed, 99 insertions(+), 8 deletions(-) (limited to 'src/micropeak/ao_log_micro.c') diff --git a/src/micropeak/Makefile b/src/micropeak/Makefile index 82944cb1..ff0a4499 100644 --- a/src/micropeak/Makefile +++ b/src/micropeak/Makefile @@ -50,7 +50,7 @@ IDPRODUCT=0 PRODUCT=MicroPeak-v0.1 PRODUCT_DEF=-DMICROPEAK CFLAGS = $(PRODUCT_DEF) -I. -I../attiny -I../core -I.. -I../drivers -CFLAGS += -g -mmcu=$(MCU) -Wall -Wstrict-prototypes -O3 -mcall-prologues -DATTINY +CFLAGS += -g -mmcu=$(MCU) -Wall -Wstrict-prototypes -O2 -mcall-prologues -DATTINY NICKLE=nickle diff --git a/src/micropeak/ao_async.c b/src/micropeak/ao_async.c index 04bba9e8..3556f54c 100644 --- a/src/micropeak/ao_async.c +++ b/src/micropeak/ao_async.c @@ -21,20 +21,51 @@ #define AO_ASYNC_BAUD 38400l #define AO_ASYNC_DELAY (uint8_t) (1000000l / AO_ASYNC_BAUD) +#define LED_PORT PORTB + +void +ao_async_start(void) +{ + LED_PORT |= (1 << AO_LED_SERIAL); +} + +void +ao_async_stop(void) +{ + LED_PORT &= ~(1 << AO_LED_SERIAL); +} + void ao_async_byte(uint8_t byte) { uint8_t b; uint16_t w; - /* start bit */ - - /* start data stop */ - w = 0x001 | (byte << 1) | 0x000; + /* start data stop */ + w = (0x000 << 0) | (byte << 1) | (0x001 << 9); + ao_arch_block_interrupts(); for (b = 0; b < 10; b++) { - ao_led_set((w & 1) << AO_LED_SERIAL); + uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL); + v |= (w & 1) << AO_LED_SERIAL; + LED_PORT = v; w >>= 1; - ao_delay_us(26); + + /* Carefully timed to hit around 9600 baud */ + asm volatile ("nop"); + asm volatile ("nop"); + + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); + + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); + asm volatile ("nop"); } + ao_arch_release_interrupts(); } diff --git a/src/micropeak/ao_async.h b/src/micropeak/ao_async.h index a06d2e1a..1b239712 100644 --- a/src/micropeak/ao_async.h +++ b/src/micropeak/ao_async.h @@ -18,6 +18,12 @@ #ifndef _AO_ASYNC_H_ #define _AO_ASYNC_H_ +void +ao_async_start(void); + +void +ao_async_stop(void); + void ao_async_byte(uint8_t byte); diff --git a/src/micropeak/ao_log_micro.c b/src/micropeak/ao_log_micro.c index 40a7a35d..d665efb5 100644 --- a/src/micropeak/ao_log_micro.c +++ b/src/micropeak/ao_log_micro.c @@ -49,6 +49,46 @@ ao_log_micro_data(void) } } +#define POLY 0x8408 + +static uint16_t +ao_log_micro_crc(uint16_t crc, uint8_t byte) +{ + uint8_t i; + + for (i = 0; i < 8; i++) { + if ((crc & 0x0001) ^ (byte & 0x0001)) + crc = (crc >> 1) ^ POLY; + else + crc = crc >> 1; + byte >>= 1; + } + return crc; +} + +static void +ao_log_hex_nibble(uint8_t b) +{ + if (b < 10) + ao_async_byte('0' + b); + else + ao_async_byte('a' - 10 + b); +} + +static void +ao_log_hex(uint8_t b) +{ + ao_log_hex_nibble(b>>4); + ao_log_hex_nibble(b&0xf); +} + +static void +ao_log_newline(void) +{ + ao_async_byte('\r'); + ao_async_byte('\n'); +} + void ao_log_micro_dump(void) { @@ -56,13 +96,26 @@ ao_log_micro_dump(void) uint16_t nbytes; uint8_t byte; uint16_t b; + uint16_t crc = 0xffff; ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples)); + if (n_samples == 0xffff) + n_samples = 0; nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples; + ao_async_start(); ao_async_byte('M'); ao_async_byte('P'); for (b = 0; b < nbytes; b++) { + if ((b & 0xf) == 0) + ao_log_newline(); ao_eeprom_read(b, &byte, 1); - ao_async_byte(byte); + ao_log_hex(byte); + crc = ao_log_micro_crc(crc, byte); } + ao_log_newline(); + crc = ~crc; + ao_log_hex(crc >> 8); + ao_log_hex(crc); + ao_log_newline(); + ao_async_stop(); } diff --git a/src/micropeak/ao_micropeak.c b/src/micropeak/ao_micropeak.c index 3bbc7eea..f579a09a 100644 --- a/src/micropeak/ao_micropeak.c +++ b/src/micropeak/ao_micropeak.c @@ -19,6 +19,7 @@ #include #include #include +#include static struct ao_ms5607_sample sample; static struct ao_ms5607_value value; -- cgit v1.2.3