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