ISSDK  1.7
IoT Sensing Software Development Kit
fxls8471q_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 fxls8471q_drv.c
37  * @brief The fxls8471q_drv.c file implements the FXLS8471Q sensor driver interfaces.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "gpio_driver.h"
44 #include "fxls8471q_drv.h"
45 
46 //-----------------------------------------------------------------------
47 // Global Variables
48 //-----------------------------------------------------------------------
52 
53 //-----------------------------------------------------------------------
54 // Functions
55 //-----------------------------------------------------------------------
56 void FXLS8471Q_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
57 {
58  spiCmdParams_t *pSlaveCmd = pCmdOut;
59 
60  uint8_t *pWBuff = fxls8471q_spiRead_CmdBuffer;
61  uint8_t *pRBuff = fxls8471q_spiRead_DataBuffer;
62 
63  /* Formatting for Read command of FXLS8471Q 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 + FXLS8471Q_SPI_CMD_LEN;
69  pSlaveCmd->pWriteBuffer = pWBuff;
70  pSlaveCmd->pReadBuffer = pRBuff;
71 }
72 
73 void FXLS8471Q_SPI_WritePreprocess(void *pCmdOut, uint32_t offset, uint32_t size, void *pWritebuffer)
74 {
75  spiCmdParams_t *pSlaveCmd = pCmdOut;
76 
77  uint8_t *pWBuff = fxls8471q_spiWrite_CmdDataBuffer;
79 
80  /* Formatting for Write command of FXLS8471Q 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 + FXLS8471Q_SPI_CMD_LEN, pWritebuffer, size);
86 
87  /* Create the slave command. */
88  pSlaveCmd->size = size + FXLS8471Q_SPI_CMD_LEN;
89  pSlaveCmd->pWriteBuffer = pWBuff;
90  pSlaveCmd->pReadBuffer = pRBuff;
91 }
92 
94  ARM_DRIVER_SPI *pBus,
95  uint8_t index,
96  void *pSlaveSelect,
97  uint8_t whoAmi)
98 {
100  uint8_t reg;
102 
103  /*! Check the input parameters. */
104  if ((pSensorHandle == NULL) || (pBus == NULL) || (pSlaveSelect == NULL))
105  {
107  }
108 
109  /*! Initialize the sensor handle. */
110  pSensorHandle->pCommDrv = pBus;
113  pSensorHandle->slaveParams.pTargetSlavePinID = pSlaveSelect;
116 
117  pSensorHandle->deviceInfo.deviceInstance = index;
118  pSensorHandle->deviceInfo.functionParam = NULL;
119  pSensorHandle->deviceInfo.idleFunction = NULL;
120 
121  /* Initialize the Slave Select Pin. */
122  pGPIODriver->pin_init(pSlaveSelect, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
123  if (pSensorHandle->slaveParams.ssActiveValue == SPI_SS_ACTIVE_LOW)
124  {
125  pGPIODriver->set_pin(pSlaveSelect);
126  }
127  else
128  {
129  pGPIODriver->clr_pin(pSlaveSelect);
130  }
131 
132  /*! Read and store the device's WHO_AM_I.*/
133  status = Register_SPI_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
134  FXLS8471Q_WHO_AM_I, 1, &reg);
135  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
136  {
137  pSensorHandle->isInitialized = false;
138  return SENSOR_ERROR_INIT;
139  }
140 
141  pSensorHandle->isInitialized = true;
142  return SENSOR_ERROR_NONE;
143 }
144 
146  registeridlefunction_t idleTask,
147  void *userParam)
148 {
149  pSensorHandle->deviceInfo.functionParam = userParam;
150  pSensorHandle->deviceInfo.idleFunction = idleTask;
151 }
152 
154 {
155  int32_t status;
156 
157  /*! Validate for the correct handle and register write list.*/
158  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
159  {
161  }
162 
163  /*! Check whether sensor handle is initialized before applying configuration.*/
164  if (pSensorHandle->isInitialized != true)
165  {
166  return SENSOR_ERROR_INIT;
167  }
168 
169  /*! Put the device into standby mode so that configuration can be applied.*/
170  status = Register_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
172  if (ARM_DRIVER_OK != status)
173  {
174  return SENSOR_ERROR_WRITE;
175  }
176 
177  /*! Apply the Sensor Configuration based on the Register Write List */
178  status = Sensor_SPI_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, &pSensorHandle->slaveParams,
179  pRegWriteList);
180  if (ARM_DRIVER_OK != status)
181  {
182  return SENSOR_ERROR_WRITE;
183  }
184 
185  /*! Put the device into active mode and ready for reading data.*/
186  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  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  /* Note:
239  * There is a Errata described in the data sheet for soft reset with SPI,
240  * which says device specific paratameters do not get updated correctly from NVM,
241  * causing inaccurate data output once the soft-reset command issued in SPI mode.
242  * The work around is hard reset, which cannot be handled in sensor driver software.
243  * So software reset command wont be issued until this issue is fixed in the silicon.
244  */
245 
246  /*! De-initialize sensor handle. */
247  pSensorHandle->isInitialized = false;
248 
249  return SENSOR_ERROR_NONE;
250 }
fpSpiWritePreprocessFn_t pWritePreprocessFN
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
#define FXLS8471Q_SPI_MAX_MSG_SIZE
The MAX size of SPI message.
Definition: fxls8471q_drv.h:78
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
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 FXLS8471Q_SPI_Initialize(fxls8471q_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor for I2C.
Definition: fxls8471q_drv.c:93
int32_t status
registerDeviceInfo_t deviceInfo
Definition: fxls8471q_drv.h:59
#define FXLS8471Q_SPI_CMD_LEN
The size of the Sensor specific SPI Header.
Definition: fxls8471q_drv.h:82
void FXLS8471Q_SPI_ReadPreprocess(void *pCmdOut, uint32_t offset, uint32_t size)
The SPI Read Pre-Process function to generate Sensor specific SPI Message Header. ...
Definition: fxls8471q_drv.c:56
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define FXLS8471Q_CTRL_REG1_MODE_STANDBY
Definition: fxls8471q.h:1684
int32_t FXLS8471Q_SPI_Deinit(fxls8471q_spi_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
void FXLS8471Q_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: fxls8471q_drv.c:73
#define FXLS8471Q_SS_ACTIVE_VALUE
Is the Slave Select Pin Active Low or High.
Definition: fxls8471q_drv.h:86
void FXLS8471Q_SPI_SetIdleTask(fxls8471q_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
spiSlaveSpecificParams_t slaveParams
Definition: fxls8471q_drv.h:62
uint8_t fxls8471q_spiRead_DataBuffer[FXLS8471Q_SPI_MAX_MSG_SIZE]
Definition: fxls8471q_drv.c:50
int32_t FXLS8471Q_SPI_ReadData(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
#define FXLS8471Q_CTRL_REG1_MODE_ACTIVE
Definition: fxls8471q.h:1685
The SPI Slave Transfer Command Params SDK2.0 Driver.
#define SPI_SS_ACTIVE_LOW
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
#define FXLS8471Q_CTRL_REG1_MODE_MASK
Definition: fxls8471q.h:1665
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: fxls8471q_drv.h:57
int32_t FXLS8471Q_SPI_Configure(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
uint8_t fxls8471q_spiWrite_CmdDataBuffer[FXLS8471Q_SPI_MAX_MSG_SIZE]
Definition: fxls8471q_drv.c:51
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.
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
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.
uint8_t fxls8471q_spiRead_CmdBuffer[FXLS8471Q_SPI_MAX_MSG_SIZE]
Definition: fxls8471q_drv.c:49
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.