/* * Copyright 2023 NXP * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ /** * @file fxls8961af_interrupt.c * @brief The fxls8961af_interrupt.c file implements the ISSDK FXLS8961AF sensor * driver example demonstration with interrupt mode. */ //----------------------------------------------------------------------- // SDK Includes //----------------------------------------------------------------------- #include "pin_mux.h" #include "clock_config.h" #include "board.h" #include "fsl_debug_console.h" //----------------------------------------------------------------------- // CMSIS Includes //----------------------------------------------------------------------- #include "Driver_I2C.h" //----------------------------------------------------------------------- // ISSDK Includes //----------------------------------------------------------------------- #include "issdk_hal.h" #include "gpio_driver.h" #include "fxls8961_drv.h" #include "systick_utils.h" //----------------------------------------------------------------------- // Macros //----------------------------------------------------------------------- #define FXLS8961_DATA_SIZE 6 //----------------------------------------------------------------------- // Constants //----------------------------------------------------------------------- /*! @brief Register settings for Interrupt (non buffered) mode. */ const registerwritelist_t cFxls8961ConfigNormal[] = { /* Set Full-scale range as 2G. */ {FXLS8961_SENS_CONFIG1, FXLS8961_SENS_CONFIG1_FSR_2G, FXLS8961_SENS_CONFIG1_FSR_MASK}, /* Set Wake Mode ODR Rate as 6.25Hz. */ {FXLS8961_SENS_CONFIG3, FXLS8961_SENS_CONFIG3_WAKE_ODR_6_25HZ, FXLS8961_SENS_CONFIG3_WAKE_ODR_MASK}, /* Enable Interrupts for Data Ready Events. */ {FXLS8961_INT_EN, FXLS8961_INT_EN_DRDY_EN_EN, FXLS8961_INT_EN_DRDY_EN_MASK}, __END_WRITE_DATA__}; /*! @brief Address of Raw Accel Data in Normal Mode. */ const registerreadlist_t cFxls8961OutputNormal[] = {{.readFrom = FXLS8961_OUT_X_LSB, .numBytes = FXLS8961_DATA_SIZE}, __END_READ_DATA__}; //----------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------- volatile bool gFxls8961DataReady = false; //----------------------------------------------------------------------- // Functions //----------------------------------------------------------------------- /*! ----------------------------------------------------------------------- * @brief This is the Sensor Data Ready ISR implementation. * @details This function sets the flag which indicates if a new sample(s) is available for reading. * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR. * @return void There is no return value. * @constraints None * @reeentrant Yes * -----------------------------------------------------------------------*/ void fxls8961_int_data_ready_callback(void *pUserData) { /*! @brief Set flag to indicate Sensor has signalled data ready. */ gFxls8961DataReady = true; } /*! ----------------------------------------------------------------------- * @brief This is the The main function implementation. * @details This function invokes board initializes routines, then then brings up the sensor and * finally enters an endless loop to continuously read available samples. * @param[in] void This is no input parameter. * @return void There is no return value. * @constraints None * @reeentrant No * -----------------------------------------------------------------------*/ int main(void) { int32_t status; uint8_t whoami; uint8_t data[FXLS8961_DATA_SIZE]; fxls8961_acceldata_t rawData; ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!! fxls8961_i2c_sensorhandle_t fxls8961Driver; GENERIC_DRIVER_GPIO *pGpioDriver = &Driver_GPIO_KSDK; /*! Initialize the MCU hardware. */ BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_SystickEnable(); BOARD_InitDebugConsole(); PRINTF("\r\n ISSDK FXLS8961 sensor driver example demonstration with interrupt mode.\r\n"); /*! Initialize FXLS8961 pin used by FRDM board */ pGpioDriver->pin_init(&FXLS8961_INT1, GPIO_DIRECTION_IN, NULL, &fxls8961_int_data_ready_callback, NULL); /*! Initialize RGB LED pin used by FRDM board */ pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL); /*! Initialize the I2C driver. */ status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT); if (ARM_DRIVER_OK != status) { PRINTF("\r\n I2C Initialization Failed\r\n"); return -1; } /*! Set the I2C Power mode. */ status = I2Cdrv->PowerControl(ARM_POWER_FULL); if (ARM_DRIVER_OK != status) { PRINTF("\r\n I2C Power Mode setting Failed\r\n"); return -1; } /*! Set the I2C bus speed. */ status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); if (ARM_DRIVER_OK != status) { PRINTF("\r\n I2C Control Mode setting Failed\r\n"); return -1; } /*! Initialize FXLS8961 sensor driver. */ status = FXLS8961_I2C_Initialize(&fxls8961Driver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, FXLS8961_I2C_ADDR, &whoami); if (SENSOR_ERROR_NONE != status) { PRINTF("\r\n Sensor Initialization Failed\r\n"); return -1; } if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami)) { PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami); } else if ((FXLS8974_WHOAMI_VALUE == whoami) || (FXLS8968_WHOAMI_VALUE == whoami)) { PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami); } else if ((FXLS8971_WHOAMI_VALUE == whoami) || (FXLS8961_WHOAMI_VALUE == whoami)) { PRINTF("\r\n Successfully Initialized Chiron with WHO_AM_I = 0x%X\r\n", whoami); } else if (FXLS8962_WHOAMI_VALUE == whoami) { PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami); } else { PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami); } /*! Set the task to be executed while waiting for I2C transactions to complete. */ FXLS8961_I2C_SetIdleTask(&fxls8961Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC); /*! Configure the FXLS8961 sensor. */ status = FXLS8961_I2C_Configure(&fxls8961Driver, cFxls8961ConfigNormal); if (SENSOR_ERROR_NONE != status) { PRINTF("\r\n FXLS8961 Sensor Configuration Failed, Err = %d\r\n", status); return -1; } PRINTF("\r\n Successfully Applied FXLS8961 Sensor Configuration\r\n"); for (;;) /* Forever loop */ { /* In ISR Mode we do not need to check Data Ready Register. * The receipt of interrupt will indicate data is ready. */ if (false == gFxls8961DataReady) { /* Loop, if new sample is not available. */ SMC_SetPowerModeWait(SMC); continue; } else { /*! Clear the data ready flag, it will be set again by the ISR. */ gFxls8961DataReady = false; pGpioDriver->toggle_pin(&GREEN_LED); } /*! Read new raw sensor data from the FXLS8961. */ status = FXLS8961_I2C_ReadData(&fxls8961Driver, cFxls8961OutputNormal, data); if (ARM_DRIVER_OK != status) { PRINTF("\r\n Read Failed. \r\n"); return -1; } /*! Process the sample and convert the raw sensor data. */ rawData.accel[0] = ((int16_t)data[1] << 8) | data[0]; rawData.accel[1] = ((int16_t)data[3] << 8) | data[2]; rawData.accel[2] = ((int16_t)data[5] << 8) | data[4]; /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */ PRINTF("\r\n X=%5d Y=%5d Z=%5d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]); ASK_USER_TO_RESUME(50); /* Ask for user input after processing 50 samples. */ } }