ISSDK  1.8
IoT Sensing Software Development Kit
fxls896x_drv.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxls896x_drv.c
10  * @brief The fxls896x_drv.c file implements the FXLS8964/67 sensor driver interfaces.
11  */
12 
13 //-----------------------------------------------------------------------
14 // ISSDK Includes
15 //-----------------------------------------------------------------------
16 #include "gpio_driver.h"
17 #include "fxls896x_drv.h"
18 #include "systick_utils.h"
19 
20 //-----------------------------------------------------------------------
21 // Global Variables
22 //-----------------------------------------------------------------------
26 
27 //-----------------------------------------------------------------------
28 // Functions
29 //-----------------------------------------------------------------------
30 void FXLS896x_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
31 {
32  spiCmdParams_t *pSlaveCmd = pCmdOut;
33 
34  uint8_t *pWBuff = fxls896x_spiRead_CmdBuffer;
35  uint8_t *pRBuff = fxls896x_spiRead_DataBuffer;
36 
37  /* Formatting for Read command of FXLS896x SENSOR. */
38  *(pWBuff) = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
39  *(pWBuff + 1) = 0x00;
40 
41  /* Create the slave read command. */
42  pSlaveCmd->size = size + FXLS896x_SPI_CMD_LEN;
43  pSlaveCmd->pWriteBuffer = pWBuff;
44  pSlaveCmd->pReadBuffer = pRBuff;
45 }
46 
47 void FXLS896x_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
48 {
49  spiCmdParams_t *pSlaveCmd = pCmdOut;
50 
51  uint8_t *pWBuff = fxls896x_spiWrite_CmdDataBuffer;
52  uint8_t *pRBuff = fxls896x_spiWrite_CmdDataBuffer + size + FXLS896x_SPI_CMD_LEN;
53 
54  /* Formatting for Write command of FXLS896x SENSOR. */
55  *(pWBuff) = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
56  *(pWBuff + 1) = 0x00;
57 
58  /* Copy the slave write command */
59  memcpy(pWBuff + FXLS896x_SPI_CMD_LEN, pWritebuffer, size);
60 
61  /* Create the slave command. */
62  pSlaveCmd->size = size + FXLS896x_SPI_CMD_LEN;
63  pSlaveCmd->pWriteBuffer = pWBuff;
64  pSlaveCmd->pReadBuffer = pRBuff;
65 }
66 
68  fxls896x_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t *whoami)
69 {
71  uint8_t reg;
73 
74  /*! Check the input parameters. */
75  if ((pSensorHandle == NULL) || (pBus == NULL) || (pSlaveSelect == NULL))
76  {
78  }
79 
80  /*! Initialize the sensor handle. */
81  pSensorHandle->pCommDrv = pBus;
84  pSensorHandle->slaveParams.pTargetSlavePinID = pSlaveSelect;
87 
88  pSensorHandle->deviceInfo.deviceInstance = index;
89  pSensorHandle->deviceInfo.functionParam = NULL;
90  pSensorHandle->deviceInfo.idleFunction = NULL;
91 
92  /* Initialize the Slave Select Pin. */
93  pGPIODriver->pin_init(pSlaveSelect, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
94  if (pSensorHandle->slaveParams.ssActiveValue == SPI_SS_ACTIVE_LOW)
95  {
96  pGPIODriver->set_pin(pSlaveSelect);
97  }
98  else
99  {
100  pGPIODriver->clr_pin(pSlaveSelect);
101  }
102 
103  /*! Read and store the device's WHO_AM_I.*/
104  status = Register_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
105  FXLS896x_WHO_AM_I, 1, &reg);
106  //if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
107  (*whoami) = reg;
108  if (ARM_DRIVER_OK != status)
109  {
110  if ((FXLS8962_WHOAMI_VALUE != reg) || (FXLS8964_WHOAMI_VALUE != reg) || (FXLS8967_WHOAMI_VALUE != reg) || (FXLS8968_WHOAMI_VALUE != reg) || (FXLS8974_WHOAMI_VALUE != reg))
111  {
112  pSensorHandle->isInitialized = false;
114  }
115  pSensorHandle->isInitialized = false;
116  return SENSOR_ERROR_INIT;
117  }
118  pSensorHandle->isInitialized = true;
119  return SENSOR_ERROR_NONE;
120 }
121 
123  registeridlefunction_t idleTask,
124  void *userParam)
125 {
126  pSensorHandle->deviceInfo.functionParam = userParam;
127  pSensorHandle->deviceInfo.idleFunction = idleTask;
128 }
129 
131 {
132  int32_t status;
133 
134  /*! Validate for the correct handle and register write list.*/
135  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
136  {
138  }
139 
140  /*! Check whether sensor handle is initialized before applying configuration.*/
141  if (pSensorHandle->isInitialized != true)
142  {
143  return SENSOR_ERROR_INIT;
144  }
145 
146  /*! Put the device into standby mode so that configuration can be applied.*/
147  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
150  if (ARM_DRIVER_OK != status)
151  {
152  return SENSOR_ERROR_WRITE;
153  }
154 
155  /*! Apply the Sensor Configuration based on the Register Write List */
156  status = Sensor_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
157  pRegWriteList);
158  if (ARM_DRIVER_OK != status)
159  {
160  return SENSOR_ERROR_WRITE;
161  }
162 
163  /*! Put the device into active mode and ready for reading data.*/
164  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
167  if (ARM_DRIVER_OK != status)
168  {
169  return SENSOR_ERROR_WRITE;
170  }
171 
172  return SENSOR_ERROR_NONE;
173 }
174 
176  const registerreadlist_t *pReadList,
177  uint8_t *pBuffer)
178 {
179  int32_t status;
180 
181  /*! Validate for the correct handle and register read list.*/
182  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
183  {
185  }
186 
187  /*! Check whether sensor handle is initialized before reading sensor data.*/
188  if (pSensorHandle->isInitialized != true)
189  {
190  return SENSOR_ERROR_INIT;
191  }
192 
193  /*! Parse through the read list and read the data one by one. */
194  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
195  pReadList, pBuffer);
196  if (ARM_DRIVER_OK != status)
197  {
198  return SENSOR_ERROR_READ;
199  }
200 
201  return SENSOR_ERROR_NONE;
202 }
203 
205 {
206  int32_t status;
207 
208  if (pSensorHandle == NULL)
209  {
211  }
212 
213  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
214  if (pSensorHandle->isInitialized != true)
215  {
216  return SENSOR_ERROR_INIT;
217  }
218 
219  /*! Trigger sensor device reset.*/
220  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
222  if (ARM_DRIVER_OK != status)
223  {
224  return SENSOR_ERROR_WRITE;
225  }
226  else
227  {
228  /*! De-initialize sensor handle. */
229  pSensorHandle->isInitialized = false;
230  }
231 
232  /* Wait for MAX of TBOOT ms after soft reset command,
233  * to allow enough time for FXLS896xAF to complete its internal boot sequence and be ready for communication. */
235 
236  return SENSOR_ERROR_NONE;
237 }
238 
240  fxls896x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t *whoami)
241 {
242  int32_t status;
243  uint8_t reg = 0;
244 
245  /*! Check the input parameters. */
246  if ((pSensorHandle == NULL) || (pBus == NULL))
247  {
249  }
250 
251  pSensorHandle->deviceInfo.deviceInstance = index;
252  pSensorHandle->deviceInfo.functionParam = NULL;
253  pSensorHandle->deviceInfo.idleFunction = NULL;
254 
255  /*! Read and store the device's WHO_AM_I.*/
256  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXLS896x_WHO_AM_I, 1, &reg);
257  //if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
258  (*whoami) = reg;
259  if (ARM_DRIVER_OK != status)
260  {
261  if ((FXLS8962_WHOAMI_VALUE != reg) || (FXLS8964_WHOAMI_VALUE != reg) || (FXLS8967_WHOAMI_VALUE != reg) || (FXLS8968_WHOAMI_VALUE != reg) || (FXLS8974_WHOAMI_VALUE != reg))
262  {
263  pSensorHandle->isInitialized = false;
265  }
266  pSensorHandle->isInitialized = false;
267  return SENSOR_ERROR_INIT;
268  }
269 
270  /*! Initialize the sensor handle. */
271  pSensorHandle->pCommDrv = pBus;
272  pSensorHandle->slaveAddress = sAddress;
273  pSensorHandle->isInitialized = true;
274  return SENSOR_ERROR_NONE;
275 }
276 
278  registeridlefunction_t idleTask,
279  void *userParam)
280 {
281  pSensorHandle->deviceInfo.functionParam = userParam;
282  pSensorHandle->deviceInfo.idleFunction = idleTask;
283 }
284 
286 {
287  int32_t status;
288 
289  /*! Validate for the correct handle and register write list.*/
290  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
291  {
293  }
294 
295  /*! Check whether sensor handle is initialized before applying configuration.*/
296  if (pSensorHandle->isInitialized != true)
297  {
298  return SENSOR_ERROR_INIT;
299  }
300 
301  /*! Put the device into standby mode so that configuration can be applied.*/
302  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
305  if (ARM_DRIVER_OK != status)
306  {
307  return SENSOR_ERROR_WRITE;
308  }
309 
310  /*! Apply the Sensor Configuration based on the Register Write List */
311  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
312  pRegWriteList);
313  if (ARM_DRIVER_OK != status)
314  {
315  return SENSOR_ERROR_WRITE;
316  }
317 
318  /*! Put the device into active mode and ready for reading data.*/
319  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
322  if (ARM_DRIVER_OK != status)
323  {
324  return SENSOR_ERROR_WRITE;
325  }
326 
327  return SENSOR_ERROR_NONE;
328 }
329 
331  const registerreadlist_t *pReadList,
332  uint8_t *pBuffer)
333 {
334  int32_t status;
335 
336  /*! Validate for the correct handle and register read list.*/
337  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
338  {
340  }
341 
342  /*! Check whether sensor handle is initialized before reading sensor data.*/
343  if (pSensorHandle->isInitialized != true)
344  {
345  return SENSOR_ERROR_INIT;
346  }
347 
348  /*! Parse through the read list and read the data one by one. */
349  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
350  pReadList, pBuffer);
351  if (ARM_DRIVER_OK != status)
352  {
353  return SENSOR_ERROR_READ;
354  }
355 
356  return SENSOR_ERROR_NONE;
357 }
358 
360 {
361  int32_t status;
362 
363  if (pSensorHandle == NULL)
364  {
366  }
367 
368  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
369  if (pSensorHandle->isInitialized != true)
370  {
371  return SENSOR_ERROR_INIT;
372  }
373 
374  /*! Trigger sensor device reset.*/
375  status =
376  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
378  if (ARM_DRIVER_OK != status)
379  {
380  return SENSOR_ERROR_WRITE;
381  }
382  else
383  {
384  /*! De-initialize sensor handle. */
385  pSensorHandle->isInitialized = false;
386  }
387 
388  /* Wait for MAX of TBOOT ms after soft reset command,
389  * to allow enough time for FXLS896xAF to complete its internal boot sequence and be ready for communication. */
391 
392  return SENSOR_ERROR_NONE;
393 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
uint32_t size
The fxls896x_drv.h file describes the FXLS8964/67AF driver interface and structures.
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.
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
GENERIC_DRIVER_GPIO * pGPIODriver
ARM_DRIVER_SPI * pCommDrv
Definition: fxls896x_drv.h:35
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.
int32_t Register_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t value, uint8_t mask, bool repeatedStart)
The interface function to write a sensor register.
void BOARD_DELAY_ms(uint32_t delay_ms)
Function to insert delays.
Definition: systick_utils.c:81
int32_t FXLS896x_I2C_Initialize(fxls896x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t *whoami)
The interface function to initialize the sensor.
Definition: fxls896x_drv.c:239
This defines the sensor specific information for SPI.
Definition: fxls896x_drv.h:32
int32_t Register_SPI_Write(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, uint8_t offset, uint8_t value, uint8_t mask)
The interface function to write a sensor register.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
registerDeviceInfo_t deviceInfo
Definition: fxls896x_drv.h:45
#define FXLS896x_TBOOT_MAX
Definition: fxls896x.h:91
#define SPI_SS_ACTIVE_LOW
#define FXLS896x_SENS_CONFIG1_ACTIVE_STANDBY
Definition: fxls896x.h:461
int32_t FXLS896x_I2C_DeInit(fxls896x_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxls896x_drv.c:359
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:47
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
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 FXLS8967_WHOAMI_VALUE
Definition: fxls8962.h:89
uint8_t fxls896x_spiWrite_CmdDataBuffer[FXLS896x_SPI_MAX_MSG_SIZE]
Definition: fxls896x_drv.c:25
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
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define FXLS896x_SPI_MAX_MSG_SIZE
Definition: fxls896x_drv.h:60
#define FXLS896x_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxls896x_drv.h:64
fpSpiReadPreprocessFn_t pReadPreprocessFN
The SPI Slave Transfer Command Params SDK2.0 Driver.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
This defines the sensor specific information for I2C.
Definition: fxls896x_drv.h:43
int32_t FXLS896x_SPI_Initialize(fxls896x_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t *whoami)
The interface function to initialize the sensor.
Definition: fxls896x_drv.c:67
int32_t FXLS896x_I2C_Configure(fxls896x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls896x_drv.c:285
#define FXLS896x_SENS_CONFIG1_RST_RST
Definition: fxls896x.h:441
int32_t FXLS896x_I2C_ReadData(fxls896x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls896x_drv.c:330
void FXLS896x_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
The SPI Read Pre-Process function to generate Sensor specific SPI Message Header. ...
Definition: fxls896x_drv.c:30
ARM_DRIVER_I2C * pCommDrv
Definition: fxls896x_drv.h:46
registerDeviceInfo_t deviceInfo
Definition: fxls896x_drv.h:34
uint8_t fxls896x_spiRead_DataBuffer[FXLS896x_SPI_MAX_MSG_SIZE]
Definition: fxls896x_drv.c:24
void FXLS896x_I2C_SetIdleTask(fxls896x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxls896x_drv.c:277
void FXLS896x_SPI_SetIdleTask(fxls896x_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: fxls896x_drv.c:122
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
ARM Systick Utilities.
#define FXLS896x_SENS_CONFIG1_RST_MASK
Definition: fxls896x.h:435
#define FXLS8974_WHOAMI_VALUE
Definition: fxls8962.h:90
#define FXLS896x_SENS_CONFIG1_ACTIVE_MASK
Definition: fxls896x.h:420
#define FXLS8968_WHOAMI_VALUE
Definition: fxls896x.h:89
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
void FXLS896x_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: fxls896x_drv.c:47
uint8_t fxls896x_spiRead_CmdBuffer[FXLS896x_SPI_MAX_MSG_SIZE]
Definition: fxls896x_drv.c:23
int32_t FXLS896x_SPI_Configure(fxls896x_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls896x_drv.c:130
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
int32_t FXLS896x_SPI_ReadData(fxls896x_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls896x_drv.c:175
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.
#define FXLS896x_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxls896x_drv.h:68
spiSlaveSpecificParams_t slaveParams
Definition: fxls896x_drv.h:37
registeridlefunction_t idleFunction
Definition: sensor_drv.h:104
#define FXLS8964_WHOAMI_VALUE
Definition: fxls8962.h:88
int32_t FXLS896x_SPI_Deinit(fxls896x_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxls896x_drv.c:204
#define FXLS896x_SENS_CONFIG1_ACTIVE_ACTIVE
Definition: fxls896x.h:462