ISSDK  1.7
IoT Sensing Software Development Kit
driver_FXLS8962.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_FXLS8962.c
36  \brief Provides init() and read() functions for the FXLS8962 3-axis accelerometer
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 "sensor_drv.h" // Contains sensor state and error definitions
43 #include "fxls8962.h" // describes the FXLS8962 register definition and its bit mask
44 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
45 #define FXLS8962_COUNTSPERG 512
46 #define FXLS8962_ACCEL_FIFO_SIZE 32
47 
48 #if F_USING_ACCEL
49 
50 // Command definition to read the WHO_AM_I value.
51 const registerreadlist_t FXLS8962_WHO_AM_I_READ[] =
52 {
53  { .readFrom = FXLS8962_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
54 };
55 
56 // Command definition to read the number of entries in the accel FIFO.
57 const registerreadlist_t FXLS8962_INT_STATUS_READ[] =
58 {
59  { .readFrom = FXLS8962_INT_STATUS, .numBytes = 1 }, __END_READ_DATA__
60 };
61 
62 // Command definition to read the number of entries in the accel FIFO.
63 registerreadlist_t FXLS8962_DATA_READ[] =
64 {
65  { .readFrom = FXLS8962_OUT_X_LSB, .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)
69 const registerwritelist_t FXLS8962_Initialization[] =
70 {
71  // write 0000 0000 = 0x00 to SENS_CONFIG1 to place FXLS8962 into standby
72  // [7-1]: Set when taking the part out of standby
73  // [0]: ACTIVE=0 for standby
74  { FXLS8962_SENS_CONFIG1, 0x00, 0x00 },
75 
76  // write 0101 0000 = 0x50 to SENS_CONFIG2 register to set High Performance Mode
77  // [7-6]: WAKE_PM=01 for High Performance Mode
78  // [5-4]: SLEEP_PM=01 for High Performance Mode
79  // [3]: LE_BE=0 for Little Endian
80  // [2]: BLOCK=0 for normal latching
81  // [1]: AINC_TEMP=0 to not include temperature in auto increment
82  // [0]: F_READ=0 for full register reads
83  { FXLS8962_SENS_CONFIG2, 0x50, 0x00 },
84 
85  // write XXXX 0000 = to SENS_CONFIG3 register to set ODR
86  // [7-4]: WAKE_ODR=1100 for 0.781Hz ODR and 4096x decimation from 3200Hz giving 0xC0
87  // [7-4]: WAKE_ODR=1011 for 1.563Hz ODR and 12048x decimation from 3200Hz giving 0xB0
88  // [7-4]: WAKE_ODR=1010 for 3.125Hz ODR and 1024x decimation from 3200Hz giving 0xA0
89  // [7-4]: WAKE_ODR=1001 for 6.25Hz ODR and 512x decimation from 3200Hz giving 0x90
90  // [7-4]: WAKE_ODR=1000 for 12.5Hz ODR and 256x decimation from 3200Hz giving 0x80
91  // [7-4]: WAKE_ODR=0111 for 25Hz ODR and 128x decimation from 3200Hz giving 0x70
92  // [7-4]: WAKE_ODR=0110 for 50Hz ODR and 64x decimation from 3200Hz giving 0x60
93  // [7-4]: WAKE_ODR=0101 for 100Hz ODR and 32x decimation from 3200Hz giving 0x50
94  // [7-4]: WAKE_ODR=0100 for 200Hz ODR and 16x decimation from 3200Hz giving 0x40
95  // [7-4]: WAKE_ODR=0011 for 400Hz ODR and 8x decimation from 3200Hz giving 0x30
96  // [7-4]: WAKE_ODR=0010 for 800Hz ODR and 4x decimation from 3200Hz giving 0x20
97  // [7-4]: WAKE_ODR=0001 for 1600Hz ODR and 2x decimation from 3200Hz giving 0x10
98  // [7-4]: WAKE_ODR=0000 for 3200Hz ODR and 1x decimation from 3200Hz giving 0x00
99  // [3-0]: SLEEP_ODR=0000
100 #if (ACCEL_ODR_HZ <= 1) // select 0.781Hz ODR
101  { FXLS8962_SENS_CONFIG3, 0xC0, 0x00 },
102 #elif (ACCEL_ODR_HZ <= 2) // select 1.5625Hz ODR
103  { FXLS8962_SENS_CONFIG3, 0x80, 0x00 },
104 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
105  { FXLS8962_SENS_CONFIG3, 0xA0, 0x00 },
106 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
107  { FXLS8962_SENS_CONFIG3, 0x90, 0x00 },
108 #elif (ACCEL_ODR_HZ <= 12) // select 12.5Hz ODR
109  { FXLS8962_SENS_CONFIG3, 0x80, 0x00 },
110 #elif (ACCEL_ODR_HZ <= 25) // select 25Hz ODR
111  { FXLS8962_SENS_CONFIG3, 0x70, 0x00 },
112 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
113  { FXLS8962_SENS_CONFIG3, 0x60, 0x00 },
114 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
115  { FXLS8962_SENS_CONFIG3, 0x50, 0x00 },
116 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
117  { FXLS8962_SENS_CONFIG3, 0x40, 0x00 },
118 #elif (ACCEL_ODR_HZ <= 400) // select 400Hz ODR
119  { FXLS8962_SENS_CONFIG3, 0x30, 0x00 },
120 #elif (ACCEL_ODR_HZ <= 800) // select 800Hz ODR
121  { FXLS8962_SENS_CONFIG3, 0x20, 0x00 },
122 #elif (ACCEL_ODR_HZ <= 1600) // select 1600Hz ODR
123  { FXLS8962_SENS_CONFIG3, 0x10, 0x00 },
124 #else // select 3200Hz ODR
125  { FXLS8962_SENS_CONFIG3, 0x00, 0x00 },
126 #endif
127 
128  // write 0000 0011 = 0x03 to SENS_CONFIG1 to configure 4g mode and Active mode
129  // [7]: RST=0 for no software reset
130  // [6-5]: ST_AXIS_SEL=00 to disable self test
131  // [4]: ST_POL=0 (default)
132  // [3]: AWS_EN=0 (default)
133  // [2-1]: FSR=01 for 4g mode
134  // [1]: ACTIVE=1 for standby
135  { FXLS8962_SENS_CONFIG1, 0x03, 0x00 },
137 };
138 
139 // All sensor drivers and initialization functions have the same prototype.
140 // sfg is a pointer to the master "global" sensor fusion structure.
141 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
142 
143 // sfg = pointer to top level (generally global) data structure for sensor fusion
144 int8_t FXLS8962_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
145 {
146  int32_t status;
147  uint8_t reg;
148  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_WHO_AM_I, 1, &reg);
149  if (status==SENSOR_ERROR_NONE) {
150  if (reg==FXLS8962_WHOAMI_VALUE) {
151  sfg->Accel.iWhoAmI = reg;
152  } else {
153  return(SENSOR_ERROR_INIT);
154  }
155  } else {
156  return(status);
157  }
158 
159  // Configure and start the FXLS8962 sensor. This does multiple register writes
160  // (see FXLS8962_Initialization definition above)
161  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_Initialization );
162 
163  // Stash some needed constants in the SF data structure for this sensor
164  sfg->Accel.iCountsPerg = FXLS8962_COUNTSPERG;
165  sfg->Accel.fgPerCount = 1.0F / FXLS8962_COUNTSPERG;
166  sfg->Accel.fgPerCount = 1.0F / FXLS8962_COUNTSPERG;
167 
168  sensor->isInitialized = F_USING_ACCEL;
169  sfg->Accel.isEnabled = true;
170 
171  return (status);
172 }
173 
174 int8_t FXLS8962_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
175 {
176  uint8_t I2C_Buffer[6 * FXLS8962_ACCEL_FIFO_SIZE]; // I2C read buffer
177  int8_t status; // I2C transaction status
178  uint8_t sensor_data_ready = 0;
179  int16_t sample[3];
180 
181  if(sensor->isInitialized != F_USING_ACCEL)
182  {
183  return SENSOR_ERROR_INIT;
184  }
185 
186  // read the FXLS8962_BUF_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
187  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_INT_STATUS_READ, I2C_Buffer );
188  if (status==SENSOR_ERROR_NONE) {
189  sensor_data_ready = I2C_Buffer[0] & 0x80;
190  // return if there are no measurements in the sensor FIFO.
191  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
192  if (sensor_data_ready == 0) return status;
193  } else {
194  return(status);
195  }
196 
197  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_DATA_READ, I2C_Buffer );
198 
199  if (status==SENSOR_ERROR_NONE) {
200  // place the 6 bytes read into the 16 bit accelerometer structure
201  sample[CHX] = (I2C_Buffer[1] << 8) | I2C_Buffer[0];
202  sample[CHY] = (I2C_Buffer[3] << 8) | I2C_Buffer[2];
203  sample[CHZ] = (I2C_Buffer[5] << 8) | I2C_Buffer[4];
204  conditionSample(sample); // truncate negative values to -32767
205  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
206  }
207 
208  return (status);
209 }
210 
211 
212 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
213 const registerwritelist_t FXLS8962_IDLE[] =
214 {
215  // Set ACTIVE = other bits unchanged
216  { FXLS8962_SENS_CONFIG1, 0x00, 0x01 },
218 };
219 
220 // FXLS8962_Idle places the sensor into Standby mode (see datasheet for wakeup time)
221 int8_t FXLS8962_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
222 {
223  int32_t status;
224  if(sensor->isInitialized == F_USING_ACCEL) {
225  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_IDLE );
226  sensor->isInitialized = 0;
227  sfg->Accel.isEnabled = false;
228  } else {
229  return SENSOR_ERROR_INIT;
230  }
231  return status;
232 }
233 #endif // if F_USING_ACCEL
The sensor_drv.h file contains sensor state and error definitions.
#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
SensorFusionGlobals sfg
int32_t status
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.
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define __END_READ_DATA__
Definition: sensor_drv.h:77
The top level fusion structure.
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
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)
#define FXLS8962_COUNTSPERG
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
int8_t FXLS8962_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
This file contains the FXLS8962 Accelerometer register definitions, access macros, and device access functions.
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:113
#define FXLS8962_ACCEL_FIFO_SIZE
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.
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
int8_t FXLS8962_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXLS8962_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
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