ISSDK  1.8
IoT Sensing Software Development Kit
driver_FXLS8471Q.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_FXLS8471Q.c
10  \brief Provides init() and read() functions for the FXLS8471Q 3-axis accel.
11 
12  Supports both I2C and SPI Interfaces. Supply sensor address=0x00 when when
13  installing sensor for SPI. Supply I2C address otherwise.
14 */
15 
16 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
17 #include "sensor_fusion.h" // Sensor fusion structures and types
18 #include "sensor_drv.h" // ISSDK - contains sensor state and error definitions
19 #include "register_io_spi.h" // ISSDK - declares low-level interface functions for single-location reading
20 #include "sensor_io_spi.h" // ISSDK - declares low-level interface functions for multi-location reading
21 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
22 #include "fxls8471q.h"
23 #include "fxls8471q_drv.h" // describes the FXLS8471Q ISSDK driver interface and structures
24 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
25 #define FXLS8471Q_COUNTSPERG 8192.0
26 #define FXLS8471Q_ACCEL_FIFO_SIZE 32
27 
28 // Command definition to read the WHO_AM_I value.
30 {
31  { .readFrom = FXLS8471Q_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
32 };
33 
34 // Command definition to read the number of entries in the accel FIFO.
36 {
37  { .readFrom = FXLS8471Q_F_STATUS, .numBytes = 1 }, __END_READ_DATA__
38 };
39 
40 // Command definition to read the number of entries in the accel FIFO.
42 {
43  { .readFrom = FXLS8471Q_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
44 };
45 
46 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
48 {
49  // write 0000 0000 = 0x00 to CTRL_REG1 to place FXLS8471 into standby
50  // [7-1] = 0000 000
51  // [0]: active=00
52  { FXLS8471Q_CTRL_REG1, 0x00, 0x00 },
53 
54  // Not applicable to the 8452/3.
55  // OK: write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
56  // [7-6]: F_MODE[1-0]=01 for FIFO continuous mode
57  // [5-0]: F_WMRK[5-0]=000000 for no FIFO watermark
59 
60  // write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
61  // [7-5]: reserved=000
62  // [4]: HPF_OUT=0
63  // [3-2]: reserved=00
64  // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
66 
67  // write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
68  // [7]: ST=0: self test disabled
69  // [6]: RST=0: reset disabled
70  // [5]: unused
71  // [4-3]: SMODS=00
72  // [2]: SLPE=0: auto sleep disabled
73  // [1-0]: mods=10 for high resolution (maximum over sampling)
75 
76  // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
77  // [7-6]: aslp_rate=00
78  // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
79  // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
80  // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
81  // [5-3]: dr=100 for 50Hz data rate giving 0x21
82  // [5-3]: dr=011 for 100Hz data rate giving 0x19
83  // [5-3]: dr=010 for 200Hz data rate giving 0x11
84  // [5-3]: dr=001 for 400Hz data rate giving 0x09
85  // [5-3]: dr=000 for 800Hz data rate giving 0x01
86  // [2]: unused=0
87  // [1]: f_read=0 for normal 16 bit reads
88  // [0]: active=1 to take the part out of standby and enable sampling
89 #if (ACCEL_ODR_HZ <= 1)
91 #elif (ACCEL_ODR_HZ <= 6)
93 #elif (ACCEL_ODR_HZ <= 12)
95 #elif (ACCEL_ODR_HZ <= 50)
97 #elif (ACCEL_ODR_HZ <= 100)
99 #elif (ACCEL_ODR_HZ <= 200)
101 #elif (ACCEL_ODR_HZ <= 400)
103 #else
104  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE , 0x00 }, // select 800Hz ODR
105 #endif
107 };
108 
109 // All sensor drivers and initialization functions have the same prototype.
110 // sfg is a pointer to the master "global" sensor fusion structure.
111 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
112 
113 // sfg = pointer to top level (generally global) data structure for sensor fusion
115 {
117  int32_t status;
118  uint8_t reg;
119  if (sensor->addr == SPI_ADDR) {
120  // Initialize the Slave Select Pin.
121  pGPIODriver->pin_init(&FXLS8471_SPI_CS, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
122  // Initialize SPI parameters used by ISSDK SPI routines
128  // Read the WhoAmI
129  status = Register_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_WHO_AM_I, 1, &reg);
130  } else {
131  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_WHO_AM_I, 1, &reg);
132  }
133  if (status==SENSOR_ERROR_NONE) {
134  sfg->Accel.iWhoAmI = reg;
136  } else {
137  return(status);
138  }
139 
140  // Configure and start the FXLS8471Q sensor. This does multiple register writes
141  if (sensor->addr == SPI_ADDR)
142  status = Sensor_SPI_Write(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_Initialization );
143  else
144  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_Initialization );
145 
146  // Stash some needed constants in the SF data structure for this sensor
147  sfg->Accel.iCountsPerg = FXLS8471Q_COUNTSPERG;
148  sfg->Accel.fgPerCount = 1.0F / FXLS8471Q_COUNTSPERG;
149 
150  sensor->isInitialized = F_USING_ACCEL;
151  sfg->Accel.isEnabled = true;
152 
153  return (status);
154 }
155 
157 {
158  uint8_t Data_Buffer[6 * FXLS8471Q_ACCEL_FIFO_SIZE]; // I2C read buffer
159  int8_t status=0; // I2C transaction status
160  int8_t j; // scratch
161  uint16_t sensor_fifo_count = 1;
162  int16_t sample[3];
163 
164  if(sensor->isInitialized != F_USING_ACCEL)
165  {
166  return SENSOR_ERROR_INIT;
167  }
168 
169  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
170  if (sensor->addr == SPI_ADDR)
171  status = Sensor_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_F_STATUS_READ, Data_Buffer );
172  else
173  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_F_STATUS_READ, Data_Buffer );
174 
175  sensor_fifo_count = Data_Buffer[0] & 0x3F;
176  if (status==SENSOR_ERROR_NONE) {
177  // return if there are no measurements in the sensor FIFO.
178  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
179  if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
180  } else {
181  return(status);
182  }
183 
184  FXLS8471Q_DATA_READ->numBytes = 6 * sensor_fifo_count;
185  if (sensor->addr == SPI_ADDR)
186  status = Sensor_SPI_Read(sensor->bus_driver, &sensor->deviceInfo, &sensor->slaveParams, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
187  else
188  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
189 
190  if (status==SENSOR_ERROR_NONE) {
191  for (j=0; j<sensor_fifo_count; j++) {
192  sample[CHX] = (Data_Buffer[6*j ] << 8) | Data_Buffer[6*j + 1] ;
193  sample[CHY] = (Data_Buffer[6*j + 2] << 8) | Data_Buffer[6*j + 3] ;
194  sample[CHZ] = (Data_Buffer[6*j + 4] << 8) | Data_Buffer[6*j + 5] ;
195  conditionSample(sample); // truncate negative values to -32767
196  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
197  }
198  }
199 
200  return (status);
201 }
202 
203 
204 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
206 {
207  // Set ACTIVE = other bits unchanged
208  { FXLS8471Q_CTRL_REG1, 0x00, 0x01 },
210 };
211 
212 // FXLS8471Q_Idle places the sensor into Standby mode (wakeup time = 2/ODR+1ms)
214 {
215  int32_t status;
216  if(sensor->isInitialized == F_USING_ACCEL) {
217  if (sensor->addr == SPI_ADDR) {
218  status = Sensor_SPI_Write(sensor->bus_driver, &sensor->deviceInfo, &(sensor->slaveParams), FXLS8471Q_IDLE );
219  } else {
220  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8471Q_IDLE );
221  }
222  sensor->isInitialized = 0;
223  sfg->Accel.isEnabled = false;
224  } else {
225  return SENSOR_ERROR_INIT;
226  }
227  return status;
228 }
const registerwritelist_t FXLS8471Q_Initialization[]
#define FXLS8471_SPI_CS
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:47
#define FXLS8471Q_F_SETUP_F_MODE_FIFOMOSTRECENT
Definition: fxls8471q.h:304
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
The sensor_io_spi.h file declares low-level interface functions for reading and writing sensor regist...
#define FXLS8471Q_CTRL_REG1_DR_1DOT56HZ
Definition: fxls8471q.h:1671
int32_t status
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
The fxls8471q.h file contains the register definitions for fxls8471q sensor driver.
int8_t FXLS8471Q_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define FXLS8471Q_ACCEL_FIFO_SIZE
GENERIC_DRIVER_GPIO * pGPIODriver
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.
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define FXLS8471Q_XYZ_DATA_CFG_FS_FS_RANGE_4G
Definition: fxls8471q.h:547
#define FXLS8471Q_CTRL_REG2_MODS_HIGHRES
Definition: fxls8471q.h:1728
The register_io_spi.h file declares low-level interface functions for reading and writing sensor regi...
#define FXLS8471Q_CTRL_REG1_MODE_ACTIVE
Definition: fxls8471q.h:1659
spiSlaveSpecificParams_t slaveParams
SPI specific parameters. Not used for I2C.
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
#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
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
registerreadlist_t FXLS8471Q_DATA_READ[]
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
const registerwritelist_t FXLS8471Q_IDLE[]
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 FXLS8471Q_WHO_AM_I_WHOAMI_VALUE
Definition: fxls8471q.h:512
#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 FXLS8471Q_CTRL_REG1_DR_6DOT25HZ
Definition: fxls8471q.h:1670
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 ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define SPI_ADDR
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.
#define FXLS8471Q_CTRL_REG1_DR_400HZ
Definition: fxls8471q.h:1665
fpSpiReadPreprocessFn_t pReadPreprocessFN
int8_t FXLS8471Q_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
const registerreadlist_t FXLS8471Q_F_STATUS_READ[]
The sensor_fusion.h file implements the top level programming interface.
#define FXLS8471Q_CTRL_REG1_DR_12DOT5HZ
Definition: fxls8471q.h:1669
#define __END_READ_DATA__
Definition: sensor_drv.h:51
int8_t FXLS8471Q_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define FXLS8471Q_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxls8471q_drv.h:56
#define FXLS8471Q_CTRL_REG1_DR_50HZ
Definition: fxls8471q.h:1668
SensorFusionGlobals sfg
#define FXLS8471Q_CTRL_REG1_DR_100HZ
Definition: fxls8471q.h:1667
uint16_t readFrom
Definition: sensor_drv.h:80
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
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:30
const registerreadlist_t FXLS8471Q_WHO_AM_I_READ[]
#define FXLS8471Q_CTRL_REG1_DR_200HZ
Definition: fxls8471q.h:1666
#define FXLS8471Q_COUNTSPERG
fpSpiWritePreprocessFn_t pWritePreprocessFN
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
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.
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:71
#define FXLS8471Q_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxls8471q_drv.h:60
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.
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...