KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lsm303agr.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 : lsm303Agr.h
11  * Created : 21 mrt. 2016
12  * Author : Vincent van Beveren
13  */
14 #ifndef DRV_I2C_lsm303Agr_H_
15 #define DRV_I2C_lsm303Agr_H_
16 
17 /**
18  * @file
19  *
20  * @ingroup i2cdrivers
21  *
22  * Driver for the ST lsm303Agr Compass sensor, which unlike the name suggests quite incompable to the LSM303D....
23  *
24  * IRQ / Fifo modes not supported.
25  */
26 
27 #include <stdint.h>
28 #include <stdbool.h>
29 
30 #include "drv/wb/i2c.h"
31 #include "drv/i2c/lsm303.h"
32 
33 
34 
35 #define LSM303AGR_ACC_PM_LOW_8BIT 0x1
36 #define LSM303AGR_ACC_PM_NORM_10BIT 0x0
37 #define LSM303AGR_ACC_PM_HIGH_12BIT 0x2
38 
39 
40 #define LSM303AGR_ACC_RATE_POWERDOWN 0x00
41 #define LSM303AGR_ACC_RATE_1HZ 0x01
42 #define LSM303AGR_ACC_RATE_10HZ 0x02
43 #define LSM303AGR_ACC_RATE_25HZ 0x03
44 #define LSM303AGR_ACC_RATE_50HZ 0x04
45 #define LSM303AGR_ACC_RATE_100HZ 0x05
46 #define LSM303AGR_ACC_RATE_200HZ 0x06
47 #define LSM303AGR_ACC_RATE_400HZ 0x07
48 #define LSM303AGR_ACC_RATE_LP_1620HZ 0x08
49 #define LSM303AGR_ACC_RATE_1344_LP5376HZ 0x09
50 
51 #define LSM303AGR_ACC_FS_2G 0x00
52 #define LSM303AGR_ACC_FS_4G 0x01
53 #define LSM303AGR_ACC_FS_8G 0x02
54 #define LSM303AGR_ACC_FS_16G 0x03
55 
56 
57 #define LSM303AGR_MAG_RATE_10HZ 0x00
58 #define LSM303AGR_MAG_RATE_20HZ 0x01
59 #define LSM303AGR_MAG_RATE_50HZ 0x02
60 #define LSM303AGR_MAG_RATE_100HZ 0x03
61 
62 #define LSM303AGR_MAG_MODE_CONTINUOUS 0x00
63 #define LSM303AGR_MAG_MODE_SINGLE 0x01
64 #define LSM303AGR_MAG_MODE_IDLE 0x02
65 
66 
67 
68  /*
69  * Cut-off frequncy depends on rate
70  *
71  * HPCF @1Hz @10Hz @25Hz @50Hz @100Hz @200Hz @400Hz @1kHz6 @5kHz
72  * AGGRESSIVE 0.02Hz 0.2Hz 0.5Hz 1Hz 2Hz 4Hz 8Hz 32Hz 100Hz
73  * STRONG 0.008Hz 0.08Hz 0.2Hz 0.5Hz 1Hz 2Hz 4Hz 16Hz 50Hz
74  * MEDIUM 0.004Hz 0.04Hz 0.1Hz 0.2Hz 0.5Hz 1Hz 2Hz 8Hz 25Hz
75  * LIGHT 0.002Hz 0.02Hz 0.05Hz 0.1Hz 0.2Hz 0.5Hz 1Hz 4Hz 12Hz
76  *
77  */
78 //! Highpass filter
79 #define LSM303AGR_ACC_HPCF_AGRESSIVE 0x0
80 #define LSM303AGR_ACC_HPCF_STRONG 0x1
81 #define LSM303AGR_ACC_HPCF_MEDIUM 0x2
82 #define LSM303AGR_ACC_HPCF_LIGHT 0x3
83 
84 
85 
86 
87 /**
88  * Magnet configuration.
89  */
90 typedef struct Lsm303AgrMagCfg_ {
91  uint16_t mode : 2; ///< one of LSM303AGR_MAG_MODE_*
92  uint16_t rate : 2; ///< One of LSM303AGR_MAG_RATE_*
93  bool tempCompEn : 1; ///< Temperature compensation enabled
94  bool lp : 1; ///< Low power mode
95  bool offCanc : 1; ///< Offset cancellation
96  bool lpf : 1; ///< Enable low-pass filter
97  bool bdu : 1; ///< Block date update enable
99 
100 //! DEfault magnet configuration
101 #define LSM303AGR_MAG_CFG_DEFAULT {\
102  .mode = LSM303AGR_MAG_MODE_CONTINUOUS, \
103  .rate = LSM303AGR_MAG_RATE_10HZ, \
104  .tempCompEn = false, \
105  .lp = false, \
106  .offCanc = false, \
107  .lpf = false, \
108  .bdu = false \
109 }
110 
111 /**
112  * Accelerator configuration structure
113  */
114 typedef struct Lsm303AgrAccCfg_
115 {
116  uint16_t rate : 4; ///< Accelerometer rate , one of LSM303AG_ACC_RATE_*
117  uint16_t hpcf : 2; ///< High-pass filter, one of LSM303AG_ACC_HPCF_*
118  uint16_t fs : 2; ///< Full scale of accelerometer, one of LSM303AGR_ACC_FS_*
119  uint16_t pm : 2; ///< Power mode
120  bool bdu:1; ///< Block data update
121  bool temp:1; ///< Temperature sensor enable.
122  bool fds:1; ///< Filter data selection
124 
125 
126 //! Default accelerometer configuration.
127 #define LSM303AGR_ACC_CFG_DEFAULT {\
128  .rate = LSM303AGR_ACC_RATE_POWERDOWN, \
129  .hpcf = LSM303AGR_ACC_HPCF_AGRESSIVE, \
130  .fs = LSM303AGR_ACC_FS_2G, \
131  .pm = LSM303AGR_ACC_PM_NORM_10BIT, \
132  .bdu = false, \
133  .temp = false, \
134  .fds= false \
135 }
136 
137 
138 
139 /**
140  * Read the magnet information.
141  *
142  * @param dev I2C WB device
143  * @param result The 3D magnet information
144  *
145  * @retval true Procedure was a success.
146  * @retval false Procedure failed
147  */
148 bool lsm303AgrMagRead(I2C_Device * dev, Lsm303Axis * result);
149 
150 
151 /**
152  * Set the Magnet configuration.
153  *
154  * @param dev I2C WB device
155  * @param config The configuration structure.
156  *
157  * @retval true Procedure was a success.
158  * @retval false Procedure failed
159  */
160 bool lsm303AgrMagCfg(I2C_Device * dev, Lsm303AgrMagCfg config);
161 
162 /**
163  * Reads the temperature.
164  *
165  * @note Oddly the documentation does not actually specify the scaling of the
166  * sensor.
167  *
168  * @note The sensor uses the same pipeline as the accelerometer. So, if the accelerometer is not operational
169  * no temperature will be read.
170  *
171  * @param dev I2C WB device
172  * @param result The temperature.
173  *
174  * @retval true Procedure was a success.
175  * @retval false Procedure failed
176  */
177 bool lsm303AgrTempRead(I2C_Device * dev, int16_t * result);
178 
179 bool lsm303AgrHasTemp(I2C_Device * dev, bool * hasData);
180 
181 /**
182  * Read the accelerometer information.
183  *
184  * @param dev I2C WB device
185  * @param addr I2C address
186  * @param result The 3D accelerometer information
187  *
188  * @retval true Procedure was a success.
189  * @retval false Procedure failed
190  */
191 bool lsm303AgrAccRead(I2C_Device * dev, Lsm303Axis * result);
192 
193 /**
194  * Set the accelerometer configuration.
195  *
196  * @param dev I2C WB device
197  * @param config The configuration structure.
198  *
199  * @retval true Procedure was a success.
200  * @retval false Procedure failed
201  */
202 bool lsm303AgrAccCfg(I2C_Device * dev, Lsm303AgrAccCfg config);
203 
204 
205 /**
206  * Checks whether or not the lsm303Agr is valid.
207  *
208  * @param dev I2C WB device
209  * @param isLSM303Agr Pointer to boolean to be filled with true or false.
210  * true indicates the lsm303Agr was found and is valid.
211  * false indicates a device was found, but not an lsm303Agr
212 
213  * @retval true Procedure was a success.
214  * @retval false Procedure failed
215  */
216 bool lsm303AgrValid(I2C_Device * dev, bool * isLSM303Agr);
217 
218 
219 #endif /* DRV_I2C_lsm303Agr_H_ */
Axis structure for 3D information.
Definition: lsm303.h:31
bool lp
Low power mode.
Definition: lsm303agr.h:94
bool tempCompEn
Temperature compensation enabled.
Definition: lsm303agr.h:93
Accelerator configuration structure.
Definition: lsm303agr.h:114
bool lsm303AgrAccCfg(I2C_Device *dev, Lsm303AgrAccCfg config)
Set the accelerometer configuration.
Definition: lsm303agr.c:223
Magnet configuration.
Definition: lsm303agr.h:90
uint16_t hpcf
High-pass filter, one of LSM303AG_ACC_HPCF_*.
Definition: lsm303agr.h:117
struct Lsm303AgrAccCfg_ Lsm303AgrAccCfg
Accelerator configuration structure.
bool lsm303AgrAccRead(I2C_Device *dev, Lsm303Axis *result)
Read the accelerometer information.
Definition: lsm303agr.c:188
Structure defines OpenCores I2C Device.
Definition: dev_i2c.h:55
uint16_t mode
one of LSM303AGR_MAG_MODE_*
Definition: lsm303agr.h:91
bool lsm303AgrMagCfg(I2C_Device *dev, Lsm303AgrMagCfg config)
Set the Magnet configuration.
Definition: lsm303agr.c:200
uint16_t pm
Power mode.
Definition: lsm303agr.h:119
bool lsm303AgrValid(I2C_Device *dev, bool *isLSM303Agr)
Checks whether or not the lsm303Agr is valid.
Definition: lsm303agr.c:163
struct Lsm303AgrMagCfg_ Lsm303AgrMagCfg
Magnet configuration.
bool lsm303AgrTempRead(I2C_Device *dev, int16_t *result)
Reads the temperature.
Definition: lsm303agr.c:146
bool offCanc
Offset cancellation.
Definition: lsm303agr.h:95
uint16_t rate
One of LSM303AGR_MAG_RATE_*.
Definition: lsm303agr.h:92
uint16_t rate
Accelerometer rate , one of LSM303AG_ACC_RATE_*.
Definition: lsm303agr.h:116
bool temp
Temperature sensor enable.
Definition: lsm303agr.h:121
bool bdu
Block data update.
Definition: lsm303agr.h:120
bool lpf
Enable low-pass filter.
Definition: lsm303agr.h:96
uint16_t fs
Full scale of accelerometer, one of LSM303AGR_ACC_FS_*.
Definition: lsm303agr.h:118
bool fds
Filter data selection.
Definition: lsm303agr.h:122
bool bdu
Block date update enable.
Definition: lsm303agr.h:97
bool lsm303AgrMagRead(I2C_Device *dev, Lsm303Axis *result)
Read the magnet information.
Definition: lsm303agr.c:176
OpenCores I2C device driver.