ISSDK  1.7
IoT Sensing Software Development Kit
driver_FXLS8471Q.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_FXLS8471Q.c
36  \brief Provides init() and read() functions for the FXLS8471Q 3-axis accel.
37 
38  Supports both I2C and SPI Interfaces. Supply sensor address=0x00 when when
39  installing sensor for SPI. Supply I2C address otherwise.
40 */
41 
42 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
43 #include "sensor_fusion.h" // Sensor fusion structures and types
44 #include "sensor_drv.h" // ISSDK - contains sensor state and error definitions
45 #include "register_io_spi.h" // ISSDK - declares low-level interface functions for single-location reading
46 #include "sensor_io_spi.h" // ISSDK - declares low-level interface functions for multi-location reading
47 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
48 #include "fxls8471q.h"
49 #include "fxls8471q_drv.h" // describes the FXLS8471Q ISSDK driver interface and structures
50 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
51 #define FXLS8471Q_COUNTSPERG 8192.0
52 #define FXLS8471Q_ACCEL_FIFO_SIZE 32
53 
54 // Command definition to read the WHO_AM_I value.
56 {
57  { .readFrom = FXLS8471Q_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
58 };
59 
60 // Command definition to read the number of entries in the accel FIFO.
62 {
63  { .readFrom = FXLS8471Q_F_STATUS, .numBytes = 1 }, __END_READ_DATA__
64 };
65 
66 // Command definition to read the number of entries in the accel FIFO.
68 {
69  { .readFrom = FXLS8471Q_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
70 };
71 
72 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
74 {
75  // write 0000 0000 = 0x00 to CTRL_REG1 to place FXLS8471 into standby
76  // [7-1] = 0000 000
77  // [0]: active=00
78  { FXLS8471Q_CTRL_REG1, 0x00, 0x00 },
79 
80  // Not applicable to the 8452/3.
81  // OK: write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
82  // [7-6]: F_MODE[1-0]=01 for FIFO continuous mode
83  // [5-0]: F_WMRK[5-0]=000000 for no FIFO watermark
85 
86  // write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
87  // [7-5]: reserved=000
88  // [4]: HPF_OUT=0
89  // [3-2]: reserved=00
90  // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
92 
93  // write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
94  // [7]: ST=0: self test disabled
95  // [6]: RST=0: reset disabled
96  // [5]: unused
97  // [4-3]: SMODS=00
98  // [2]: SLPE=0: auto sleep disabled
99  // [1-0]: mods=10 for high resolution (maximum over sampling)
101 
102  // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
103  // [7-6]: aslp_rate=00
104  // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
105  // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
106  // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
107  // [5-3]: dr=100 for 50Hz data rate giving 0x21
108  // [5-3]: dr=011 for 100Hz data rate giving 0x19
109  // [5-3]: dr=010 for 200Hz data rate giving 0x11
110  // [5-3]: dr=001 for 400Hz data rate giving 0x09
111  // [5-3]: dr=000 for 800Hz data rate giving 0x01
112  // [2]: unused=0
113  // [1]: f_read=0 for normal 16 bit reads
114  // [0]: active=1 to take the part out of standby and enable sampling
115 #if (ACCEL_ODR_HZ <= 1)
117 #elif (ACCEL_ODR_HZ <= 6)
119 #elif (ACCEL_ODR_HZ <= 12)
121 #elif (ACCEL_ODR_HZ <= 50)
123 #elif (ACCEL_ODR_HZ <= 100)
125 #elif (ACCEL_ODR_HZ <= 200)
127 #elif (ACCEL_ODR_HZ <= 400)
129 #else
130  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE , 0x00 }, // select 800Hz ODR
131 #endif
133 };
134 
135 // All sensor drivers and initialization functions have the same prototype.
136 // sfg is a pointer to the master "global" sensor fusion structure.
137 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
138 
139 // sfg = pointer to top level (generally global) data structure for sensor fusion
141 {
143  int32_t status;
144  uint8_t reg;
145  if (sensor->addr == SPI_ADDR) {
146  // Initialize the Slave Select Pin.
147  pGPIODriver->pin_init(&FXLS8471_SPI_CS, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
148  // Initialize SPI parameters used by ISSDK SPI routines
154  // Read the WhoAmI
155  status = Register_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_WHO_AM_I, 1, &reg);
156  } else {
157  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_WHO_AM_I, 1, &reg);
158  }
159  if (status==SENSOR_ERROR_NONE) {
160  sfg->Accel.iWhoAmI = reg;
162  } else {
163  return(status);
164  }
165 
166  // Configure and start the FXLS8471Q sensor. This does multiple register writes
167  if (sensor->addr == SPI_ADDR)
168  status = Sensor_SPI_Write(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_Initialization );
169  else
170  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_Initialization );
171 
172  // Stash some needed constants in the SF data structure for this sensor
173  sfg->Accel.iCountsPerg = FXLS8471Q_COUNTSPERG;
174  sfg->Accel.fgPerCount = 1.0F / FXLS8471Q_COUNTSPERG;
175 
176  sensor->isInitialized = F_USING_ACCEL;
177  sfg->Accel.isEnabled = true;
178 
179  return (status);
180 }
181 
183 {
184  uint8_t Data_Buffer[6 * FXLS8471Q_ACCEL_FIFO_SIZE]; // I2C read buffer
185  int8_t status=0; // I2C transaction status
186  int8_t j; // scratch
187  uint16_t sensor_fifo_count = 1;
188  int16_t sample[3];
189 
190  if(sensor->isInitialized != F_USING_ACCEL)
191  {
192  return SENSOR_ERROR_INIT;
193  }
194 
195  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
196  if (sensor->addr == SPI_ADDR)
197  status = Sensor_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_F_STATUS_READ, Data_Buffer );
198  else
199  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_F_STATUS_READ, Data_Buffer );
200 
201  sensor_fifo_count = Data_Buffer[0] & 0x3F;
202  if (status==SENSOR_ERROR_NONE) {
203  // return if there are no measurements in the sensor FIFO.
204  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
205  if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
206  } else {
207  return(status);
208  }
209 
210  FXLS8471Q_DATA_READ->numBytes = 6 * sensor_fifo_count;
211  if (sensor->addr == SPI_ADDR)
212  status = Sensor_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &sensor->slaveParams, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
213  else
214  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
215 
216  if (status==SENSOR_ERROR_NONE) {
217  for (j=0; j<sensor_fifo_count; j++) {
218  sample[CHX] = (Data_Buffer[6*j ] << 8) | Data_Buffer[6*j + 1] ;
219  sample[CHY] = (Data_Buffer[6*j + 2] << 8) | Data_Buffer[6*j + 3] ;
220  sample[CHZ] = (Data_Buffer[6*j + 4] << 8) | Data_Buffer[6*j + 5] ;
221  conditionSample(sample); // truncate negative values to -32767
222  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
223  }
224  }
225 
226  return (status);
227 }
228 
229 
230 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
232 {
233  // Set ACTIVE = other bits unchanged
234  { FXLS8471Q_CTRL_REG1, 0x00, 0x01 },
236 };
237 
238 // FXLS8471Q_Idle places the sensor into Standby mode (wakeup time = 2/ODR+1ms)
240 {
241  int32_t status;
242  if(sensor->isInitialized == F_USING_ACCEL) {
243  if (sensor->addr == SPI_ADDR) {
244  status = Sensor_SPI_Write(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_IDLE );
245  } else {
246  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_IDLE );
247  }
248  sensor->isInitialized = 0;
249  sfg->Accel.isEnabled = false;
250  } else {
251  return SENSOR_ERROR_INIT;
252  }
253  return status;
254 }
The sensor_drv.h file contains sensor state and error definitions.
fpSpiWritePreprocessFn_t pWritePreprocessFN
#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
#define FXLS8471Q_CTRL_REG1_DR_1DOT56HZ
Definition: fxls8471q.h:1697
const registerwritelist_t FXLS8471Q_Initialization[]
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
SensorFusionGlobals sfg
int32_t status
#define FXLS8471Q_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxls8471q_drv.h:82
void FXLS8471Q_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
The SPI Read Pre-Process function to generate Sensor specific SPI Message Header. ...
Definition: fxls8471q_drv.c:56
#define FXLS8471Q_WHO_AM_I_WHOAMI_VALUE
Definition: fxls8471q.h:538
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 FXLS8471Q_CTRL_REG1_DR_50HZ
Definition: fxls8471q.h:1694
#define FXLS8471Q_CTRL_REG2_MODS_HIGHRES
Definition: fxls8471q.h:1754
#define __END_READ_DATA__
Definition: sensor_drv.h:77
registerreadlist_t FXLS8471Q_DATA_READ[]
The top level fusion structure.
void FXLS8471Q_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
The SPI Write Pre-Process function to generate Sensor specific SPI Message Header.
Definition: fxls8471q_drv.c:73
const registerwritelist_t FXLS8471Q_IDLE[]
#define FXLS8471Q_F_SETUP_F_MODE_FIFOMOSTRECENT
Definition: fxls8471q.h:330
#define FXLS8471Q_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxls8471q_drv.h:86
uint16_t addr
I2C address if applicable.
#define FXLS8471Q_CTRL_REG1_DR_200HZ
Definition: fxls8471q.h:1692
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 FXLS8471Q_COUNTSPERG
#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)
spiSlaveSpecificParams_t slaveParams
SPI specific parameters. Not used for I2C.
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
#define FXLS8471Q_CTRL_REG1_MODE_ACTIVE
Definition: fxls8471q.h:1685
int8_t FXLS8471Q_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define SPI_ADDR
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
The fxls8471q.h file contains the register definitions for fxls8471q sensor driver.
The sensor_io_spi.h file declares low-level interface functions for reading and writing sensor regist...
GENERIC_DRIVER_GPIO * pGPIODriver
int32_t Sensor_SPI_Write(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_spi.c:97
#define FXLS8471Q_CTRL_REG1_DR_6DOT25HZ
Definition: fxls8471q.h:1696
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXLS8471_SPI_CS
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
int32_t Register_SPI_Read(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pReadParams, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
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.
int8_t FXLS8471Q_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
This structure defines the Write command List.
Definition: sensor_drv.h:94
#define FXLS8471Q_CTRL_REG1_DR_12DOT5HZ
Definition: fxls8471q.h:1695
#define FXLS8471Q_ACCEL_FIFO_SIZE
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 FXLS8471Q_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int32_t Sensor_SPI_Read(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pReadParams, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
#define FXLS8471Q_CTRL_REG1_DR_100HZ
Definition: fxls8471q.h:1693
const registerreadlist_t FXLS8471Q_F_STATUS_READ[]
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
The register_io_spi.h file declares low-level interface functions for reading and writing sensor regi...
const registerreadlist_t FXLS8471Q_WHO_AM_I_READ[]
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
#define FXLS8471Q_CTRL_REG1_DR_400HZ
Definition: fxls8471q.h:1691
fpSpiReadPreprocessFn_t pReadPreprocessFN
#define FXLS8471Q_XYZ_DATA_CFG_FS_FS_RANGE_4G
Definition: fxls8471q.h:573
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