ISSDK  1.8
IoT Sensing Software Development Kit
mma865x_freefall-detection.c
Go to the documentation of this file.
1 /*
2  * Copyright 2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file mma865x_freefall-detection.c
10 * @brief The mma865x_freefall-detection.c file implements the ISSDK
11 * MMA865x sensor example demonstrating configuring MMA8652 Accel
12 * and enabling detection of freefall event.
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 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "gpio_driver.h"
33 #include "mma865x_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 /* FF_MT freefall counter register values for High resolution Mode and ODR = 200Hz.
39  * These values have been derived based on the MMA865x DataSheet and Application Note AN4070 for MMA8451 (the same is
40  * applicable to MMA865x too).
41  * http://cache.freescale.com/files/sensors/doc/app_note/AN4070.pdf */
42 #define FF_MT_WT_DBCNT 0x18 /* Debounce count value. */
43 #define FF_MT_THS_VALUE 0x08 /* Threshold Value. */
44 
45 //-----------------------------------------------------------------------
46 // Constants
47 //-----------------------------------------------------------------------
48 /*! @brief Register settings for freefall detection. */
50  /*! Configure the MMA865x to set FS Range as 2g. */
52  /*! Configure the MMA865x to set Debounce counter value. */
54  /*! Configure the MMA865x to set Debounce counter to be cleared on favourable events and the thresholds . */
57  /*! Configure the MMA865x to set freefall Mode and enable all XYZ axis events and event latching. */
61  /*! Configure the MMA865x to set interrupt polarity as Active High. */
63  /*! Configure the MMA865x to enable Interrupts for Data Ready. */
65  /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
67  /*! Configure the MMA865x to set ODR to 100Hz. */
70 
71 /*! @brief Address of Freefall Status Register. */
73 
74 //-----------------------------------------------------------------------
75 // Global Variables
76 //-----------------------------------------------------------------------
77 volatile bool gMma865xIntFlag = false;
78 
79 //-----------------------------------------------------------------------
80 // Functions
81 //-----------------------------------------------------------------------
82 /*! -----------------------------------------------------------------------
83  * @brief This is the Sensor ISR implementation.
84  * @details This function sets the flag which indicates if a new sample(s) is available for reading or new
85  * event has occurred.
86  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
87  * @return void There is no return value.
88  * @constraints None
89  * @reeentrant Yes
90  * -----------------------------------------------------------------------*/
91 void mma865x_isr_callback(void *pUserData)
92 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
93  gMma865xIntFlag = true;
94 }
95 
96 /*! -----------------------------------------------------------------------
97  * @brief This is the The main function implementation.
98  * @details This function invokes board initializes routines, then then brings up the sensor and
99  * finally enters an endless loop to continuously read available samples.
100  * @param[in] void This is no input parameter.
101  * @return void There is no return value.
102  * @constraints None
103  * @reeentrant No
104  * -----------------------------------------------------------------------*/
105 int main(void)
106 {
107  int32_t status;
108 
109  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
110  mma865x_i2c_sensorhandle_t mma865xDriver;
112 
113  BOARD_InitPins();
116 
117  PRINTF("\r\n ISSDK MMA865x sensor driver example for Freefall Detection. \r\n");
118 
119  /*! Initialize MMA865x pin used by FRDM board */
120  pGpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_isr_callback, NULL);
121 
122  /*! Initialize the I2C driver. */
123  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
124  if (ARM_DRIVER_OK != status)
125  {
126  PRINTF("\r\n I2C Initialization Failed\r\n");
127  return -1;
128  }
129 
130  /*! Set the I2C Power mode. */
131  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
132  if (ARM_DRIVER_OK != status)
133  {
134  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
135  return -1;
136  }
137 
138  /*! Set the I2C bus speed. */
139  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
140  if (ARM_DRIVER_OK != status)
141  {
142  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
143  return -1;
144  }
145 
146  /*! Initialize the MMA865x sensor driver. */
149  if (SENSOR_ERROR_NONE != status)
150  {
151  PRINTF("\r\n Sensor Initialization Failed\r\n");
152  return -1;
153  }
154  PRINTF("\r\n Successfully Initialized Sensor\r\n");
155 
156  /*! Set the task to be executed while waiting for I2C transactions to complete. */
158 
159  /*! Configure the MMA865x sensor for Freefall detection Mode. */
160  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigFreeFall);
161  if (SENSOR_ERROR_NONE != status)
162  {
163  PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
164  return -1;
165  }
166  PRINTF("\r\n Successfully Applied MMA865x Sensor Configuration for Freefall Detection\r\n");
167  PRINTF("\r\n MMA865x is now active and detecting freefall... \r\n");
168 
169  for (;;) /* Forever loop */
170  { /* In ISR Mode we do not need to check Event Ready Register.
171  * The receipt of interrupt will indicate Event has occured. */
172  if (false == gMma865xIntFlag)
173  { /* Loop, if new sample is not available. */
175  continue;
176  }
177  else
178  { /*! Clear the event flag, it will be set again by the ISR. */
179  gMma865xIntFlag = false;
180  }
181 
182  /*! Display that a freefall event has been detected. */
183  PRINTF("\r\n Freefall detected !!!\r\n");
184  }
185 }
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define MMA865x_FF_MT_CFG_YEFE_MASK
Definition: mma865x.h:854
#define MMA865x_CTRL_REG1_DR_200HZ
Definition: mma865x.h:1550
This structure defines the Write command List.
Definition: sensor_drv.h:68
This defines the sensor specific information.
Definition: mma865x_drv.h:29
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1
Definition: mma865x.h:1851
int32_t status
#define MMA865x_XYZ_DATA_CFG_FS_MASK
Definition: mma865x.h:530
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 MMA865x_FF_MT_CFG_XEFE_EN
Definition: mma865x.h:881
#define MMA8652_INT1
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
const registerwritelist_t cMma865xConfigFreeFall[]
Register settings for freefall detection.
#define SMC
Definition: lpc54114.h:118
#define MMA865x_FF_MT_THS_THS_MASK
Definition: mma865x.h:983
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
const registerreadlist_t cMma865xFreeFallEvent[]
Address of Freefall Status Register.
#define MMA865x_FF_MT_THS_DBCNTM_INC_CLR
Definition: mma865x.h:994
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define MMA865x_FF_MT_CFG_ZEFE_EN
Definition: mma865x.h:875
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_EN
Definition: mma865x.h:1776
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK
Definition: mma865x.h:1743
int main(void)
This is the The main function implementation.
#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
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
int32_t MMA865x_I2C_Configure(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma865x_drv.c:61
#define __END_READ_DATA__
Definition: sensor_drv.h:51
void mma865x_isr_callback(void *pUserData)
This is the Sensor ISR implementation.
The mma865x_drv.h file describes the MMA865x driver interface and structures.
#define MMA865x_CTRL_REG1_ACTIVE_MASK
Definition: mma865x.h:1528
#define MMA865x_CTRL_REG1_ACTIVE_ACTIVATED
Definition: mma865x.h:1559
ARM_DRIVER_I2C * I2Cdrv
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK
Definition: mma865x.h:1818
#define MMA865x_FF_MT_CFG_XEFE_MASK
Definition: mma865x.h:851
volatile bool gMma865xIntFlag
uint16_t readFrom
Definition: sensor_drv.h:80
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 FF_MT_THS_VALUE
#define MMA865x_XYZ_DATA_CFG_FS_2G
Definition: mma865x.h:542
#define MMA865x_FF_MT_CFG_ZEFE_MASK
Definition: mma865x.h:857
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
#define MMA8652_I2C_ADDR
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
void MMA865x_I2C_SetIdleTask(mma865x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma865x_drv.c:53
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
#define MMA865x_FF_MT_CFG_OAE_MASK
Definition: mma865x.h:860
#define MMA865x_FF_MT_THS_DBCNTM_MASK
Definition: mma865x.h:986
int32_t MMA865x_I2C_Initialize(mma865x_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: mma865x_drv.c:22
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define MMA865x_FF_MT_CFG_OAE_FREEFALL
Definition: mma865x.h:872
#define FF_MT_WT_DBCNT
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878