ISSDK  1.7
IoT Sensing Software Development Kit
fxos8700_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 fxos8700_drv.c
37  * @brief The fxos8700_drv.c file implements the fxos8700 sensor driver functional interfaces.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "gpio_driver.h"
44 #include "fxos8700_drv.h"
45 
46 //-----------------------------------------------------------------------
47 // Global Variables
48 //-----------------------------------------------------------------------
52 
53 //-----------------------------------------------------------------------
54 // Functions
55 //-----------------------------------------------------------------------
56 void FXOS8700_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
57 {
58  spiCmdParams_t *pSlaveCmd = pCmdOut;
59 
60  uint8_t *pWBuff = fxos8700_spiRead_CmdBuffer;
61  uint8_t *pRBuff = fxos8700_spiRead_DataBuffer;
62 
63  /* Formatting for Read command of FXOS8700 SENSOR. */
64  *(pWBuff) = offset & 0x7F; /* offset is the internal register address of the sensor at which write is performed. */
65  *(pWBuff + 1) = offset & 0x80;
66 
67  /* Create the slave read command. */
68  pSlaveCmd->size = size + FXOS8700_SPI_CMD_LEN;
69  pSlaveCmd->pWriteBuffer = pWBuff;
70  pSlaveCmd->pReadBuffer = pRBuff;
71 }
72 
73 void FXOS8700_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
74 {
75  spiCmdParams_t *pSlaveCmd = pCmdOut;
76 
77  uint8_t *pWBuff = fxos8700_spiWrite_CmdDataBuffer;
78  uint8_t *pRBuff = fxos8700_spiWrite_CmdDataBuffer + size + FXOS8700_SPI_CMD_LEN;
79 
80  /* Formatting for Write command of FXOS8700 SENSOR. */
81  *(pWBuff) = offset | 0x80; /* offset is the internal register address of the sensor at which write is performed. */
82  *(pWBuff + 1) = offset & 0x80;
83 
84  /* Copy the slave write command */
85  memcpy(pWBuff + FXOS8700_SPI_CMD_LEN, pWritebuffer, size);
86 
87  /* Create the slave command. */
88  pSlaveCmd->size = size + FXOS8700_SPI_CMD_LEN;
89  pSlaveCmd->pWriteBuffer = pWBuff;
90  pSlaveCmd->pReadBuffer = pRBuff;
91 }
92 
94  fxos8700_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
95 {
99 
100  /*! Check the input parameters. */
101  if ((pSensorHandle == NULL) || (pBus == NULL) || (pSlaveSelect == NULL))
102  {
104  }
105 
106  /*! Initialize the sensor handle. */
107  pSensorHandle->pCommDrv = pBus;
110  pSensorHandle->slaveParams.pTargetSlavePinID = pSlaveSelect;
111  pSensorHandle->slaveParams.spiCmdLen = FXOS8700_SPI_CMD_LEN;
113 
114  pSensorHandle->deviceInfo.deviceInstance = index;
115  pSensorHandle->deviceInfo.functionParam = NULL;
116  pSensorHandle->deviceInfo.idleFunction = NULL;
117 
118  /* Initialize the Slave Select Pin. */
119  pGPIODriver->pin_init(pSlaveSelect, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
120  if (pSensorHandle->slaveParams.ssActiveValue == SPI_SS_ACTIVE_LOW)
121  {
122  pGPIODriver->set_pin(pSlaveSelect);
123  }
124  else
125  {
126  pGPIODriver->clr_pin(pSlaveSelect);
127  }
128 
129  /*! Read and store the device's WHO_AM_I.*/
130  status = Register_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
131  FXOS8700_WHO_AM_I, 1, &reg);
132  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
133  {
134  pSensorHandle->isInitialized = false;
135  return SENSOR_ERROR_INIT;
136  }
137 
138  pSensorHandle->isInitialized = true;
139  return SENSOR_ERROR_NONE;
140 }
141 
143  registeridlefunction_t idleTask,
144  void *userParam)
145 {
146  pSensorHandle->deviceInfo.functionParam = userParam;
147  pSensorHandle->deviceInfo.idleFunction = idleTask;
148 }
149 
151 {
152  int32_t status;
153 
154  /*! Validate for the correct handle and register write list.*/
155  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
156  {
158  }
159 
160  /*! Check whether sensor handle is initialized before applying configuration.*/
161  if (pSensorHandle->isInitialized != true)
162  {
163  return SENSOR_ERROR_INIT;
164  }
165 
166  /*! Put the device into standby mode so that configuration can be applied.*/
167  status =
168  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 =
185  Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
187  if (ARM_DRIVER_OK != status)
188  {
189  return SENSOR_ERROR_WRITE;
190  }
191 
192  return SENSOR_ERROR_NONE;
193 }
194 
196  const registerreadlist_t *pReadList,
197  uint8_t *pBuffer)
198 {
199  int32_t status;
200 
201  /*! Validate for the correct handle and register read list.*/
202  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
203  {
205  }
206 
207  /*! Check whether sensor handle is initialized before reading sensor data.*/
208  if (pSensorHandle->isInitialized != true)
209  {
210  return SENSOR_ERROR_INIT;
211  }
212 
213  /*! Parse through the read list and read the data one by one. */
214  status = Sensor_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
215  pReadList, pBuffer);
216  if (ARM_DRIVER_OK != status)
217  {
218  return SENSOR_ERROR_READ;
219  }
220 
221  return SENSOR_ERROR_NONE;
222 }
223 
225 {
226  if (pSensorHandle == NULL)
227  {
229  }
230 
231  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
232  if (pSensorHandle->isInitialized != true)
233  {
234  return SENSOR_ERROR_INIT;
235  }
236 
237  /*! Trigger sensor device reset.*/
238  Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
240 
241  /*! De-initialize sensor handle.
242  * We do not validate write success since 8700 reset before sensing ack for the transaction. */
243  pSensorHandle->isInitialized = false;
244 
245  return SENSOR_ERROR_NONE;
246 }
247 
249  fxos8700_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
250 {
251  int32_t status;
253 
254  if ((pSensorHandle == NULL) || (pBus == NULL))
255  {
257  }
258 
259  pSensorHandle->deviceInfo.deviceInstance = index;
260  pSensorHandle->deviceInfo.functionParam = NULL;
261  pSensorHandle->deviceInfo.idleFunction = NULL;
262 
263  /*! Read and store the device's WHO_AM_I.*/
264  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXOS8700_WHO_AM_I, 1, &reg);
265  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
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  /* Appy the Sensor Configuration based on the Register 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  if (pSensorHandle == NULL)
363  {
365  }
366 
367  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
368  if (pSensorHandle->isInitialized != true)
369  {
370  return SENSOR_ERROR_INIT;
371  }
372 
373  /*! Trigger sensor device reset.*/
374  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
376  pSensorHandle->isInitialized = false;
377  return SENSOR_ERROR_NONE;
378 }
#define FXOS8700_CTRL_REG1_ACTIVE_STANDBY_MODE
Definition: fxos8700.h:1567
fpSpiWritePreprocessFn_t pWritePreprocessFN
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
#define FXOS8700_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxos8700_drv.h:99
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:278
registerDeviceInfo_t deviceInfo
Definition: fxos8700_drv.h:61
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
ARM_DRIVER_SPI * pCommDrv
Definition: fxos8700_drv.h:62
int32_t status
uint8_t fxos8700_spiRead_DataBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:50
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:56
ARM_DRIVER_I2C * pCommDrv
Definition: fxos8700_drv.h:73
#define FXOS8700_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxos8700_drv.h:103
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
int32_t FXOS8700_SPI_Deinit(fxos8700_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxos8700_drv.c:224
This defines the sensor specific information for I2C.
Definition: fxos8700_drv.h:70
uint8_t FXOS8700_WHO_AM_I_t
Definition: fxos8700.h:649
uint8_t fxos8700_spiWrite_CmdDataBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:51
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:142
int32_t FXOS8700_I2C_Deinit(fxos8700_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: fxos8700_drv.c:360
spiSlaveSpecificParams_t slaveParams
Definition: fxos8700_drv.h:64
#define FXOS8700_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxos8700_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 FXOS8700_CTRL_REG1_ACTIVE_ACTIVE_MODE
Definition: fxos8700.h:1566
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:248
The SPI Slave Transfer Command Params SDK2.0 Driver.
uint8_t fxos8700_spiRead_CmdBuffer[FXOS8700_SPI_MAX_MSG_SIZE]
Definition: fxos8700_drv.c:49
#define SPI_SS_ACTIVE_LOW
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:331
registerDeviceInfo_t deviceInfo
Definition: fxos8700_drv.h:72
#define FXOS8700_CTRL_REG2_RST_MASK
Definition: fxos8700.h:1603
GENERIC_DRIVER_GPIO * pGPIODriver
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:195
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
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:286
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
uint32_t size
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 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:73
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.
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:93
This defines the sensor specific information for SPI.
Definition: fxos8700_drv.h:59
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 FXOS8700_CTRL_REG1_ACTIVE_MASK
Definition: fxos8700.h:1521
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 FXOS8700_CTRL_REG2_RST_EN
Definition: fxos8700.h:1615
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:150
fpSpiReadPreprocessFn_t pReadPreprocessFN
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