1 /*
2 * Copyright (c) 2013-2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_mma.h"
10
11 /******************************************************************************
12 * Code
13 ******************************************************************************/
MMA_Init(mma_handle_t * handle,mma_config_t * config)14 status_t MMA_Init(mma_handle_t *handle, mma_config_t *config)
15 {
16 assert(handle);
17 assert(config);
18
19 uint8_t val = 0;
20 uint32_t i = 0;
21
22 /* Initialize the callback function. */
23 handle->I2C_SendFunc = config->I2C_SendFunc;
24 handle->I2C_ReceiveFunc = config->I2C_ReceiveFunc;
25 /* Set Slave Address. */
26 handle->slaveAddress = config->slaveAddress;
27
28 if (MMA_ReadReg(handle, kMMA8451_WHO_AM_I, &val) != kStatus_Success)
29 {
30 return kStatus_Fail;
31 }
32
33 if (val != kMMA8451_WHO_AM_I_Device_ID)
34 {
35 return kStatus_Fail;
36 }
37
38 /* Reset sensor */
39 if (MMA_ReadReg(handle, kMMA8451_CTRL_REG2, &val) != kStatus_Success)
40 {
41 return kStatus_Fail;
42 }
43 val |= (uint8_t)0x40;
44 if (MMA_WriteReg(handle, kMMA8451_CTRL_REG2, val) != kStatus_Success)
45 {
46 return kStatus_Fail;
47 }
48 /* Delay to ensure reliable sensor reset */
49 for (i = 0; i < SystemCoreClock / 1000U; i++)
50 {
51 __NOP();
52 }
53 /* Put the mma8451 into standby mode */
54 if (MMA_ReadReg(handle, kMMA8451_CTRL_REG1, &val) != kStatus_Success)
55 {
56 return kStatus_Fail;
57 }
58 val &= (uint8_t)(~(0x01));
59 if (MMA_WriteReg(handle, kMMA8451_CTRL_REG1, val) != kStatus_Success)
60 {
61 return kStatus_Fail;
62 }
63 /* Set the range, -4g to 4g */
64 if (MMA_ReadReg(handle, kMMA8451_XYZ_DATA_CFG, &val) != kStatus_Success)
65 {
66 return kStatus_Fail;
67 }
68 val &= (uint8_t)(~0x03);
69 val |= 0x01;
70 if (MMA_WriteReg(handle, kMMA8451_XYZ_DATA_CFG, val) != kStatus_Success)
71 {
72 return kStatus_Fail;
73 }
74 /* Set the F_MODE, disable FIFO */
75 if (MMA_ReadReg(handle, kMMA8451_F_SETUP, &val) != kStatus_Success)
76 {
77 return kStatus_Fail;
78 }
79
80 val &= 0x3F;
81 if (MMA_WriteReg(handle, kMMA8451_F_SETUP, val) != kStatus_Success)
82 {
83 return kStatus_Fail;
84 }
85 /* Put the mma8451 into active mode */
86 if (MMA_ReadReg(handle, kMMA8451_CTRL_REG1, &val) != kStatus_Success)
87 {
88 return kStatus_Fail;
89 }
90 val |= 0x01;
91 val &= (uint8_t)(~0x02); /* set F_READ to 0 */
92 if (MMA_WriteReg(handle, kMMA8451_CTRL_REG1, val) != kStatus_Success)
93 {
94 return kStatus_Fail;
95 }
96 return kStatus_Success;
97 }
98
MMA_ReadSensorData(mma_handle_t * handle,mma_data_t * accel)99 status_t MMA_ReadSensorData(mma_handle_t *handle, mma_data_t *accel)
100 {
101 uint8_t val = 0U;
102 uint8_t ucStatus = 0;
103
104 do
105 {
106 if (MMA_ReadReg(handle, kMMA8451_STATUS, &ucStatus) != kStatus_Success)
107 {
108 return kStatus_Fail;
109 }
110 } while (!(ucStatus & 0x08));
111
112 if (MMA_ReadReg(handle, kMMA8451_OUT_X_MSB, &val) != kStatus_Success)
113 {
114 return kStatus_Fail;
115 }
116 accel->accelXMSB = val;
117 if (MMA_ReadReg(handle, kMMA8451_OUT_X_LSB, &val) != kStatus_Success)
118 {
119 return kStatus_Fail;
120 }
121 accel->accelXLSB = val;
122
123 if (MMA_ReadReg(handle, kMMA8451_OUT_Y_MSB, &val) != kStatus_Success)
124 {
125 return kStatus_Fail;
126 }
127 accel->accelYMSB = val;
128 if (MMA_ReadReg(handle, kMMA8451_OUT_Y_LSB, &val) != kStatus_Success)
129 {
130 return kStatus_Fail;
131 }
132 accel->accelYLSB = val;
133
134 if (MMA_ReadReg(handle, kMMA8451_OUT_Z_MSB, &val) != kStatus_Success)
135 {
136 return kStatus_Fail;
137 }
138 accel->accelZMSB = val;
139 if (MMA_ReadReg(handle, kMMA8451_OUT_Z_LSB, &val) != kStatus_Success)
140 {
141 return kStatus_Fail;
142 }
143 accel->accelZLSB = val;
144
145 return kStatus_Success;
146 }
147
MMA_ReadReg(mma_handle_t * handle,uint8_t reg,uint8_t * val)148 status_t MMA_ReadReg(mma_handle_t *handle, uint8_t reg, uint8_t *val)
149 {
150 assert(handle);
151 assert(val);
152
153 if (!handle->I2C_ReceiveFunc)
154 {
155 return kStatus_Fail;
156 }
157
158 return handle->I2C_ReceiveFunc(handle->slaveAddress, reg, 1, val, 1);
159 }
160
MMA_WriteReg(mma_handle_t * handle,uint8_t reg,uint8_t val)161 status_t MMA_WriteReg(mma_handle_t *handle, uint8_t reg, uint8_t val)
162 {
163 assert(handle);
164
165 if (!handle->I2C_SendFunc)
166 {
167 return kStatus_Fail;
168 }
169
170 return handle->I2C_SendFunc(handle->slaveAddress, reg, 1, val);
171 }
172