embkernel
 All Classes Functions Variables Typedefs Groups Pages
LSM303DLHC.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef LSM303DLHC_HPP_
7 #define LSM303DLHC_HPP_
8 
9 #include "DrvI2cMaster.hpp"
10 
11 class LSM303DLHC {
12 public:
13  LSM303DLHC(DrvI2cMaster* mI2c);
14  ~LSM303DLHC();
15 
16  void init();
17  bool readAccXyz(float* x, float* y, float* z);
18 
19 private:
20  //Slave address
21  constexpr static uint8_t ACC_I2C_ADDRESS = 0x32;
22  constexpr static uint8_t MAG_I2C_ADDRESS = 0x3C;
23 
24  //Registers
25  constexpr static uint8_t LSM303DLHC_CTRL_REG1_A = 0x20; // Control register 1 acceleration
26  constexpr static uint8_t LSM303DLHC_CTRL_REG2_A = 0x21; // Control register 2 acceleration
27  constexpr static uint8_t LSM303DLHC_CTRL_REG3_A = 0x22; // Control register 3 acceleration
28  constexpr static uint8_t LSM303DLHC_CTRL_REG4_A = 0x23; // Control register 4 acceleration
29  constexpr static uint8_t LSM303DLHC_CTRL_REG5_A = 0x24; // Control register 5 acceleration
30  constexpr static uint8_t LSM303DLHC_CTRL_REG6_A = 0x25; // Control register 6 acceleration
31  constexpr static uint8_t LSM303DLHC_REFERENCE_A = 0x26; // Reference register acceleration
32  constexpr static uint8_t LSM303DLHC_STATUS_REG_A = 0x27; // Status register acceleration
33  constexpr static uint8_t LSM303DLHC_OUT_X_L_A = 0x28; // Output Register X acceleration
34  constexpr static uint8_t LSM303DLHC_OUT_X_H_A = 0x29; // Output Register X acceleration
35  constexpr static uint8_t LSM303DLHC_OUT_Y_L_A = 0x2A; // Output Register Y acceleration
36  constexpr static uint8_t LSM303DLHC_OUT_Y_H_A = 0x2B; // Output Register Y acceleration
37  constexpr static uint8_t LSM303DLHC_OUT_Z_L_A = 0x2C; // Output Register Z acceleration
38  constexpr static uint8_t LSM303DLHC_OUT_Z_H_A = 0x2D; // Output Register Z acceleration
39  constexpr static uint8_t LSM303DLHC_FIFO_CTRL_REG_A = 0x2E; // Fifo control Register acceleration
40  constexpr static uint8_t LSM303DLHC_FIFO_SRC_REG_A = 0x2F; // Fifo src Register acceleration
41  constexpr static uint8_t LSM303DLHC_INT1_CFG_A = 0x30; // Interrupt 1 configuration Register acceleration
42  constexpr static uint8_t LSM303DLHC_INT1_SOURCE_A = 0x31; // Interrupt 1 source Register acceleration
43  constexpr static uint8_t LSM303DLHC_INT1_THS_A = 0x32; // Interrupt 1 Threshold register acceleration
44  constexpr static uint8_t LSM303DLHC_INT1_DURATION_A = 0x33; // Interrupt 1 DURATION register acceleration
45  constexpr static uint8_t LSM303DLHC_INT2_CFG_A = 0x34; // Interrupt 2 configuration Register acceleration
46  constexpr static uint8_t LSM303DLHC_INT2_SOURCE_A = 0x35; // Interrupt 2 source Register acceleration
47  constexpr static uint8_t LSM303DLHC_INT2_THS_A = 0x36; // Interrupt 2 Threshold register acceleration
48  constexpr static uint8_t LSM303DLHC_INT2_DURATION_A = 0x37; // Interrupt 2 DURATION register acceleration
49  constexpr static uint8_t LSM303DLHC_CLICK_CFG_A = 0x38; // Click configuration Register acceleration
50  constexpr static uint8_t LSM303DLHC_CLICK_SOURCE_A = 0x39; // Click 2 source Register acceleration
51  constexpr static uint8_t LSM303DLHC_CLICK_THS_A = 0x3A; // Click 2 Threshold register acceleration
52  constexpr static uint8_t LSM303DLHC_TIME_LIMIT_A = 0x3B; // Time Limit Register acceleration
53  constexpr static uint8_t LSM303DLHC_TIME_LATENCY_A = 0x3C; // Time Latency Register acceleration
54  constexpr static uint8_t LSM303DLHC_TIME_WINDOW_A = 0x3D; // Time window register acceleration
55  constexpr static uint8_t LSM303DLHC_CRA_REG_M = 0x00; // Control register A magnetic field
56  constexpr static uint8_t LSM303DLHC_CRB_REG_M = 0x01; // Control register B magnetic field
57  constexpr static uint8_t LSM303DLHC_MR_REG_M = 0x02; // Control register MR magnetic field
58  constexpr static uint8_t LSM303DLHC_OUT_X_H_M = 0x03; // Output Register X magnetic field
59  constexpr static uint8_t LSM303DLHC_OUT_X_L_M = 0x04; // Output Register X magnetic field
60  constexpr static uint8_t LSM303DLHC_OUT_Z_H_M = 0x05; // Output Register Z magnetic field
61  constexpr static uint8_t LSM303DLHC_OUT_Z_L_M = 0x06; // Output Register Z magnetic field
62  constexpr static uint8_t LSM303DLHC_OUT_Y_H_M = 0x07; // Output Register Y magnetic field
63  constexpr static uint8_t LSM303DLHC_OUT_Y_L_M = 0x08; // Output Register Y magnetic field
64  constexpr static uint8_t LSM303DLHC_SR_REG_M = 0x09; // Status Register magnetic field
65  constexpr static uint8_t LSM303DLHC_IRA_REG_M = 0x0A; // IRA Register magnetic field
66  constexpr static uint8_t LSM303DLHC_IRB_REG_M = 0x0B; // IRB Register magnetic field
67  constexpr static uint8_t LSM303DLHC_IRC_REG_M = 0x0C; // IRC Register magnetic field
68  constexpr static uint8_t LSM303DLHC_TEMP_OUT_H_M = 0x31; // Temperature Register magnetic field
69  constexpr static uint8_t LSM303DLHC_TEMP_OUT_L_M = 0x32; // Temperature Register magnetic field
70 
71  //Acc_Power_Mode_selection
72  constexpr static uint8_t LSM303DLHC_NORMAL_MODE = 0x00;
73  constexpr static uint8_t LSM303DLHC_LOWPOWER_MODE = 0x08;
74 
75  //Acc_OutPut_DataRate_Selection
76  constexpr static uint8_t LSM303DLHC_ODR_1_HZ = 0x10; // Output Data Rate = 1 Hz
77  constexpr static uint8_t LSM303DLHC_ODR_10_HZ = 0x20; // Output Data Rate = 10 Hz
78  constexpr static uint8_t LSM303DLHC_ODR_25_HZ = 0x30; // Output Data Rate = 25 Hz
79  constexpr static uint8_t LSM303DLHC_ODR_50_HZ = 0x40; // Output Data Rate = 50 Hz
80  constexpr static uint8_t LSM303DLHC_ODR_100_HZ = 0x50; // Output Data Rate = 100 Hz
81  constexpr static uint8_t LSM303DLHC_ODR_200_HZ = 0x60; // Output Data Rate = 200 Hz
82  constexpr static uint8_t LSM303DLHC_ODR_400_HZ = 0x70; // Output Data Rate = 400 Hz
83  constexpr static uint8_t LSM303DLHC_ODR_1620_HZ_LP = 0x80; // Output Data Rate = 1620 Hz only in Low Power Mode
84  constexpr static uint8_t LSM303DLHC_ODR_1344_HZ = 0x90; // Output Data Rate = 1344 Hz in Normal mode and 5376 Hz in Low Power Mode
85 
86  //Acc_Axes_Selection
87  constexpr static uint8_t LSM303DLHC_X_ENABLE = 0x01;
88  constexpr static uint8_t LSM303DLHC_Y_ENABLE = 0x02;
89  constexpr static uint8_t LSM303DLHC_Z_ENABLE = 0x04;
90  constexpr static uint8_t LSM303DLHC_AXES_ENABLE = 0x07;
91  constexpr static uint8_t LSM303DLHC_AXES_DISABLE = 0x00;
92 
93  //Acc_High_Resolution
94  constexpr static uint8_t LSM303DLHC_HR_ENABLE = 0x08;
95  constexpr static uint8_t LSM303DLHC_HR_DISABLE = 0x00;
96 
97  //Acc_Full_Scale_Selection
98  constexpr static uint8_t LSM303DLHC_FULLSCALE_2G = 0x00; // ±2 g
99  constexpr static uint8_t LSM303DLHC_FULLSCALE_4G = 0x10; // ±4 g
100  constexpr static uint8_t LSM303DLHC_FULLSCALE_8G = 0x20; // ±8 g
101  constexpr static uint8_t LSM303DLHC_FULLSCALE_16G = 0x30; // ±16 g
102 
103  //Acc_Block_Data_Update
104  constexpr static uint8_t LSM303DLHC_BlockUpdate_Continous = 0x00; // Continuos Update
105  constexpr static uint8_t LSM303DLHC_BlockUpdate_Single = 0x80; // Single Update: output registers not updated until MSB and LSB reading
106 
107  //Acc_Endian_Data_selection
108  constexpr static uint8_t LSM303DLHC_BLE_LSB = 0x00; // Little Endian: data LSB @ lower address
109  constexpr static uint8_t LSM303DLHC_BLE_MSB = 0x40; // Big Endian: data MSB @ lower address
110 
111  //Acc_Boot_Mode_selection
112  constexpr static uint8_t LSM303DLHC_BOOT_NORMALMODE = 0x00;
113  constexpr static uint8_t LSM303DLHC_BOOT_REBOOTMEMORY = 0x80;
114 
115  //Acc_High_Pass_Filter_Mode
116  constexpr static uint8_t LSM303DLHC_HPM_NORMAL_MODE_RES = 0x00;
117  constexpr static uint8_t LSM303DLHC_HPM_REF_SIGNAL = 0x40;
118  constexpr static uint8_t LSM303DLHC_HPM_NORMAL_MODE = 0x80;
119  constexpr static uint8_t LSM303DLHC_HPM_AUTORESET_INT = 0xC0;
120 
121  //Acc_High_Pass_CUT OFF_Frequency
122  constexpr static uint8_t LSM303DLHC_HPFCF_8 = 0x00;
123  constexpr static uint8_t LSM303DLHC_HPFCF_16 = 0x10;
124  constexpr static uint8_t LSM303DLHC_HPFCF_32 = 0x20;
125  constexpr static uint8_t LSM303DLHC_HPFCF_64 = 0x30;
126 
127  //Acc_High_Pass_Filter_status
128  constexpr static uint8_t LSM303DLHC_HIGHPASSFILTER_DISABLE = 0x00;
129  constexpr static uint8_t LSM303DLHC_HIGHPASSFILTER_ENABLE = 0x08;
130 
131  //Acc_High_Pass_Filter_Click_status
132  constexpr static uint8_t LSM303DLHC_HPF_CLICK_DISABLE = 0x00;
133  constexpr static uint8_t LSM303DLHC_HPF_CLICK_ENABLE = 0x04;
134 
135  //Acc_High_Pass_Filter_AOI1_status
136  constexpr static uint8_t LSM303DLHC_HPF_AOI1_DISABLE = 0x00;
137  constexpr static uint8_t LSM303DLHC_HPF_AOI1_ENABLE = 0x01;
138 
139  //Acc_High_Pass_Filter_AOI2_status
140  constexpr static uint8_t LSM303DLHC_HPF_AOI2_DISABLE = 0x00;
141  constexpr static uint8_t LSM303DLHC_HPF_AOI2_ENABLE = 0x02;
142 
143  //Acc_LSM303DLHC_Interrupt1_Configuration_definition
144  constexpr static uint8_t LSM303DLHC_IT1_CLICK = 0x80;
145  constexpr static uint8_t LSM303DLHC_IT1_AOI1 = 0x40;
146  constexpr static uint8_t LSM303DLHC_IT1_AOI2 = 0x20;
147  constexpr static uint8_t LSM303DLHC_IT1_DRY1 = 0x10;
148  constexpr static uint8_t LSM303DLHC_IT1_DRY2 = 0x08;
149  constexpr static uint8_t LSM303DLHC_IT1_WTM = 0x04;
150  constexpr static uint8_t LSM303DLHC_IT1_OVERRUN = 0x02;
151 
152  //Acc_LSM303DLHC_Interrupt2_Configuration_definition
153  constexpr static uint8_t LSM303DLHC_IT2_CLICK = 0x80;
154  constexpr static uint8_t LSM303DLHC_IT2_INT1 = 0x40;
155  constexpr static uint8_t LSM303DLHC_IT2_INT2 = 0x20;
156  constexpr static uint8_t LSM303DLHC_IT2_BOOT = 0x10;
157  constexpr static uint8_t LSM303DLHC_IT2_ACT = 0x08;
158  constexpr static uint8_t LSM303DLHC_IT2_HLACTIVE = 0x02;
159 
160  //Acc_INT_Combination_Status
161  constexpr static uint8_t LSM303DLHC_OR_COMBINATION = 0x00; // OR combination of enabled IRQs
162  constexpr static uint8_t LSM303DLHC_AND_COMBINATION = 0x80; // AND combination of enabled IRQs
163  constexpr static uint8_t LSM303DLHC_MOV_RECOGNITION = 0x40; // 6D movement recognition
164  constexpr static uint8_t LSM303DLHC_POS_RECOGNITION = 0xC0; // 6D position recognition
165 
166  //Acc_INT_Axes
167  constexpr static uint8_t LSM303DLHC_Z_HIGH = 0x20; // Z High enabled IRQs
168  constexpr static uint8_t LSM303DLHC_Z_LOW = 0x10; // Z low enabled IRQs
169  constexpr static uint8_t LSM303DLHC_Y_HIGH = 0x08; // Y High enabled IRQs
170  constexpr static uint8_t LSM303DLHC_Y_LOW = 0x04; // Y low enabled IRQs
171  constexpr static uint8_t LSM303DLHC_X_HIGH = 0x02; // X High enabled IRQs
172  constexpr static uint8_t LSM303DLHC_X_LOW = 0x01; // X low enabled IRQs
173 
174  //Acc_INT_Click
175  constexpr static uint8_t LSM303DLHC_Z_DOUBLE_CLICK = 0x20; // Z double click IRQs
176  constexpr static uint8_t LSM303DLHC_Z_SINGLE_CLICK = 0x10; // Z single click IRQs
177  constexpr static uint8_t LSM303DLHC_Y_DOUBLE_CLICK = 0x08; // Y double click IRQs
178  constexpr static uint8_t LSM303DLHC_Y_SINGLE_CLICK = 0x04; // Y single click IRQs
179  constexpr static uint8_t LSM303DLHC_X_DOUBLE_CLICK = 0x02; // X double click IRQs
180  constexpr static uint8_t LSM303DLHC_X_SINGLE_CLICK = 0x01; // X single click IRQs
181 
182  //Acc_INT1_Interrupt_status
183  constexpr static uint8_t LSM303DLHC_INT1INTERRUPT_DISABLE = 0x00;
184  constexpr static uint8_t LSM303DLHC_INT1INTERRUPT_ENABLE = 0x80;
185 
186  //Acc_INT1_Interrupt_ActiveEdge
187  constexpr static uint8_t LSM303DLHC_INT1INTERRUPT_LOW_EDGE = 0x20;
188  constexpr static uint8_t LSM303DLHC_INT1INTERRUPT_HIGH_EDGE = 0x00;
189 
190  //Mag_Data_Rate
191  constexpr static uint8_t LSM303DLHC_ODR_0_75_HZ = 0x00; // Output Data Rate = 0.75 Hz
192  constexpr static uint8_t LSM303DLHC_ODR_1_5_HZ = 0x04; // Output Data Rate = 1.5 Hz
193  constexpr static uint8_t LSM303DLHC_ODR_3_0_HZ = 0x08; // Output Data Rate = 3 Hz
194  constexpr static uint8_t LSM303DLHC_ODR_7_5_HZ = 0x0C; // Output Data Rate = 7.5 Hz
195  constexpr static uint8_t LSM303DLHC_ODR_15_HZ = 0x10; // Output Data Rate = 15 Hz
196  constexpr static uint8_t LSM303DLHC_ODR_30_HZ = 0x14; // Output Data Rate = 30 Hz
197  constexpr static uint8_t LSM303DLHC_ODR_75_HZ = 0x18; // Output Data Rate = 75 Hz
198  constexpr static uint8_t LSM303DLHC_ODR_220_HZ = 0x1C; // Output Data Rate = 220 Hz
199 
200  //Mag_Full_Scale
201  constexpr static uint8_t LSM303DLHC_FS_1_3_GA = 0x20; // Full scale = ±1.3 Gauss
202  constexpr static uint8_t LSM303DLHC_FS_1_9_GA = 0x40; // Full scale = ±1.9 Gauss
203  constexpr static uint8_t LSM303DLHC_FS_2_5_GA = 0x60; // Full scale = ±2.5 Gauss
204  constexpr static uint8_t LSM303DLHC_FS_4_0_GA = 0x80; // Full scale = ±4.0 Gauss
205  constexpr static uint8_t LSM303DLHC_FS_4_7_GA = 0xA0; // Full scale = ±4.7 Gauss
206  constexpr static uint8_t LSM303DLHC_FS_5_6_GA = 0xC0; // Full scale = ±5.6 Gauss
207  constexpr static uint8_t LSM303DLHC_FS_8_1_GA = 0xE0; // Full scale = ±8.1 Gauss
208 
209  //Mag_Working_Mode
210  constexpr static uint8_t LSM303DLHC_CONTINUOS_CONVERSION = 0x00; // Continuous-Conversion Mode
211  constexpr static uint8_t LSM303DLHC_SINGLE_CONVERSION = 0x01; // Single-Conversion Mode
212  constexpr static uint8_t LSM303DLHC_SLEEP = 0x02; // Sleep Mode
213 
214  //Mag_Temperature_Sensor
215  constexpr static uint8_t LSM303DLHC_TEMPSENSOR_ENABLE = 0x80; // Temp sensor Enable
216  constexpr static uint8_t LSM303DLHC_TEMPSENSOR_DISABLE = 0x00; // Temp sensor Disable
217 
218  bool writeMagRegister(uint8_t reg, uint8_t value);
219  bool readMagRegisters(uint8_t reg, uint8_t* buffer, size_t len);
220  bool writeAccRegister(uint8_t reg, uint8_t value);
221  bool readAccRegisters(uint8_t reg, uint8_t* buffer, size_t len);
222  DrvI2cMaster* mI2c;
223 };
224 
225 #endif /* LSM303DLHC_HPP_ */