2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_HMC5x83 HMC5x83 Functions
6 * @brief Deals with the hardware interface to the magnetometers
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief HMC5x83 functions header.
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifndef PIOS_HMC5x83_H
32 #define PIOS_HMC5x83_H
34 #include <pios_sensors.h>
35 /* HMC5x83 Addresses */
36 #define PIOS_HMC5x83_I2C_ADDR 0x1E
37 #define PIOS_HMC5x83_I2C_READ_ADDR 0x3D
38 #define PIOS_HMC5x83_I2C_WRITE_ADDR 0x3C
40 #define PIOS_HMC5x83_SPI_READ_FLAG 0x80
41 #define PIOS_HMC5x83_SPI_AUTOINCR_FLAG 0x40
42 #define PIOS_HMC5x83_CONFIG_REG_A (uint8_t)0x00
43 #define PIOS_HMC5x83_CONFIG_REG_B (uint8_t)0x01
44 #define PIOS_HMC5x83_MODE_REG (uint8_t)0x02
45 #define PIOS_HMC5x83_DATAOUT_XMSB_REG 0x03
46 #define PIOS_HMC5x83_DATAOUT_XLSB_REG 0x04
47 #define PIOS_HMC5x83_DATAOUT_ZMSB_REG 0x05
48 #define PIOS_HMC5x83_DATAOUT_ZLSB_REG 0x06
49 #define PIOS_HMC5x83_DATAOUT_YMSB_REG 0x07
50 #define PIOS_HMC5x83_DATAOUT_YLSB_REG 0x08
51 #define PIOS_HMC5x83_DATAOUT_STATUS_REG 0x09
52 #define PIOS_HMC5x83_DATAOUT_IDA_REG 0x0A
53 #define PIOS_HMC5x83_DATAOUT_IDB_REG 0x0B
54 #define PIOS_HMC5x83_DATAOUT_IDC_REG 0x0C
56 /* Output Data Rate */
57 #define PIOS_HMC5x83_ODR_0_75 0x00
58 #define PIOS_HMC5x83_ODR_1_5 0x04
59 #define PIOS_HMC5x83_ODR_3 0x08
60 #define PIOS_HMC5x83_ODR_7_5 0x0C
61 #define PIOS_HMC5x83_ODR_15 0x10
62 #define PIOS_HMC5x83_ODR_30 0x14
63 #define PIOS_HMC5x83_ODR_75 0x18
65 /* Measure configuration */
66 #define PIOS_HMC5x83_MEASCONF_NORMAL 0x00
67 #define PIOS_HMC5x83_MEASCONF_BIAS_POS 0x01
68 #define PIOS_HMC5x83_MEASCONF_BIAS_NEG 0x02
71 #define PIOS_HMC5x83_GAIN_0_88 0x00
72 #define PIOS_HMC5x83_GAIN_1_3 0x20
73 #define PIOS_HMC5x83_GAIN_1_9 0x40
74 #define PIOS_HMC5x83_GAIN_2_5 0x60
75 #define PIOS_HMC5x83_GAIN_4_0 0x80
76 #define PIOS_HMC5x83_GAIN_4_7 0xA0
77 #define PIOS_HMC5x83_GAIN_5_6 0xC0
78 #define PIOS_HMC5x83_GAIN_8_1 0xE0
80 #define PIOS_HMC5x83_CTRLA_TEMP 0x40
83 #define PIOS_HMC5x83_MODE_CONTINUOUS 0x00
84 #define PIOS_HMC5x83_MODE_SINGLE 0x01
85 #define PIOS_HMC5x83_MODE_IDLE 0x02
86 #define PIOS_HMC5x83_MODE_SLEEP 0x03
88 /* Sensitivity Conversion Values */
89 #define PIOS_HMC5x83_Sensitivity_0_88Ga 1370 // LSB/Ga
90 #define PIOS_HMC5x83_Sensitivity_1_3Ga 1090 // LSB/Ga
91 #define PIOS_HMC5x83_Sensitivity_1_9Ga 820 // LSB/Ga
92 #define PIOS_HMC5x83_Sensitivity_2_5Ga 660 // LSB/Ga
93 #define PIOS_HMC5x83_Sensitivity_4_0Ga 440 // LSB/Ga
94 #define PIOS_HMC5x83_Sensitivity_4_7Ga 390 // LSB/Ga
95 #define PIOS_HMC5x83_Sensitivity_5_6Ga 330 // LSB/Ga
96 #define PIOS_HMC5x83_Sensitivity_8_1Ga 230 // LSB/Ga --> NOT RECOMMENDED
99 #define PIOS_HMC5x83_DATAOUT_STATUS_RDY 0x01
101 typedef uintptr_t pios_hmc5x83_dev_t
;
103 struct pios_hmc5x83_io_driver
{
104 int32_t (*Write
)(pios_hmc5x83_dev_t handler
, uint8_t address
, uint8_t buffer
);
105 int32_t (*Read
)(pios_hmc5x83_dev_t handler
, uint8_t address
, uint8_t *buffer
, uint8_t len
);
108 #ifdef PIOS_INCLUDE_SPI
109 extern const struct pios_hmc5x83_io_driver PIOS_HMC5x83_SPI_DRIVER
;
112 #ifdef PIOS_INCLUDE_I2C
113 extern const struct pios_hmc5x83_io_driver PIOS_HMC5x83_I2C_DRIVER
;
115 // xyz axis orientation
116 enum PIOS_HMC5X83_ORIENTATION
{
117 PIOS_HMC5X83_ORIENTATION_EAST_NORTH_UP
,
118 PIOS_HMC5X83_ORIENTATION_SOUTH_EAST_UP
,
119 PIOS_HMC5X83_ORIENTATION_WEST_SOUTH_UP
,
120 PIOS_HMC5X83_ORIENTATION_NORTH_WEST_UP
,
121 PIOS_HMC5X83_ORIENTATION_EAST_SOUTH_DOWN
,
122 PIOS_HMC5X83_ORIENTATION_SOUTH_WEST_DOWN
,
123 PIOS_HMC5X83_ORIENTATION_WEST_NORTH_DOWN
,
124 PIOS_HMC5X83_ORIENTATION_NORTH_EAST_DOWN
,
128 struct pios_hmc5x83_cfg
{
129 #ifdef PIOS_HMC5X83_HAS_GPIOS
130 const struct pios_exti_cfg
*exti_cfg
; /* Pointer to the EXTI configuration */
132 uint8_t M_ODR
; // OUTPUT DATA RATE --> here below the relative define (See datasheet page 11 for more details) */
133 uint8_t Meas_Conf
; // Measurement Configuration,: Normal, positive bias, or negative bias --> here below the relative define */
134 uint8_t Gain
; // Gain Configuration, select the full scale --> here below the relative define (See datasheet page 11 for more details) */
136 bool TempCompensation
; // enable temperature sensor on HMC5983 for temperature gain compensation
137 enum PIOS_HMC5X83_ORIENTATION Orientation
;
138 const struct pios_hmc5x83_io_driver
*Driver
;
141 /* Public Functions */
142 extern pios_hmc5x83_dev_t
PIOS_HMC5x83_Init(const struct pios_hmc5x83_cfg
*cfg
, uint32_t port_id
, uint8_t device_num
);
143 extern void PIOS_HMC5x83_Register(pios_hmc5x83_dev_t handler
, PIOS_SENSORS_TYPE sensortype
);
145 extern bool PIOS_HMC5x83_NewDataAvailable(pios_hmc5x83_dev_t handler
);
146 extern int32_t PIOS_HMC5x83_ReadMag(pios_hmc5x83_dev_t handler
, int16_t out
[3]);
147 extern uint8_t PIOS_HMC5x83_ReadID(pios_hmc5x83_dev_t handler
, uint8_t out
[4]);
148 extern int32_t PIOS_HMC5x83_Test(pios_hmc5x83_dev_t handler
);
149 extern bool PIOS_HMC5x83_IRQHandler(pios_hmc5x83_dev_t handler
);
151 extern const PIOS_SENSORS_Driver PIOS_HMC5x83_Driver
;
153 #endif /* PIOS_HMC5x83_H */