ISSDK  1.8
IoT Sensing Software Development Kit
driver_FXLS8952.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /*! \file driver_FXLS8952.c
10  \brief Provides init() and read() functions for the FXLS8952 3-axis accelerometer
11 */
12 
13 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
14 #include "sensor_fusion.h" // Sensor fusion structures and types
15 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
16 #include "sensor_drv.h" // Contains sensor state and error definitions
17 #include "FXLS8952.h" // describes the FXLS8952 register definition and its bit mask
18 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
19 #define FXLS8952_COUNTSPERG 512
20 #define FXLS8952_ACCEL_FIFO_SIZE 32
21 
22 #if F_USING_ACCEL
23 
24 // Command definition to read the WHO_AM_I value.
25 const registerreadlist_t FXLS8952_WHO_AM_I_READ[] =
26 {
27  { .readFrom = FXLS8952_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
28 };
29 
30 // Command definition to read the number of entries in the accel FIFO.
31 const registerreadlist_t FXLS8952_BUF_STATUS_READ[] =
32 {
33  { .readFrom = FXLS8952_BUF_STATUS, .numBytes = 1 }, __END_READ_DATA__
34 };
35 
36 // Command definition to read the number of entries in the accel FIFO.
37 registerreadlist_t FXLS8952_DATA_READ[] =
38 {
39  { .readFrom = FXLS8952_BUF_X_LSB, .numBytes = 6 }, __END_READ_DATA__
40 };
41 
42 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
43 const registerwritelist_t FXLS8952_Initialization[] =
44 {
45  // write 0000 0000 = 0x00 to SENS_CONFIG1 to place FXLS8952 into standby
46  // [7-1]: Set when taking the part out of standby
47  // [0]: ACTIVE=0 for standby
48  { FXLS8952_SENS_CONFIG1, 0x00, 0x00 },
49 
50  // write 0010 0000 = 0x20 to BUF_CONFIG1 to enable FIFO in continuous (circular) mode
51  // [7]: BUF_TYPE=0 for FIFO (not FILO) operation
52  // [6-5]: BUF_MODE[1-0]=01 for FIFO continuous mode
53  // [4]: BUF_GATE=0 to flush buffer on transitions
54  // [3]: TRIG_SDCD_AND=0
55  // [2]: TRIG_SDCD_OR=0
56  // [1] Reserved=0
57  // [0]: TRIG_ORIENT=0
58  { FXLS8952_BUF_CONFIG1, 0x20, 0x00 },
59 
60  // write 0101 0000 = 0x50 to SENS_CONFIG2 register to set High Performance Mode
61  // [7-6]: WAKE_PM=01 for High Performance Mode
62  // [5-4]: SLEEP_PM=01 for High Performance Mode
63  // [3]: LE_BE=0 for Little Endian
64  // [2]: BLOCK=0 for normal latching
65  // [1]: AINC_TEMP=0 to not include temperature in auto increment
66  // [0]: F_READ=0 for normal 12 bit reads
67  { FXLS8952_SENS_CONFIG2, 0x50, 0x00 },
68 
69  // write XXXX 0000 = to SENS_CONFIG3 register to set ODR
70  // [7-4]: WAKE_ODR=1100 for 0.781Hz ODR and 4096x decimation from 3200Hz giving 0xC0
71  // [7-4]: WAKE_ODR=1011 for 1.56Hz ODR and 12048x decimation from 3200Hz giving 0xB0
72  // [7-4]: WAKE_ODR=1010 for 3.125Hz ODR and 1024x decimation from 3200Hz giving 0xA0
73  // [7-4]: WAKE_ODR=1001 for 6.25Hz ODR and 512x decimation from 3200Hz giving 0x90
74  // [7-4]: WAKE_ODR=1000 for 12.5Hz ODR and 256x decimation from 3200Hz giving 0x80
75  // [7-4]: WAKE_ODR=0111 for 25Hz ODR and 128x decimation from 3200Hz giving 0x70
76  // [7-4]: WAKE_ODR=0110 for 50Hz ODR and 64x decimation from 3200Hz giving 0x60
77  // [7-4]: WAKE_ODR=0101 for 100Hz ODR and 32x decimation from 3200Hz giving 0x50
78  // [7-4]: WAKE_ODR=0100 for 200Hz ODR and 16x decimation from 3200Hz giving 0x40
79  // [7-4]: WAKE_ODR=0011 for 400Hz ODR and 8x decimation from 3200Hz giving 0x30
80  // [7-4]: WAKE_ODR=0010 for 800Hz ODR and 4x decimation from 3200Hz giving 0x20
81  // [7-4]: WAKE_ODR=0001 for 1600Hz ODR and 2x decimation from 3200Hz giving 0x10
82  // [7-4]: WAKE_ODR=0000 for 3200Hz ODR and 1x decimation from 3200Hz giving 0x00
83  // [3-0]: SLEEP_ODR=0000
84 #if (ACCEL_ODR_HZ <= 1) // select 0.781Hz ODR
85  { FXLS8952_SENS_CONFIG3, 0xC0, 0x00 },
86 #elif (ACCEL_ODR_HZ <= 2) // select 1.5625Hz ODR
87  { FXLS8952_SENS_CONFIG3, 0x80, 0x00 },
88 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
89  { FXLS8952_SENS_CONFIG3, 0xA0, 0x00 },
90 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
91  { FXLS8952_SENS_CONFIG3, 0x90, 0x00 },
92 #elif (ACCEL_ODR_HZ <= 12) // select 12.5Hz ODR
93  { FXLS8952_SENS_CONFIG3, 0x80, 0x00 },
94 #elif (ACCEL_ODR_HZ <= 25) // select 25Hz ODR
95  { FXLS8952_SENS_CONFIG3, 0x70, 0x00 },
96 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
97  { FXLS8952_SENS_CONFIG3, 0x60, 0x00 },
98 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
99  { FXLS8952_SENS_CONFIG3, 0x50, 0x00 },
100 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
101  { FXLS8952_SENS_CONFIG3, 0x40, 0x00 },
102 #elif (ACCEL_ODR_HZ <= 400) // select 400Hz ODR
103  { FXLS8952_SENS_CONFIG3, 0x30, 0x00 },
104 #elif (ACCEL_ODR_HZ <= 800) // select 800Hz ODR
105  { FXLS8952_SENS_CONFIG3, 0x20, 0x00 },
106 #elif (ACCEL_ODR_HZ <= 1600) // select 1600Hz ODR
107  { FXLS8952_SENS_CONFIG3, 0x10, 0x00 },
108 #else // select 3200Hz ODR
109  { FXLS8952_SENS_CONFIG3, 0x00, 0x00 },
110 #endif
111 
112  // write 0000 0011 = 0x03 to SENS_CONFIG1 to configure 4g mode and Active mode
113  // [7]: RST=0 for no software reset
114  // [6-5]: ST_AXIS_SEL=00 to disable self test
115  // [4]: ST_POL=0 (default)
116  // [3]: AWS_EN=0 (default)
117  // [2-1]: FSR=01 for 4g mode
118  // [1]: ACTIVE=1 for standby
119  { FXLS8952_SENS_CONFIG1, 0x03, 0x00 },
121 };
122 
123 // All sensor drivers and initialization functions have the same prototype.
124 // sfg is a pointer to the master "global" sensor fusion structure.
125 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
126 
127 // sfg = pointer to top level (generally global) data structure for sensor fusion
128 int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
129 {
130  int32_t status;
131  uint8_t reg;
132  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_WHO_AM_I, 1, &reg);
133  if (status==SENSOR_ERROR_NONE) {
134  if (reg==FXLS8952_WHOAMI_VALUE) {
135  sfg->Accel.iWhoAmI = reg;
136  } else {
137  return(SENSOR_ERROR_INIT);
138  }
139  } else {
140  return(status);
141  }
142 
143  // Configure and start the FXLS8952 sensor. This does multiple register writes
144  // (see FXLS8952_Initialization definition above)
145  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_Initialization );
146 
147  // Stash some needed constants in the SF data structure for this sensor
148  sfg->Accel.iCountsPerg = FXLS8952_COUNTSPERG;
149  sfg->Accel.fgPerCount = 1.0F / FXLS8952_COUNTSPERG;
150  sfg->Accel.fgPerCount = 1.0F / FXLS8952_COUNTSPERG;
151 
152  sensor->isInitialized = F_USING_ACCEL;
153  sfg->Accel.isEnabled = true;
154 
155  return (status);
156 }
157 
158 int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
159 {
160  uint8_t I2C_Buffer[6 * FXLS8952_ACCEL_FIFO_SIZE]; // I2C read buffer
161  int8_t status; // I2C transaction status
162  int8_t j; // scratch
163  uint8_t sensor_fifo_count = 1;
164  int16_t sample[3];
165 
166  if(sensor->isInitialized != F_USING_ACCEL)
167  {
168  return SENSOR_ERROR_INIT;
169  }
170 
171  // read the XLS8952_BUF_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
172  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_BUF_STATUS_READ, I2C_Buffer );
173  if (status==SENSOR_ERROR_NONE) {
174  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
175  // return if there are no measurements in the sensor FIFO.
176  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
177  if (sensor_fifo_count == 0) return status;
178  } else {
179  return(status);
180  }
181 
182  FXLS8952_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
183  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_DATA_READ, I2C_Buffer );
184 
185  if (status==SENSOR_ERROR_NONE) {
186  // process the pthisAccel->iFIFOCount * three channels of data
187  for (j = 0; j < sensor_fifo_count; j++)
188  {
189  // place the 6 bytes read into the 16 bit accelerometer structure
190  sample[CHX] = (I2C_Buffer[6 * j + 1] << 8) | I2C_Buffer[6 * j + 0];
191  sample[CHY] = (I2C_Buffer[6 * j + 3] << 8) | I2C_Buffer[6 * j + 2];
192  sample[CHZ] = (I2C_Buffer[6 * j + 5] << 8) | I2C_Buffer[6 * j + 4];
193  conditionSample(sample); // truncate negative values to -32767
194  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
195  }
196  }
197 
198  return (status);
199 }
200 
201 
202 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
203 const registerwritelist_t FXLS8952_IDLE[] =
204 {
205  // Set ACTIVE = other bits unchanged
206  { FXLS8952_SENS_CONFIG1, 0x00, 0x01 },
208 };
209 
210 // FXLS8952_Idle places the sensor into Standby mode (see datasheet for wakeup time)
211 int8_t FXLS8952_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
212 {
213  int32_t status;
214  if(sensor->isInitialized == F_USING_ACCEL) {
215  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8952_IDLE );
216  sensor->isInitialized = 0;
217  sfg->Accel.isEnabled = false;
218  } else {
219  return SENSOR_ERROR_INIT;
220  }
221  return status;
222 }
223 #endif // if F_USING_ACCEL
#define FXLS8952_ACCEL_FIFO_SIZE
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.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
Provides function prototypes for driver level interfaces.
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 FXLS8952_COUNTSPERG
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:62
void addToFifo(union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The sensor_drv.h file contains sensor state and error definitions.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
The top level fusion structure.
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:61
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
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:71
registerDeviceInfo_t deviceInfo
I2C device context.
The sensor_fusion.h file implements the top level programming interface.
#define __END_READ_DATA__
Definition: sensor_drv.h:51
SensorFusionGlobals sfg
uint16_t readFrom
Definition: sensor_drv.h:80
int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
int8_t FXLS8952_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:60
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
uint16_t addr
I2C address if applicable.
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...