1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _FSL_ICM42688P_H_
8 #define _FSL_ICM42688P_H_
9 
10 #include "fsl_common.h"
11 
12 #define INVALID_DATA (-32768)
13 
14 /* Locate in bank 0. */
15 #define DEVICE_CONFIG      0x11U
16 #define INT_CONFIG         0x14U
17 #define TEMP_DATA1         0x1DU
18 #define TEMP_DATA0         0x1EU
19 #define PWR_MGMT0          0x4EU
20 #define GYRO_CONFIG0       0x4FU
21 #define ACCEL_CONFIG0      0x50U
22 #define GYRO_CONFIG1       0x51U
23 #define GYRO_ACCEL_CONFIG0 0x52U
24 #define ACCEL_CONFIG1      0x53U
25 #define WHO_AM_I           0x75U
26 #define WHO_AM_I_VALUE     0x47U
27 #define BANK_SEL           0x76U
28 
29 #define ACCEL_DATA_X1 0x1FU
30 #define ACCEL_DATA_X0 0x20U
31 
32 #define ACCEL_DATA_Y1 0x21U
33 #define ACCEL_DATA_Y0 0x22U
34 
35 #define ACCEL_DATA_Z1 0x23U
36 #define ACCEL_DATA_Z0 0x24U
37 
38 #define GYRO_DATA_X1 0x25U
39 #define GYRO_DATA_X0 0x26U
40 
41 #define GYRO_DATA_Y1 0x27U
42 #define GYRO_DATA_Y0 0x28U
43 
44 #define GYRO_DATA_Z1 0x29U
45 #define GYRO_DATA_Z0 0x2AU
46 
47 #define FIFO_CONFIG  0x16U
48 #define FIFO_CONFIG1 0x5FU
49 #define FIFO_DATA    0x30U
50 #define APEX_CONFIG0 0x56U
51 
52 /* Locate in bank 4. */
53 #define INT_SOURCE10 0x51U
54 
55 /*! @brief Define sensor access function. */
56 typedef status_t (*sensor_write_transfer_func_t)(uint8_t deviceAddress,
57                                                  uint32_t regAddress,
58                                                  uint8_t *regData,
59                                                  size_t dataSize);
60 typedef status_t (*sensor_read_transfer_func_t)(uint8_t deviceAddress,
61                                                 uint32_t regAddress,
62                                                 uint8_t *regData,
63                                                 size_t dataSize);
64 
65 typedef struct _icm42688p_sensor_data
66 {
67     int16_t accelDataX;
68     int16_t accelDataY;
69     int16_t accelDataZ;
70     int16_t gyroDataX;
71     int16_t gyroDataY;
72     int16_t gyroDataZ;
73 } icm42688p_sensor_data_t;
74 
75 typedef struct _icm42688p_handle
76 {
77     sensor_write_transfer_func_t Sensor_WriteTransfer;
78     sensor_read_transfer_func_t Sensor_ReadTransfer;
79     uint8_t sensorAddress;
80 } icm42688p_handle_t;
81 
82 typedef struct _icm42688p_config
83 {
84     sensor_write_transfer_func_t Sensor_WriteTransfer;
85     sensor_read_transfer_func_t Sensor_ReadTransfer;
86     uint8_t sensorAddress;
87     bool isReset;
88 } icm42688p_config_t;
89 
90 #if defined(__cplusplus)
91 extern "C" {
92 #endif
93 
94 /*!
95  * @brief Create handle for ICM42688P, reset the sensor per user configuration.
96  *
97  * @param icm42688p_handle The pointer to #icm42688p_handle_t.
98  *
99  * @return kStatus_Success if success or kStatus_Fail if error.
100  */
101 status_t ICM42688P_Init(icm42688p_handle_t *handle, icm42688p_config_t *config);
102 
103 /*!
104  * @brief Write Register with register data buffer.
105  *
106  * @param handle The pointer to #icm42688p_handle_t.
107  * @param regAddress register address to write.
108  * @param regData The pointer to data buffer to be write to the reg.
109  * @param dataSize Size of the regData.
110  *
111  * @return kStatus_Success if success or kStatus_Fail if error.
112  */
113 status_t ICM42688P_WriteReg(icm42688p_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize);
114 
115 /*!
116  * @brief Read Register to speficied data buffer.
117  *
118  * @param handle The pointer to #icm42688p_handle_t.
119  * @param regAddress register address to read.
120  * @param regData The pointer to data buffer to store the read out data.
121  * @param dataSize Size of the regData to be read.
122  *
123  * @return kStatus_Success if success or kStatus_Fail if error.
124  */
125 status_t ICM42688P_ReadReg(icm42688p_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize);
126 
127 /*!
128  * @brief Enable ACCEL and GYRO sensors.
129  *
130  * @param handle The pointer to #icm42688p_handle_t.
131  *
132  * @return kStatus_Success if success or kStatus_Fail if error.
133  */
134 status_t ICM42688P_EnableSensors(icm42688p_handle_t *handle);
135 
136 /*!
137  * @brief Configure ICM42688P sensor to generate TAP detect IBI.
138  *
139  * @param handle The pointer to #icm42688p_handle_t.
140  *
141  * @return kStatus_Success if success or kStatus_Fail if error.
142  */
143 status_t ICM42688P_ConfigureTapDetectIBI(icm42688p_handle_t *handle);
144 
145 /*!
146  * @brief Read sensor data from fifo.
147  *
148  * @param handle The pointer to #icm42688p_handle_t.
149  * @param icm42688p_sensor_data_t The pointer to #icm42688p_sensor_data_t which stores the read out data.
150  *
151  * @return kStatus_Success if success or kStatus_Fail if error.
152  * @return kStatus_NoData There's invalid data in sensorData structure.
153  */
154 status_t ICM42688P_ReadSensorData(icm42688p_handle_t *handle, icm42688p_sensor_data_t *sensorData);
155 #if defined(__cplusplus)
156 }
157 #endif /* __cplusplus */
158 
159 #endif /* _FSL_ICM42688P_H_ */
160