ISSDK  1.7
IoT Sensing Software Development Kit
driver_FXOS8700.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file driver_FXOS8700.c
36  \brief Provides init() and read() functions for the FXOS8700 6-axis accel plus mag
37 */
38 
39 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
40 #include "sensor_fusion.h" // Sensor fusion structures and types
41 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
42 #include "fxos8700.h" // describes the FXOS8700 register definition and its bit mask
43 #include "fxos8700_drv.h" // Low level IS-SDK prototype driver
44 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
45 #include "status.h"
46 
47 #define FXOS8700_ACCEL_FIFO_SIZE 32 ///< FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO
48 #define FXOS8700_MAG_FIFO_SIZE 1 ///< FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO
49 
50 // Command definition to read the WHO_AM_I value.
52 {
53  { .readFrom = FXOS8700_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
54 };
55 
56 // Command definition to read the number of entries in the accel FIFO.
58 {
59  { .readFrom = FXOS8700_STATUS, .numBytes = 1 }, __END_READ_DATA__
60 };
61 
62 // Command definition to read the number of entries in the accel FIFO.
64 {
65  { .readFrom = FXOS8700_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
66 };
67 
68 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
70 {
71  // Command definition to write 0000 0000 = 0x00 to CTRL_REG1 to place FXOS8700 into standby
72  // [7-1] = 0000 000
73  // [0]: active=0
74  { FXOS8700_CTRL_REG1, 0x00, 0x00 }, // write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
75 
76  // [7-6]: f_mode[1-0]=01 for FIFO continuous mode
77  // [5-0]: f_wmrk[5-0]=000000 for no FIFO watermark
78  { FXOS8700_F_SETUP, 0x40, 0x00 },
79 
80  // write 0001 1111 = 0x1F to M_CTRL_REG1
81  // [7]: m_acal=0: auto calibration disabled
82  // [6]: m_rst=0: one-shot magnetic reset disabled
83  // [5]: m_ost=0: one-shot magnetic measurement disabled
84  // [4-2]: m_os=111=7: maximum oversampling to reduce magnetometer noise
85  // [1-0]: m_hms=11=3: select hybrid mode with accel and magnetometer active
86  { FXOS8700_M_CTRL_REG1, 0x1F, 0x00 }, // write 0000 0000 = 0x00 to M_CTRL_REG2
87 
88  // [7]: reserved
89  // [6]: reserved
90  // [5]: hyb_autoinc_mode=0 to ensure address wraparound to 0x00 to clear accelerometer FIFO in one read
91  // [4]: m_maxmin_dis=0 to retain default min/max latching even though not used
92  // [3]: m_maxmin_dis_ths=0
93  // [2]: m_maxmin_rst=0
94  // [1-0]: m_rst_cnt=00 to enable magnetic reset each cycle
95  { FXOS8700_M_CTRL_REG2, 0x00, 0x00 },
96 
97  // write 0000 0001= 0x01 to XYZ_DATA_CFG register
98  // [7]: reserved
99  // [6]: reserved
100  // [5]: reserved
101  // [4]: hpf_out=0
102  // [3]: reserved
103  // [2]: reserved
104  // [1-0]: fs=01 for 4g mode: 2048 counts / g = 8192 counts / g after 2 bit left shift
105  { FXOS8700_XYZ_DATA_CFG, 0x01, 0x00 }, // write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
106 
107  // [7]: st=0: self test disabled
108  // [6]: rst=0: reset disabled
109  // [5]: unused
110  // [4-3]: smods=00
111  // [2]: slpe=0: auto sleep disabled
112  // [1-0]: mods=10 for high resolution (maximum over sampling)
113  { FXOS8700_CTRL_REG2, 0x02, 0x00 },
114 
115  // write 00XX X101 = 0x0D to accelerometer control register 1
116  // since this is a hybrid sensor with identical accelerometer and magnetometer ODR the value for ACCEL_ODR_HZ is used
117  // [7-6]: aslp_rate=00
118  // [5-3]: dr=111 for 0.78Hz data rate giving 0x3D
119  // [5-3]: dr=110 for 3.125Hz data rate giving 0x35
120  // [5-3]: dr=101 for 6.25Hz data rate giving 0x2D
121  // [5-3]: dr=100 for 25Hz data rate giving 0x25
122  // [5-3]: dr=011 for 50Hz data rate giving 0x1D
123  // [5-3]: dr=010 for 100Hz data rate giving 0x15
124  // [5-3]: dr=001 for 200Hz data rate giving 0x0D
125  // [5-3]: dr=000 for 400Hz data rate giving 0x05
126  // [2]: lnoise=1 for low noise mode (since we're in 4g mode)
127  // [1]: f_read=0 for normal 16 bit reads
128  // [0]: active=1 to take the part out of standby and enable sampling
129 #if (ACCEL_ODR_HZ <= 1) // select 0.78Hz ODR
130  { FXOS8700_CTRL_REG1, 0x3D, 0x00 },
131 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
132  { FXOS8700_CTRL_REG1, 0x35, 0x00 },
133 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
134  { FXOS8700_CTRL_REG1, 0x2D, 0x00 },
135 #elif (ACCEL_ODR_HZ <= 30) // select 25Hz ODR
136  { FXOS8700_CTRL_REG1, 0x25, 0x00 },
137 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
138  { FXOS8700_CTRL_REG1, 0x1D, 0x00 },
139 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
140  { FXOS8700_CTRL_REG1, 0x15, 0x00 },
141 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
142  { FXOS8700_CTRL_REG1, 0x0D, 0x00 },
143 #else // select 400Hz ODR
144  { FXOS8700_CTRL_REG1, 0x05, 0x00 },
145 #endif
147 };
148 
149 #define FXOS8700_COUNTSPERG 8192.0
150 #define FXOS8700_COUNTSPERUT 10
151 
152 // All sensor drivers and initialization functions have the same prototype
153 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
154 
155 // sfg = pointer to top level (generally global) data structure for sensor fusion
157 {
158  int32_t status;
159  uint8_t reg;
160 
161  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_WHO_AM_I, 1, &reg);
162 
163  if (status==SENSOR_ERROR_NONE) {
164 #if F_USING_ACCEL
165  sfg->Accel.iWhoAmI = reg;
166  sfg->Accel.iCountsPerg = FXOS8700_COUNTSPERG;
167  sfg->Accel.fgPerCount = 1.0F / FXOS8700_COUNTSPERG;
168 #endif
169 #if F_USING_MAG
170  sfg->Mag.iWhoAmI = reg;
172  sfg->Mag.fCountsPeruT = (float) FXOS8700_COUNTSPERUT;
173  sfg->Mag.fuTPerCount = 1.0F / FXOS8700_COUNTSPERUT;
174 #endif
175  if (reg != FXOS8700_WHO_AM_I_PROD_VALUE) {
176  return SENSOR_ERROR_INIT; // The whoAmI did not match
177  }
178  } else {
179  // whoAmI will rettain default value of zero
180  // return with error
181  return status;
182  }
183 
184  // Configure and start the fxos8700 sensor. This does multiple register writes
185  // (see FXOS8700_Initialization definition above)
186  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_Initialization );
188 #if F_USING_ACCEL
189  sfg->Accel.isEnabled = true;
190 #endif
191 #if F_USING_MAG
192  sfg->Mag.isEnabled = true;
193 #endif
194 
195  return (status);
196 }
197 
198 #if F_USING_ACCEL
199 int8_t FXOS8700_ReadAccData(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
200 {
201  uint8_t I2C_Buffer[6 * FXOS8700_ACCEL_FIFO_SIZE]; // I2C read buffer
202  int32_t status; // I2C transaction status
203  int8_t j; // scratch
204  uint8_t sensor_fifo_count;
205  int16_t sample[3];
206 
207  if(!(sensor->isInitialized & F_USING_ACCEL))
208  {
209  return SENSOR_ERROR_INIT;
210  }
211 
212  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
213  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_F_STATUS_READ, I2C_Buffer );
214  if (status != SENSOR_ERROR_NONE) return(status);
215  else {
216 #ifdef SIMULATOR_MODE
217  sensor_fifo_count = 1;
218 #else
219  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
220 #endif
221  // return if there are no measurements in the sensor FIFO.
222  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
223  if (sensor_fifo_count == 0) return(SENSOR_ERROR_READ);
224  }
225 
226  FXOS8700_DATA_READ[0].readFrom = FXOS8700_OUT_X_MSB;
227  FXOS8700_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
228  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_DATA_READ, I2C_Buffer );
229 
230  if (status==SENSOR_ERROR_NONE) {
231  for (j = 0; j < sensor_fifo_count; j++) {
232  sample[CHX] = (I2C_Buffer[6 * j] << 8) | (I2C_Buffer[6 * j + 1]); // decode X
233  sample[CHY] = (I2C_Buffer[6 * j + 2] << 8) | (I2C_Buffer[6 * j + 3]); // decode Y
234  sample[CHZ] = (I2C_Buffer[6 * j + 4] << 8) | (I2C_Buffer[6 * j + 5]); // decode Z
235  conditionSample(sample); // truncate negative values to -32767
236  // place the 6 bytes read into the 16 bit accelerometer structure
237  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
238  }
239  }
240 
241  return (status);
242 }
243 #endif
244 #if F_USING_MAG
245 // read FXOS8700 magnetometer over I2C
247 {
248  uint8_t I2C_Buffer[6]; // I2C read buffer
249  int32_t status; // I2C transaction status
250  int16_t sample[3];
251 
252  if(!(sensor->isInitialized & F_USING_MAG))
253  {
254  return SENSOR_ERROR_INIT;
255  }
256 
257  // read the six sequential magnetometer output bytes
258  FXOS8700_DATA_READ[0].readFrom = FXOS8700_M_OUT_X_MSB;
259  FXOS8700_DATA_READ[0].numBytes = 6;
260  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_DATA_READ, I2C_Buffer );
261  if (status==SENSOR_ERROR_NONE) {
262  // place the 6 bytes read into the magnetometer structure
263  sample[CHX] = (I2C_Buffer[0] << 8) | I2C_Buffer[1];
264  sample[CHY] = (I2C_Buffer[2] << 8) | I2C_Buffer[3];
265  sample[CHZ] = (I2C_Buffer[4] << 8) | I2C_Buffer[5];
266  conditionSample(sample); // truncate negative values to -32767
267  addToFifo((union FifoSensor*) &(sfg->Mag), MAG_FIFO_SIZE, sample);
268  }
269  return status;
270 }
271 #endif
272 // This is the composite read function that handles both accel and mag portions of the FXOS8700
273 
274 // It returns the first failing status flag
276 {
277  int8_t sts1 = 0;
278  int8_t sts2 = 0;
279 #if F_USING_ACCEL
280  sts1 = FXOS8700_ReadAccData(sensor, sfg);
281 #endif
282 
283 #if F_USING_MAG
284  sts2 = FXOS8700_ReadMagData(sensor, sfg);
285 #endif
286 
287  if (sts1)
288  return (sts1);
289  else
290  return (sts2);
291 }
292 
293 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
295 {
296  // Set ACTIVE = other bits unchanged
297  { FXOS8700_CTRL_REG1, 0x00, 0x01 },
299 };
300 
301 // FXOS8700_Idle places the entire sensor into STANDBY mode (wakeup time = 1/ODR+1ms)
302 // This driver is all-on or all-off. It does not support mag or accel only.
303 // If you want that functionality, you can write your own using the initialization
304 // function in this file as a starting template. We've chosen not to cover all
305 // permutations in the interest of simplicity.
307 {
308  int32_t status;
309  if(sensor->isInitialized == (F_USING_ACCEL|F_USING_MAG)) {
310  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXOS8700_FULL_IDLE );
311  sensor->isInitialized = 0;
312 #if F_USING_ACCEL
313  sfg->Accel.isEnabled = false;
314 #endif
315 #if F_USING_MAG
316  sfg->Mag.isEnabled = false;
317 #endif
318  } else {
319  return SENSOR_ERROR_INIT;
320  }
321  return status;
322 }
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:87
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
SensorFusionGlobals sfg
int32_t status
const registerwritelist_t FXOS8700_FULL_IDLE[]
float fuTPerCount
uT per count
#define FXOS8700_ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define MAG_FIFO_SIZE
FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO.
int32_t Sensor_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
uint8_t iWhoAmI
sensor whoami
The fxos8700.h file contains the register definitions for FXOS8700 sensor driver. ...
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
int8_t FXOS8700_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define __END_READ_DATA__
Definition: sensor_drv.h:77
bool isEnabled
true if the device is sampling
The top level fusion structure.
#define FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:172
struct MagSensor Mag
magnetometer storage
uint16_t addr
I2C address if applicable.
void addToFifo(union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
int8_t FXOS8700_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The sensor_fusion.h file implements the top level programming interface.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:88
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
int16_t iCountsPeruT
counts per uT
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
const registerwritelist_t FXOS8700_Initialization[]
Application-specific status subsystem.
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
const registerreadlist_t FXOS8700_F_STATUS_READ[]
int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
#define FXOS8700_COUNTSPERG
int8_t FXOS8700_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
float fCountsPeruT
counts per uT
int8_t FXOS8700_ReadMagData(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
const registerreadlist_t FXOS8700_WHO_AM_I_READ[]
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define F_USING_MAG
Definition: magnetic.h:47
registerreadlist_t FXOS8700_DATA_READ[]
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
#define FXOS8700_COUNTSPERUT
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:97