X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fdrivers%2Fao_mmc5983.c;h=02df7a2661e82149403f16215db53101c0c2ef3b;hb=HEAD;hp=4dd4a598c2c0ba04a3df2471e89c9b9af49f4e66;hpb=236d80fac2b4293c5750c9d80b387afd4d0fc5cc;p=fw%2Faltos diff --git a/src/drivers/ao_mmc5983.c b/src/drivers/ao_mmc5983.c index 4dd4a598..02df7a26 100644 --- a/src/drivers/ao_mmc5983.c +++ b/src/drivers/ao_mmc5983.c @@ -30,6 +30,60 @@ static struct ao_mmc5983_sample ao_mmc5983_offset; static uint8_t ao_mmc5983_configured; #ifdef MMC5983_I2C +#ifdef AO_MMC5983_I2C_INDEX + +static void +ao_mmc5983_start(void) { + ao_i2c_get(AO_MMC5983_I2C_INDEX); +} + +static void +ao_mmc5983_stop(void) { + ao_i2c_put(AO_MMC5983_I2C_INDEX); +} + +static void +ao_mmc5983_reg_write(uint8_t addr, uint8_t data) +{ + uint8_t d[2]; + + d[0] = addr; + d[1] = data; + + ao_mmc5983_start(); + ao_i2c_start(AO_MMC5983_I2C_INDEX, MMC5983_I2C_ADDR); + ao_i2c_send(d, 2, AO_MMC5983_I2C_INDEX, true); + ao_mmc5983_stop(); +} + +static uint8_t +ao_mmc5983_reg_read(uint8_t addr) +{ + uint8_t d[1]; + + ao_mmc5983_start(); + ao_i2c_start(AO_MMC5983_I2C_INDEX, MMC5983_I2C_ADDR); + d[0] = addr; + ao_i2c_send(d, 1, AO_MMC5983_I2C_INDEX, false); + ao_i2c_start(AO_MMC5983_I2C_INDEX, MMC5983_I2C_ADDR | 1); + ao_i2c_recv(d, 1, AO_MMC5983_I2C_INDEX, true); + ao_mmc5983_stop(); + return d[0]; +} + +static void +ao_mmc5983_raw(struct ao_mmc5983_raw *raw) +{ + ao_mmc5983_start(); + ao_i2c_start(AO_MMC5983_I2C_INDEX, MMC5983_I2C_ADDR); + raw->addr = MMC5983_X_OUT_0; + ao_i2c_send(&(raw->addr), 1, AO_MMC5983_I2C_INDEX, false); + ao_i2c_start(AO_MMC5983_I2C_INDEX, MMC5983_I2C_ADDR | 1); + ao_i2c_recv(&(raw->x0), sizeof(*raw) - 1, AO_MMC5983_I2C_INDEX, true); + ao_mmc5983_stop(); +} + +#else #include static void @@ -70,8 +124,10 @@ ao_mmc5983_raw(struct ao_mmc5983_raw *raw) ao_i2c_bit_stop(); } +#endif + #else -#define AO_MMC5983_SPI_SPEED ao_spi_speed(2000000) +#define AO_MMC5983_SPI_SPEED ao_spi_speed(AO_MMC5983_SPI_INDEX, 2000000) static void ao_mmc5983_start(void) { @@ -141,7 +197,7 @@ sat_sub(int16_t a, int16_t b) v = -32768; if (v > 32767) v = 32767; - return v; + return (int16_t) v; } /* Wait for a synchronous sample to finish */ @@ -179,9 +235,9 @@ ao_mmc5983_sample(struct ao_mmc5983_sample *s) ao_mmc5983_raw(&raw); /* Bias by 32768 to convert from uint16_t to int16_t */ - s->x = (int32_t) (((uint16_t) raw.x0 << 8) | raw.x1) - 32768; - s->y = (int32_t) (((uint16_t) raw.y0 << 8) | raw.y1) - 32768; - s->z = (int32_t) (((uint16_t) raw.z0 << 8) | raw.z1) - 32768;; + s->x = (int16_t) ((((uint16_t) raw.x0 << 8) | raw.x1) - 32768); + s->y = (int16_t) ((((uint16_t) raw.y0 << 8) | raw.y1) - 32768); + s->z = (int16_t) ((((uint16_t) raw.z0 << 8) | raw.z1) - 32768); } /* Synchronously sample the sensors */ @@ -217,9 +273,9 @@ ao_mmc5983_cal(void) ao_mmc5983_sync_sample(&reset); /* The zero point is the average of SET and RESET values */ - ao_mmc5983_offset.x = ((int32_t) set.x + (int32_t) reset.x) / 2; - ao_mmc5983_offset.y = ((int32_t) set.y + (int32_t) reset.y) / 2; - ao_mmc5983_offset.z = ((int32_t) set.z + (int32_t) reset.z) / 2; + ao_mmc5983_offset.x = (int16_t) (((int32_t) set.x + (int32_t) reset.x) / 2); + ao_mmc5983_offset.y = (int16_t) (((int32_t) set.y + (int32_t) reset.y) / 2); + ao_mmc5983_offset.z = (int16_t) (((int32_t) set.z + (int32_t) reset.z) / 2); } /* Configure the device to automatically sample at 200Hz */ @@ -356,9 +412,7 @@ ao_mmc5983_init(void) { ao_mmc5983_configured = 0; -#ifdef MMC5983_I2C - ao_enable_output(AO_MMC5983_SPI_CS_PORT, AO_MMC5983_SPI_CS_PIN, 1); -#else +#ifndef MMC5983_I2C ao_enable_input(AO_MMC5983_SPI_MISO_PORT, AO_MMC5983_SPI_MISO_PIN, AO_EXTI_MODE_PULL_NONE);