ISSDK  1.8
IoT Sensing Software Development Kit
fxls8974cf_motion_wakeup.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxls8974_motion_wakeup.c
10  * @brief The fxls8974_motion_wakeup.c file implements the ISSDK FXLS8974CF I2C sensor driver
11  * example demonstrating motion detection and Auto-Wake/Sleep features.
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 // ISSDK Includes
24 //-----------------------------------------------------------------------
25 #include "issdk_hal.h"
26 #include "gpio_driver.h"
27 #include "fxls8974_drv.h"
28 #include "systick_utils.h"
29 
30 //-----------------------------------------------------------------------
31 // CMSIS Includes
32 //-----------------------------------------------------------------------
33 #include "Driver_I2C.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 #define FXLS8974_DATA_SIZE 6
39 #define FXLS8974_STANDBY_MODE 0
40 #define FXLS8974_ACTIVE_MODE 1
41 //-----------------------------------------------------------------------
42 // Constants
43 //-----------------------------------------------------------------------
44 /*! @brief Register settings for configuring SDCD-OT for tap-detection and Auto-Wake/Sleep in interrupt mode. */
46  /* Set Full-scale range as 4G. */
48  /* Set Wake ODR as 400Hz & Sleep Mode ODR as 6.25Hz. */
50  /* Enable SDCD OT for all 3 axes X, Y & Z and within-thresholds event latch disabled. */
53  /* Enabling SDCD and Relative Data (N) � Data (N-1) mode for transient detection */
55  /* Set the SDCD_OT debounce count to 0 */
56  {FXLS8974_SDCD_OT_DBCNT, 0, 0},
57  /* Set the SDCD lower and upper thresholds to +/-100mg*/
58  {FXLS8974_SDCD_LTHS_LSB, 0xCC, 0},
59  {FXLS8974_SDCD_LTHS_MSB, 0xFF, 0},
60  {FXLS8974_SDCD_UTHS_LSB, 0x34, 0},
61  {FXLS8974_SDCD_UTHS_MSB, 0x00, 0},
62  /* Enable SDCD outside of thresholds event Auto-WAKE/SLEEP transition source enable. */
64  /* Set the ASLP count to 5sec */
65  {FXLS8974_ASLP_COUNT_LSB, 0xD0, 0},
66  {FXLS8974_ASLP_COUNT_MSB, 0x07, 0},
67  /* Enable Interrupts for WAKE mode. */
71 
72 /*! @brief Read register list to read SysMode Register. */
74 
75 /*! @brief Read register list to read INT_STATUS Register. */
77 
78 //-----------------------------------------------------------------------
79 // Global Variables
80 //-----------------------------------------------------------------------
81 volatile bool gFxls8974IntFlag = false;
82 
83  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
86 //-----------------------------------------------------------------------
87 // Functions
88 //-----------------------------------------------------------------------
89 /*! -----------------------------------------------------------------------
90  * @brief This is the Sensor Data Ready ISR implementation.
91  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
92  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
93  * @return void There is no return value.
94  * -----------------------------------------------------------------------*/
95 void fxls8974_int_callback(void *pUserData)
96 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
97  gFxls8974IntFlag = true;
98 }
99 
100 /*! -----------------------------------------------------------------------
101  * @brief This is the The main function implementation.
102  * @details This function invokes board initializes routines, then then brings up the sensor and
103  * finally enters an endless loop to continuously read available samples.
104  * @param[in] void This is no input parameter.
105  * @return void There is no return value.
106  * -----------------------------------------------------------------------*/
107 int main(void)
108 {
109  int32_t status;
110  uint8_t whoami;
111  uint8_t intStatus, eventStatus = 0;
112  uint8_t sleeptowake = 0;
113  uint8_t waketosleep = 0;
114  uint8_t firsttransition = 1;
115  uint8_t onetime_modetransition = 1;
116 
117  /*! Initialize the MCU hardware. */
118  BOARD_InitPins();
122 
123  PRINTF("\r\n ISSDK FXLS8974CF sensor driver example to detect motion event & AWS\r\n");
124 
125  /*! Initialize FXLS8974 pin used by FRDM board */
126  pGpioDriver->pin_init(&FXLS8974_INT1, GPIO_DIRECTION_IN, NULL, &fxls8974_int_callback, NULL);
127 
128  /*! Initialize RGB LED pin used by FRDM board */
129  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
130 
131  /*! Initialize the I2C driver. */
132  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
133  if (ARM_DRIVER_OK != status)
134  {
135  PRINTF("\r\n I2C Initialization Failed\r\n");
136  return -1;
137  }
138 
139  /*! Set the I2C Power mode. */
140  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
141  if (ARM_DRIVER_OK != status)
142  {
143  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
144  return -1;
145  }
146 
147  /*! Set the I2C bus speed. */
148  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
149  if (ARM_DRIVER_OK != status)
150  {
151  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
152  return -1;
153  }
154 
155  /*! Initialize FXLS8974 sensor driver. */
157  &whoami);
158  if (ARM_DRIVER_OK != status)
159  {
160  PRINTF("\r\n Sensor Initialization Failed\r\n");
161  return -1;
162  }
163  if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami))
164  {
165  PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami);
166  }
167  else if ((FXLS8974_WHOAMI_VALUE == whoami) || (FXLS8968_WHOAMI_VALUE == whoami))
168  {
169  PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami);
170  }
171  else if (FXLS8962_WHOAMI_VALUE == whoami)
172  {
173  PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami);
174  }
175  else
176  {
177  PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami);
178  return -1;
179  }
180 
181  /*! Set the task to be executed while waiting for I2C transactions to complete. */
183 
184  /*! Configure the FXLS8974 sensor. */
185  status = FXLS8974_I2C_Configure(&fxls8974Driver, cFxls8974AwsConfig);
186  if (SENSOR_ERROR_NONE != status)
187  {
188  PRINTF("\r\n FXLS8974 Sensor Configuration Failed, Err = %d\r\n", status);
189  return -1;
190  }
191  PRINTF("\r\n Successfully Applied FXLS8974 Sensor Configuration\r\n");
192 
193  for (;;) /* Forever loop */
194  {
195  eventStatus = 0;
196  /*! Read new raw sensor data from the FXLS8974. */
197  status = FXLS8974_I2C_ReadData(&fxls8974Driver, cFxls8974ReadSysMode, &eventStatus);
198  if (ARM_DRIVER_OK != status)
199  {
200  return status;
201  }
202 
203  if (eventStatus == FXLS8974_SYS_MODE_SYS_MODE_WAKE)
204  {
205  if (true == gFxls8974IntFlag)
206  {
207  if (sleeptowake == 1)
208  {
209  /*! Wake Mode Detected. */
210  PRINTF("\r\n Motion Detected....\r\n");
211  PRINTF("\r\n Motion Wake Mode Detected....SYSMODE = %d\r\n", eventStatus);
212  PRINTF("\r\n MCU woke-up on sensor motion event\r\n");
213  PRINTF("\r\n Will enter sleep mode after expiration of ASLP counter = ~5sec\r\n\r\n");
214  sleeptowake = 0;
215  }
216  pGpioDriver->set_pin(&RED_LED);
217  pGpioDriver->clr_pin(&GREEN_LED);
218  waketosleep = 1;
219  }
220  }
221  else
222  {
223  if ((waketosleep == 1) || (firsttransition == 1))
224  {
225  if (1 == onetime_modetransition)
226  {
227  onetime_modetransition = 0;
228  }
229 
230  status = FXLS8974_I2C_ReadData(&fxls8974Driver, cFxls8974ReadIntStatus, &intStatus);
231  if (ARM_DRIVER_OK != status)
232  {
233  return status;
234  }
235  PRINTF("\r\n ASLP counter expired....\r\n");
236  PRINTF("\r\n Going to Sleep Mode....SYSMODE = %d\r\n", eventStatus);
237  PRINTF("\r\n Putting MCU in low power sleep\r\n\r\n");
238  waketosleep = 0;
239  firsttransition = 0;
240  }
241  pGpioDriver->set_pin(&GREEN_LED);
242  pGpioDriver->clr_pin(&RED_LED);
243  sleeptowake = 1;
244  gFxls8974IntFlag = false;
246  continue;
247  }
248  }
249 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
#define FXLS8974_SENS_CONFIG3_SLEEP_ODR_MASK
Definition: fxls8974.h:548
#define FXLS8974_SYS_MODE_SYS_MODE_WAKE
Definition: fxls8974.h:385
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXLS8974_SENS_CONFIG1_FSR_MASK
Definition: fxls8974.h:423
int32_t FXLS8974_I2C_Configure(fxls8974_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls8974_drv.c:285
int32_t status
#define FXLS8974_SDCD_CONFIG1_Y_OT_EN_MASK
Definition: fxls8974.h:1523
int32_t FXLS8974_I2C_Initialize(fxls8974_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t *whoami)
The interface function to initialize the sensor.
Definition: fxls8974_drv.c:239
status_t SMC_SetPowerModeVlpr(void *arg)
Configures the system to VLPR power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:169
#define FXLS8974_INT1
#define FXLS8974_SDCD_CONFIG2_REF_UPDM_MASK
Definition: fxls8974.h:1618
#define FXLS8974_SDCD_CONFIG2_REF_UPDM_SDCD_REF
Definition: fxls8974.h:1633
gpioHandleKSDK_t RED_LED
Definition: frdm_k64f.c:181
#define FXLS8974_SDCD_CONFIG1_X_OT_EN_MASK
Definition: fxls8974.h:1526
const registerreadlist_t cFxls8974ReadSysMode[]
Read register list to read SysMode Register.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
This defines the sensor specific information for I2C.
Definition: fxls8974_drv.h:43
#define SMC
Definition: lpc54114.h:118
#define FXLS8974_INT_PIN_SEL_WK_OUT_INT2_MASK
Definition: fxls8974.h:913
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:47
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXLS8967_WHOAMI_VALUE
Definition: fxls8962.h:89
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define FXLS8974_I2C_ADDR
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define FXLS8974_SENS_CONFIG4_INT_POL_MASK
Definition: fxls8974.h:618
#define FXLS8974_INT_PIN_SEL_WK_OUT_INT2_DIS
Definition: fxls8974.h:968
const registerwritelist_t cFxls8974AwsConfig[]
Register settings for configuring SDCD-OT for tap-detection and Auto-Wake/Sleep in interrupt mode...
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
int32_t FXLS8974_I2C_ReadData(fxls8974_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls8974_drv.c:330
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
#define FXLS8974_INT_EN_WAKE_OUT_EN_MASK
Definition: fxls8974.h:829
#define FXLS8974_SDCD_CONFIG1_Z_OT_EN_EN
Definition: fxls8974.h:1556
#define FXLS8974_SDCD_CONFIG1_X_OT_EN_EN
Definition: fxls8974.h:1548
#define FXLS8974_SENS_CONFIG1_FSR_4G
Definition: fxls8974.h:455
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXLS8974_SDCD_CONFIG2_SDCD_EN_MASK
Definition: fxls8974.h:1621
#define FXLS8974_SDCD_CONFIG2_SDCD_EN_EN
Definition: fxls8974.h:1628
#define FXLS8974_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8974.h:551
volatile bool gFxls8974IntFlag
fxls8974_i2c_sensorhandle_t fxls8974Driver
#define FXLS8974_SDCD_CONFIG1_OT_ELE_DIS
Definition: fxls8974.h:1538
void FXLS8974_I2C_SetIdleTask(fxls8974_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxls8974_drv.c:277
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXLS8974_INT_EN_WAKE_OUT_EN_EN
Definition: fxls8974.h:876
ARM_DRIVER_I2C * I2Cdrv
int main(void)
This is the The main function implementation.
#define FXLS8974_SENS_CONFIG4_WK_SDCD_OT_EN
Definition: fxls8974.h:655
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:155
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define FXLS8974_WHOAMI_VALUE
Definition: fxls8962.h:90
The fxls8974_drv.h file describes the FXLS8974CF driver interface and structures. ...
#define FXLS8974_SENS_CONFIG3_WAKE_ODR_400HZ
Definition: fxls8974.h:560
#define FXLS8968_WHOAMI_VALUE
Definition: fxls896x.h:89
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define FXLS8974_SENS_CONFIG4_INT_POL_ACT_HIGH
Definition: fxls8974.h:680
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
GENERIC_DRIVER_GPIO * pGpioDriver
#define FXLS8974_SDCD_CONFIG1_Z_OT_EN_MASK
Definition: fxls8974.h:1520
#define FXLS8974_SENS_CONFIG3_SLEEP_ODR_6_25HZ
Definition: fxls8974.h:579
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define FXLS8964_WHOAMI_VALUE
Definition: fxls8962.h:88
const registerreadlist_t cFxls8974ReadIntStatus[]
Read register list to read INT_STATUS Register.
#define FXLS8974_SDCD_CONFIG1_Y_OT_EN_EN
Definition: fxls8974.h:1552
#define FXLS8974_SENS_CONFIG4_WK_SDCD_OT_MASK
Definition: fxls8974.h:633
#define FXLS8974_SDCD_CONFIG1_OT_ELE_MASK
Definition: fxls8974.h:1532
void fxls8974_int_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.