ISSDK  1.7
IoT Sensing Software Development Kit
fxls8962_drv.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 /**
36  * @file fxls8962_drv.c
37  * @brief The fxls8962_drv.c file implements the FXLS8962 sensor driver interfaces.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "gpio_driver.h"
44 #include "fxls8962_drv.h"
45 #include "systick_utils.h"
46 
47 //-----------------------------------------------------------------------
48 // Global Variables
49 //-----------------------------------------------------------------------
53 
54 //-----------------------------------------------------------------------
55 // Functions
56 //-----------------------------------------------------------------------
57 void FXLS8962_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
58 {
59  spiCmdParams_t *pSlaveCmd = pCmdOut;
60 
61  uint8_t *pWBuff = fxls8962_spiRead_CmdBuffer;
62  uint8_t *pRBuff = fxls8962_spiRead_DataBuffer;
63 
64  /* Formatting for Read command of FXLS8962 SENSOR. */
65  *(pWBuff) = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
66  *(pWBuff + 1) = 0x00;
67 
68  /* Create the slave read command. */
69  pSlaveCmd->size = size + FXLS8962_SPI_CMD_LEN;
70  pSlaveCmd->pWriteBuffer = pWBuff;
71  pSlaveCmd->pReadBuffer = pRBuff;
72 }
73 
74 void FXLS8962_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
75 {
76  spiCmdParams_t *pSlaveCmd = pCmdOut;
77 
78  uint8_t *pWBuff = fxls8962_spiWrite_CmdDataBuffer;
79  uint8_t *pRBuff = fxls8962_spiWrite_CmdDataBuffer + size + FXLS8962_SPI_CMD_LEN;
80 
81  /* Formatting for Write command of FXLS8962 SENSOR. */
82  *(pWBuff) = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
83  *(pWBuff + 1) = 0x00;
84 
85  /* Copy the slave write command */
86  memcpy(pWBuff + FXLS8962_SPI_CMD_LEN, pWritebuffer, size);
87 
88  /* Create the slave command. */
89  pSlaveCmd->size = size + FXLS8962_SPI_CMD_LEN;
90  pSlaveCmd->pWriteBuffer = pWBuff;
91  pSlaveCmd->pReadBuffer = pRBuff;
92 }
93 
95  fxls8962_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
96 {
98  uint8_t reg;
100 
101  /*! Check the input parameters. */
102  if ((pSensorHandle == NULL) || (pBus == NULL) || (pSlaveSelect == NULL))
103  {
105  }
106 
107  /*! Initialize the sensor handle. */
108  pSensorHandle->pCommDrv = pBus;
111  pSensorHandle->slaveParams.pTargetSlavePinID = pSlaveSelect;
112  pSensorHandle->slaveParams.spiCmdLen = FXLS8962_SPI_CMD_LEN;
114 
115  pSensorHandle->deviceInfo.deviceInstance = index;
116  pSensorHandle->deviceInfo.functionParam = NULL;
117  pSensorHandle->deviceInfo.idleFunction = NULL;
118 
119  /* Initialize the Slave Select Pin. */
120  pGPIODriver->pin_init(pSlaveSelect, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
121  if (pSensorHandle->slaveParams.ssActiveValue == SPI_SS_ACTIVE_LOW)
122  {
123  pGPIODriver->set_pin(pSlaveSelect);
124  }
125  else
126  {
127  pGPIODriver->clr_pin(pSlaveSelect);
128  }
129 
130  /*! Read and store the device's WHO_AM_I.*/
131  status = Register_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
132  FXLS8962_WHO_AM_I, 1, &reg);
133  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
134  {
135  pSensorHandle->isInitialized = false;
136  return SENSOR_ERROR_INIT;
137  }
138 
139  pSensorHandle->isInitialized = true;
140  return SENSOR_ERROR_NONE;
141 }
142 
144  registeridlefunction_t idleTask,
145  void *userParam)
146 {
147  pSensorHandle->deviceInfo.functionParam = userParam;
148  pSensorHandle->deviceInfo.idleFunction = idleTask;
149 }
150 
152 {
153  int32_t status;
154 
155  /*! Validate for the correct handle and register write list.*/
156  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
157  {
159  }
160 
161  /*! Check whether sensor handle is initialized before applying configuration.*/
162  if (pSensorHandle->isInitialized != true)
163  {
164  return SENSOR_ERROR_INIT;
165  }
166 
167  /*! Put the device into standby mode so that configuration can be applied.*/
168  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
171  if (ARM_DRIVER_OK != status)
172  {
173  return SENSOR_ERROR_WRITE;
174  }
175 
176  /*! Apply the Sensor Configuration based on the Register Write List */
177  status = Sensor_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
178  pRegWriteList);
179  if (ARM_DRIVER_OK != status)
180  {
181  return SENSOR_ERROR_WRITE;
182  }
183 
184  /*! Put the device into active mode and ready for reading data.*/
185  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
188  if (ARM_DRIVER_OK != status)
189  {
190  return SENSOR_ERROR_WRITE;
191  }
192 
193  return SENSOR_ERROR_NONE;
194 }
195 
197  const registerreadlist_t *pReadList,
198  uint8_t *pBuffer)
199 {
200  int32_t status;
201 
202  /*! Validate for the correct handle and register read list.*/
203  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
204  {
206  }
207 
208  /*! Check whether sensor handle is initialized before reading sensor data.*/
209  if (pSensorHandle->isInitialized != true)
210  {
211  return SENSOR_ERROR_INIT;
212  }
213 
214  /*! Parse through the read list and read the data one by one. */
215  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
216  pReadList, pBuffer);
217  if (ARM_DRIVER_OK != status)
218  {
219  return SENSOR_ERROR_READ;
220  }
221 
222  return SENSOR_ERROR_NONE;
223 }
224 
226 {
227  int32_t status;
228 
229  if (pSensorHandle == NULL)
230  {
232  }
233 
234  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
235  if (pSensorHandle->isInitialized != true)
236  {
237  return SENSOR_ERROR_INIT;
238  }
239 
240  /*! Trigger sensor device reset.*/
241  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
243  if (ARM_DRIVER_OK != status)
244  {
245  return SENSOR_ERROR_WRITE;
246  }
247  else
248  {
249  /*! De-initialize sensor handle. */
250  pSensorHandle->isInitialized = false;
251  }
252 
253  /* Wait for MAX of TBOOT ms after soft reset command,
254  * to allow enough time for FXLS8962AF to complete its internal boot sequence and be ready for communication. */
256 
257  return SENSOR_ERROR_NONE;
258 }
259 
261  fxls8962_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
262 {
263  int32_t status;
264  uint8_t reg;
265 
266  /*! Check the input parameters. */
267  if ((pSensorHandle == NULL) || (pBus == NULL))
268  {
270  }
271 
272  pSensorHandle->deviceInfo.deviceInstance = index;
273  pSensorHandle->deviceInfo.functionParam = NULL;
274  pSensorHandle->deviceInfo.idleFunction = NULL;
275 
276  /*! Read and store the device's WHO_AM_I.*/
277  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXLS8962_WHO_AM_I, 1, &reg);
278  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
279  {
280  pSensorHandle->isInitialized = false;
281  return SENSOR_ERROR_INIT;
282  }
283 
284  /*! Initialize the sensor handle. */
285  pSensorHandle->pCommDrv = pBus;
286  pSensorHandle->slaveAddress = sAddress;
287  pSensorHandle->isInitialized = true;
288  return SENSOR_ERROR_NONE;
289 }
290 
292  registeridlefunction_t idleTask,
293  void *userParam)
294 {
295  pSensorHandle->deviceInfo.functionParam = userParam;
296  pSensorHandle->deviceInfo.idleFunction = idleTask;
297 }
298 
300 {
301  int32_t status;
302 
303  /*! Validate for the correct handle and register write list.*/
304  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
305  {
307  }
308 
309  /*! Check whether sensor handle is initialized before applying configuration.*/
310  if (pSensorHandle->isInitialized != true)
311  {
312  return SENSOR_ERROR_INIT;
313  }
314 
315  /*! Put the device into standby mode so that configuration can be applied.*/
316  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
319  if (ARM_DRIVER_OK != status)
320  {
321  return SENSOR_ERROR_WRITE;
322  }
323 
324  /*! Apply the Sensor Configuration based on the Register Write List */
325  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
326  pRegWriteList);
327  if (ARM_DRIVER_OK != status)
328  {
329  return SENSOR_ERROR_WRITE;
330  }
331 
332  /*! Put the device into active mode and ready for reading data.*/
333  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
336  if (ARM_DRIVER_OK != status)
337  {
338  return SENSOR_ERROR_WRITE;
339  }
340 
341  return SENSOR_ERROR_NONE;
342 }
343 
345  const registerreadlist_t *pReadList,
346  uint8_t *pBuffer)
347 {
348  int32_t status;
349 
350  /*! Validate for the correct handle and register read list.*/
351  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
352  {
354  }
355 
356  /*! Check whether sensor handle is initialized before reading sensor data.*/
357  if (pSensorHandle->isInitialized != true)
358  {
359  return SENSOR_ERROR_INIT;
360  }
361 
362  /*! Parse through the read list and read the data one by one. */
363  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
364  pReadList, pBuffer);
365  if (ARM_DRIVER_OK != status)
366  {
367  return SENSOR_ERROR_READ;
368  }
369 
370  return SENSOR_ERROR_NONE;
371 }
372 
374 {
375  int32_t status;
376 
377  if (pSensorHandle == NULL)
378  {
380  }
381 
382  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
383  if (pSensorHandle->isInitialized != true)
384  {
385  return SENSOR_ERROR_INIT;
386  }
387 
388  /*! Trigger sensor device reset.*/
389  status =
390  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
392  if (ARM_DRIVER_OK != status)
393  {
394  return SENSOR_ERROR_WRITE;
395  }
396  else
397  {
398  /*! De-initialize sensor handle. */
399  pSensorHandle->isInitialized = false;
400  }
401 
402  /* Wait for MAX of TBOOT ms after soft reset command,
403  * to allow enough time for FXLS8962AF to complete its internal boot sequence and be ready for communication. */
405 
406  return SENSOR_ERROR_NONE;
407 }
uint8_t fxls8962_spiWrite_CmdDataBuffer[FXLS8962_SPI_MAX_MSG_SIZE]
Definition: fxls8962_drv.c:52
fpSpiWritePreprocessFn_t pWritePreprocessFN
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
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:74
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:260
#define FXLS8962_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxls8962_drv.h:91
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
uint8_t fxls8962_spiRead_CmdBuffer[FXLS8962_SPI_MAX_MSG_SIZE]
Definition: fxls8962_drv.c:50
This defines the sensor specific information for I2C.
Definition: fxls8962_drv.h:70
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:151
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:299
int32_t status
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:291
void BOARD_DELAY_ms(uint32_t delay_ms)
Function to insert delays.
registerDeviceInfo_t deviceInfo
Definition: fxls8962_drv.h:72
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(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define FXLS8962_SENS_CONFIG1_ACTIVE_ACTIVE
Definition: fxls8962.h:485
int32_t FXLS8962_I2C_DeInit(fxls8962_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxls8962_drv.c:373
uint8_t fxls8962_spiRead_DataBuffer[FXLS8962_SPI_MAX_MSG_SIZE]
Definition: fxls8962_drv.c:51
#define FXLS8962_TBOOT_MAX
Definition: fxls8962.h:114
int32_t FXLS8962_SPI_Deinit(fxls8962_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxls8962_drv.c:225
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:94
#define FXLS8962_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxls8962_drv.h:87
#define FXLS8962_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxls8962_drv.h:95
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
#define FXLS8962_SENS_CONFIG1_RST_MASK
Definition: fxls8962.h:458
The fxls8962_drv.h file describes the FXLS8962AF driver interface and structures. ...
#define FXLS8962_SENS_CONFIG1_RST_RST
Definition: fxls8962.h:464
spiSlaveSpecificParams_t slaveParams
Definition: fxls8962_drv.h:64
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:196
The SPI Slave Transfer Command Params SDK2.0 Driver.
#define SPI_SS_ACTIVE_LOW
registerDeviceInfo_t deviceInfo
Definition: fxls8962_drv.h:61
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
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:143
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.
uint32_t size
This defines the sensor specific information for SPI.
Definition: fxls8962_drv.h:59
ARM_DRIVER_I2C * pCommDrv
Definition: fxls8962_drv.h:73
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.
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:57
ARM Systick Utilities.
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.
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
ARM_DRIVER_SPI * pCommDrv
Definition: fxls8962_drv.h:62
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:344
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 FXLS8962_SENS_CONFIG1_ACTIVE_STANDBY
Definition: fxls8962.h:484
fpSpiReadPreprocessFn_t pReadPreprocessFN
#define FXLS8962_SENS_CONFIG1_ACTIVE_MASK
Definition: fxls8962.h:443
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.
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