ISSDK  1.7
IoT Sensing Software Development Kit
driver_FXLS8952.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_FXLS8952.c
36  \brief Provides init() and read() functions for the FXLS8952 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 "FXLS8952.h" // describes the FXLS8952 register definition and its bit mask
44 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
45 #define FXLS8952_COUNTSPERG 512
46 #define FXLS8952_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 FXLS8952_WHO_AM_I_READ[] =
52 {
53  { .readFrom = FXLS8952_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 FXLS8952_BUF_STATUS_READ[] =
58 {
59  { .readFrom = FXLS8952_BUF_STATUS, .numBytes = 1 }, __END_READ_DATA__
60 };
61 
62 // Command definition to read the number of entries in the accel FIFO.
63 registerreadlist_t FXLS8952_DATA_READ[] =
64 {
65  { .readFrom = FXLS8952_BUF_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 FXLS8952_Initialization[] =
70 {
71  // write 0000 0000 = 0x00 to SENS_CONFIG1 to place FXLS8952 into standby
72  // [7-1]: Set when taking the part out of standby
73  // [0]: ACTIVE=0 for standby
74  { FXLS8952_SENS_CONFIG1, 0x00, 0x00 },
75 
76  // write 0010 0000 = 0x20 to BUF_CONFIG1 to enable FIFO in continuous (circular) mode
77  // [7]: BUF_TYPE=0 for FIFO (not FILO) operation
78  // [6-5]: BUF_MODE[1-0]=01 for FIFO continuous mode
79  // [4]: BUF_GATE=0 to flush buffer on transitions
80  // [3]: TRIG_SDCD_AND=0
81  // [2]: TRIG_SDCD_OR=0
82  // [1] Reserved=0
83  // [0]: TRIG_ORIENT=0
84  { FXLS8952_BUF_CONFIG1, 0x20, 0x00 },
85 
86  // write 0101 0000 = 0x50 to SENS_CONFIG2 register to set High Performance Mode
87  // [7-6]: WAKE_PM=01 for High Performance Mode
88  // [5-4]: SLEEP_PM=01 for High Performance Mode
89  // [3]: LE_BE=0 for Little Endian
90  // [2]: BLOCK=0 for normal latching
91  // [1]: AINC_TEMP=0 to not include temperature in auto increment
92  // [0]: F_READ=0 for normal 12 bit reads
93  { FXLS8952_SENS_CONFIG2, 0x50, 0x00 },
94 
95  // write XXXX 0000 = to SENS_CONFIG3 register to set ODR
96  // [7-4]: WAKE_ODR=1100 for 0.781Hz ODR and 4096x decimation from 3200Hz giving 0xC0
97  // [7-4]: WAKE_ODR=1011 for 1.56Hz ODR and 12048x decimation from 3200Hz giving 0xB0
98  // [7-4]: WAKE_ODR=1010 for 3.125Hz ODR and 1024x decimation from 3200Hz giving 0xA0
99  // [7-4]: WAKE_ODR=1001 for 6.25Hz ODR and 512x decimation from 3200Hz giving 0x90
100  // [7-4]: WAKE_ODR=1000 for 12.5Hz ODR and 256x decimation from 3200Hz giving 0x80
101  // [7-4]: WAKE_ODR=0111 for 25Hz ODR and 128x decimation from 3200Hz giving 0x70
102  // [7-4]: WAKE_ODR=0110 for 50Hz ODR and 64x decimation from 3200Hz giving 0x60
103  // [7-4]: WAKE_ODR=0101 for 100Hz ODR and 32x decimation from 3200Hz giving 0x50
104  // [7-4]: WAKE_ODR=0100 for 200Hz ODR and 16x decimation from 3200Hz giving 0x40
105  // [7-4]: WAKE_ODR=0011 for 400Hz ODR and 8x decimation from 3200Hz giving 0x30
106  // [7-4]: WAKE_ODR=0010 for 800Hz ODR and 4x decimation from 3200Hz giving 0x20
107  // [7-4]: WAKE_ODR=0001 for 1600Hz ODR and 2x decimation from 3200Hz giving 0x10
108  // [7-4]: WAKE_ODR=0000 for 3200Hz ODR and 1x decimation from 3200Hz giving 0x00
109  // [3-0]: SLEEP_ODR=0000
110 #if (ACCEL_ODR_HZ <= 1) // select 0.781Hz ODR
111  { FXLS8952_SENS_CONFIG3, 0xC0, 0x00 },
112 #elif (ACCEL_ODR_HZ <= 2) // select 1.5625Hz ODR
113  { FXLS8952_SENS_CONFIG3, 0x80, 0x00 },
114 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
115  { FXLS8952_SENS_CONFIG3, 0xA0, 0x00 },
116 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
117  { FXLS8952_SENS_CONFIG3, 0x90, 0x00 },
118 #elif (ACCEL_ODR_HZ <= 12) // select 12.5Hz ODR
119  { FXLS8952_SENS_CONFIG3, 0x80, 0x00 },
120 #elif (ACCEL_ODR_HZ <= 25) // select 25Hz ODR
121  { FXLS8952_SENS_CONFIG3, 0x70, 0x00 },
122 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
123  { FXLS8952_SENS_CONFIG3, 0x60, 0x00 },
124 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
125  { FXLS8952_SENS_CONFIG3, 0x50, 0x00 },
126 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
127  { FXLS8952_SENS_CONFIG3, 0x40, 0x00 },
128 #elif (ACCEL_ODR_HZ <= 400) // select 400Hz ODR
129  { FXLS8952_SENS_CONFIG3, 0x30, 0x00 },
130 #elif (ACCEL_ODR_HZ <= 800) // select 800Hz ODR
131  { FXLS8952_SENS_CONFIG3, 0x20, 0x00 },
132 #elif (ACCEL_ODR_HZ <= 1600) // select 1600Hz ODR
133  { FXLS8952_SENS_CONFIG3, 0x10, 0x00 },
134 #else // select 3200Hz ODR
135  { FXLS8952_SENS_CONFIG3, 0x00, 0x00 },
136 #endif
137 
138  // write 0000 0011 = 0x03 to SENS_CONFIG1 to configure 4g mode and Active mode
139  // [7]: RST=0 for no software reset
140  // [6-5]: ST_AXIS_SEL=00 to disable self test
141  // [4]: ST_POL=0 (default)
142  // [3]: AWS_EN=0 (default)
143  // [2-1]: FSR=01 for 4g mode
144  // [1]: ACTIVE=1 for standby
145  { FXLS8952_SENS_CONFIG1, 0x03, 0x00 },
147 };
148 
149 // All sensor drivers and initialization functions have the same prototype.
150 // sfg is a pointer to the master "global" sensor fusion structure.
151 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
152 
153 // sfg = pointer to top level (generally global) data structure for sensor fusion
154 int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
155 {
156  int32_t status;
157  uint8_t reg;
158  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_WHO_AM_I, 1, &reg);
159  if (status==SENSOR_ERROR_NONE) {
160  if (reg==FXLS8952_WHOAMI_VALUE) {
161  sfg->Accel.iWhoAmI = reg;
162  } else {
163  return(SENSOR_ERROR_INIT);
164  }
165  } else {
166  return(status);
167  }
168 
169  // Configure and start the FXLS8952 sensor. This does multiple register writes
170  // (see FXLS8952_Initialization definition above)
171  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_Initialization );
172 
173  // Stash some needed constants in the SF data structure for this sensor
174  sfg->Accel.iCountsPerg = FXLS8952_COUNTSPERG;
175  sfg->Accel.fgPerCount = 1.0F / FXLS8952_COUNTSPERG;
176  sfg->Accel.fgPerCount = 1.0F / FXLS8952_COUNTSPERG;
177 
178  sensor->isInitialized = F_USING_ACCEL;
179  sfg->Accel.isEnabled = true;
180 
181  return (status);
182 }
183 
184 int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
185 {
186  uint8_t I2C_Buffer[6 * FXLS8952_ACCEL_FIFO_SIZE]; // I2C read buffer
187  int8_t status; // I2C transaction status
188  int8_t j; // scratch
189  uint8_t sensor_fifo_count = 1;
190  int16_t sample[3];
191 
192  if(sensor->isInitialized != F_USING_ACCEL)
193  {
194  return SENSOR_ERROR_INIT;
195  }
196 
197  // read the XLS8952_BUF_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
198  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_BUF_STATUS_READ, I2C_Buffer );
199  if (status==SENSOR_ERROR_NONE) {
200  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
201  // return if there are no measurements in the sensor FIFO.
202  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
203  if (sensor_fifo_count == 0) return status;
204  } else {
205  return(status);
206  }
207 
208  FXLS8952_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
209  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_DATA_READ, I2C_Buffer );
210 
211  if (status==SENSOR_ERROR_NONE) {
212  // process the pthisAccel->iFIFOCount * three channels of data
213  for (j = 0; j < sensor_fifo_count; j++)
214  {
215  // place the 6 bytes read into the 16 bit accelerometer structure
216  sample[CHX] = (I2C_Buffer[6 * j + 1] << 8) | I2C_Buffer[6 * j + 0];
217  sample[CHY] = (I2C_Buffer[6 * j + 3] << 8) | I2C_Buffer[6 * j + 2];
218  sample[CHZ] = (I2C_Buffer[6 * j + 5] << 8) | I2C_Buffer[6 * j + 4];
219  conditionSample(sample); // truncate negative values to -32767
220  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
221  }
222  }
223 
224  return (status);
225 }
226 
227 
228 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
229 const registerwritelist_t FXLS8952_IDLE[] =
230 {
231  // Set ACTIVE = other bits unchanged
232  { FXLS8952_SENS_CONFIG1, 0x00, 0x01 },
234 };
235 
236 // FXLS8952_Idle places the sensor into Standby mode (see datasheet for wakeup time)
237 int8_t FXLS8952_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
238 {
239  int32_t status;
240  if(sensor->isInitialized == F_USING_ACCEL) {
241  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_IDLE );
242  sensor->isInitialized = 0;
243  sfg->Accel.isEnabled = false;
244  } else {
245  return SENSOR_ERROR_INIT;
246  }
247  return status;
248 }
249 #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
int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
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)
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
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.
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
#define FXLS8952_ACCEL_FIFO_SIZE
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
int8_t FXLS8952_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
#define FXLS8952_COUNTSPERG
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