1 /*
2  * Copyright 2021-2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #include "fsl_icm42688p.h"
8 
9 /*******************************************************************************
10  * Definitions
11  ******************************************************************************/
12 /*******************************************************************************
13  * Prototypes
14  ******************************************************************************/
15 /*******************************************************************************
16  * Variables
17  ******************************************************************************/
18 /*******************************************************************************
19  * Code
20  ******************************************************************************/
ICM42688P_WriteReg(icm42688p_handle_t * handle,uint32_t regAddress,uint8_t * regData,size_t dataSize)21 status_t ICM42688P_WriteReg(icm42688p_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize)
22 {
23     status_t result = kStatus_Success;
24     result          = handle->Sensor_WriteTransfer(handle->sensorAddress, regAddress, regData, dataSize);
25 
26     return (result == kStatus_Success) ? result : kStatus_Fail;
27 }
28 
ICM42688P_ReadReg(icm42688p_handle_t * handle,uint32_t regAddress,uint8_t * regData,size_t dataSize)29 status_t ICM42688P_ReadReg(icm42688p_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize)
30 {
31     status_t result = kStatus_Success;
32     result          = handle->Sensor_ReadTransfer(handle->sensorAddress, regAddress, regData, dataSize);
33 
34     return (result == kStatus_Success) ? result : kStatus_Fail;
35 }
36 
ICM42688P_Init(icm42688p_handle_t * handle,icm42688p_config_t * config)37 status_t ICM42688P_Init(icm42688p_handle_t *handle, icm42688p_config_t *config)
38 {
39     assert(handle != NULL);
40     assert(config != NULL);
41     assert(config->Sensor_WriteTransfer != NULL);
42     assert(config->Sensor_ReadTransfer != NULL);
43 
44     handle->Sensor_WriteTransfer = config->Sensor_WriteTransfer;
45     handle->Sensor_ReadTransfer  = config->Sensor_ReadTransfer;
46     handle->sensorAddress        = config->sensorAddress;
47 
48     status_t result = kStatus_Success;
49 
50     if (config->isReset)
51     {
52         uint8_t resetVal = 0x01U;
53         uint8_t bankSel  = 0;
54 
55         result = ICM42688P_WriteReg(handle, BANK_SEL, &bankSel, 1U);
56         if (result != kStatus_Success)
57         {
58             return result;
59         }
60         result = ICM42688P_WriteReg(handle, DEVICE_CONFIG, &resetVal, 1U);
61         if (result != kStatus_Success)
62         {
63             return result;
64         }
65         SDK_DelayAtLeastUs(1000, SystemCoreClock);
66     }
67 
68     return result;
69 }
70 
ICM42688P_EnableSensors(icm42688p_handle_t * handle)71 status_t ICM42688P_EnableSensors(icm42688p_handle_t *handle)
72 {
73     uint8_t sensorConfig = 0x1FU;
74     uint8_t fifoConfData = 0x03U;
75     uint8_t fifoInitVal  = 0x40U;
76     uint8_t whoamiVal    = 0;
77     status_t result      = kStatus_Success;
78 
79     result = ICM42688P_ReadReg(handle, WHO_AM_I, &whoamiVal, 1U);
80     if (result != kStatus_Success)
81     {
82         return result;
83     }
84     assert(whoamiVal == WHO_AM_I_VALUE);
85 
86     result = ICM42688P_WriteReg(handle, PWR_MGMT0, &sensorConfig, 1U);
87     if (result != kStatus_Success)
88     {
89         return result;
90     }
91 
92     result = ICM42688P_WriteReg(handle, FIFO_CONFIG, &fifoInitVal, 1U);
93     if (result != kStatus_Success)
94     {
95         return result;
96     }
97 
98     return ICM42688P_WriteReg(handle, FIFO_CONFIG1, &fifoConfData, 1U);
99 }
100 
ICM42688P_ConfigureTapDetectIBI(icm42688p_handle_t * handle)101 status_t ICM42688P_ConfigureTapDetectIBI(icm42688p_handle_t *handle)
102 {
103     uint8_t confData = 0x40U;
104     uint8_t bankSel  = 4;
105     uint8_t enTapIbi = 0x1U;
106     status_t result  = kStatus_Success;
107 
108     result = ICM42688P_WriteReg(handle, APEX_CONFIG0, &confData, 1U);
109     if (result != kStatus_Success)
110     {
111         return result;
112     }
113 
114     result = ICM42688P_WriteReg(handle, BANK_SEL, &bankSel, 1U);
115     if (result != kStatus_Success)
116     {
117         return result;
118     }
119 
120     return ICM42688P_WriteReg(handle, INT_SOURCE10, &enTapIbi, 1U);
121 }
122 
ICM42688P_ReadSensorData(icm42688p_handle_t * handle,icm42688p_sensor_data_t * sensorData)123 status_t ICM42688P_ReadSensorData(icm42688p_handle_t *handle, icm42688p_sensor_data_t *sensorData)
124 {
125     status_t result = kStatus_Success;
126     uint8_t fifoData[16];
127 
128     result = ICM42688P_ReadReg(handle, FIFO_DATA, fifoData, 16U);
129     if (result == kStatus_Success)
130     {
131         sensorData->accelDataX = (int16_t)(uint16_t)(((uint16_t)fifoData[1] << 8U) | (uint16_t)fifoData[2]);
132         sensorData->accelDataY = (int16_t)(uint16_t)(((uint16_t)fifoData[3] << 8U) | (uint16_t)fifoData[4]);
133         sensorData->accelDataZ = (int16_t)(uint16_t)(((uint16_t)fifoData[5] << 8U) | (uint16_t)fifoData[6]);
134         sensorData->gyroDataX  = (int16_t)(uint16_t)(((uint16_t)fifoData[7] << 8U) | (uint16_t)fifoData[8]);
135         sensorData->gyroDataY  = (int16_t)(uint16_t)(((uint16_t)fifoData[9] << 8U) | (uint16_t)fifoData[10]);
136         sensorData->gyroDataZ  = (int16_t)(uint16_t)(((uint16_t)fifoData[11] << 8U) | (uint16_t)fifoData[12]);
137     }
138 
139     if ((sensorData->accelDataX == INVALID_DATA) || (sensorData->gyroDataX == INVALID_DATA))
140     {
141         result = kStatus_NoData;
142     }
143 
144     return result;
145 }
146