-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmpl3115_drv.c
162 lines (137 loc) · 5.18 KB
/
mpl3115_drv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* @file mpl3115_drv.c
* @brief The mpl3115_drv.c file implements the mpl3115 functional interface.
*/
//-----------------------------------------------------------------------
// ISSDK Includes
//-----------------------------------------------------------------------
#include "mpl3115_drv.h"
//-----------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------
int32_t MPL3115_I2C_Initialize(
mpl3115_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
{
int32_t status;
uint8_t reg;
/*! Check the input parameters. */
if ((pSensorHandle == NULL) || (pBus == NULL))
{
return SENSOR_ERROR_INVALID_PARAM;
}
pSensorHandle->deviceInfo.deviceInstance = index;
pSensorHandle->deviceInfo.functionParam = NULL;
pSensorHandle->deviceInfo.idleFunction = NULL;
/*! Read and store the device's WHO_AM_I.*/
status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, MPL3115_WHO_AM_I, 1, ®);
if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
{
pSensorHandle->isInitialized = false;
return SENSOR_ERROR_INIT;
}
/*! Initialize the sensor handle. */
pSensorHandle->pCommDrv = pBus;
pSensorHandle->slaveAddress = sAddress;
pSensorHandle->isInitialized = true;
return SENSOR_ERROR_NONE;
}
void MPL3115_I2C_SetIdleTask(mpl3115_i2c_sensorhandle_t *pSensorHandle,
registeridlefunction_t idleTask,
void *userParam)
{
pSensorHandle->deviceInfo.functionParam = userParam;
pSensorHandle->deviceInfo.idleFunction = idleTask;
}
int32_t MPL3115_I2C_Configure(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
{
int32_t status;
/*! Validate for the correct handle and register write list.*/
if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
{
return SENSOR_ERROR_INVALID_PARAM;
}
/*! Check whether sensor handle is initialized before applying configuration.*/
if (pSensorHandle->isInitialized != true)
{
return SENSOR_ERROR_INIT;
}
/*! Put the device into standby mode so that configuration can be applied.*/
status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
MPL3115_CTRL_REG1, MPL3115_CTRL_REG1_SBYB_STANDBY, MPL3115_CTRL_REG1_SBYB_MASK, false);
if (ARM_DRIVER_OK != status)
{
return SENSOR_ERROR_WRITE;
}
/*! Apply the Sensor Configuration based on the Register Write List */
status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
pRegWriteList);
if (ARM_DRIVER_OK != status)
{
return SENSOR_ERROR_WRITE;
}
/*! Put the device into active mode and ready for reading data.*/
status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
MPL3115_CTRL_REG1, MPL3115_CTRL_REG1_SBYB_ACTIVE, MPL3115_CTRL_REG1_SBYB_MASK, false);
if (ARM_DRIVER_OK != status)
{
return SENSOR_ERROR_WRITE;
}
return SENSOR_ERROR_NONE;
}
int32_t MPL3115_I2C_ReadData(mpl3115_i2c_sensorhandle_t *pSensorHandle,
const registerreadlist_t *pReadList,
uint8_t *pBuffer)
{
int32_t status;
/*! Validate for the correct handle and register read list.*/
if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
{
return SENSOR_ERROR_INVALID_PARAM;
}
/*! Check whether sensor handle is initialized before reading sensor data.*/
if (pSensorHandle->isInitialized != true)
{
return SENSOR_ERROR_INIT;
}
/*! Parse through the read list and read the data one by one. */
status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
pReadList, pBuffer);
if (ARM_DRIVER_OK != status)
{
return SENSOR_ERROR_READ;
}
return SENSOR_ERROR_NONE;
}
int32_t MPL3115_I2C_DeInit(mpl3115_i2c_sensorhandle_t *pSensorHandle)
{
int32_t status;
if (pSensorHandle == NULL)
{
return SENSOR_ERROR_INVALID_PARAM;
}
/*! Check whether sensor handle is initialized before triggering sensor reset.*/
if (pSensorHandle->isInitialized != true)
{
return SENSOR_ERROR_INIT;
}
/*! Trigger sensor device reset.*/
status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
MPL3115_CTRL_REG1, MPL3115_CTRL_REG1_RST_EN, MPL3115_CTRL_REG1_RST_MASK, false);
if (ARM_DRIVER_OK != status)
{
return SENSOR_ERROR_WRITE;
}
else
{
/*! De-initialize sensor handle. */
pSensorHandle->isInitialized = false;
}
return SENSOR_ERROR_NONE;
}