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 <ao_i2c_bit.h>
static void
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) {
v = -32768;
if (v > 32767)
v = 32767;
- return v;
+ return (int16_t) v;
}
/* Wait for a synchronous sample to finish */
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 */
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 */
{
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);