1 /*
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_fxls.h"
9 
10 /******************************************************************************
11  * Code
12  ******************************************************************************/
FXLS_Init(fxls_handle_t * fxls_handle,fxls_config_t * config)13 status_t FXLS_Init(fxls_handle_t *fxls_handle, fxls_config_t *config)
14 {
15     assert(fxls_handle != NULL);
16     assert(config != NULL);
17     assert(config->I2C_SendFunc != NULL);
18     assert(config->I2C_ReceiveFunc != NULL);
19 
20     uint8_t tmp[1] = {0};
21 
22     /* Initialize the I2C access function. */
23     fxls_handle->I2C_SendFunc    = config->I2C_SendFunc;
24     fxls_handle->I2C_ReceiveFunc = config->I2C_ReceiveFunc;
25     /* Set Slave Address. */
26     fxls_handle->slaveAddress = config->slaveAddress;
27 
28     if (FXLS_ReadReg(fxls_handle, WHO_AM_I_REG, tmp, 1) != kStatus_Success)
29     {
30         return kStatus_Fail;
31     }
32 
33     if (tmp[0] != kFXLS_WHO_AM_I_Device_ID)
34     {
35         return kStatus_Fail;
36     }
37 
38     /* Setup SENS_CONFIG1 register to enter standby mode. */
39     if (FXLS_ReadReg(fxls_handle, SENS_CONFIG1_REG, tmp, 1) != kStatus_Success)
40     {
41         return kStatus_Fail;
42     }
43 
44     if (FXLS_WriteReg(fxls_handle, SENS_CONFIG1_REG, tmp[0] & (uint8_t)~ACTIVE_MASK) != kStatus_Success)
45     {
46         return kStatus_Fail;
47     }
48 
49     /* Read again to make sure we are in standby mode. */
50     if (FXLS_ReadReg(fxls_handle, SENS_CONFIG1_REG, tmp, 1) != kStatus_Success)
51     {
52         return kStatus_Fail;
53     }
54 
55     if ((tmp[0] & ACTIVE_MASK) == ACTIVE_MASK)
56     {
57         return kStatus_Fail;
58     }
59 
60     if (FXLS_WriteReg(fxls_handle, SENS_CONFIG2_REG, 0) != kStatus_Success)
61     {
62         return kStatus_Fail;
63     }
64 
65     if (FXLS_WriteReg(fxls_handle, SENS_CONFIG4_REG, 0x1) != kStatus_Success)
66     {
67         return kStatus_Fail;
68     }
69 
70     if (FXLS_WriteReg(fxls_handle, SENS_CONFIG1_REG, ACTIVE_MASK) != kStatus_Success)
71     {
72         return kStatus_Fail;
73     }
74 
75     /* Read SENS_CONFIG1 register again to ensure we are in active mode */
76     if (FXLS_ReadReg(fxls_handle, SENS_CONFIG1_REG, tmp, 1) != kStatus_Success)
77     {
78         return kStatus_Fail;
79     }
80 
81     if ((tmp[0] & ACTIVE_MASK) != ACTIVE_MASK)
82     {
83         return kStatus_Fail;
84     }
85 
86     if (FXLS_WriteReg(fxls_handle, SENS_CONFIG4_REG, 0x5) != kStatus_Success)
87     {
88         return kStatus_Fail;
89     }
90 
91     return kStatus_Success;
92 }
93 
FXLS_ReadAccelRawData(fxls_handle_t * fxls_handle,fxls_accel_raw_data_t * accelRawData)94 status_t FXLS_ReadAccelRawData(fxls_handle_t *fxls_handle, fxls_accel_raw_data_t *accelRawData)
95 {
96     status_t status = kStatus_Success;
97 
98     if ((FXLS_ReadReg(fxls_handle, OUT_X_LSB_REG, (uint8_t *)accelRawData, 6)) != kStatus_Success)
99     {
100         status = kStatus_Fail;
101     }
102 
103     return status;
104 }
105 
FXLS_ReadAccelData(fxls_handle_t * fxls_handle,fxls_accel_data_t * accelData)106 status_t FXLS_ReadAccelData(fxls_handle_t *fxls_handle, fxls_accel_data_t *accelData)
107 {
108     status_t status     = kStatus_Success;
109     uint8_t tmp_buff[6] = {0};
110 
111     if ((FXLS_ReadReg(fxls_handle, OUT_X_LSB_REG, tmp_buff, 6)) != kStatus_Success)
112     {
113         status = kStatus_Fail;
114     }
115     else
116     {
117         /* Get the X and Y data from the sensor data structure in 12 bit left format data*/
118         accelData->accelX = (int16_t)(uint16_t)(((uint16_t)tmp_buff[1] << 8) | (uint16_t)tmp_buff[0]);
119         accelData->accelY = (int16_t)(uint16_t)(((uint16_t)tmp_buff[3] << 8) | (uint16_t)tmp_buff[2]);
120         accelData->accelZ = (int16_t)(uint16_t)(((uint16_t)tmp_buff[5] << 8) | (uint16_t)tmp_buff[4]);
121     }
122 
123     return status;
124 }
125 
FXLS_ReadReg(fxls_handle_t * handle,uint8_t reg,uint8_t * val,uint8_t bytesNumber)126 status_t FXLS_ReadReg(fxls_handle_t *handle, uint8_t reg, uint8_t *val, uint8_t bytesNumber)
127 {
128     assert(handle != NULL);
129     assert(val != NULL);
130 
131     if ((handle->I2C_ReceiveFunc) == NULL)
132     {
133         return kStatus_Fail;
134     }
135     else
136     {
137         return handle->I2C_ReceiveFunc(handle->slaveAddress, reg, 1, val, bytesNumber);
138     }
139 }
140 
FXLS_WriteReg(fxls_handle_t * handle,uint8_t reg,uint8_t val)141 status_t FXLS_WriteReg(fxls_handle_t *handle, uint8_t reg, uint8_t val)
142 {
143     assert(handle != NULL);
144 
145     if ((handle->I2C_SendFunc) == NULL)
146     {
147         return kStatus_Fail;
148     }
149     else
150     {
151         return handle->I2C_SendFunc(handle->slaveAddress, reg, 1, val);
152     }
153 }
154