ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_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 fxos8700_drv.c
11  * @brief The fxos8700_drv.c file implements the fxos8700 sensor driver functional interfaces.
12  */
13 
14 //-----------------------------------------------------------------------
15 // ISSDK Includes
16 //-----------------------------------------------------------------------
17 #include "gpio_driver.h"
18 #include "fxos8700_drv.h"
19 
20 //-----------------------------------------------------------------------
21 // Global Variables
22 //-----------------------------------------------------------------------
26 
27 //-----------------------------------------------------------------------
28 // Functions
29 //-----------------------------------------------------------------------
30 void FXOS8700_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
31 {
32  spiCmdParams_t *pSlaveCmd = pCmdOut;
33 
34  uint8_t *pWBuff = fxos8700_spiRead_CmdBuffer;
35  uint8_t *pRBuff = fxos8700_spiRead_DataBuffer;
36 
37  /* Formatting for Read command of FXOS8700 SENSOR. */
38  *(pWBuff) = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
39  *(pWBuff + 1) = offset & 0x80;
40 
41  /* Create the slave read command. */
42  pSlaveCmd->size = size + FXOS8700_SPI_CMD_LEN;
43  pSlaveCmd->pWriteBuffer = pWBuff;
44  pSlaveCmd->pReadBuffer = pRBuff;
45 }
46 
47 void FXOS8700_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
48 {
49  spiCmdParams_t *pSlaveCmd = pCmdOut;
50 
51  uint8_t *pWBuff = fxos8700_spiWrite_CmdDataBuffer;
52  uint8_t *pRBuff = fxos8700_spiWrite_CmdDataBuffer + size + FXOS8700_SPI_CMD_LEN;
53 
54  /* Formatting for Write command of FXOS8700 SENSOR. */
55  *(pWBuff) = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
56  *(pWBuff + 1) = offset & 0x80;
57 
58  /* Copy the slave write command */
59  memcpy(pWBuff + FXOS8700_SPI_CMD_LEN, pWritebuffer, size);
60 
61  /* Create the slave command. */
62  pSlaveCmd->size = size + FXOS8700_SPI_CMD_LEN;
63  pSlaveCmd->pWriteBuffer = pWBuff;
64  pSlaveCmd->pReadBuffer = pRBuff;
65 }
66 
68  fxos8700_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
69 {
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  FXOS8700_WHO_AM_I, 1, &reg);
106  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
107  {
108  pSensorHandle->isInitialized = false;
109  return SENSOR_ERROR_INIT;
110  }
111 
112  pSensorHandle->isInitialized = true;
113  return SENSOR_ERROR_NONE;
114 }
115 
117  registeridlefunction_t idleTask,
118  void *userParam)
119 {
120  pSensorHandle->deviceInfo.functionParam = userParam;
121  pSensorHandle->deviceInfo.idleFunction = idleTask;
122 }
123 
125 {
126  int32_t status;
127 
128  /*! Validate for the correct handle and register write list.*/
129  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
130  {
132  }
133 
134  /*! Check whether sensor handle is initialized before applying configuration.*/
135  if (pSensorHandle->isInitialized != true)
136  {
137  return SENSOR_ERROR_INIT;
138  }
139 
140  /*! Put the device into standby mode so that configuration can be applied.*/
141  status =
142  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 =
159  Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
161  if (ARM_DRIVER_OK != status)
162  {
163  return SENSOR_ERROR_WRITE;
164  }
165 
166  return SENSOR_ERROR_NONE;
167 }
168 
170  const registerreadlist_t *pReadList,
171  uint8_t *pBuffer)
172 {
173  int32_t status;
174 
175  /*! Validate for the correct handle and register read list.*/
176  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
177  {
179  }
180 
181  /*! Check whether sensor handle is initialized before reading sensor data.*/
182  if (pSensorHandle->isInitialized != true)
183  {
184  return SENSOR_ERROR_INIT;
185  }
186 
187  /*! Parse through the read list and read the data one by one. */
188  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
189  pReadList, pBuffer);
190  if (ARM_DRIVER_OK != status)
191  {
192  return SENSOR_ERROR_READ;
193  }
194 
195  return SENSOR_ERROR_NONE;
196 }
197 
199 {
200  if (pSensorHandle == NULL)
201  {
203  }
204 
205  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
206  if (pSensorHandle->isInitialized != true)
207  {
208  return SENSOR_ERROR_INIT;
209  }
210 
211  /*! Trigger sensor device reset.*/
212  Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
214 
215  /*! De-initialize sensor handle.
216  * We do not validate write success since 8700 reset before sensing ack for the transaction. */
217  pSensorHandle->isInitialized = false;
218 
219  return SENSOR_ERROR_NONE;
220 }
221 
223  fxos8700_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
224 {
225  int32_t status;
227 
228  if ((pSensorHandle == NULL) || (pBus == NULL))
229  {
231  }
232 
233  pSensorHandle->deviceInfo.deviceInstance = index;
234  pSensorHandle->deviceInfo.functionParam = NULL;
235  pSensorHandle->deviceInfo.idleFunction = NULL;
236 
237  /*! Read and store the device's WHO_AM_I.*/
238  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXOS8700_WHO_AM_I, 1, &reg);
239  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
240  {
241  pSensorHandle->isInitialized = false;
242  return SENSOR_ERROR_INIT;
243  }
244 
245  /*! Initialize the sensor handle. */
246  pSensorHandle->pCommDrv = pBus;
247  pSensorHandle->slaveAddress = sAddress;
248  pSensorHandle->isInitialized = true;
249  return SENSOR_ERROR_NONE;
250 }
251 
253  registeridlefunction_t idleTask,
254  void *userParam)
255 {
256  pSensorHandle->deviceInfo.functionParam = userParam;
257  pSensorHandle->deviceInfo.idleFunction = idleTask;
258 }
259 
261 {
262  int32_t status;
263 
264  /*! Validate for the correct handle and register write list.*/
265  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
266  {
268  }
269 
270  /*! Check whether sensor handle is initialized before applying configuration.*/
271  if (pSensorHandle->isInitialized != true)
272  {
273  return SENSOR_ERROR_INIT;
274  }
275 
276  /* Put the device into standby mode so that configuration can be applied.*/
277  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
280  if (ARM_DRIVER_OK != status)
281  {
282  return SENSOR_ERROR_WRITE;
283  }
284 
285  /* Appy the Sensor Configuration based on the Register List */
286  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
287  pRegWriteList);
288  if (ARM_DRIVER_OK != status)
289  {
290  return SENSOR_ERROR_WRITE;
291  }
292 
293  /* Put the device into active mode and ready for reading data.*/
294  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
297  if (ARM_DRIVER_OK != status)
298  {
299  return SENSOR_ERROR_WRITE;
300  }
301 
302  return SENSOR_ERROR_NONE;
303 }
304 
306  const registerreadlist_t *pReadList,
307  uint8_t *pBuffer)
308 {
309  int32_t status;
310 
311  /*! Validate for the correct handle and register read list.*/
312  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
313  {
315  }
316 
317  /*! Check whether sensor handle is initialized before reading sensor data.*/
318  if (pSensorHandle->isInitialized != true)
319  {
320  return SENSOR_ERROR_INIT;
321  }
322 
323  /*! Parse through the read list and read the data one by one*/
324  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
325  pReadList, pBuffer);
326  if (ARM_DRIVER_OK != status)
327  {
328  return SENSOR_ERROR_READ;
329  }
330 
331  return SENSOR_ERROR_NONE;
332 }
333 
335 {
336  if (pSensorHandle == NULL)
337  {
339  }
340 
341  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
342  if (pSensorHandle->isInitialized != true)
343  {
344  return SENSOR_ERROR_INIT;
345  }
346 
347  /*! Trigger sensor device reset.*/
348  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
350  pSensorHandle->isInitialized = false;
351  return SENSOR_ERROR_NONE;
352 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
uint32_t size
int32_t FXOS8700_I2C_Initialize(fxos8700_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: fxos8700_drv.c:222
uint8_t fxos8700_spiRead_CmdBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:23
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_I2C * pCommDrv
Definition: fxos8700_drv.h:47
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.
#define FXOS8700_CTRL_REG1_ACTIVE_ACTIVE_MODE
Definition: fxos8700.h:1540
uint8_t fxos8700_spiRead_DataBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:24
int32_t FXOS8700_I2C_Deinit(fxos8700_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxos8700_drv.c:334
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
spiSlaveSpecificParams_t slaveParams
Definition: fxos8700_drv.h:38
#define FXOS8700_CTRL_REG2_RST_MASK
Definition: fxos8700.h:1577
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: fxos8700_drv.h:44
int32_t FXOS8700_I2C_Configure(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxos8700_drv.c:260
#define FXOS8700_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxos8700_drv.h:77
int32_t FXOS8700_SPI_Initialize(fxos8700_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: fxos8700_drv.c:67
int32_t FXOS8700_I2C_ReadData(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxos8700_drv.c:305
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
int32_t FXOS8700_SPI_ReadData(fxos8700_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxos8700_drv.c:169
fpSpiReadPreprocessFn_t pReadPreprocessFN
The SPI Slave Transfer Command Params SDK2.0 Driver.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
#define FXOS8700_CTRL_REG1_ACTIVE_STANDBY_MODE
Definition: fxos8700.h:1541
void FXOS8700_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
The SPI Read Pre-Process function to generate Sensor specific SPI Message Header. ...
Definition: fxos8700_drv.c:30
This defines the sensor specific information for SPI.
Definition: fxos8700_drv.h:33
#define FXOS8700_CTRL_REG1_ACTIVE_MASK
Definition: fxos8700.h:1495
#define FXOS8700_CTRL_REG2_RST_EN
Definition: fxos8700.h:1589
#define FXOS8700_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxos8700_drv.h:73
void FXOS8700_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: fxos8700_drv.c:47
uint8_t fxos8700_spiWrite_CmdDataBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:25
int32_t FXOS8700_SPI_Configure(fxos8700_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxos8700_drv.c:124
registerDeviceInfo_t deviceInfo
Definition: fxos8700_drv.h:35
void FXOS8700_SPI_SetIdleTask(fxos8700_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: fxos8700_drv.c:116
uint8_t FXOS8700_WHO_AM_I_t
Definition: fxos8700.h:623
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
int32_t FXOS8700_SPI_Deinit(fxos8700_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxos8700_drv.c:198
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
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 FXOS8700_I2C_SetIdleTask(fxos8700_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxos8700_drv.c:252
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 FXOS8700_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxos8700_drv.h:69
ARM_DRIVER_SPI * pCommDrv
Definition: fxos8700_drv.h:36
registerDeviceInfo_t deviceInfo
Definition: fxos8700_drv.h:46