ISSDK  1.7
IoT Sensing Software Development Kit
fxas21002_drv.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 /**
36  * @file fxas21002_drv.c
37  * @brief The fxas21002_drv.c file implements the FXAS21002 sensor driver interfaces.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "gpio_driver.h"
44 #include "fxas21002_drv.h"
45 
46 //-----------------------------------------------------------------------
47 // Global Variables
48 //-----------------------------------------------------------------------
52 
53 //-----------------------------------------------------------------------
54 // Functions
55 //-----------------------------------------------------------------------
56 void FXAS21002_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
57 {
58  spiCmdParams_t *pSlaveCmd = pCmdOut;
59 
60  uint8_t *pWBuff = fxas21002_spiRead_CmdBuffer;
61  uint8_t *pRBuff = fxas21002_spiRead_DataBuffer;
62 
63  /* Formatting for Read command of FXAS21002 SENSOR. */
64  *pWBuff = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
65 
66  // Create the slave read command.
67  pSlaveCmd->size = size + FXAS21002_SPI_CMD_LEN;
68  pSlaveCmd->pWriteBuffer = pWBuff;
69  pSlaveCmd->pReadBuffer = pRBuff;
70 }
71 
72 void FXAS21002_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
73 {
74  spiCmdParams_t *pSlaveCmd = pCmdOut;
75 
76  uint8_t *pWBuff = fxas21002_spiWrite_CmdDataBuffer;
78 
79  /* Formatting for Write command of FXAS21002 SENSOR. */
80  *pWBuff = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
81 
82  /* Copy the slave write command */
83  memcpy(pWBuff + FXAS21002_SPI_CMD_LEN, pWritebuffer, size);
84 
85  /* Create the slave command. */
86  pSlaveCmd->size = size + FXAS21002_SPI_CMD_LEN;
87  pSlaveCmd->pWriteBuffer = pWBuff;
88  pSlaveCmd->pReadBuffer = pRBuff;
89 }
90 
92  ARM_DRIVER_SPI *pBus,
93  uint8_t index,
94  void *pSlaveSelect,
95  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;
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  FXAS21002_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,
170  if (ARM_DRIVER_OK != status)
171  {
172  return SENSOR_ERROR_WRITE;
173  }
174 
175  /*! Apply the Sensor Configuration based on the Register Write List */
176  status = Sensor_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
177  pRegWriteList);
178  if (ARM_DRIVER_OK != status)
179  {
180  return SENSOR_ERROR_WRITE;
181  }
182 
183  /*! Put the device into active mode and ready for reading data.*/
184  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
186  if (ARM_DRIVER_OK != status)
187  {
188  return SENSOR_ERROR_WRITE;
189  }
190 
191  return SENSOR_ERROR_NONE;
192 }
193 
195  const registerreadlist_t *pReadList,
196  uint8_t *pBuffer)
197 {
198  int32_t status;
199 
200  /*! Validate for the correct handle and register read list.*/
201  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
202  {
204  }
205 
206  /*! Check whether sensor handle is initialized before reading sensor data.*/
207  if (pSensorHandle->isInitialized != true)
208  {
209  return SENSOR_ERROR_INIT;
210  }
211 
212  /*! Parse through the read list and read the data one by one. */
213  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
214  pReadList, pBuffer);
215  if (ARM_DRIVER_OK != status)
216  {
217  return SENSOR_ERROR_READ;
218  }
219 
220  return SENSOR_ERROR_NONE;
221 }
222 
224 {
225  int32_t status;
226 
227  if (pSensorHandle == NULL)
228  {
230  }
231 
232  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
233  if (pSensorHandle->isInitialized != true)
234  {
235  return SENSOR_ERROR_INIT;
236  }
237 
238  /*! Trigger sensor device reset.*/
239  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
241  if (ARM_DRIVER_OK != status)
242  {
243  return SENSOR_ERROR_WRITE;
244  }
245  else
246  {
247  /*! De-initialize sensor handle. */
248  pSensorHandle->isInitialized = false;
249  }
250 
251  return SENSOR_ERROR_NONE;
252 }
253 
255  fxas21002_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
256 {
257  int32_t status;
258  uint8_t reg;
259 
260  /*! Check the input parameters. */
261  if ((pSensorHandle == NULL) || (pBus == NULL))
262  {
264  }
265 
266  pSensorHandle->deviceInfo.deviceInstance = index;
267  pSensorHandle->deviceInfo.functionParam = NULL;
268  pSensorHandle->deviceInfo.idleFunction = NULL;
269 
270  /*! Read and store the device's WHO_AM_I.*/
271  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXAS21002_WHO_AM_I, 1, &reg);
272  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
273  {
274  pSensorHandle->isInitialized = false;
275  return SENSOR_ERROR_INIT;
276  }
277 
278  /*! Initialize the sensor handle. */
279  pSensorHandle->pCommDrv = pBus;
280  pSensorHandle->slaveAddress = sAddress;
281  pSensorHandle->isInitialized = true;
282  return SENSOR_ERROR_NONE;
283 }
284 
286  registeridlefunction_t idleTask,
287  void *userParam)
288 {
289  pSensorHandle->deviceInfo.functionParam = userParam;
290  pSensorHandle->deviceInfo.idleFunction = idleTask;
291 }
292 
294 {
295  int32_t status;
296 
297  /*! Validate for the correct handle and register write list.*/
298  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
299  {
301  }
302 
303  /*! Check whether sensor handle is initialized before applying configuration.*/
304  if (pSensorHandle->isInitialized != true)
305  {
306  return SENSOR_ERROR_INIT;
307  }
308 
309  /*! Put the device into standby mode so that configuration can be applied.*/
310  status =
311  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
313  if (ARM_DRIVER_OK != status)
314  {
315  return SENSOR_ERROR_WRITE;
316  }
317 
318  /*! Apply the Sensor Configuration based on the Register Write List */
319  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
320  pRegWriteList);
321  if (ARM_DRIVER_OK != status)
322  {
323  return SENSOR_ERROR_WRITE;
324  }
325 
326  /*! Put the device into active mode and ready for reading data.*/
327  status =
328  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
330  if (ARM_DRIVER_OK != status)
331  {
332  return SENSOR_ERROR_WRITE;
333  }
334 
335  return SENSOR_ERROR_NONE;
336 }
337 
339  const registerreadlist_t *pReadList,
340  uint8_t *pBuffer)
341 {
342  int32_t status;
343 
344  /*! Validate for the correct handle and register read list.*/
345  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
346  {
348  }
349 
350  /*! Check whether sensor handle is initialized before reading sensor data.*/
351  if (pSensorHandle->isInitialized != true)
352  {
353  return SENSOR_ERROR_INIT;
354  }
355 
356  /*! Parse through the read list and read the data one by one. */
357  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
358  pReadList, pBuffer);
359  if (ARM_DRIVER_OK != status)
360  {
361  return SENSOR_ERROR_READ;
362  }
363 
364  return SENSOR_ERROR_NONE;
365 }
366 
368 {
369  int32_t status;
370 
371  if (pSensorHandle == NULL)
372  {
374  }
375 
376  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
377  if (pSensorHandle->isInitialized != true)
378  {
379  return SENSOR_ERROR_INIT;
380  }
381 
382  /*! Trigger sensor device reset.*/
383  status =
384  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
386  if (ARM_DRIVER_OK != status)
387  {
388  return SENSOR_ERROR_WRITE;
389  }
390  else
391  {
392  /*! De-initialize sensor handle. */
393  pSensorHandle->isInitialized = false;
394  }
395 
396  return SENSOR_ERROR_NONE;
397 }
#define FXAS21002_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxas21002_drv.h:95
int32_t FXAS21002_SPI_ReadData(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
fpSpiWritePreprocessFn_t pWritePreprocessFN
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
int32_t FXAS21002_I2C_Deinit(fxas21002_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
int32_t status
void FXAS21002_SPI_SetIdleTask(fxas21002_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
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
The fxas21002_drv.h file describes the fxas21002 driver interface and structures. ...
int32_t FXAS21002_SPI_Deinit(fxas21002_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
void FXAS21002_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: fxas21002_drv.c:72
#define FXAS21002_CTRL_REG1_RST_MASK
Definition: fxas21002.h:716
void FXAS21002_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
The SPI Read Pre-Process function to generate Sensor specific SPI Message Header. ...
Definition: fxas21002_drv.c:56
int32_t FXAS21002_SPI_Configure(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define FXAS21002_CTRL_REG1_MODE_STANDBY
Definition: fxas21002.h:723
This defines the sensor specific information for I2C.
Definition: fxas21002_drv.h:70
uint8_t fxas21002_spiRead_CmdBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:49
registerDeviceInfo_t deviceInfo
Definition: fxas21002_drv.h:61
int32_t FXAS21002_SPI_Initialize(fxas21002_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: fxas21002_drv.c:91
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
int32_t FXAS21002_I2C_ReadData(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
The SPI Slave Transfer Command Params SDK2.0 Driver.
#define SPI_SS_ACTIVE_LOW
uint8_t fxas21002_spiRead_DataBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:50
#define FXAS21002_CTRL_REG1_RST_TRIGGER
Definition: fxas21002.h:739
GENERIC_DRIVER_GPIO * pGPIODriver
int32_t FXAS21002_I2C_Configure(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
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
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
void FXAS21002_I2C_SetIdleTask(fxas21002_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
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.
spiSlaveSpecificParams_t slaveParams
Definition: fxas21002_drv.h:64
This defines the sensor specific information for SPI.
Definition: fxas21002_drv.h:59
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
#define FXAS21002_CTRL_REG1_MODE_MASK
Definition: fxas21002.h:707
#define FXAS21002_CTRL_REG1_MODE_ACTIVE
Definition: fxas21002.h:728
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 FXAS21002_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxas21002_drv.h:91
uint8_t fxas21002_spiWrite_CmdDataBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:51
fpSpiReadPreprocessFn_t pReadPreprocessFN
int32_t FXAS21002_I2C_Initialize(fxas21002_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
#define FXAS21002_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxas21002_drv.h:99
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
registerDeviceInfo_t deviceInfo
Definition: fxas21002_drv.h:72