34 #if defined(PIOS_INCLUDE_HMC5983)
41 #define HMC5983_TASK_PRIORITY PIOS_THREAD_PRIO_HIGHEST
42 #define HMC5983_TASK_STACK_BYTES 512
43 #define PIOS_HMC5983_MAX_DOWNSAMPLE 1
48 enum pios_hmc5983_dev_magic {
49 PIOS_HMC5983_DEV_MAGIC = 0x3e8f18ac,
57 struct pios_thread *
task;
59 enum pios_hmc5983_dev_magic
magic;
64 static int32_t PIOS_HMC5983_ClaimBus(
void);
65 static int32_t PIOS_HMC5983_ReleaseBus(
void);
66 static int32_t PIOS_HMC5983_Read(uint8_t address, uint8_t *buffer, uint8_t len);
67 static int32_t PIOS_HMC5983_Write(uint8_t address, uint8_t buffer);
68 static void PIOS_HMC5983_Task(
void *parameters);
71 static struct hmc5983_dev *dev;
76 static struct hmc5983_dev *PIOS_HMC5983_alloc(
void) {
77 struct hmc5983_dev *hmc5983_dev;
79 hmc5983_dev = (
struct hmc5983_dev *)
PIOS_malloc(
sizeof(*hmc5983_dev));
80 if (!hmc5983_dev)
return (NULL);
82 hmc5983_dev->magic = PIOS_HMC5983_DEV_MAGIC;
85 if (hmc5983_dev->queue == NULL) {
91 if (hmc5983_dev->data_ready_sema == NULL) {
103 static int32_t PIOS_HMC5983_Validate(
struct hmc5983_dev *dev)
107 if (dev->magic != PIOS_HMC5983_DEV_MAGIC)
109 if (dev->spi_id == 0)
120 dev = (
struct hmc5983_dev *)PIOS_HMC5983_alloc();
125 dev->spi_id = spi_id;
126 dev->slave_num = slave_num;
128 #ifdef PIOS_HMC5983_HAS_GPIOS
132 if (PIOS_HMC5983_Config(cfg) != 0)
137 dev->task =
PIOS_Thread_Create(PIOS_HMC5983_Task,
"pios_hmc5983", HMC5983_TASK_STACK_BYTES, NULL, HMC5983_TASK_PRIORITY);
218 static uint8_t CTRLB = 0x00;
221 uint8_t CTRLA = 0x00;
226 CTRLB |= (uint8_t)(cfg->
Gain);
227 MODE |= (uint8_t)(cfg->
Mode);
251 if (PIOS_HMC5983_Validate(dev) != 0)
260 switch (CTRLB & 0xE0) {
289 int16_t mag_x, mag_y, mag_z;
290 mag_x = ((int16_t)((uint16_t) buffer[0] << 8) + buffer[1]) * 1000 / sensitivity;
291 mag_z = ((int16_t)((uint16_t) buffer[2] << 8) + buffer[3]) * 1000 / sensitivity;
292 mag_y = ((int16_t)((uint16_t) buffer[4] << 8) + buffer[5]) * 1000 / sensitivity;
295 switch (dev->cfg->Orientation) {
297 mag_data->
x = -mag_x;
299 mag_data->
z = -mag_z;
302 mag_data->
x = -mag_y;
303 mag_data->
y = -mag_x;
304 mag_data->
z = -mag_z;
308 mag_data->
y = -mag_y;
309 mag_data->
z = -mag_z;
314 mag_data->
z = -mag_z;
317 mag_data->
x = -mag_x;
318 mag_data->
y = -mag_y;
323 mag_data->
y = -mag_x;
332 mag_data->
x = -mag_y;
338 #if PIOS_HMC5983_READ_TEMPERATURE
339 uint8_t temperature[2];
340 float mag_temperature;
342 mag_temperature = (((uint16_t)temperature[0] << 8) + temperature[1]) / 128 + 25;
356 static uint8_t PIOS_HMC5983_ReadID(uint8_t out[4])
367 static int32_t PIOS_HMC5983_ClaimBus(
void)
369 if (PIOS_HMC5983_Validate(dev) != 0)
383 static int32_t PIOS_HMC5983_ReleaseBus(
void)
385 if (PIOS_HMC5983_Validate(dev) != 0)
402 static int32_t PIOS_HMC5983_Read(uint8_t address, uint8_t *buffer, uint8_t len)
404 if (PIOS_HMC5983_ClaimBus() != 0)
409 PIOS_HMC5983_ReleaseBus();
413 return PIOS_HMC5983_ReleaseBus();
424 static int32_t PIOS_HMC5983_Write(uint8_t address, uint8_t buffer)
426 if (PIOS_HMC5983_ClaimBus() != 0)
432 return PIOS_HMC5983_ReleaseBus();
439 int32_t PIOS_HMC5983_Test(
void)
442 uint8_t registers[3] = { 0, 0, 0 };
451 PIOS_HMC5983_ReadID((uint8_t *)
id);
452 if ((
id[0] !=
'H') || (
id[1] !=
'4') || (
id[2] !=
'3'))
465 if (PIOS_HMC5983_ReadMag(&
values) != 0)
492 if (PIOS_HMC5983_ReadMag(&
values) != 0)
519 if (PIOS_HMC5983_Validate(dev) != 0)
531 static void PIOS_HMC5983_Task(
void *parameters)
534 if (PIOS_HMC5983_Validate(dev) != 0) {
545 if (PIOS_HMC5983_ReadMag(&mag_data) == 0)
#define PIOS_HMC5983_MEASCONF_BIAS_POS
#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.
#define PIOS_HMC5983_ENABLE_TEMP_SENSOR
#define PIOS_HMC5983_READ_MODE
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
int32_t PIOS_EXTI_Init(const struct pios_exti_cfg *cfg)
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
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_DATAOUT_STATUS_REG
#define PIOS_HMC5983_Sensitivity_5_6Ga
#define PIOS_HMC5983_MODE_REG
static struct flyingpicmd_cfg_fa cfg
#define PIOS_HMC5983_Sensitivity_1_3Ga
#define PIOS_HMC5983_Sensitivity_8_1Ga
const struct pios_exti_cfg * exti_cfg
#define PIOS_HMC5983_MODE_CONTINUOUS
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
bool PIOS_HMC5983_IRQHandler(void)
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.
static TaskInfoRunningElem task
#define PIOS_HMC5983_MODE_SINGLE
#define PIOS_HMC5983_Sensitivity_0_88Ga
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_Sensitivity_2_5Ga
#define PIOS_HMC5983_DATAOUT_IDA_REG
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
static struct pios_queue * queue
uint16_t values[FPPROTO_MAX_SERVOS]
#define PIOS_HMC5983_DATAOUT_TEMPMSB_REG
#define PIOS_HMC5983_CONFIG_REG_A
#define PIOS_Assert(test)
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
#define PIOS_HMC5983_GAIN_8_1
#define PIOS_HMC5983_Sensitivity_1_9Ga
#define PIOS_HMC5983_MODE_IDLE
#define PIOS_HMC5983_ODR_15
int32_t PIOS_HMC5983_Init(pios_spi_t spi_id, uint32_t slave_num, const struct pios_hmc5983_cfg *cfg)
#define PIOS_HMC5983_CONFIG_REG_B