1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_MMA8451Q_H_
10 #define _FSL_MMA8451Q_H_
11 
12 #include "fsl_common.h"
13 
14 #define WHO_AM_I_REG                   kMMA8451_WHO_AM_I
15 #define MMA8451Q_ACCEL_RESOLUTION_BITS 14
16 
17 /*!
18  * @brief Register definitions for the MMA8451.
19  */
20 enum _mma8451_constants
21 {
22     kMMA8451_STATUS             = 0x00,
23     kMMA8451_OUT_X_MSB          = 0x01,
24     kMMA8451_OUT_X_LSB          = 0x02,
25     kMMA8451_OUT_Y_MSB          = 0x03,
26     kMMA8451_OUT_Y_LSB          = 0x04,
27     kMMA8451_OUT_Z_MSB          = 0x05,
28     kMMA8451_OUT_Z_LSB          = 0x06,
29     kMMA8451_F_SETUP            = 0x09,
30     kMMA8451_TRIG_CFG           = 0x0a,
31     kMMA8451_SYSMOD             = 0x0b,
32     kMMA8451_INT_SOURCE         = 0x0c,
33     kMMA8451_WHO_AM_I           = 0x0d,
34     kMMA8451_WHO_AM_I_Device_ID = 0x1a,
35     kMMA8451_XYZ_DATA_CFG       = 0x0e,
36     kMMA8451_CTRL_REG1          = 0x2a,
37     kMMA8451_CTRL_REG2          = 0x2b,
38     kMMA8451_CTRL_REG3          = 0x2c,
39     kMMA8451_CTRL_REG4          = 0x2d,
40     kMMA8451_CTRL_REG5          = 0x2e
41 };
42 
43 typedef struct _mma_data
44 {
45     uint8_t accelXMSB;
46     uint8_t accelXLSB;
47     uint8_t accelYMSB;
48     uint8_t accelYLSB;
49     uint8_t accelZMSB;
50     uint8_t accelZLSB;
51 } mma_data_t;
52 
53 /*! @brief Define I2C access function. */
54 typedef status_t (*I2C_SendFunc_t)(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff);
55 typedef status_t (*I2C_ReceiveFunc_t)(
56     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);
57 
58 /*! @brief mma8451q configure definition. This structure should be global.*/
59 typedef struct _mma_handle
60 {
61     /* Pointer to the user-defined I2C Send Data function. */
62     I2C_SendFunc_t I2C_SendFunc;
63     /* Pointer to the user-defined I2C Receive Data function. */
64     I2C_ReceiveFunc_t I2C_ReceiveFunc;
65     /* The I2C slave address . */
66     uint8_t slaveAddress;
67 } mma_handle_t;
68 
69 /*! @brief mma8451q configure structure.*/
70 typedef struct _mma_config
71 {
72     /* Pointer to the user-defined I2C Send Data function. */
73     I2C_SendFunc_t I2C_SendFunc;
74     /* Pointer to the user-defined I2C Receive Data function. */
75     I2C_ReceiveFunc_t I2C_ReceiveFunc;
76     /* The I2C slave address . */
77     uint8_t slaveAddress;
78 } mma_config_t;
79 
80 /*******************************************************************************
81  * API
82  ******************************************************************************/
83 
84 #if defined(__cplusplus)
85 extern "C" {
86 #endif
87 
88 /*!
89  * @brief Initialize the MMA8451 driver instance.
90  *
91  * @param accel_device Device driver state structure that will be filled in by this function.
92  *      It is the responsibility of the caller to provide storage for this structure, and
93  *      to free that storage when the driver is no longer needed.
94  *
95  * @return kStatus_Success if success or kStatus_Fail if error.
96  */
97 status_t MMA_Init(mma_handle_t *handle, mma_config_t *config);
98 
99 /*!
100  * @brief Read the current acceleration values.
101  *
102  * @param handle Pointer to a valid accel_device instance structure.
103  * @param accel Pointer to a structure that will be filled in with the current acceleration
104  *      values for all three axes.
105  *
106  * @return kStatus_Success if success or kStatus_Fail if error.
107  */
108 status_t MMA_ReadSensorData(mma_handle_t *handle, mma_data_t *accel);
109 
110 /*!
111  * @brief Read the value of register in accelerometer.
112  *
113  * @param handle Pointer to a valid accel_device instance structure.
114  * @param reg variable store address of register
115  * @param val pointer store return value.
116  *
117  * @return kStatus_Success if success or kStatus_Fail if error.
118  */
119 status_t MMA_ReadReg(mma_handle_t *handle, uint8_t reg, uint8_t *val);
120 
121 /*!
122  * @brief Write the value to register of accelerometer.
123  *
124  * @param handle Pointer to a valid accel_device instance structure.
125  * @param reg variable store address of register
126  * @param val pointer store value which is writen to accelerometer.
127  *
128  * @return kStatus_Success if success or kStatus_Fail if error.
129  */
130 status_t MMA_WriteReg(mma_handle_t *handle, uint8_t reg, uint8_t val);
131 
132 /*!
133  * @brief Get device accelerator resolution bits.
134  *
135  * @return accelerator resolution bits.
136  */
MMA_GetResolutionBits(void)137 static inline uint8_t MMA_GetResolutionBits(void)
138 {
139     return MMA8451Q_ACCEL_RESOLUTION_BITS;
140 }
141 
142 #if defined(__cplusplus)
143 }
144 #endif
145 
146 #endif /* _FSL_MMA8451Q_H_ */
147