ISSDK  1.7
IoT Sensing Software Development Kit
mma865x_drv.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 mma865x_drv.c
37  * @brief The mma865x_drv.c file implements the mma865x functional interface.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "mma865x_drv.h"
44 
45 //-----------------------------------------------------------------------
46 // Functions
47 //-----------------------------------------------------------------------
49  mma865x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
50 {
52  uint8_t reg;
53 
54  /*! Check the input parameters. */
55  if ((pSensorHandle == NULL) || (pBus == NULL))
56  {
58  }
59 
60  pSensorHandle->deviceInfo.deviceInstance = index;
61  pSensorHandle->deviceInfo.functionParam = NULL;
62  pSensorHandle->deviceInfo.idleFunction = NULL;
63 
64  /*! Read and store the device's WHO_AM_I.*/
65  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, MMA865x_WHO_AM_I, 1, &reg);
66  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
67  {
68  pSensorHandle->isInitialized = false;
69  return SENSOR_ERROR_INIT;
70  }
71 
72  /*! Initialize the sensor handle. */
73  pSensorHandle->pCommDrv = pBus;
74  pSensorHandle->slaveAddress = sAddress;
75  pSensorHandle->isInitialized = true;
76  return SENSOR_ERROR_NONE;
77 }
78 
80  registeridlefunction_t idleTask,
81  void *userParam)
82 {
83  pSensorHandle->deviceInfo.functionParam = userParam;
84  pSensorHandle->deviceInfo.idleFunction = idleTask;
85 }
86 
88 {
90 
91  /*! Validate for the correct handle and register write list.*/
92  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
93  {
95  }
96 
97  /*! Check whether sensor handle is initialized before applying configuration.*/
98  if (pSensorHandle->isInitialized != true)
99  {
100  return SENSOR_ERROR_INIT;
101  }
102 
103  /* Put the device into standby mode so that configuration can be applied. */
104  status =
105  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
107  if (ARM_DRIVER_OK != status)
108  {
109  return SENSOR_ERROR_WRITE;
110  }
111 
112  /*! Apply the Sensor Configuration based on the Register Write List */
113  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
114  pRegWriteList);
115  if (ARM_DRIVER_OK != status)
116  {
117  return SENSOR_ERROR_WRITE;
118  }
119 
120  /*! Put the device into active mode and ready for reading data.*/
121  status =
122  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
124  if (ARM_DRIVER_OK != status)
125  {
126  return SENSOR_ERROR_WRITE;
127  }
128 
129  return SENSOR_ERROR_NONE;
130 }
131 
133  const registerreadlist_t *pReadList,
134  uint8_t *pBuffer)
135 {
136  int32_t status;
137 
138  /*! Validate for the correct handle and register read list.*/
139  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
140  {
142  }
143 
144  /*! Check whether sensor handle is initialized before reading sensor data.*/
145  if (pSensorHandle->isInitialized != true)
146  {
147  return SENSOR_ERROR_INIT;
148  }
149 
150  /*! Parse through the read list and read the data one by one. */
151  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
152  pReadList, pBuffer);
153  if (ARM_DRIVER_OK != status)
154  {
155  return SENSOR_ERROR_READ;
156  }
157 
158  return SENSOR_ERROR_NONE;
159 }
160 
162 {
163  int32_t status;
164 
165  if (pSensorHandle == NULL)
166  {
168  }
169 
170  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
171  if (pSensorHandle->isInitialized != true)
172  {
173  return SENSOR_ERROR_INIT;
174  }
175 
176  /*! Trigger sensor device reset.*/
177  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
179  if (ARM_DRIVER_OK != status)
180  {
181  return SENSOR_ERROR_WRITE;
182  }
183  else
184  {
185  /*! De-initialize sensor handle. */
186  pSensorHandle->isInitialized = false;
187  }
188 
189  return SENSOR_ERROR_NONE;
190 }
int32_t MMA865x_I2C_Initialize(mma865x_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: mma865x_drv.c:48
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
int32_t MMA865x_I2C_ReadData(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma865x_drv.c:132
int32_t status
#define MMA865x_CTRL_REG2_RST_EN
Definition: mma865x.h:1614
#define MMA865x_CTRL_REG1_ACTIVE_MASK
Definition: mma865x.h:1528
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 defines the sensor specific information.
Definition: mma865x_drv.h:55
registerDeviceInfo_t deviceInfo
Definition: mma865x_drv.h:57
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define MMA865x_CTRL_REG1_ACTIVE_ACTIVATED
Definition: mma865x.h:1559
void MMA865x_I2C_SetIdleTask(mma865x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma865x_drv.c:79
int32_t MMA865x_I2C_Configure(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma865x_drv.c:87
The mma865x_drv.h file describes the MMA865x driver interface and structures.
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
int32_t MMA865x_I2C_DeInit(mma865x_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: mma865x_drv.c:161
#define MMA865x_CTRL_REG2_RST_MASK
Definition: mma865x.h:1601
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.
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 MMA865x_CTRL_REG1_ACTIVE_STANDBY
Definition: mma865x.h:1558
ARM_DRIVER_I2C * pCommDrv
Definition: mma865x_drv.h:58
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