1 /*
2  * Copyright 2023 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxls8961af_interrupt.c
10  * @brief The fxls8961af_interrupt.c file implements the ISSDK FXLS8961AF sensor
11  *        driver example demonstration with interrupt mode.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "pin_mux.h"
18 #include "clock_config.h"
19 #include "board.h"
20 #include "fsl_debug_console.h"
21 
22 //-----------------------------------------------------------------------
23 // CMSIS Includes
24 //-----------------------------------------------------------------------
25 #include "Driver_I2C.h"
26 
27 //-----------------------------------------------------------------------
28 // ISSDK Includes
29 //-----------------------------------------------------------------------
30 #include "issdk_hal.h"
31 #include "gpio_driver.h"
32 #include "fxls8961_drv.h"
33 #include "systick_utils.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 #define FXLS8961_DATA_SIZE 6
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! @brief Register settings for Interrupt (non buffered) mode. */
44 const registerwritelist_t cFxls8961ConfigNormal[] = {
45     /* Set Full-scale range as 2G. */
46     {FXLS8961_SENS_CONFIG1, FXLS8961_SENS_CONFIG1_FSR_2G, FXLS8961_SENS_CONFIG1_FSR_MASK},
47     /* Set Wake Mode ODR Rate as 6.25Hz. */
48     {FXLS8961_SENS_CONFIG3, FXLS8961_SENS_CONFIG3_WAKE_ODR_6_25HZ, FXLS8961_SENS_CONFIG3_WAKE_ODR_MASK},
49     /* Enable Interrupts for Data Ready Events. */
50     {FXLS8961_INT_EN, FXLS8961_INT_EN_DRDY_EN_EN, FXLS8961_INT_EN_DRDY_EN_MASK},
51     __END_WRITE_DATA__};
52 
53 /*! @brief Address of Raw Accel Data in Normal Mode. */
54 const registerreadlist_t cFxls8961OutputNormal[] = {{.readFrom = FXLS8961_OUT_X_LSB, .numBytes = FXLS8961_DATA_SIZE},
55                                                     __END_READ_DATA__};
56 
57 //-----------------------------------------------------------------------
58 // Global Variables
59 //-----------------------------------------------------------------------
60 volatile bool gFxls8961DataReady = false;
61 
62 //-----------------------------------------------------------------------
63 // Functions
64 //-----------------------------------------------------------------------
65 /*! -----------------------------------------------------------------------
66  *  @brief       This is the Sensor Data Ready ISR implementation.
67  *  @details     This function sets the flag which indicates if a new sample(s) is available for reading.
68  *  @param[in]   pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
69  *  @return      void  There is no return value.
70  *  @constraints None
71  *  @reeentrant  Yes
72  *  -----------------------------------------------------------------------*/
fxls8961_int_data_ready_callback(void * pUserData)73 void fxls8961_int_data_ready_callback(void *pUserData)
74 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
75     gFxls8961DataReady = true;
76 }
77 
78 /*! -----------------------------------------------------------------------
79  *  @brief       This is the The main function implementation.
80  *  @details     This function invokes board initializes routines, then then brings up the sensor and
81  *               finally enters an endless loop to continuously read available samples.
82  *  @param[in]   void This is no input parameter.
83  *  @return      void  There is no return value.
84  *  @constraints None
85  *  @reeentrant  No
86  *  -----------------------------------------------------------------------*/
main(void)87 int main(void)
88 {
89     int32_t status;
90     uint8_t whoami;
91     uint8_t data[FXLS8961_DATA_SIZE];
92     fxls8961_acceldata_t rawData;
93 
94     ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
95     fxls8961_i2c_sensorhandle_t fxls8961Driver;
96     GENERIC_DRIVER_GPIO *pGpioDriver = &Driver_GPIO_KSDK;
97 
98     /*! Initialize the MCU hardware. */
99     BOARD_InitPins();
100     BOARD_BootClockRUN();
101     BOARD_SystickEnable();
102     BOARD_InitDebugConsole();
103 
104     PRINTF("\r\n ISSDK FXLS8961 sensor driver example demonstration with interrupt mode.\r\n");
105 
106     /*! Initialize FXLS8961 pin used by FRDM board */
107     pGpioDriver->pin_init(&FXLS8961_INT1, GPIO_DIRECTION_IN, NULL, &fxls8961_int_data_ready_callback, NULL);
108 
109     /*! Initialize RGB LED pin used by FRDM board */
110     pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
111 
112     /*! Initialize the I2C driver. */
113     status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
114     if (ARM_DRIVER_OK != status)
115     {
116         PRINTF("\r\n I2C Initialization Failed\r\n");
117         return -1;
118     }
119 
120     /*! Set the I2C Power mode. */
121     status = I2Cdrv->PowerControl(ARM_POWER_FULL);
122     if (ARM_DRIVER_OK != status)
123     {
124         PRINTF("\r\n I2C Power Mode setting Failed\r\n");
125         return -1;
126     }
127 
128     /*! Set the I2C bus speed. */
129     status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
130     if (ARM_DRIVER_OK != status)
131     {
132         PRINTF("\r\n I2C Control Mode setting Failed\r\n");
133         return -1;
134     }
135 
136     /*! Initialize FXLS8961 sensor driver. */
137     status = FXLS8961_I2C_Initialize(&fxls8961Driver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, FXLS8961_I2C_ADDR,
138                                      &whoami);
139     if (SENSOR_ERROR_NONE != status)
140     {
141         PRINTF("\r\n Sensor Initialization Failed\r\n");
142         return -1;
143     }
144     if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami))
145     {
146     	PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami);
147     }
148     else if ((FXLS8974_WHOAMI_VALUE == whoami) || (FXLS8968_WHOAMI_VALUE == whoami))
149     {
150     	PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami);
151     }
152     else if ((FXLS8971_WHOAMI_VALUE == whoami) || (FXLS8961_WHOAMI_VALUE == whoami))
153     {
154     	PRINTF("\r\n Successfully Initialized Chiron with WHO_AM_I = 0x%X\r\n", whoami);
155     }
156     else if (FXLS8962_WHOAMI_VALUE == whoami)
157     {
158     	PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami);
159     }
160     else
161     {
162     	PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami);
163     }
164 
165     /*!  Set the task to be executed while waiting for I2C transactions to complete. */
166     FXLS8961_I2C_SetIdleTask(&fxls8961Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
167 
168     /*! Configure the FXLS8961 sensor. */
169     status = FXLS8961_I2C_Configure(&fxls8961Driver, cFxls8961ConfigNormal);
170     if (SENSOR_ERROR_NONE != status)
171     {
172         PRINTF("\r\n FXLS8961 Sensor Configuration Failed, Err = %d\r\n", status);
173         return -1;
174     }
175     PRINTF("\r\n Successfully Applied FXLS8961 Sensor Configuration\r\n");
176 
177     for (;;) /* Forever loop */
178     {        /* In ISR Mode we do not need to check Data Ready Register.
179               * The receipt of interrupt will indicate data is ready. */
180         if (false == gFxls8961DataReady)
181         { /* Loop, if new sample is not available. */
182             SMC_SetPowerModeWait(SMC);
183             continue;
184         }
185         else
186         { /*! Clear the data ready flag, it will be set again by the ISR. */
187             gFxls8961DataReady = false;
188             pGpioDriver->toggle_pin(&GREEN_LED);
189         }
190 
191         /*! Read new raw sensor data from the FXLS8961. */
192         status = FXLS8961_I2C_ReadData(&fxls8961Driver, cFxls8961OutputNormal, data);
193         if (ARM_DRIVER_OK != status)
194         {
195             PRINTF("\r\n Read Failed. \r\n");
196             return -1;
197         }
198 
199         /*! Process the sample and convert the raw sensor data. */
200         rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
201         rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
202         rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
203 
204         /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
205         PRINTF("\r\n X=%5d Y=%5d Z=%5d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
206         ASK_USER_TO_RESUME(50); /* Ask for user input after processing 50 samples. */
207     }
208 }
209