From b9bf8e01e243508297f28b102cb2477dc1bc74df Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 12 Oct 2012 14:22:41 -0700 Subject: altos: Add initial micropeak implementation Blinks out max height in decimeters, stores previous flight data to internal eeprom. Signed-off-by: Keith Packard --- src/micropeak/ao_async.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/micropeak/ao_async.c (limited to 'src/micropeak/ao_async.c') diff --git a/src/micropeak/ao_async.c b/src/micropeak/ao_async.c new file mode 100644 index 00000000..04bba9e8 --- /dev/null +++ b/src/micropeak/ao_async.c @@ -0,0 +1,40 @@ +/* + * Copyright © 2012 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include + +#define AO_ASYNC_BAUD 38400l +#define AO_ASYNC_DELAY (uint8_t) (1000000l / AO_ASYNC_BAUD) + +void +ao_async_byte(uint8_t byte) +{ + uint8_t b; + uint16_t w; + + /* start bit */ + + /* start data stop */ + w = 0x001 | (byte << 1) | 0x000; + + for (b = 0; b < 10; b++) { + ao_led_set((w & 1) << AO_LED_SERIAL); + w >>= 1; + ao_delay_us(26); + } +} -- 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_async.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