ISSDK  1.7
IoT Sensing Software Development Kit
mpl3115_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 mpl3115_drv.c
37  * @brief The mpl3115_drv.c file implements the mpl3115 functional interface.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "mpl3115_drv.h"
44 
45 //-----------------------------------------------------------------------
46 // Functions
47 //-----------------------------------------------------------------------
49  mpl3115_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, MPL3115_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 = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
106  if (ARM_DRIVER_OK != status)
107  {
108  return SENSOR_ERROR_WRITE;
109  }
110 
111  /*! Apply the Sensor Configuration based on the Register Write List */
112  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
113  pRegWriteList);
114  if (ARM_DRIVER_OK != status)
115  {
116  return SENSOR_ERROR_WRITE;
117  }
118 
119  /*! Put the device into active mode and ready for reading data.*/
120  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
122  if (ARM_DRIVER_OK != status)
123  {
124  return SENSOR_ERROR_WRITE;
125  }
126 
127  return SENSOR_ERROR_NONE;
128 }
129 
131  const registerreadlist_t *pReadList,
132  uint8_t *pBuffer)
133 {
134  int32_t status;
135 
136  /*! Validate for the correct handle and register read list.*/
137  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
138  {
140  }
141 
142  /*! Check whether sensor handle is initialized before reading sensor data.*/
143  if (pSensorHandle->isInitialized != true)
144  {
145  return SENSOR_ERROR_INIT;
146  }
147 
148  /*! Parse through the read list and read the data one by one. */
149  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
150  pReadList, pBuffer);
151  if (ARM_DRIVER_OK != status)
152  {
153  return SENSOR_ERROR_READ;
154  }
155 
156  return SENSOR_ERROR_NONE;
157 }
158 
160 {
161  int32_t status;
162 
163  if (pSensorHandle == NULL)
164  {
166  }
167 
168  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
169  if (pSensorHandle->isInitialized != true)
170  {
171  return SENSOR_ERROR_INIT;
172  }
173 
174  /*! Trigger sensor device reset.*/
175  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
177  if (ARM_DRIVER_OK != status)
178  {
179  return SENSOR_ERROR_WRITE;
180  }
181  else
182  {
183  /*! De-initialize sensor handle. */
184  pSensorHandle->isInitialized = false;
185  }
186 
187  return SENSOR_ERROR_NONE;
188 }
registerDeviceInfo_t deviceInfo
Definition: mpl3115_drv.h:64
#define MPL3115_CTRL_REG1_SBYB_MASK
Definition: mpl3115.h:846
ARM_DRIVER_I2C * pCommDrv
Definition: mpl3115_drv.h:65
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
int32_t MPL3115_I2C_Initialize(mpl3115_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: mpl3115_drv.c:48
#define MPL3115_CTRL_REG1_RST_EN
Definition: mpl3115.h:873
int32_t MPL3115_I2C_ReadData(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mpl3115_drv.c:130
int32_t status
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
void MPL3115_I2C_SetIdleTask(mpl3115_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mpl3115_drv.c:79
int32_t MPL3115_I2C_Configure(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mpl3115_drv.c:87
int32_t MPL3115_I2C_DeInit(mpl3115_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: mpl3115_drv.c:159
This defines the sensor specific information.
Definition: mpl3115_drv.h:62
#define MPL3115_CTRL_REG1_SBYB_ACTIVE
Definition: mpl3115.h:869
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define MPL3115_CTRL_REG1_RST_MASK
Definition: mpl3115.h:852
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
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.
#define MPL3115_CTRL_REG1_SBYB_STANDBY
Definition: mpl3115.h:868
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
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