ISSDK  1.8
IoT Sensing Software Development Kit
mma865x_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 mma865x_freefall.c
11  * @brief The mma865x_freefall.c file implements the ISSDK MMA865x 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 //-----------------------------------------------------------------------
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 = 100Hz.
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 0x28 /* Debounce count value. */
43 #define FF_MT_THS_VALUE 0x03 /* 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 ODR to 100Hz. */
54  /*! Configure the MMA865x to set High Resolution mode. */
56  /*! Configure the MMA865x to set interrupt polarity as Active High. */
58  /*! Configure the MMA865x to enable Interrupts for Data Ready. */
60  /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
62  /*! Configure the MMA865x to set freefall Mode and enable all XYZ axis events and event latching. */
67  /*! Configure the MMA865x to set Debounce counter to be cleared on favourable events and the thresholds . */
70  /*! Configure the MMA865x to set Debounce counter value. */
73 
74 /*! @brief Address of Freefall Status Register. */
76 
77 //-----------------------------------------------------------------------
78 // Global Variables
79 //-----------------------------------------------------------------------
80 volatile bool gMma865xEventReady;
81 
82 //-----------------------------------------------------------------------
83 // Functions
84 //-----------------------------------------------------------------------
85 /*! @brief This is the Sensor Event Ready ISR implementation. */
86 void mma865x_int_event_ready_callback(void *pUserData)
87 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
88  gMma865xEventReady = true;
89 }
90 
91 /*!
92  * @brief Main function
93  */
94 int main(void)
95 {
97  uint8_t dataReady;
98  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
99  mma865x_i2c_sensorhandle_t mma865xDriver;
101 
102  BOARD_InitPins();
105 
106  PRINTF("\r\n ISSDK MMA865x sensor driver example for Freefall Detection. \r\n");
107 
108  /*! Initialize MMA865x pin used by FRDM board */
110 
111  /*! Initialize the I2C driver. */
112  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
113  if (ARM_DRIVER_OK != status)
114  {
115  PRINTF("\r\n I2C Initialization Failed\r\n");
116  return -1;
117  }
118 
119  /*! Set the I2C Power mode. */
120  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
121  if (ARM_DRIVER_OK != status)
122  {
123  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
124  return -1;
125  }
126 
127  /*! Set the I2C bus speed. */
128  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
129  if (ARM_DRIVER_OK != status)
130  {
131  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
132  return -1;
133  }
134 
135  /*! Initialize the MMA865x sensor driver. */
138  if (SENSOR_ERROR_NONE != status)
139  {
140  PRINTF("\r\n Sensor Initialization Failed\r\n");
141  return -1;
142  }
143  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
144 
145  /*! Set the task to be executed while waiting for I2C transactions to complete. */
147 
148  /* Set data not ready, event data will be available after sensor is configured and free fall detected. */
149  gMma865xEventReady = false;
150  dataReady = 0;
151 
152  /*! Configure the MMA865x sensor for Freefall detection Mode. */
153  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigFreeFall);
154  if (SENSOR_ERROR_NONE != status)
155  {
156  PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
157  return -1;
158  }
159  PRINTF("\r\n MMA865x now active and detecting freefall... \r\n");
160 
161  for (;;) /* Forever loop */
162  { /* In ISR Mode we do not need to check Event Ready Register.
163  * The receipt of interrupt will indicate Event has occured. */
164  if (false == gMma865xEventReady)
165  { /* Loop, if new sample is not available. */
167  continue;
168  }
169  else
170  { /*! Clear the data ready flag, it will be set again by the ISR. */
171  gMma865xEventReady = false;
172  }
173 
174  /*! Read the Freefall event FLAGs from MMA865x. */
175  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xFreeFallEvent, &dataReady);
176  if (SENSOR_ERROR_NONE != status)
177  {
178  PRINTF("\r\n Read Failed\r\n");
179  return -1;
180  }
181 
183  { /* Loop, if new event is not detected. */
184  continue;
185  }
186 
187  /*! Display that a freefall event has been detected. */
188  PRINTF("\r\n Freefall detected !!!\r\n");
189  }
190 }
191 ////////////////////////////////////////////////////////////////////////////////
192 // EOF
193 ////////////////////////////////////////////////////////////////////////////////
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define MMA865x_FF_MT_CFG_YEFE_MASK
Definition: mma865x.h:854
#define MMA865x_FF_MT_SRC_EA_MASK
Definition: mma865x.h:937
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
const registerreadlist_t cMma865xFreeFallEvent[]
Address of Freefall Status Register.
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
#define SMC
Definition: lpc54114.h:118
volatile bool gMma865xEventReady
#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.
#define MMA865x_FF_MT_THS_DBCNTM_INC_CLR
Definition: mma865x.h:994
int32_t MMA865x_I2C_ReadData(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma865x_drv.c:106
#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
void mma865x_int_event_ready_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
#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
#define MMA865x_CTRL_REG2_MODS_MASK
Definition: mma865x.h:1592
#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
int main(void)
Main function.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
const registerwritelist_t cMma865xConfigFreeFall[]
Register settings for freefall detection.
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
The mma865x_drv.h file describes the MMA865x driver interface and structures.
ARM_DRIVER_I2C * I2Cdrv
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK
Definition: mma865x.h:1818
#define FF_MT_THS_VALUE
#define MMA865x_FF_MT_CFG_XEFE_MASK
Definition: mma865x.h:851
uint16_t readFrom
Definition: sensor_drv.h:80
#define MMA865x_CTRL_REG2_MODS_HR
Definition: mma865x.h:1623
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 MMA865x_XYZ_DATA_CFG_FS_2G
Definition: mma865x.h:542
#define MMA865x_FF_MT_CFG_ZEFE_MASK
Definition: mma865x.h:857
#define FF_MT_WT_DBCNT
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
#define MMA865x_FF_MT_CFG_ELE_MASK
Definition: mma865x.h:863
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 MMA865x_FF_MT_SRC_EA_NONE
Definition: mma865x.h:944
#define MMA865x_FF_MT_CFG_ELE_EN
Definition: mma865x.h:871
#define MMA865x_CTRL_REG1_DR_100HZ
Definition: mma865x.h:1551
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878