ISSDK  1.8
IoT Sensing Software Development Kit
fxls8962_freefall.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file fxls8962_freefall.c
11  * @brief The fxls8962_freefall.c file implements the ISSDK FXLS8962 sensor driver
12  * example demonstration for Freefall Detection.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.h"
21 #include "fsl_debug_console.h"
22 
23 //-----------------------------------------------------------------------
24 // CMSIS Includes
25 //-----------------------------------------------------------------------
26 #include "Driver_I2C.h"
27 
28 /* ISSDK Includes */
29 #include "issdk_hal.h"
30 #include "gpio_driver.h"
31 #include "fxls8962_drv.h"
32 #include "systick_utils.h"
33 
34 //-----------------------------------------------------------------------
35 // Macros
36 //-----------------------------------------------------------------------
37 /* SDCD free-fall counter register values.
38  * These values have been derived form the Application Note AN4070 for MMA8451 (the same is applicable to FXLS8962 too).
39  * http://cache.freescale.com/files/sensors/doc/app_note/AN4070.pdf */
40 #define SDCD_WT_DBCNT 0x0A /* Debounce count value. */
41 #define SDCD_LTHS_LSB 0x33 /* Lower Threshold LSB value. */
42 #define SDCD_LTHS_MSB 0x0F /* Lower Threshold MSB value. */
43 #define SDCD_UTHS_LSB 0xCD /* Upper Threshold LSB value. */
44 #define SDCD_UTHS_MSB 0x00 /* Upper Threshold MSB value. */
45 
46 //-----------------------------------------------------------------------
47 // Constants
48 //-----------------------------------------------------------------------
49 /*! @brief Register settings for free-fall detection. */
51  /* Set Wake Mode ODR Rate as 100Hz. */
53  /* Enable Within-Threshold Event-Laching-Enable and X,Y,Z Axis functions. */
58  /* Enable SDCD function; Set SDCD internal reference values update mode to fixed value; Set Debounce counter to be
59  cleared whenever the SDCD within thresholds. */
63  /* Set WT INT Enable. */
65 
66  /* Set SDCD Debounce counter value. */
68  /* Set SDCD Lower Threshold LSB value. */
70  /* Set SDCD Lower Threshold MSB value. */
72  /* Set SDCH Upper Threshold LSB value. */
74  /* Set SDCH Upper Threshold MSB value. */
77 
78 /*! @brief Address of SDCD Status Register. */
81 
82 //-----------------------------------------------------------------------
83 // Global Variables
84 //-----------------------------------------------------------------------
85 volatile bool gFxls8962EventReady = false;
86 
87 //-----------------------------------------------------------------------
88 // Functions
89 //-----------------------------------------------------------------------
90 /*! @brief This is the Sensor Data Ready ISR implementation. */
91 void fxls8962_int_event_ready_callback(void *pUserData)
92 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
93  gFxls8962EventReady = true;
94 }
95 
96 /*!
97  * @brief Main function
98  */
99 int main(void)
100 {
101  int32_t status;
102  uint8_t whoami;
103  uint8_t dataReady;
104  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
107 
108  BOARD_InitPins();
112 
113  PRINTF("\r\n ISSDK FXLS8962 sensor driver example for Freefall Detection. \r\n");
114 
115  /*! Initialize FXLS8962 pin used by FRDM board */
117 
118  /*! Initialize the I2C driver. */
119  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
120  if (ARM_DRIVER_OK != status)
121  {
122  PRINTF("\r\n I2C Initialization Failed\r\n");
123  return -1;
124  }
125 
126  /*! Set the I2C Power mode. */
127  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
128  if (ARM_DRIVER_OK != status)
129  {
130  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
131  return -1;
132  }
133 
134  /*! Set the I2C bus speed. */
135  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
136  if (ARM_DRIVER_OK != status)
137  {
138  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
139  return -1;
140  }
141 
142  /*! Initialize the FXLS8962 sensor driver. */
144  &whoami);
145  if (SENSOR_ERROR_NONE != status)
146  {
147  PRINTF("\r\n Sensor Initialization Failed\r\n");
148  return -1;
149  }
150  if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami))
151  {
152  PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami);
153  }
154  else if (FXLS8974_WHOAMI_VALUE == whoami)
155  {
156  PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami);
157  }
158  else if (FXLS8962_WHOAMI_VALUE == whoami)
159  {
160  PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami);
161  }
162  else
163  {
164  PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami);
165  }
166 
167  /*! Set the task to be executed while waiting for I2C transactions to complete. */
169 
170  /* Set data not ready, event data will be available after sensor is configured and free fall detected. */
171  dataReady = 0;
172 
173  /*! Configure the FXLS8962 sensor for Freefall detection Mode. */
174  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigFreeFall);
175  if (SENSOR_ERROR_NONE != status)
176  {
177  PRINTF("\r\n FXLS8962 Sensor Configuration Failed, Err = %d \r\n", status);
178  return -1;
179  }
180  PRINTF("\r\n FXLS8962 now active and detecting freefall... \r\n");
181 
182  for (;;) /* Forever loop */
183  { /* In ISR Mode we do not need to check Data Ready Register.
184  * The receipt of interrupt will indicate data is ready. */
185  if (false == gFxls8962EventReady)
186  { /* Loop, if new sample is not available. */
188  continue;
189  }
190  else
191  { /*! Clear the data ready flag, it will be set again by the ISR. */
192  gFxls8962EventReady = false;
193  }
194 
195  /*! Read the Freefall event FLAGs from FXLS8962. */
196  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962FreeFallEvent, &dataReady);
197  if (SENSOR_ERROR_NONE != status)
198  {
199  PRINTF("\r\n Read Failed\r\n");
200  return -1;
201  }
202 
204  { /* Loop, if new event is not detected. */
205  continue;
206  }
207 
208  /*! Display that a freefall event has been detected. */
209  PRINTF("\r\n Freefall detected !!!\r\n");
210  }
211 }
212 ////////////////////////////////////////////////////////////////////////////////
213 // EOF
214 ////////////////////////////////////////////////////////////////////////////////
#define SDCD_LTHS_LSB
#define FXLS8962_SDCD_CONFIG1_Y_WT_EN_MASK
Definition: fxls8962.h:1514
#define FXLS8962_SDCD_CONFIG1_Y_WT_EN_EN
Definition: fxls8962.h:1564
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define SDCD_UTHS_LSB
int32_t status
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
The fxls8962_drv.h file describes the FXLS8962AF driver interface and structures. ...
#define FXLS8962_SDCD_CONFIG1_WT_ELE_EN
Definition: fxls8962.h:1544
#define FXLS8962_INT1
int32_t FXLS8962_I2C_Initialize(fxls8962_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: fxls8962_drv.c:240
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define FXLS8962_SDCD_CONFIG2_SDCD_EN_MASK
Definition: fxls8962.h:1621
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8962.h:551
This defines the sensor specific information for I2C.
Definition: fxls8962_drv.h:44
#define FXLS8967_WHOAMI_VALUE
Definition: fxls8962.h:89
#define FXLS8962_SDCD_CONFIG2_SDCD_EN_EN
Definition: fxls8962.h:1628
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define FXLS8962_SDCD_INT_SRC2_WT_EA_MASK
Definition: fxls8962.h:1456
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define FXLS8962_SDCD_CONFIG2_REF_UPDM_MASK
Definition: fxls8962.h:1618
#define SDCD_WT_DBCNT
fxls8962_i2c_sensorhandle_t fxls8962Driver
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define SDCD_UTHS_MSB
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
#define FXLS8962_SDCD_INT_SRC2_WT_EA_EVENT_NO
Definition: fxls8962.h:1462
int main(void)
Main function.
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXLS8962_INT_EN_SDCD_WT_EN_EN
Definition: fxls8962.h:863
const registerreadlist_t cFxls8962FreeFallEvent[]
Address of SDCD Status Register.
int32_t FXLS8962_I2C_ReadData(fxls8962_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls8962_drv.c:331
ARM_DRIVER_I2C * I2Cdrv
void fxls8962_int_event_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXLS8962_SDCD_CONFIG1_Z_WT_EN_EN
Definition: fxls8962.h:1568
const registerwritelist_t cFxls8962ConfigFreeFall[]
Register settings for free-fall detection.
#define FXLS8962_SDCD_CONFIG1_WT_ELE_MASK
Definition: fxls8962.h:1529
#define FXLS8962_SDCD_CONFIG2_WT_DBCTM_MASK
Definition: fxls8962.h:1612
#define FXLS8962_SDCD_CONFIG2_REF_UPDM_FIXED_VAL
Definition: fxls8962.h:1637
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_100HZ
Definition: fxls8962.h:562
#define FXLS8962_INT_EN_SDCD_WT_EN_MASK
Definition: fxls8962.h:841
#define FXLS8962_SDCD_CONFIG1_Z_WT_EN_MASK
Definition: fxls8962.h:1511
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
int32_t FXLS8962_I2C_Configure(fxls8962_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls8962_drv.c:286
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
#define FXLS8962_SDCD_CONFIG1_X_WT_EN_EN
Definition: fxls8962.h:1560
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
#define FXLS8962_SDCD_CONFIG2_WT_DBCTM_CLEARED
Definition: fxls8962.h:1648
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define FXLS8974_WHOAMI_VALUE
Definition: fxls8962.h:90
#define SDCD_LTHS_MSB
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
void FXLS8962_I2C_SetIdleTask(fxls8962_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxls8962_drv.c:278
#define FXLS8962_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
#define FXLS8962_SDCD_CONFIG1_X_WT_EN_MASK
Definition: fxls8962.h:1517
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
volatile bool gFxls8962EventReady