KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lsm303d.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2016 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : lsm303d.h
11  * Created : 21 mrt. 2016
12  * Author : Vincent van Beveren
13  */
14 #ifndef DRV_I2C_LSM303D_H_
15 #define DRV_I2C_LSM303AGR_H_
16 
17 /**
18  * @file
19  *
20  * @ingroup i2cdrivers
21  *
22  * Driver for the ST LSM303Dx Compass sensor.
23  */
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 
28 #include "drv/wb/i2c.h"
29 #include "drv/i2c/lsm303.h"
30 
31 
32 
33 
34 #define LSM303D_MAGSCALE_2GAUSS 0x00
35 #define LSM303D_MAGSCALE_4GAUSS 0x01
36 #define LSM303D_MAGSCALE_8GAUSS 0x02
37 #define LSM303D_MAGSCALE_12GAUSS 0x03
38 
39 #define LSM303D_MAGRATE_3HZ125 0x00
40 #define LSM303D_MAGRATE_6HZ25 0x01
41 #define LSM303D_MAGRATE_12HZ5 0x02
42 #define LSM303D_MAGRATE_25HZ 0x03
43 #define LSM303D_MAGRATE_50HZ 0x04
44 #define LSM303D_MAGRATE_100HZ 0x05
45 #define LSM303D_MAGRATE_DO_NOT_USE 0x06
46 
47 #define LSM303D_MAGMODE_CONTINUOUS 0x00
48 #define LSM303D_MAGMODE_SINGLE 0x01
49 #define LSM303D_MAGMODE_POWERDOWN 0x02
50 
51 #define LSM303D_ACCRATE_POWERDOWN 0x00
52 #define LSM303D_ACCRATE_3HZ125 0x01
53 #define LSM303D_ACCRATE_6HZ25 0x02
54 #define LSM303D_ACCRATE_12HZ5 0x03
55 #define LSM303D_ACCRATE_25HZ 0x04
56 #define LSM303D_ACCRATE_50HZ 0x05
57 #define LSM303D_ACCRATE_100HZ 0x06
58 #define LSM303D_ACCRATE_200HZ 0x07
59 #define LSM303D_ACCRATE_400HZ 0x08
60 #define LSM303D_ACCRATE_800HZ 0x09
61 #define LSM303D_ACCRATE_1600HZ 0x0A
62 
63 #define LSM303D_ACCAAF_773HZ 0x00
64 #define LSM303D_ACCAAF_194HZ 0x01
65 #define LSM303D_ACCAAF_362HZ 0x02
66 #define LSM303D_ACCAAF_50HZ 0x03
67 
68 #define LSM303D_ACCSCALE_2G 0x00
69 #define LSM303D_ACCSCALE_4G 0x01
70 #define LSM303D_ACCSCALE_6G 0x02
71 #define LSM303D_ACCSCALE_8G 0x03
72 #define LSM303D_ACCSCALE_16G 0x04
73 
74 #define LSM303D_ACCHPF_NORMAL 0x00
75 #define LSM303D_ACCHPF_REFSIG 0x01
76 #define LSM303D_ACCHPF_AUTORESET 0x03
77 
78 
79 /**
80  * Generic LSM303D configuration
81  */
82 typedef struct Lsm303DCfg_ {
83  bool bdu : 1; ///!< Block data update
84  bool temp : 1; ///!< Temperature sensor (coupled to magnet)
85  bool tempOnly : 1; ///!< Disables the magnet, providing only temperature
86 } Lsm303DCfg;
87 
88 //! Default generic configuration
89 #define LSM303D_CFG_DEFAULT { \
90  .bdu = false, \
91  .temp = false, \
92  .tempOnly = false \
93 }
94 
95 /**
96  * Magnet configuration.
97  */
98 typedef struct Lsm303DMagCfg_ {
99  uint8_t magScale : 2; ///< Magnet scale, use one of LSM303D_MAGSCALEE_*
100  uint8_t magRate : 3; ///< Magnet rate, use one of LSM303D_MAGRATE_*
101  uint8_t magMode : 2; ///< Magnetic sensor mode selection, LSM303D_MAGMODE_*
102  bool lowPower: 1; ///< Magnet in low power mode.
103  bool hiRes : 1; ///< High resolution
104 } Lsm303DMagCfg;
105 
106 //! DEfault magnet configuration
107 #define LSM303D_MAGCFG_DEFAULT {\
108  .magScale = LSM303D_MAGSCALE_2GAUSS, \
109  .magRate = LSM303D_MAGRATE_DO_NOT_USE, /* oddly enough */ \
110  .magMode = LSM303D_MAGMODE_POWERDOWN, \
111  .lowPower = false, \
112  .hiRes = false }
113 
114 /**
115  * Accelerator configuration structure
116  */
117 typedef struct Lsm303DAccCfg_ {
118  uint16_t accRate : 4; ///< Accelerometer rate , one of LSM303D_ACCRATE_*
119  uint16_t accAAF : 2; ///< Anti-alias filter, one of LSM303D_ACCAAF_*
120  uint16_t accHPF : 2; ///< High-pass filter, one of LSM303D_ACCHPF_*
121  uint16_t accScale : 3; ///< Accelerometer scale, one of LSM303D_ACCSCALE_*
122  bool selfTest : 1; ///< Enabled self-test
123  bool filterAcc: 1; ///< Filter acceleration data
124 } Lsm303DAccCfg;
125 
126 
127 //! Default accelerometer configuration.
128 #define LSM303D_ACCCFG_DEFAULT {\
129  .accRate = LSM303D_ACCRATE_POWERDOWN, \
130  .accAAF = LSM303D_ACCAAF_773HZ, \
131  .accHPF = LSM303D_ACCHPF_NORMAL, \
132  .accScale = LSM303D_ACCSCALE_2G, \
133  .selfTest = false, \
134  .filterAcc= false}
135 
136 
137 /*
138 #define LSM303D_FIFOMODE_BYPASS 0
139 #define LSM303D_FIFOMODE_FIFO 1
140 #define LSM303D_FIFOMODE_STREAM 2
141 #define LSM303D_FIFOMODE_STREAM_TO_FIFO 3
142 #define LSM303D_FIFOMODE_BYPASS_TO_STREAM 4
143 
144 
145 
146 typedef struct Lsm303FifoCfg_ {
147  uint32_t mode : 3; ///< Set FIFO mode, one of LSM303D_FIFOMODE_*
148  uint32_t thresLvl : 5; ///< FIFO threshold level
149  bool enable : 1; ///< Enable FIFO mode
150  bool prgThresh : 1; ///< Enable programmable threshold
151 
152 } Lsm303FifoCfg;
153 */
154 
155 
156 /**
157  * Read the magnet information.
158  *
159  * @param dev I2C WB device
160  * @param addr I2C address
161  * @param result The 3D magnet information
162  *
163  * @retval true Procedure was a success.
164  * @retval false Procedure failed
165  */
166 bool lsm303DMagnetRead(I2C_Device * dev, uint8_t addr, Lsm303Axis * result);
167 
168 
169 /**
170  * Set the Magnet configuration.
171  *
172  * @param dev I2C WB device
173  * @param addr I2C address
174  * @param config The configuration structure.
175  *
176  * @retval true Procedure was a success.
177  * @retval false Procedure failed
178  */
179 bool lsm303DMagnetCfg(I2C_Device * dev, uint8_t addr, Lsm303DMagCfg config);
180 
181 /**
182  * Reads the temperature.
183  *
184  * @note Oddly the documentation does not actually specify the scaling of the
185  * sensor.
186  *
187  * @param dev I2C WB device
188  * @param addr I2C address
189  * @param result The temperature.
190  *
191  * @retval true Procedure was a success.
192  * @retval false Procedure failed
193  */
194 bool lsm303DTempRead(I2C_Device * dev, uint8_t addr, int16_t * result);
195 
196 /**
197  * Read the accelerometer information.
198  *
199  * @param dev I2C WB device
200  * @param addr I2C address
201  * @param result The 3D accelerometer information
202  *
203  * @retval true Procedure was a success.
204  * @retval false Procedure failed
205  */
206 bool lsm303DAccelRead(I2C_Device * dev, uint8_t addr, Lsm303Axis * result);
207 
208 /**
209  * Set the accelerometer configuration.
210  *
211  * @param dev I2C WB device
212  * @param addr I2C address
213  * @param config The configuration structure.
214  *
215  * @retval true Procedure was a success.
216  * @retval false Procedure failed
217  */
218 bool lsm303DAccelCfg(I2C_Device * dev, uint8_t addr, Lsm303DAccCfg config);
219 
220 
221 /**
222  * Checks whether or not the LSM303D is valid.
223  *
224  * @param dev I2C WB device
225  * @param addr I2C address
226  * @param isLSM303 Pointer to boolean to be filled with true or false.
227  * true indicates the LSM303D was found and is valid.
228  * false indicates a device was found, but not an LSM303D
229 
230  * @retval true Procedure was a success.
231  * @retval false Procedure failed
232  */
233 bool lsm303DValid(I2C_Device * dev, uint8_t addr, bool * isLSM303);
234 
235 /**
236  * Sets the generic device configuration parameters.
237  *
238  * @param dev I2C WB device
239  * @param addr I2C address
240  * @param config The configuration structure.
241  *
242  * @retval true Procedure was a success.
243  * @retval false Procedure failed
244  */
245 bool lsm303DCfg(I2C_Device * dev, uint8_t addr, Lsm303DCfg config);
246 
247 
248 #endif /* DRV_I2C_LSM303D_H_ */
uint16_t accAAF
Anti-alias filter, one of LSM303D_ACCAAF_*.
Definition: lsm303d.h:119
Axis structure for 3D information.
Definition: lsm303.h:31
uint8_t magMode
Magnetic sensor mode selection, LSM303D_MAGMODE_*.
Definition: lsm303d.h:101
uint16_t accScale
Accelerometer scale, one of LSM303D_ACCSCALE_*.
Definition: lsm303d.h:121
bool filterAcc
Filter acceleration data.
Definition: lsm303d.h:123
bool lsm303DMagnetCfg(I2C_Device *dev, uint8_t addr, Lsm303DMagCfg config)
Set the Magnet configuration.
Definition: lsm303d.c:130
Generic LSM303D configuration.
Definition: lsm303d.h:82
bool hiRes
High resolution.
Definition: lsm303d.h:103
bool selfTest
Enabled self-test.
Definition: lsm303d.h:122
uint16_t accRate
Accelerometer rate , one of LSM303D_ACCRATE_*.
Definition: lsm303d.h:118
uint8_t magRate
Magnet rate, use one of LSM303D_MAGRATE_*.
Definition: lsm303d.h:100
Accelerator configuration structure.
Definition: lsm303d.h:117
bool lsm303DAccelRead(I2C_Device *dev, uint8_t addr, Lsm303Axis *result)
Read the accelerometer information.
Definition: lsm303d.c:118
bool tempOnly
!&lt; Temperature sensor (coupled to magnet)
Definition: lsm303d.h:85
Structure defines OpenCores I2C Device.
Definition: dev_i2c.h:55
bool lowPower
Magnet in low power mode.
Definition: lsm303d.h:102
bool temp
!&lt; Block data update
Definition: lsm303d.h:84
Magnet configuration.
Definition: lsm303d.h:98
struct Lsm303DMagCfg_ Lsm303DMagCfg
Magnet configuration.
bool lsm303DValid(I2C_Device *dev, uint8_t addr, bool *isLSM303)
Checks whether or not the LSM303D is valid.
Definition: lsm303d.c:92
bool lsm303DMagnetRead(I2C_Device *dev, uint8_t addr, Lsm303Axis *result)
Read the magnet information.
Definition: lsm303d.c:106
uint16_t accHPF
High-pass filter, one of LSM303D_ACCHPF_*.
Definition: lsm303d.h:120
bool lsm303DAccelCfg(I2C_Device *dev, uint8_t addr, Lsm303DAccCfg config)
Set the accelerometer configuration.
Definition: lsm303d.c:154
bool lsm303DCfg(I2C_Device *dev, uint8_t addr, Lsm303DCfg config)
Sets the generic device configuration parameters.
Definition: lsm303d.c:179
bool lsm303DTempRead(I2C_Device *dev, uint8_t addr, int16_t *result)
Reads the temperature.
struct Lsm303DAccCfg_ Lsm303DAccCfg
Accelerator configuration structure.
struct Lsm303DCfg_ Lsm303DCfg
Generic LSM303D configuration.
OpenCores I2C device driver.
uint8_t magScale
Magnet scale, use one of LSM303D_MAGSCALEE_*.
Definition: lsm303d.h:99