diff options
| author | Bdale Garbee <bdale@gag.com> | 2012-05-16 09:13:53 -0600 | 
|---|---|---|
| committer | Bdale Garbee <bdale@gag.com> | 2012-05-16 09:13:53 -0600 | 
| commit | 6a973f788563ccc66b01cc7557a004dabef18d09 (patch) | |
| tree | ffbc8faad73cde7934c4050deb840092430a311f /src/drivers/ao_mpu6000.c | |
| parent | d387f246b24502642b76aad04eb3e0f1a5b78a05 (diff) | |
| parent | da2c920b9f3378d5a18551e008c1da5dace1e0ef (diff) | |
Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
Diffstat (limited to 'src/drivers/ao_mpu6000.c')
| -rw-r--r-- | src/drivers/ao_mpu6000.c | 231 | 
1 files changed, 204 insertions, 27 deletions
| diff --git a/src/drivers/ao_mpu6000.c b/src/drivers/ao_mpu6000.c index 290f1390..d27c42b0 100644 --- a/src/drivers/ao_mpu6000.c +++ b/src/drivers/ao_mpu6000.c @@ -23,64 +23,241 @@ static uint8_t	ao_mpu6000_wake;  static uint8_t	ao_mpu6000_configured;  static void -ao_mpu6000_isr(void) +ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len)  { -	ao_exti_disable(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN); -	ao_mpu6000_wake = 1; -	ao_wakeup(&ao_mpu6000_wake); +	ao_i2c_get(AO_MPU6000_I2C_INDEX); +	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE); +	ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE); +	ao_i2c_send(data, len, AO_MPU6000_I2C_INDEX, TRUE); +	ao_i2c_put(AO_MPU6000_I2C_INDEX);  }  static void -ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len) +ao_mpu6000_reg_write(uint8_t addr, uint8_t value)  { +	uint8_t	d[2] = { addr, value };  	ao_i2c_get(AO_MPU6000_I2C_INDEX);  	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE); -	ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX); -	if (len) -		ao_i2c_send(data, len, AO_MPU6000_I2C_INDEX); -	ao_i2c_stop(AO_MPU6000_I2C_INDEX); +	ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, TRUE);  	ao_i2c_put(AO_MPU6000_I2C_INDEX);  }  static void -ao_mpu6000_read(uint8_t addr, uint8_t *data, uint8_t len) +ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)  {  	ao_i2c_get(AO_MPU6000_I2C_INDEX);  	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE); -	ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX); +	ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);  	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ); -	if (len) -		ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX); -	ao_i2c_stop(AO_MPU6000_I2C_INDEX); +	ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);  	ao_i2c_put(AO_MPU6000_I2C_INDEX);  } +static uint8_t +ao_mpu6000_reg_read(uint8_t addr) +{ +	uint8_t	value; +	ao_i2c_get(AO_MPU6000_I2C_INDEX); +	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE); +	ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE); +	ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ); +	ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, TRUE); +	ao_i2c_put(AO_MPU6000_I2C_INDEX); +	return value; +} + +static void +ao_mpu6000_sample(struct ao_mpu6000_sample *sample) +{ +	uint16_t	*d = (uint16_t *) sample; +	int		i = sizeof (*sample) / 2; + +	ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample)); +	/* byte swap (sigh) */ +	while (i--) { +		uint16_t	t = *d; +		*d++ = (t >> 8) | (t << 8); +	} +} + +#define G	981	/* in cm/s² */ + +static int16_t /* cm/s² */ +ao_mpu6000_accel(int16_t v) +{ +	return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767); +} + +static int16_t	/* deg*10/s */ +ao_mpu6000_gyro(int16_t v) +{ +	return (int16_t) ((v * (int32_t) 20000) / 32767); +} + +static uint8_t +ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which) +{ +	int16_t	diff = test - normal; + +	if (diff < MPU6000_ST_ACCEL(16) / 2) { +		printf ("%s accel self test value too small (normal %d, test %d)\n", +			which, normal, test); +		return FALSE; +	} +	if (diff > MPU6000_ST_ACCEL(16) * 2) { +		printf ("%s accel self test value too large (normal %d, test %d)\n", +			which, normal, test); +		return FALSE; +	} +	return TRUE; +} + +static uint8_t +ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which) +{ +	int16_t	diff = test - normal; + +	if (diff < 0) +		diff = -diff; +	if (diff < MPU6000_ST_GYRO(2000) / 2) { +		printf ("%s gyro self test value too small (normal %d, test %d)\n", +			which, normal, test); +		return FALSE; +	} +	if (diff > MPU6000_ST_GYRO(2000) * 2) { +		printf ("%s gyro self test value too large (normal %d, test %d)\n", +			which, normal, test); +		return FALSE; +	} +	return TRUE; +} +  static void  ao_mpu6000_setup(void)  { +	struct ao_mpu6000_sample	normal_mode, test_mode; +	int				t; +  	if (ao_mpu6000_configured)  		return; -	/* Enable the EXTI interrupt for the appropriate pin */ -	ao_enable_port(AO_MPU6000_INT_PORT); -	ao_exti_setup(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN, -		      AO_EXTI_MODE_FALLING, ao_mpu6000_isr); +	/* Reset the whole chip */ +	 +	ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1, +			     (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET)); +	while (ao_mpu6000_reg_read(MPU6000_PWR_MGMT_1) & +	       (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET)) +		ao_yield(); + +	/* Reset signal conditioning */ +	ao_mpu6000_reg_write(MPU6000_USER_CONTROL, +			     (0 << MPU6000_USER_CONTROL_FIFO_EN) | +			     (0 << MPU6000_USER_CONTROL_I2C_MST_EN) | +			     (0 << MPU6000_USER_CONTROL_I2C_IF_DIS) | +			     (0 << MPU6000_USER_CONTROL_FIFO_RESET) | +			     (0 << MPU6000_USER_CONTROL_I2C_MST_RESET) | +			     (1 << MPU6000_USER_CONTROL_SIG_COND_RESET)); + +	while (ao_mpu6000_reg_read(MPU6000_USER_CONTROL) & (1 << MPU6000_USER_CONTROL_SIG_COND_RESET)) +		ao_yield(); + +	/* Reset signal paths */ +	ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET, +			     (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) | +			     (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) | +			     (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET)); + +	ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET, +			     (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) | +			     (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) | +			     (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET)); + +	/* Select clocks, disable sleep */ +	ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1, +			     (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) | +			     (0 << MPU6000_PWR_MGMT_1_SLEEP) | +			     (0 << MPU6000_PWR_MGMT_1_CYCLE) | +			     (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) | +			     (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL)); +	/* Set sample rate divider to sample at full speed +	ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0); + +	/* Disable filtering */ +	ao_mpu6000_reg_write(MPU6000_CONFIG, +			     (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) | +			     (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG)); + +	/* Configure accelerometer to +/-16G in self-test mode */ +	ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG, +			     (1 << MPU600_ACCEL_CONFIG_XA_ST) | +			     (1 << MPU600_ACCEL_CONFIG_YA_ST) | +			     (1 << MPU600_ACCEL_CONFIG_ZA_ST) | +			     (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL)); + +	/* Configure gyro to +/- 2000°/s in self-test mode */ +	ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG, +			     (1 << MPU600_GYRO_CONFIG_XG_ST) | +			     (1 << MPU600_GYRO_CONFIG_YG_ST) | +			     (1 << MPU600_GYRO_CONFIG_ZG_ST) | +			     (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL)); + +	ao_delay(AO_MS_TO_TICKS(200)); +	ao_mpu6000_sample(&test_mode); + +	/* Configure accelerometer to +/-16G */ +	ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG, +			     (0 << MPU600_ACCEL_CONFIG_XA_ST) | +			     (0 << MPU600_ACCEL_CONFIG_YA_ST) | +			     (0 << MPU600_ACCEL_CONFIG_ZA_ST) | +			     (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL)); + +	/* Configure gyro to +/- 2000°/s */ +	ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG, +			     (0 << MPU600_GYRO_CONFIG_XG_ST) | +			     (0 << MPU600_GYRO_CONFIG_YG_ST) | +			     (0 << MPU600_GYRO_CONFIG_ZG_ST) | +			     (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL)); + +	ao_delay(AO_MS_TO_TICKS(10)); +	ao_mpu6000_sample(&normal_mode); +	 +	ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x"); +	ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y"); +	ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z"); + +	ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x"); +	ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y"); +	ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z"); + +	/* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */ +	ao_mpu6000_reg_write(MPU6000_CONFIG, +			     (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) | +			     (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG)); + +	/* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */ +	ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, +			     1000 / 200 - 1); +	 +	ao_delay(AO_MS_TO_TICKS(100));  	ao_mpu6000_configured = 1;  } +  static void  ao_mpu6000_show(void)  { -	uint8_t	addr; -	uint8_t	data[14]; -	uint8_t i; - -	ao_mpu6000_read(MPU6000_WHO_AM_I, data, 1); -	printf ("mpu6000 WHO_AM_I: %02x\n", data[0]); -	ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, data, 1); -	for (i = 0; i < 14; i++) -		printf ("reg %02x: %02x\n", i + MPU6000_ACCEL_XOUT_H, data[i]); +	struct ao_mpu6000_sample	sample; + +	ao_mpu6000_setup(); +	ao_mpu6000_sample(&sample); +	printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n", +		ao_mpu6000_accel(sample.accel_x), +		ao_mpu6000_accel(sample.accel_y), +		ao_mpu6000_accel(sample.accel_z), +		ao_mpu6000_gyro(sample.gyro_x), +		ao_mpu6000_gyro(sample.gyro_y), +		ao_mpu6000_gyro(sample.gyro_z));  }  static const struct ao_cmds ao_mpu6000_cmds[] = { | 
