34 #if defined(PIOS_INCLUDE_HMC5983_I2C)
42 #define HMC5983_TASK_PRIORITY PIOS_THREAD_PRIO_HIGHEST
43 #define HMC5983_TASK_STACK_BYTES 512
44 #define PIOS_HMC5983_MAX_DOWNSAMPLE 1
49 enum pios_hmc5983_dev_magic {
50 PIOS_HMC5983_DEV_MAGIC = 0x3d8efeed,
57 struct pios_thread *
task;
59 enum pios_hmc5983_dev_magic
magic;
65 static int32_t PIOS_HMC5983_Read(uint8_t address, uint8_t * buffer, uint8_t len);
66 static int32_t PIOS_HMC5983_Write(uint8_t address, uint8_t buffer);
67 static void PIOS_HMC5983_Task(
void *parameters);
69 static struct hmc5983_dev *dev;
74 static struct hmc5983_dev * PIOS_HMC5983_alloc(
void)
76 struct hmc5983_dev *hmc5983_dev;
78 hmc5983_dev = (
struct hmc5983_dev *)
PIOS_malloc(
sizeof(*hmc5983_dev));
79 if (!hmc5983_dev)
return (NULL);
81 hmc5983_dev->magic = PIOS_HMC5983_DEV_MAGIC;
84 if (hmc5983_dev->queue == NULL) {
96 static int32_t PIOS_HMC5983_Validate(
struct hmc5983_dev *dev)
100 if (dev->magic != PIOS_HMC5983_DEV_MAGIC)
102 if (dev->i2c_id == 0)
111 int32_t PIOS_HMC5983_I2C_Init(
pios_i2c_t i2c_id,
114 dev = (
struct hmc5983_dev *) PIOS_HMC5983_alloc();
119 dev->i2c_id = i2c_id;
122 #ifdef PIOS_HMC5983_HAS_GPIOS
131 dev->data_ready_sema = NULL;
134 dev->data_ready_sema = NULL;
137 if (PIOS_HMC5983_Config(cfg) != 0)
142 dev->task =
PIOS_Thread_Create(PIOS_HMC5983_Task,
"pios_hmc5983", HMC5983_TASK_STACK_BYTES, NULL, HMC5983_TASK_PRIORITY);
155 if (PIOS_HMC5983_Validate(dev) != 0)
158 dev->orientation = orientation;
249 static uint16_t PIOS_HMC5983_Config_GetSensitivity()
251 switch (dev->cfg->Gain) {
281 if (PIOS_HMC5983_Validate(dev) != 0)
289 uint8_t buffer_read[8];
291 if (temperature == NULL)
292 n_read =
sizeof(buffer_read) - 2;
294 n_read =
sizeof(buffer_read);
298 uint8_t buffer_write[2] = {
308 .len =
sizeof(addr_read),
322 .len =
sizeof(buffer_write),
330 int16_t mag_x, mag_y, mag_z;
331 uint16_t sensitivity = PIOS_HMC5983_Config_GetSensitivity();
332 mag_x = ((int16_t) ((uint16_t)buffer_read[0] << 8) + buffer_read[1]) * 1000 / sensitivity;
333 mag_z = ((int16_t) ((uint16_t)buffer_read[2] << 8) + buffer_read[3]) * 1000 / sensitivity;
334 mag_y = ((int16_t) ((uint16_t)buffer_read[4] << 8) + buffer_read[5]) * 1000 / sensitivity;
337 switch (dev->orientation) {
339 mag_data->
x = -mag_x;
341 mag_data->
z = -mag_z;
344 mag_data->
x = -mag_y;
345 mag_data->
y = -mag_x;
346 mag_data->
z = -mag_z;
350 mag_data->
y = -mag_y;
351 mag_data->
z = -mag_z;
356 mag_data->
z = -mag_z;
359 mag_data->
x = -mag_x;
360 mag_data->
y = -mag_y;
365 mag_data->
y = -mag_x;
374 mag_data->
x = -mag_y;
381 if (temperature != NULL) {
382 *temperature = (float)(((uint16_t)buffer_read[6] << 8) + buffer_read[7]) / 128.f + 25.f;
393 static uint8_t PIOS_HMC5983_ReadID(uint8_t out[4])
410 static int32_t PIOS_HMC5983_Read(uint8_t address, uint8_t * buffer, uint8_t
len)
412 if(PIOS_HMC5983_Validate(dev) != 0)
415 uint8_t addr_buffer[] = {
424 .len =
sizeof(addr_buffer),
448 static int32_t PIOS_HMC5983_Write(uint8_t address, uint8_t buffer)
450 if(PIOS_HMC5983_Validate(dev) != 0)
476 int32_t PIOS_HMC5983_Test(
void)
480 PIOS_HMC5983_ReadID((uint8_t *)
id);
481 if((
id[0] !=
'H') || (
id[1] !=
'4') || (
id[2] !=
'3'))
492 if (PIOS_HMC5983_Validate(dev) != 0)
504 static void PIOS_HMC5983_Task(
void *parameters)
506 while (PIOS_HMC5983_Validate(dev) != 0) {
510 uint32_t sample_delay;
512 switch (dev->cfg->M_ODR) {
514 sample_delay = 1000 / 0.75f + 0.99999f;
517 sample_delay = 1000 / 1.5f + 0.99999f;
520 sample_delay = 1000 / 3.0f + 0.99999f;
523 sample_delay = 1000 / 7.5f + 0.99999f;
526 sample_delay = 1000 / 15.0f + 0.99999f;
529 sample_delay = 1000 / 30.0f + 0.99999f;
533 sample_delay = 1000 / 75.0f + 0.99999f;
550 if (PIOS_HMC5983_ReadMag(&mag_data, NULL) == 0)
uint32_t PIOS_Thread_Systime(void)
#define PIOS_HMC5983_Sensitivity_4_7Ga
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
#define PIOS_SEMAPHORE_TIMEOUT_MAX
Main PiOS header to include all the compiled in PiOS options.
HMC5983 functions header.
int32_t PIOS_EXTI_Init(const struct pios_exti_cfg *cfg)
#define PIOS_HMC5983_I2C_ADDR
#define PIOS_HMC5983_ODR_1_5
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 PIOS_HMC5983_GAIN_4_0
#define PIOS_HMC5983_Sensitivity_5_6Ga
uint8_t data[XFER_BYTES_PER_PACKET]
#define PIOS_HMC5983_MODE_REG
static struct flyingpicmd_cfg_fa cfg
enum pios_hmc5983_orientation Orientation
#define PIOS_HMC5983_Sensitivity_1_3Ga
#define PIOS_HMC5983_GAIN_1_3
#define PIOS_HMC5983_Sensitivity_8_1Ga
const struct pios_exti_cfg * exti_cfg
#define PIOS_HMC5983_GAIN_0_88
#define PIOS_HMC5983_MODE_CONTINUOUS
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
#define PIOS_HMC5983_ODR_3
struct pios_i2c_adapter * pios_i2c_t
bool PIOS_HMC5983_IRQHandler(void)
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.
static TaskInfoRunningElem task
#define PIOS_HMC5983_ODR_75
#define PIOS_HMC5983_ODR_0_75
int32_t PIOS_I2C_Transfer(pios_i2c_t i2c_id, const struct pios_i2c_txn txn_list[], uint32_t num_txns)
#define PIOS_HMC5983_Sensitivity_0_88Ga
#define PIOS_HMC5983_GAIN_1_9
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
#define PIOS_HMC5983_GAIN_5_6
void PIOS_free(void *buf)
void PIOS_Thread_Sleep(uint32_t time_ms)
Pios sensor structure for generic mag data.
#define PIOS_HMC5983_Sensitivity_4_0Ga
#define PIOS_HMC5983_DATAOUT_XMSB_REG
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
#define PIOS_HMC5983_ODR_7_5
#define PIOS_HMC5983_Sensitivity_2_5Ga
#define PIOS_HMC5983_DATAOUT_IDA_REG
static struct pios_queue * queue
#define PIOS_HMC5983_GAIN_2_5
#define PIOS_HMC5983_CONFIG_REG_A
#define PIOS_HMC5983_ODR_30
#define PIOS_Assert(test)
#define PIOS_HMC5983_GAIN_4_7
#define PIOS_HMC5983_GAIN_8_1
#define PIOS_HMC5983_Sensitivity_1_9Ga
#define PIOS_HMC5983_ODR_15
#define PIOS_HMC5983_CONFIG_REG_B