33 #if defined(PIOS_INCLUDE_LIS3MDL)
38 #include "physical_constants.h"
43 #define PIOS_LIS_SPI_SPEED 9400000
50 enum lis3mdl_dev_magic {
51 PIOS_LIS_DEV_MAGIC = 0x3353494c
58 enum lis3mdl_dev_magic
magic;
61 uint32_t spi_slave_mag;
74 static int32_t PIOS_LIS_Validate(
struct lis3mdl_dev *dev);
76 static bool PIOS_LIS_Callback(
void *ctx,
void *output,
77 int ms_to_wait,
int *next_call);
83 static int32_t PIOS_LIS_ClaimBus();
89 static int32_t PIOS_LIS_ReleaseBus();
92 uint8_t address, uint8_t *buffer);
94 uint8_t address, uint8_t buffer);
100 dev = (
struct lis3mdl_dev *)
PIOS_malloc(
sizeof(*dev));
104 dev->magic = PIOS_LIS_DEV_MAGIC;
109 static int32_t PIOS_LIS_Validate(
struct lis3mdl_dev *dev)
113 if (dev->magic != PIOS_LIS_DEV_MAGIC)
115 if (dev->spi_id == 0)
121 uint8_t address, uint8_t expect) {
124 int32_t ret = PIOS_LIS_ReadReg(lis_dev, address, &c);
132 "LIS: Assertion failed: *(%02x) == %02x (expect %02x)\n",
137 DEBUG_PRINTF(2,
"LIS: Assertion passed: *(%02x) == %02x\n", address,
151 lis_dev->spi_id = spi_id;
152 lis_dev->spi_slave_mag = slave_mag;
215 PIOS_LIS_Callback, lis_dev);
246 uint8_t address, uint8_t *buffer)
251 if (PIOS_LIS_ClaimBus(lis_dev) != 0)
258 PIOS_LIS_ReleaseBus(lis_dev);
264 uint8_t address, uint8_t buffer)
271 if (PIOS_LIS_ClaimBus(lis_dev) != 0)
277 PIOS_LIS_ReleaseBus(lis_dev);
282 static bool PIOS_LIS_Callback(
void *ctx,
void *output,
283 int ms_to_wait,
int *next_call)
299 if (PIOS_LIS_ClaimBus(lis_dev) != 0)
311 (uint8_t *) sensor_buf,
312 sizeof(sensor_buf)) < 0) {
313 PIOS_LIS_ReleaseBus(lis_dev);
317 PIOS_LIS_ReleaseBus(lis_dev);
319 #define GET_SB_FROM_REGNO(x) \
320 (sensor_buf[ ((x) - LIS_REG_MAG_OUTX_L) / 2])
337 switch (lis_dev->cfg->orientation) {
390 #endif // PIOS_INCLUDE_LIS3MDL
Main PiOS header to include all the compiled in PiOS options.
#define LIS_CTRL3_MODE_CONTINUOUS
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
struct lis3mdl_dev * lis3mdl_dev_t
void * PIOS_malloc(size_t size)
#define LIS_ADDRESS_AUTOINCREMENT
#define DEBUG_PRINTF(level,...)
#define LIS_CTRL1_OPMODE_UHP
static struct flyingpicmd_cfg_fa cfg
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
int32_t PIOS_SPI_SetClockSpeed(pios_spi_t spi_dev, uint32_t speed)
int32_t PIOS_SPI_TransferBlock(pios_spi_t spi_dev, const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
static float mag_scale[3]
const struct pios_spi_cfg * cfg
int32_t PIOS_SENSORS_RegisterCallback(enum pios_sensor_type type, PIOS_SENSOR_Callback_t callback, void *ctx)
Register a callback-based sensor with the PIOS_SENSORS interface.
#define LIS_RANGE_12GAU_COUNTS_PER_MGAU
int32_t PIOS_LIS3MDL_SPI_Init(lis3mdl_dev_t *dev, pios_spi_t spi_id, uint32_t slave_mag, const struct pios_lis3mdl_cfg *cfg)
Initialize the LIS3MDL mag.
Pios sensor structure for generic mag data.
#define LIS_CTRL4_OPMODEZ_UHP
Includes PiOS and core architecture components.
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
#define PIOS_Assert(test)
#define LIS_CTRL2_FS_12GAU