diff options
Diffstat (limited to 'src/drivers')
| -rw-r--r-- | src/drivers/ao_at24c.c | 104 | ||||
| -rw-r--r-- | src/drivers/ao_mpu6000.h | 3 | ||||
| -rw-r--r-- | src/drivers/ao_ms5607.c | 70 | ||||
| -rw-r--r-- | src/drivers/ao_ms5607.h | 3 | ||||
| -rw-r--r-- | src/drivers/ao_ms5607_convert.c | 58 | ||||
| -rw-r--r-- | src/drivers/ao_pad.c | 71 | ||||
| -rw-r--r-- | src/drivers/ao_radio_master.c | 2 | 
7 files changed, 246 insertions, 65 deletions
diff --git a/src/drivers/ao_at24c.c b/src/drivers/ao_at24c.c new file mode 100644 index 00000000..2a23be3a --- /dev/null +++ b/src/drivers/ao_at24c.c @@ -0,0 +1,104 @@ +/* + * Copyright © 2012 Keith Packard <keithp@keithp.com> + * + * 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 <ao.h> + +#if HAS_EEPROM +#define AO_AT24C_ADDR		0xa0 +#define AO_AT24C_ADDR_WRITE	(AO_AT24C_ADDR|0) +#define AO_AT24C_ADDR_READ	(AO_AT24C_ADDR|1) +#define AO_AT24C_PAGE_LEN	128 + +/* Total bytes of available storage */ +__pdata ao_pos_t	ao_storage_total = 64l * 1024l; + +/* Storage unit size - device reads and writes must be within blocks of this size. */ +__pdata uint16_t	ao_storage_unit = 128; + +static void +ao_at24c_set_address(uint8_t addr, ao_pos_t pos) +{ +	uint8_t	a[2]; + +	a[0] = pos >> 8; +	a[1] = pos; +	ao_i2c_start_bus(addr); +	ao_i2c_send_bus(a, 2, 0); +} + +/* + * Erase the specified sector + */ +uint8_t +ao_storage_erase(ao_pos_t pos) __reentrant +{ +	if (pos >= ao_storage_total || pos + AO_AT24C_PAGE_LEN > ao_storage_total) +		return 0; + +	ao_mutex_get(&ao_at24c_mutex); +	ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos); +	ao_i2c_send_fixed_bus(0xff, AO_AT24C_PAGE_LEN, 1); +	ao_mutex_put(&ao_at24c_mutex); +	return 1; +} + +/* + * Write to flash + */ +uint8_t +ao_storage_device_write(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant +{ +	if (pos >= ao_storage_total || pos + len > ao_storage_total) +		return 0; + +	ao_mutex_get(&ao_m25_mutex); +	ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos); +	ao_i2c_send_bus(d, len, 1); +	ao_mutex_put(&ao_m25_mutex); +	return 1; +} + +/* + * Read from flash + */ +uint8_t +ao_storage_device_read(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant +{ +	if (pos >= ao_storage_total || pos + len > ao_storage_total) +		return 0; +	ao_mutex_get(&ao_m25_mutex); +	ao_at24c_set_address(AO_AT24C_ADDR_READ, pos); +	ao_i2c_recv_bus(d, len, 1); +	ao_mutex_put(&ao_m25_mutex); +	return 1; +} + +void +ao_storage_flush(void) __reentrant +{ +} + +void +ao_storage_setup(void) +{ +} + +void +ao_storage_device_init(void) +{ +} +#endif diff --git a/src/drivers/ao_mpu6000.h b/src/drivers/ao_mpu6000.h index ab36d6f2..6aada9a9 100644 --- a/src/drivers/ao_mpu6000.h +++ b/src/drivers/ao_mpu6000.h @@ -145,6 +145,9 @@  /* Self test gyro is approximately 50°/s */  #define MPU6000_ST_GYRO(full_scale)	((int16_t) (((int32_t) 32767 * (int32_t) 50) / (full_scale))) +#define MPU6000_GYRO_FULLSCALE	2000 +#define MPU6000_ACCEL_FULLSCALE	16 +  struct ao_mpu6000_sample {  	int16_t		accel_x;  	int16_t		accel_y; diff --git a/src/drivers/ao_ms5607.c b/src/drivers/ao_ms5607.c index 736e115b..077a40e6 100644 --- a/src/drivers/ao_ms5607.c +++ b/src/drivers/ao_ms5607.c @@ -17,7 +17,7 @@  #include <ao.h>  #include <ao_exti.h> -#include "ao_ms5607.h" +#include <ao_ms5607.h>  #if HAS_MS5607 || HAS_MS5611 @@ -27,12 +27,12 @@ static uint8_t	  		ms5607_configured;  static void  ao_ms5607_start(void) {  	ao_spi_get(AO_MS5607_SPI_INDEX,AO_SPI_SPEED_FAST); -	stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 0); +	ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 0);  }  static void  ao_ms5607_stop(void) { -	stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1); +	ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 1);  	ao_spi_put(AO_MS5607_SPI_INDEX);  } @@ -53,7 +53,6 @@ ao_ms5607_crc(uint8_t *prom)  	uint8_t		crc_byte = prom[15];  	uint8_t 	cnt;  	uint16_t	n_rem = 0; -	uint16_t	crc_read;  	uint8_t		n_bit;  	prom[15] = 0; @@ -89,9 +88,11 @@ ao_ms5607_prom_read(struct ao_ms5607_prom *prom)  	}  	crc = ao_ms5607_crc((uint8_t *) prom);  	if (crc != (((uint8_t *) prom)[15] & 0xf)) { +#if HAS_TASK  		printf ("MS5607 PROM CRC error (computed %x actual %x)\n",  			crc, (((uint8_t *) prom)[15] & 0xf));  		flush(); +#endif  		ao_panic(AO_PANIC_SELF_TEST_MS5607);  	} @@ -105,7 +106,7 @@ ao_ms5607_prom_read(struct ao_ms5607_prom *prom)  #endif  } -static void +void  ao_ms5607_setup(void)  {  	if (ms5607_configured) @@ -115,33 +116,34 @@ ao_ms5607_setup(void)  	ao_ms5607_prom_read(&ms5607_prom);  } -static uint8_t	ao_ms5607_done; +static volatile uint8_t	ao_ms5607_done;  static void  ao_ms5607_isr(void)  {  	ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);  	ao_ms5607_done = 1; -	ao_wakeup(&ao_ms5607_done); +	ao_wakeup((void *) &ao_ms5607_done);  }  static uint32_t  ao_ms5607_get_sample(uint8_t cmd) {  	uint8_t	reply[3];  	uint8_t read; -	uint16_t now;  	ao_ms5607_done = 0;  	ao_ms5607_start();  	ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX); +  	ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN); +  #if AO_MS5607_PRIVATE_PINS  	ao_spi_put(AO_MS5607_SPI_INDEX);  #endif  	cli();  	while (!ao_ms5607_done) -		ao_sleep(&ao_ms5607_done); +		ao_sleep((void *) &ao_ms5607_done);  	sei();  #if AO_MS5607_PRIVATE_PINS  	stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1); @@ -165,48 +167,9 @@ ao_ms5607_sample(struct ao_ms5607_sample *sample)  	sample->temp = ao_ms5607_get_sample(AO_MS5607_CONVERT_D2_2048);  } -void -ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value) -{ -	uint8_t	addr; -	int32_t	dT; -	int32_t TEMP; -	int64_t OFF; -	int64_t SENS; -	int32_t P; - -	dT = sample->temp - ((int32_t) ms5607_prom.tref << 8); -	 -	TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23); - -#if HAS_MS5611 -	OFF = ((int64_t) ms5607_prom.off << 16) + (((int64_t) ms5607_prom.tco * dT) >> 7); -	SENS = ((int64_t) ms5607_prom.sens << 15) + (((int64_t) ms5607_prom.tcs * dT) >> 8); -#else -	OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6); -	SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7); -#endif - -	if (TEMP < 2000) { -		int32_t	T2 = ((int64_t) dT * (int64_t) dT) >> 31; -		int32_t TEMPM = TEMP - 2000; -		int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; -		int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; -		if (TEMP < 1500) { -			int32_t TEMPP = TEMP + 1500; -			int64_t TEMPP2 = TEMPP * TEMPP; -			OFF2 = OFF2 + 15 * TEMPP2; -			SENS2 = SENS2 + 8 * TEMPP2; -		} -		TEMP -= T2; -		OFF -= OFF2; -		SENS -= SENS2; -	} - -	value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; -	value->temp = TEMP; -} +#include "ao_ms5607_convert.c" +#if HAS_TASK  struct ao_ms5607_sample	ao_ms5607_current;  static void @@ -253,15 +216,18 @@ __code struct ao_cmds ao_ms5607_cmds[] = {  	{ ao_ms5607_dump,	"B\0Display MS5607 data" },  	{ 0, NULL },  }; +#endif /* HAS_TASK */  void  ao_ms5607_init(void)  {  	ms5607_configured = 0; -	ao_cmd_register(&ao_ms5607_cmds[0]);  	ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN)); +#if HAS_TASK +	ao_cmd_register(&ao_ms5607_cmds[0]);  	ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607"); +#endif  	/* Configure the MISO pin as an interrupt; when the  	 * conversion is complete, the MS5607 will raise this @@ -272,12 +238,14 @@ ao_ms5607_init(void)  		      AO_EXTI_MODE_RISING,  		      ao_ms5607_isr); +#ifdef STM_MODER_ALTERNATE  	/* Reset the pin from INPUT to ALTERNATE so that SPI works  	 * This needs an abstraction at some point...  	 */  	stm_moder_set(AO_MS5607_MISO_PORT,  		      AO_MS5607_MISO_PIN,  		      STM_MODER_ALTERNATE); +#endif  }  #endif diff --git a/src/drivers/ao_ms5607.h b/src/drivers/ao_ms5607.h index 4c29f6a7..b2f98a59 100644 --- a/src/drivers/ao_ms5607.h +++ b/src/drivers/ao_ms5607.h @@ -59,6 +59,9 @@ struct ao_ms5607_value {  extern struct ao_ms5607_sample	ao_ms5607_current;  void +ao_ms5607_setup(void); + +void  ao_ms5607_init(void);  void diff --git a/src/drivers/ao_ms5607_convert.c b/src/drivers/ao_ms5607_convert.c new file mode 100644 index 00000000..e61d19ed --- /dev/null +++ b/src/drivers/ao_ms5607_convert.c @@ -0,0 +1,58 @@ +/* + * Copyright © 2012 Keith Packard <keithp@keithp.com> + * + * 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 <ao_ms5607.h> + +void +ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value) +{ +	int32_t	dT; +	int32_t TEMP; +	int64_t OFF; +	int64_t SENS; + +	dT = sample->temp - ((int32_t) ms5607_prom.tref << 8); +	 +	TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23); + +#if HAS_MS5611 +	OFF = ((int64_t) ms5607_prom.off << 16) + (((int64_t) ms5607_prom.tco * dT) >> 7); +	SENS = ((int64_t) ms5607_prom.sens << 15) + (((int64_t) ms5607_prom.tcs * dT) >> 8); +#else +	OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6); +	SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7); +#endif + +	if (TEMP < 2000) { +		int32_t	T2 = ((int64_t) dT * (int64_t) dT) >> 31; +		int32_t TEMPM = TEMP - 2000; +		int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; +		int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; +		if (TEMP < 1500) { +			int32_t TEMPP = TEMP + 1500; +			int64_t TEMPP2 = TEMPP * TEMPP; +			OFF2 = OFF2 + 15 * TEMPP2; +			SENS2 = SENS2 + 8 * TEMPP2; +		} +		TEMP -= T2; +		OFF -= OFF2; +		SENS -= SENS2; +	} + +	value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; +	value->temp = TEMP; +} diff --git a/src/drivers/ao_pad.c b/src/drivers/ao_pad.c index 55e6289d..6cec98ab 100644 --- a/src/drivers/ao_pad.c +++ b/src/drivers/ao_pad.c @@ -40,24 +40,62 @@ static __pdata uint8_t	ao_pad_debug;  #endif  static void +ao_siren(uint8_t v) +{ +#ifdef AO_SIREN +	ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v); +#else +	ao_beep(v ? AO_BEEP_MID : 0); +#endif +} + +static void +ao_strobe(uint8_t v) +{ +#ifdef AO_STROBE +	ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v); +#endif +} + +static void  ao_pad_run(void)  { +	uint8_t	pins; +  	for (;;) {  		while (!ao_pad_ignite)  			ao_sleep(&ao_pad_ignite);  		/*  		 * Actually set the pad bits  		 */ -		AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | ao_pad_ignite; +		pins = 0; +#if AO_PAD_NUM > 0 +		if (ao_pad_ignite & (1 << 0)) +			pins |= (1 << AO_PAD_PIN_0); +#endif +#if AO_PAD_NUM > 1 +		if (ao_pad_ignite & (1 << 1)) +			pins |= (1 << AO_PAD_PIN_1); +#endif +#if AO_PAD_NUM > 2 +		if (ao_pad_ignite & (1 << 2)) +			pins |= (1 << AO_PAD_PIN_2); +#endif +#if AO_PAD_NUM > 3 +		if (ao_pad_ignite & (1 << 3)) +			pins |= (1 << AO_PAD_PIN_3); +#endif +		AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | pins;  		while (ao_pad_ignite) {  			ao_pad_ignite = 0; +  			ao_delay(AO_PAD_FIRE_TIME);  		}  		AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);  	}  } -#define AO_PAD_ARM_BEEP_INTERVAL	200 +#define AO_PAD_ARM_SIREN_INTERVAL	200  static void  ao_pad_monitor(void) @@ -139,22 +177,27 @@ ao_pad_monitor(void)  			prev = cur;  		} +		if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME) +			ao_pad_armed = 0; +  		if (ao_pad_armed) { +			ao_strobe(1);  			if (sample & 2) -				ao_beep(AO_BEEP_HIGH); +				ao_siren(1);  			else -				ao_beep(AO_BEEP_LOW); +				ao_siren(0);  			beeping = 1;  		} else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {  			if (arm_beep_time == 0) { -				arm_beep_time = AO_PAD_ARM_BEEP_INTERVAL; +				arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;  				beeping = 1; -				ao_beep(AO_BEEP_HIGH); +				ao_siren(1);  			}  			--arm_beep_time;  		} else if (beeping) {  			beeping = 0; -			ao_beep(0); +			ao_siren(0); +			ao_strobe(0);  		}  	}  } @@ -181,7 +224,6 @@ ao_pad(void)  	int16_t	time_difference;  	int8_t	ret; -	ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));  	ao_pad_box = 0;  	ao_led_set(0);  	ao_led_on(AO_LED_POWER); @@ -197,9 +239,6 @@ ao_pad(void)  		PRINTD ("tick %d box %d cmd %d channels %02x\n",  			command.tick, command.box, command.cmd, command.channels); -		if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME) -			ao_pad_armed = 0; -  		switch (command.cmd) {  		case AO_LAUNCH_ARM:  			if (command.box != ao_pad_box) { @@ -207,7 +246,7 @@ ao_pad(void)  				break;  			} -			if (command.channels & ~(AO_PAD_ALL_PINS)) +			if (command.channels & ~(AO_PAD_ALL_CHANNELS))  				break;  			time_difference = command.tick - ao_time(); @@ -232,7 +271,7 @@ ao_pad(void)  			query.tick = ao_time();  			query.box = ao_pad_box; -			query.channels = AO_PAD_ALL_PINS; +			query.channels = AO_PAD_ALL_CHANNELS;  			query.armed = ao_pad_armed;  			PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",  				query.tick, query.box, query.channels, query.armed, @@ -349,6 +388,12 @@ ao_pad_init(void)  #if AO_PAD_NUM > 3  	ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);  #endif +#ifdef AO_STROBE +	ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0); +#endif +#ifdef AO_SIREN +	ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0); +#endif  	ao_cmd_register(&ao_pad_cmds[0]);  	ao_add_task(&ao_pad_task, ao_pad, "pad listener");  	ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter"); diff --git a/src/drivers/ao_radio_master.c b/src/drivers/ao_radio_master.c index 73ac3c03..4a37ace0 100644 --- a/src/drivers/ao_radio_master.c +++ b/src/drivers/ao_radio_master.c @@ -53,7 +53,7 @@ ao_radio_master_start(void)  {  	ao_spi_get_bit(AO_RADIO_CS_PORT, AO_RADIO_CS_PIN, AO_RADIO_CS,  		       AO_RADIO_SPI_BUS, -		       AO_SPI_SPEED_200kHz); +		       AO_SPI_SPEED_2MHz);  }  static void  | 
