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