40 #if defined(PIOS_INCLUDE_BMX055)
45 #include "physical_constants.h"
50 #define PIOS_BMX_TASK_PRIORITY PIOS_THREAD_PRIO_HIGHEST
51 #define PIOS_BMX_TASK_STACK 640
53 #define PIOS_BMX_QUEUE_LEN 2
55 #define PIOS_BMX_SPI_SPEED 10000000
62 enum pios_bmx055_dev_magic {
63 PIOS_BMX_DEV_MAGIC = 0x58786d42
69 struct pios_bmx055_dev {
70 enum pios_bmx055_dev_magic
magic;
73 uint32_t spi_slave_gyro;
74 uint32_t spi_slave_accel;
79 struct pios_thread *task_handle;
81 volatile uint32_t interrupt_count;
85 static struct pios_bmx055_dev *bmx_dev;
97 static int32_t PIOS_BMX_Validate(
struct pios_bmx055_dev *dev);
99 static void PIOS_BMX_Task(
void *parameters);
105 static int32_t PIOS_BMX_ClaimBus(
int slave);
111 static int32_t PIOS_BMX_ReleaseBus(
int slave);
113 static int32_t PIOS_BMX_ReadReg(
int slave, uint8_t address, uint8_t *buffer);
114 static int32_t PIOS_BMX_WriteReg(
int slave, uint8_t address, uint8_t buffer);
118 struct pios_bmx055_dev *dev;
120 dev = (
struct pios_bmx055_dev *)
PIOS_malloc(
sizeof(*bmx_dev));
124 dev->magic = PIOS_BMX_DEV_MAGIC;
127 if (dev->accel_queue == NULL) {
133 if (dev->gyro_queue == NULL) {
140 if (dev->data_ready_sema == NULL) {
150 static int32_t PIOS_BMX_Validate(
struct pios_bmx055_dev *dev)
154 if (dev->magic != PIOS_BMX_DEV_MAGIC)
156 if (dev->spi_id == 0)
161 static int32_t AssertReg(
int slave, uint8_t address, uint8_t expect) {
164 int32_t ret = PIOS_BMX_ReadReg(slave, address, &c);
171 DEBUG_PRINTF(2,
"BMX: Assertion failed: *(%d:%02x) == %02x (expect %02x)\n",
172 slave, address, c, expect);
176 DEBUG_PRINTF(2,
"BMX: Assertion passed: *(%d:%02x) == %02x\n", slave, address,
183 uint32_t slave_gyro, uint32_t slave_accel,
188 bmx_dev = PIOS_BMX_Alloc(cfg);
193 bmx_dev->spi_id = spi_id;
194 bmx_dev->spi_slave_gyro = slave_gyro;
195 bmx_dev->spi_slave_accel = slave_accel;
213 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_gyro,
219 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_accel,
229 ret = AssertReg(bmx_dev->spi_slave_gyro,
235 ret = AssertReg(bmx_dev->spi_slave_accel,
242 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_accel,
248 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_accel,
255 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_gyro,
261 ret = PIOS_BMX_WriteReg(bmx_dev->spi_slave_gyro,
272 if (!bmx_dev->cfg->skip_startup_irq_check) {
273 for (
int i=0;
i<2;
i++) {
274 uint32_t ref_val = bmx_dev->interrupt_count;
277 while (bmx_dev->interrupt_count == ref_val) {
280 return -PIOS_BMX_ERROR_NOIRQ;
288 PIOS_BMX_Task,
"pios_bmx", PIOS_BMX_TASK_STACK,
289 NULL, PIOS_BMX_TASK_PRIORITY);
305 static int32_t PIOS_BMX_ClaimBus(
int slave)
307 if (PIOS_BMX_Validate(bmx_dev) != 0)
318 static int32_t PIOS_BMX_ReleaseBus(
int slave)
320 if (PIOS_BMX_Validate(bmx_dev) != 0)
330 static int32_t PIOS_BMX_ReadReg(
int slave, uint8_t address, uint8_t *buffer)
332 if (PIOS_BMX_ClaimBus(slave) != 0)
338 PIOS_BMX_ReleaseBus(slave);
343 static int32_t PIOS_BMX_WriteReg(
int slave, uint8_t address, uint8_t buffer)
345 if (PIOS_BMX_ClaimBus(slave) != 0)
351 PIOS_BMX_ReleaseBus(slave);
357 bool PIOS_BMX_IRQHandler(
void)
359 if (PIOS_BMX_Validate(bmx_dev) != 0)
364 bmx_dev->interrupt_count++;
372 static void PIOS_BMX_Task(
void *parameters)
399 PIOS_BMX_ReleaseBus(
false);
410 PIOS_BMX_ReleaseBus(
false);
421 #define PACK_REG12_ADDR_OFFSET(b, reg, off) (int16_t) ( (b[reg-off] & 0xf0) | (b[reg-off+1] << 8) )
422 #define PACK_REG16_ADDR_OFFSET(b, reg, off) (int16_t) ( (b[reg-off]) | (b[reg-off+1] << 8) )
435 switch (bmx_dev->cfg->orientation) {
437 accel_data.y = accel_x;
438 accel_data.x = accel_y;
439 accel_data.z = -accel_z;
440 gyro_data.y = gyro_x;
441 gyro_data.x = gyro_y;
442 gyro_data.z = -gyro_z;
445 accel_data.y = -accel_y;
446 accel_data.x = accel_x;
447 accel_data.z = -accel_z;
448 gyro_data.y = -gyro_y;
449 gyro_data.x = gyro_x;
450 gyro_data.z = -gyro_z;
453 accel_data.y = -accel_x;
454 accel_data.x = -accel_y;
455 accel_data.z = -accel_z;
456 gyro_data.y = -gyro_x;
457 gyro_data.x = -gyro_y;
458 gyro_data.z = -gyro_z;
461 accel_data.y = accel_y;
462 accel_data.x = -accel_x;
463 accel_data.z = -accel_z;
464 gyro_data.y = gyro_y;
465 gyro_data.x = -gyro_x;
466 gyro_data.z = -gyro_z;
469 accel_data.y = -accel_x;
470 accel_data.x = accel_y;
471 accel_data.z = accel_z;
472 gyro_data.y = -gyro_x;
473 gyro_data.x = gyro_y;
474 gyro_data.z = gyro_z;
477 accel_data.y = accel_y;
478 accel_data.x = accel_x;
479 accel_data.z = accel_z;
480 gyro_data.y = gyro_y;
481 gyro_data.x = gyro_x;
482 gyro_data.z = gyro_z;
485 accel_data.y = accel_x;
486 accel_data.x = -accel_y;
487 accel_data.z = accel_z;
488 gyro_data.y = gyro_x;
489 gyro_data.x = -gyro_y;
490 gyro_data.z = gyro_z;
493 accel_data.y = -accel_y;
494 accel_data.x = -accel_x;
495 accel_data.z = accel_z;
496 gyro_data.y = -gyro_y;
497 gyro_data.x = -gyro_x;
498 gyro_data.z = gyro_z;
510 accel_data.temperature = accel_temp;
516 gyro_data.temperature = accel_temp;
523 #endif // PIOS_INCLUDE_BMX055
#define BMX055_REG_ACC_Y_LSB
uint32_t PIOS_DELAY_DiffuS(uint32_t raw)
Subtract raw time from now and convert to us.
#define BMX055_REG_ACC_X_LSB
#define BMX055_REG_ACC_Z_LSB
#define BMX055_REG_ACC_PMU_BW
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
#define PIOS_SEMAPHORE_TIMEOUT_MAX
#define BMX055_REG_GYRO_CHIPID
#define BMX055_REG_ACC_TEMP
void PIOS_SENSORS_SetMaxGyro(int32_t rate)
Set the maximum gyro rate in deg/s.
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_BMX055_SPI_Init(pios_bmx055_dev_t *dev, pios_spi_t spi_id, uint32_t slave_gyro, uint32_t slave_accel, const struct pios_bmx055_cfg *cfg)
Initialize the BMX-xxxx 6/9-axis sensor on SPI.
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
#define BMX055_VAL_ACC_BGW_SOFTRESET_REQ
int32_t PIOS_EXTI_Init(const struct pios_exti_cfg *cfg)
#define BMX055_VAL_GYRO_CHIPID
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
#define BMX055_REG_GYRO_Z_LSB
#define BMX055_ACC_TEMP_OFFSET
void * PIOS_malloc(size_t size)
bool PIOS_Queue_Send(struct pios_queue *queuep, const void *itemp, uint32_t timeout_ms)
bool PIOS_Semaphore_Give_FromISR(struct pios_semaphore *sema, bool *woken)
#define BMX055_REG_GYRO_BGW_SOFTRESET
#define DEBUG_PRINTF(level,...)
#define BMX055_REG_ACC_BGW_SOFTRESET
void PIOS_Queue_Delete(struct pios_queue *queuep)
#define BMX055_VAL_ACC_PMU_RANGE_16G
void PIOS_EXTI_DeInit(const struct pios_exti_cfg *cfg)
static struct flyingpicmd_cfg_fa cfg
#define BMX055_VAL_GYRO_BGW_SOFTRESET_REQ
static float gyro_scale[3]
#define BMX055_REG_ACC_CHIPID
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
#define BMX055_VAL_ACC_PMU_BW_500HZ
static float accel_scale[3]
Pios sensor structure for generic accel data.
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)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
int32_t PIOS_SENSORS_Register(enum pios_sensor_type type, struct pios_queue *queue)
Register a queue-based sensor with the PIOS_SENSORS interface.
#define BMX055_REG_GYRO_Z_MSB
#define BMX055_REG_GYRO_X_LSB
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Pios sensor structure for generic gyro data.
struct pios_bmx055_dev * pios_bmx055_dev_t
#define BMX055_REG_ACC_PMU_RANGE
void PIOS_free(void *buf)
void PIOS_Thread_Sleep(uint32_t time_ms)
#define BMX055_REG_GYRO_RANGE
#define BMX055_REG_GYRO_BW
void PIOS_SENSORS_SetSampleRate(enum pios_sensor_type type, uint32_t sample_rate)
Set the sample rate of a sensor (Hz)
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
Includes PiOS and core architecture components.
#define BMX055_VAL_GYRO_RANGE_2000DPS
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
#define PIOS_Assert(test)
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
#define BMX055_REG_GYRO_Y_LSB
#define BMX055_VAL_GYRO_BW_116HZ
#define BMX055_VAL_ACC_CHIPID
uint32_t PIOS_DELAY_GetRaw()
Get the raw delay timer, useful for timing.