ISSDK  1.8
IoT Sensing Software Development Kit
fxas21002_drv.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 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 fxas21002_drv.c
11  * @brief The fxas21002_drv.c file implements the FXAS21002 sensor driver interfaces.
12  */
13 
14 //-----------------------------------------------------------------------
15 // ISSDK Includes
16 //-----------------------------------------------------------------------
17 #include "gpio_driver.h"
18 #include "fxas21002_drv.h"
19 
20 //-----------------------------------------------------------------------
21 // Global Variables
22 //-----------------------------------------------------------------------
26 
27 //-----------------------------------------------------------------------
28 // Functions
29 //-----------------------------------------------------------------------
30 void FXAS21002_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
31 {
32  spiCmdParams_t *pSlaveCmd = pCmdOut;
33 
34  uint8_t *pWBuff = fxas21002_spiRead_CmdBuffer;
35  uint8_t *pRBuff = fxas21002_spiRead_DataBuffer;
36 
37  /* Formatting for Read command of FXAS21002 SENSOR. */
38  *pWBuff = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
39 
40  // Create the slave read command.
41  pSlaveCmd->size = size + FXAS21002_SPI_CMD_LEN;
42  pSlaveCmd->pWriteBuffer = pWBuff;
43  pSlaveCmd->pReadBuffer = pRBuff;
44 }
45 
46 void FXAS21002_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
47 {
48  spiCmdParams_t *pSlaveCmd = pCmdOut;
49 
50  uint8_t *pWBuff = fxas21002_spiWrite_CmdDataBuffer;
52 
53  /* Formatting for Write command of FXAS21002 SENSOR. */
54  *pWBuff = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
55 
56  /* Copy the slave write command */
57  memcpy(pWBuff + FXAS21002_SPI_CMD_LEN, pWritebuffer, size);
58 
59  /* Create the slave command. */
60  pSlaveCmd->size = size + FXAS21002_SPI_CMD_LEN;
61  pSlaveCmd->pWriteBuffer = pWBuff;
62  pSlaveCmd->pReadBuffer = pRBuff;
63 }
64 
66  ARM_DRIVER_SPI *pBus,
67  uint8_t index,
68  void *pSlaveSelect,
69  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  FXAS21002_WHO_AM_I, 1, &reg);
107  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
108  {
109  pSensorHandle->isInitialized = false;
110  return SENSOR_ERROR_INIT;
111  }
112 
113  pSensorHandle->isInitialized = true;
114  return SENSOR_ERROR_NONE;
115 }
116 
118  registeridlefunction_t idleTask,
119  void *userParam)
120 {
121  pSensorHandle->deviceInfo.functionParam = userParam;
122  pSensorHandle->deviceInfo.idleFunction = idleTask;
123 }
124 
126 {
127  int32_t status;
128 
129  /*! Validate for the correct handle and register write list.*/
130  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
131  {
133  }
134 
135  /*! Check whether sensor handle is initialized before applying configuration.*/
136  if (pSensorHandle->isInitialized != true)
137  {
138  return SENSOR_ERROR_INIT;
139  }
140 
141  /*! Put the device into standby mode so that configuration can be applied.*/
142  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
144  if (ARM_DRIVER_OK != status)
145  {
146  return SENSOR_ERROR_WRITE;
147  }
148 
149  /*! Apply the Sensor Configuration based on the Register Write List */
150  status = Sensor_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
151  pRegWriteList);
152  if (ARM_DRIVER_OK != status)
153  {
154  return SENSOR_ERROR_WRITE;
155  }
156 
157  /*! Put the device into active mode and ready for reading data.*/
158  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
160  if (ARM_DRIVER_OK != status)
161  {
162  return SENSOR_ERROR_WRITE;
163  }
164 
165  return SENSOR_ERROR_NONE;
166 }
167 
169  const registerreadlist_t *pReadList,
170  uint8_t *pBuffer)
171 {
172  int32_t status;
173 
174  /*! Validate for the correct handle and register read list.*/
175  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
176  {
178  }
179 
180  /*! Check whether sensor handle is initialized before reading sensor data.*/
181  if (pSensorHandle->isInitialized != true)
182  {
183  return SENSOR_ERROR_INIT;
184  }
185 
186  /*! Parse through the read list and read the data one by one. */
187  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
188  pReadList, pBuffer);
189  if (ARM_DRIVER_OK != status)
190  {
191  return SENSOR_ERROR_READ;
192  }
193 
194  return SENSOR_ERROR_NONE;
195 }
196 
198 {
199  int32_t status;
200 
201  if (pSensorHandle == NULL)
202  {
204  }
205 
206  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
207  if (pSensorHandle->isInitialized != true)
208  {
209  return SENSOR_ERROR_INIT;
210  }
211 
212  /*! Trigger sensor device reset.*/
213  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
215  if (ARM_DRIVER_OK != status)
216  {
217  return SENSOR_ERROR_WRITE;
218  }
219  else
220  {
221  /*! De-initialize sensor handle. */
222  pSensorHandle->isInitialized = false;
223  }
224 
225  return SENSOR_ERROR_NONE;
226 }
227 
229  fxas21002_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
230 {
231  int32_t status;
232  uint8_t reg;
233 
234  /*! Check the input parameters. */
235  if ((pSensorHandle == NULL) || (pBus == NULL))
236  {
238  }
239 
240  pSensorHandle->deviceInfo.deviceInstance = index;
241  pSensorHandle->deviceInfo.functionParam = NULL;
242  pSensorHandle->deviceInfo.idleFunction = NULL;
243 
244  /*! Read and store the device's WHO_AM_I.*/
245  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXAS21002_WHO_AM_I, 1, &reg);
246  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
247  {
248  pSensorHandle->isInitialized = false;
249  return SENSOR_ERROR_INIT;
250  }
251 
252  /*! Initialize the sensor handle. */
253  pSensorHandle->pCommDrv = pBus;
254  pSensorHandle->slaveAddress = sAddress;
255  pSensorHandle->isInitialized = true;
256  return SENSOR_ERROR_NONE;
257 }
258 
260  registeridlefunction_t idleTask,
261  void *userParam)
262 {
263  pSensorHandle->deviceInfo.functionParam = userParam;
264  pSensorHandle->deviceInfo.idleFunction = idleTask;
265 }
266 
268 {
269  int32_t status;
270 
271  /*! Validate for the correct handle and register write list.*/
272  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
273  {
275  }
276 
277  /*! Check whether sensor handle is initialized before applying configuration.*/
278  if (pSensorHandle->isInitialized != true)
279  {
280  return SENSOR_ERROR_INIT;
281  }
282 
283  /*! Put the device into standby mode so that configuration can be applied.*/
284  status =
285  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
287  if (ARM_DRIVER_OK != status)
288  {
289  return SENSOR_ERROR_WRITE;
290  }
291 
292  /*! Apply the Sensor Configuration based on the Register Write List */
293  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
294  pRegWriteList);
295  if (ARM_DRIVER_OK != status)
296  {
297  return SENSOR_ERROR_WRITE;
298  }
299 
300  /*! Put the device into active mode and ready for reading data.*/
301  status =
302  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
304  if (ARM_DRIVER_OK != status)
305  {
306  return SENSOR_ERROR_WRITE;
307  }
308 
309  return SENSOR_ERROR_NONE;
310 }
311 
313  const registerreadlist_t *pReadList,
314  uint8_t *pBuffer)
315 {
316  int32_t status;
317 
318  /*! Validate for the correct handle and register read list.*/
319  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
320  {
322  }
323 
324  /*! Check whether sensor handle is initialized before reading sensor data.*/
325  if (pSensorHandle->isInitialized != true)
326  {
327  return SENSOR_ERROR_INIT;
328  }
329 
330  /*! Parse through the read list and read the data one by one. */
331  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
332  pReadList, pBuffer);
333  if (ARM_DRIVER_OK != status)
334  {
335  return SENSOR_ERROR_READ;
336  }
337 
338  return SENSOR_ERROR_NONE;
339 }
340 
342 {
343  int32_t status;
344 
345  if (pSensorHandle == NULL)
346  {
348  }
349 
350  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
351  if (pSensorHandle->isInitialized != true)
352  {
353  return SENSOR_ERROR_INIT;
354  }
355 
356  /*! Trigger sensor device reset.*/
357  status =
358  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
360  if (ARM_DRIVER_OK != status)
361  {
362  return SENSOR_ERROR_WRITE;
363  }
364  else
365  {
366  /*! De-initialize sensor handle. */
367  pSensorHandle->isInitialized = false;
368  }
369 
370  return SENSOR_ERROR_NONE;
371 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
int32_t FXAS21002_SPI_Configure(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
uint32_t size
#define FXAS21002_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxas21002_drv.h:73
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
This defines the sensor specific information for SPI.
Definition: fxas21002_drv.h:33
GENERIC_DRIVER_GPIO * pGPIODriver
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.
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 FXAS21002_I2C_SetIdleTask(fxas21002_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
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 FXAS21002_CTRL_REG1_RST_TRIGGER
Definition: fxas21002.h:713
#define SPI_SS_ACTIVE_LOW
registerDeviceInfo_t deviceInfo
Definition: fxas21002_drv.h:46
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:46
int32_t FXAS21002_SPI_Deinit(fxas21002_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
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.
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:30
int32_t FXAS21002_I2C_Configure(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
This defines the sensor specific information for I2C.
Definition: fxas21002_drv.h:44
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
spiSlaveSpecificParams_t slaveParams
Definition: fxas21002_drv.h:38
fpSpiReadPreprocessFn_t pReadPreprocessFN
The SPI Slave Transfer Command Params SDK2.0 Driver.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
uint8_t fxas21002_spiRead_CmdBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:23
#define FXAS21002_CTRL_REG1_MODE_ACTIVE
Definition: fxas21002.h:702
The fxas21002_drv.h file describes the fxas21002 driver interface and structures. ...
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.
#define FXAS21002_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxas21002_drv.h:69
uint8_t fxas21002_spiWrite_CmdDataBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:25
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
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.
This structure defines the Read command List.
Definition: sensor_drv.h:78
uint8_t fxas21002_spiRead_DataBuffer[FXAS21002_SPI_MAX_MSG_SIZE]
Definition: fxas21002_drv.c:24
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:65
#define FXAS21002_CTRL_REG1_MODE_STANDBY
Definition: fxas21002.h:697
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 FXAS21002_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxas21002_drv.h:65
void FXAS21002_SPI_SetIdleTask(fxas21002_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
registerDeviceInfo_t deviceInfo
Definition: fxas21002_drv.h:35
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
int32_t FXAS21002_I2C_Deinit(fxas21002_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
#define FXAS21002_CTRL_REG1_MODE_MASK
Definition: fxas21002.h:681
#define FXAS21002_CTRL_REG1_RST_MASK
Definition: fxas21002.h:690