ISSDK  1.7
IoT Sensing Software Development Kit
mma865x_freefall.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file mma865x_freefall.c
37  * @brief The mma865x_freefall.c file implements the ISSDK MMA865x sensor driver
38  * example demonstration for Freefall Detection.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "clock_config.h"
47 #include "fsl_debug_console.h"
48 
49 //-----------------------------------------------------------------------
50 // CMSIS Includes
51 //-----------------------------------------------------------------------
52 #include "Driver_I2C.h"
53 
54 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "mma865x_drv.h"
60 
61 //-----------------------------------------------------------------------
62 // Macros
63 //-----------------------------------------------------------------------
64 /* FF_MT freefall counter register values for High resolution Mode and ODR = 100Hz.
65  * These values have been derived based on the MMA865x DataSheet and Application Note AN4070 for MMA8451 (the same is
66  * applicable to MMA865x too).
67  * http://cache.freescale.com/files/sensors/doc/app_note/AN4070.pdf */
68 #define FF_MT_WT_DBCNT 0x28 /* Debounce count value. */
69 #define FF_MT_THS_VALUE 0x03 /* Threshold Value. */
70 
71 //-----------------------------------------------------------------------
72 // Constants
73 //-----------------------------------------------------------------------
74 /*! @brief Register settings for freefall detection. */
76  {/*! Configure the MMA865x to set FS Range as 2g. */
78  /*! Configure the MMA865x to set ODR to 100Hz. */
80  /*! Configure the MMA865x to set High Resolution mode. */
82  /*! Configure the MMA865x to set interrupt polarity as Active High. */
84  /*! Configure the MMA865x to enable Interrupts for Data Ready. */
86  /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
88  /*! Configure the MMA865x to set freefall Mode and enable all XYZ axis events and event latching. */
93  /*! Configure the MMA865x to set Debounce counter to be cleared on favourable events and the thresholds . */
96  /*! Configure the MMA865x to set Debounce counter value. */
99 
100 /*! @brief Address of Freefall Status Register. */
102 
103 //-----------------------------------------------------------------------
104 // Global Variables
105 //-----------------------------------------------------------------------
106 volatile bool gMma865xEventReady;
107 
108 //-----------------------------------------------------------------------
109 // Functions
110 //-----------------------------------------------------------------------
111 /*! @brief This is the Sensor Event Ready ISR implementation. */
112 void mma865x_int_event_ready_callback(void *pUserData)
113 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
114  gMma865xEventReady = true;
115 }
116 
117 /*!
118  * @brief Main function
119  */
120 int main(void)
121 {
122  int32_t status;
123  uint8_t dataReady;
124  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
125  mma865x_i2c_sensorhandle_t mma865xDriver;
127 
128  BOARD_InitPins();
131 
132  PRINTF("\r\n ISSDK MMA865x sensor driver example for Freefall Detection. \r\n");
133 
134  /*! Initialize MMA865x pin used by FRDM board */
136 
137  /*! Initialize the I2C driver. */
138  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
139  if (ARM_DRIVER_OK != status)
140  {
141  PRINTF("\r\n I2C Initialization Failed\r\n");
142  return -1;
143  }
144 
145  /*! Set the I2C Power mode. */
146  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
147  if (ARM_DRIVER_OK != status)
148  {
149  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
150  return -1;
151  }
152 
153  /*! Set the I2C bus speed. */
154  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
155  if (ARM_DRIVER_OK != status)
156  {
157  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
158  return -1;
159  }
160 
161  /*! Initialize the MMA865x sensor driver. */
164  if (SENSOR_ERROR_NONE != status)
165  {
166  PRINTF("\r\n Sensor Initialization Failed\r\n");
167  return -1;
168  }
169  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
170 
171  /*! Set the task to be executed while waiting for I2C transactions to complete. */
173 
174  /* Set data not ready, event data will be available after sensor is configured and free fall detected. */
175  gMma865xEventReady = false;
176  dataReady = 0;
177 
178  /*! Configure the MMA865x sensor for Freefall detection Mode. */
179  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigFreeFall);
180  if (SENSOR_ERROR_NONE != status)
181  {
182  PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
183  return -1;
184  }
185  PRINTF("\r\n MMA865x now active and detecting freefall... \r\n");
186 
187  for (;;) /* Forever loop */
188  { /* In ISR Mode we do not need to check Event Ready Register.
189  * The receipt of interrupt will indicate Event has occured. */
190  if (false == gMma865xEventReady)
191  { /* Loop, if new sample is not available. */
193  continue;
194  }
195  else
196  { /*! Clear the data ready flag, it will be set again by the ISR. */
197  gMma865xEventReady = false;
198  }
199 
200  /*! Read the Freefall event FLAGs from MMA865x. */
201  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xFreeFallEvent, &dataReady);
202  if (SENSOR_ERROR_NONE != status)
203  {
204  PRINTF("\r\n Read Failed\r\n");
205  return -1;
206  }
207 
209  { /* Loop, if new event is not detected. */
210  continue;
211  }
212 
213  /*! Display that a freefall event has been detected. */
214  PRINTF("\r\n Freefall detected !!!\r\n");
215  }
216 }
217 ////////////////////////////////////////////////////////////////////////////////
218 // EOF
219 ////////////////////////////////////////////////////////////////////////////////
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
#define MMA865x_XYZ_DATA_CFG_FS_MASK
Definition: mma865x.h:530
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
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:48
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
#define BOARD_BootClockRUN
Definition: clock_config.h:45
int main(void)
Main function.
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK
Definition: mma865x.h:1818
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
const registerreadlist_t cMma865xFreeFallEvent[]
Address of Freefall Status Register.
#define MMA865x_FF_MT_THS_THS_MASK
Definition: mma865x.h:983
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:132
int32_t status
#define MMA865x_FF_MT_CFG_ELE_EN
Definition: mma865x.h:871
void BOARD_InitDebugConsole(void)
Definition: board.c:41
This defines the sensor specific information.
Definition: mma865x_drv.h:55
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define MMA865x_FF_MT_CFG_YEFE_MASK
Definition: mma865x.h:854
GENERIC_DRIVER_GPIO * pGpioDriver
volatile bool gMma865xEventReady
#define MMA8652_I2C_ADDR
#define MMA865x_CTRL_REG1_DR_100HZ
Definition: mma865x.h:1551
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:79
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_EN
Definition: mma865x.h:1776
void mma865x_int_event_ready_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
#define MMA865x_FF_MT_CFG_OAE_FREEFALL
Definition: mma865x.h:872
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:87
const registerwritelist_t cMma865xConfigFreeFall[]
Register settings for freefall detection.
#define MMA865x_FF_MT_CFG_XEFE_EN
Definition: mma865x.h:881
#define MMA865x_XYZ_DATA_CFG_FS_2G
Definition: mma865x.h:542
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK
Definition: mma865x.h:1743
#define MMA865x_CTRL_REG2_MODS_MASK
Definition: mma865x.h:1592
#define MMA865x_FF_MT_CFG_ZEFE_EN
Definition: mma865x.h:875
#define MMA865x_FF_MT_CFG_ZEFE_MASK
Definition: mma865x.h:857
#define FF_MT_THS_VALUE
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1
Definition: mma865x.h:1851
#define MMA865x_FF_MT_SRC_EA_NONE
Definition: mma865x.h:944
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define MMA8652_INT1
#define MMA865x_FF_MT_CFG_XEFE_MASK
Definition: mma865x.h:851
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
The mma865x_drv.h file describes the MMA865x driver interface and structures.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
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
#define MMA865x_FF_MT_CFG_ELE_MASK
Definition: mma865x.h:863
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
#define MMA865x_FF_MT_SRC_EA_MASK
Definition: mma865x.h:937
#define MMA865x_CTRL_REG2_MODS_HR
Definition: mma865x.h:1623
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878
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:181
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define MMA865x_FF_MT_THS_DBCNTM_MASK
Definition: mma865x.h:986
#define FF_MT_WT_DBCNT
#define MMA865x_FF_MT_CFG_OAE_MASK
Definition: mma865x.h:860