ISSDK  1.8
IoT Sensing Software Development Kit
mma865x_doubletap-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 /**
10  * @file mma865x_doubletap-detection.c
11 * @brief The mma865x_doubletap-detection.c file implements the ISSDK MMA865x sensor
12 * example demonstration for double-tap 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 /* Double-Tap detection threshold values.
39  * Refer: https://www.nxp.com/docs/en/application-note/AN3919.pdf
40 */
41 #define PULSE_THX 0x12 /* X-axis pulse threshold value. */
42 #define PULSE_THY 0x12 /* Y-axis pulse threshold value. */
43 #define PULSE_THZ 0x12 /* Z-axis pulse threshold value. */
44 #define PULSE_TL 0x0C /* Time limit value for pulse detection. */
45 #define PULSE_LT 0x14 /* Latency time for second pulse detection. */
46 #define PULSE_WT 0x1E /* Window time for second pulse detection. */
47 
48 //-----------------------------------------------------------------------
49 // Constants
50 //-----------------------------------------------------------------------
52  {/* HP_FILTER_CUTOFF to be set to 0x00 */
54  /* Enable double-pulse event on X, Y & Z axis */
57  /* Set thresholds to be used by the system to start the pulse-event detection procedure */
61  /* Set Pulse time limit threshold to PULSE_TL */
63  /* Set Pulse latency time threshold to PULSE_LT */
65  /* Set Pulse window time threshold to PULSE_WT */
67  /* Configure MMA8652 in standby mode, normal read and noise mode, 100Hz ODR and auto-wake frequency of 50Hz */
70  /* Configure MMA8652 in low power wake mode */
72  /* Configure INT1/INT2 interrupt logic polarity to Active high */
77 
78 /*! @brief Read register list for MMA8652 to read status bits for the pulse detection function. */
80 
81 //-----------------------------------------------------------------------
82 // Global Variables
83 //-----------------------------------------------------------------------
84 volatile bool gMma865xIntFlag = false;
85 
86 //-----------------------------------------------------------------------
87 // Functions
88 //-----------------------------------------------------------------------
89 /*! -----------------------------------------------------------------------
90  * @brief This is the Sensor Event ISR implementation.
91  * @details This function sets the flag which indicates if a new event is detected.
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  * @constraints None
95  * @reeentrant Yes
96  * -----------------------------------------------------------------------*/
97 void mma865x_isr_callback(void *pUserData)
98 { /*! @brief Set flag to indicate Sensor has detected an event. */
99  gMma865xIntFlag = true;
100 }
101 
102 /*! -----------------------------------------------------------------------
103  * @brief This is the The main function implementation.
104  * @details This function invokes board initializes routines, then then brings up the sensor and
105  * finally enters an endless loop to continuously read available samples.
106  * @param[in] void This is no input parameter.
107  * @return void There is no return value.
108  * @constraints None
109  * @reeentrant No
110  * -----------------------------------------------------------------------*/
111 int main(void)
112 {
113  int32_t status;
114  uint8_t eventStatus = 0;
115 
116  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
117  mma865x_i2c_sensorhandle_t mma865xDriver;
118  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
119 
120  /*! Initialize the MCU hardware. */
121  BOARD_InitPins();
124 
125  PRINTF("\r\n ISSDK MMA865x sensor driver example for Double-Tap detection.\r\n");
126 
127  /*! Initialize MMA865x pin used by FRDM board */
128  gpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_isr_callback, NULL);
129 
130  /*! Initialize RGB LED pin used by FRDM board */
131  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
132 
133  /*! Initialize the I2C driver. */
134  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\n I2C Initialization Failed\r\n");
138  return -1;
139  }
140 
141  /*! Set the I2C Power mode. */
142  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
143  if (ARM_DRIVER_OK != status)
144  {
145  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
146  return -1;
147  }
148 
149  /*! Set the I2C bus speed. */
150  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
151  if (ARM_DRIVER_OK != status)
152  {
153  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
154  return -1;
155  }
156 
157  /*! Initialize the MMA865x sensor driver. */
160  if (SENSOR_ERROR_NONE != status)
161  {
162  PRINTF("\r\n Sensor Initialization Failed\r\n");
163  return -1;
164  }
165  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
166 
167  /*! Set the task to be executed while waiting for I2C transactions to complete. */
169 
170  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigDoubleTap);
171 
172  if (SENSOR_ERROR_NONE != status)
173  {
174  PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
175  return -1;
176  }
177  PRINTF("\r\n Successfully Applied MMA865x Sensor Configuration for Double-Tap Detection\r\n");
178  PRINTF("\r\n MMA865x is now active and detecting double-tap... \r\n");
179 
180  for (;;) /* Forever loop */
181  { /* In ISR Mode we do not need to check Data Ready Register.
182  * The receipt of interrupt will indicate data is ready. */
183  if (false == gMma865xIntFlag)
184  { /* Loop, if new sample is not available. */
186  continue;
187  }
188  else
189  { /*! Clear the data ready flag, it will be set again by the ISR. */
190  gMma865xIntFlag = false;
191  gpioDriver->toggle_pin(&GREEN_LED);
192  }
193 
194  status = MMA865x_I2C_ReadData(&mma865xDriver, gMma865xReadPulseSrc, &eventStatus);
195  if (ARM_DRIVER_OK != status)
196  {
197  return status;
198  }
199 
200  if (0 == (eventStatus & MMA865x_PULSE_SRC_DPE_MASK))
201  { /* continue, if new event is not detected. */
202  continue;
203  }
204 
205  PRINTF("\r\n Double-Tap Detected !!!");
206  }
207 }
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define PULSE_WT
const registerreadlist_t gMma865xReadPulseSrc[]
Read register list for MMA8652 to read status bits for the pulse detection function.
#define MMA865x_HP_FILTER_CUTOFF_PULSE_HPF_BYP_MASK
Definition: mma865x.h:580
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_PULSE_THSY_THSY_MASK
Definition: mma865x.h:1428
#define PULSE_THY
int32_t status
#define MMA865x_CTRL_REG4_INT_EN_PULSE_EN
Definition: mma865x.h:1774
#define PULSE_THZ
#define MMA865x_PULSE_CFG_YDPEFE_EN
Definition: mma865x.h:1284
volatile bool gMma865xIntFlag
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_PULSE_CFG_ZDPEFE_EN
Definition: mma865x.h:1280
#define MMA8652_INT1
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define MMA865x_PULSE_CFG_XDPEFE_MASK
Definition: mma865x.h:1243
#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_PULSE_THSZ_THSZ_MASK
Definition: mma865x.h:1455
#define MMA865x_HP_FILTER_CUTOFF_PULSE_HPF_BYP_ENABLED
Definition: mma865x.h:587
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 I2C_S_DRIVER
Definition: issdk_hal.h:33
#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
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
#define MMA865x_CTRL_REG5_INT_CFG_PULSE_INT1
Definition: mma865x.h:1849
#define MMA865x_CTRL_REG4_INT_EN_PULSE_MASK
Definition: mma865x.h:1746
int main(void)
This is the The main function implementation.
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 MMA865x_CTRL_REG1_ACTIVE_STANDBY
Definition: mma865x.h:1558
#define __END_READ_DATA__
Definition: sensor_drv.h:51
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_F_READ_MASK
Definition: mma865x.h:1531
ARM_DRIVER_I2C * I2Cdrv
#define MMA865x_PULSE_CFG_ZDPEFE_MASK
Definition: mma865x.h:1255
uint16_t readFrom
Definition: sensor_drv.h:80
#define MMA865x_PULSE_SRC_DPE_MASK
Definition: mma865x.h:1337
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define MMA865x_PULSE_CFG_YDPEFE_MASK
Definition: mma865x.h:1249
#define MMA865x_CTRL_REG1_ASLP_RATE_MASK
Definition: mma865x.h:1537
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 PULSE_LT
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 MMA865x_PULSE_CFG_XDPEFE_EN
Definition: mma865x.h:1288
#define MMA865x_PULSE_THSX_THSX_MASK
Definition: mma865x.h:1401
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define MMA865x_CTRL_REG5_INT_CFG_PULSE_MASK
Definition: mma865x.h:1821
#define PULSE_TL
#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 PULSE_THX
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
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_CTRL_REG1_ASLP_RATE_50HZ
Definition: mma865x.h:1544
#define MMA865x_CTRL_REG2_MODS_LP
Definition: mma865x.h:1624
const registerwritelist_t cMma865xConfigDoubleTap[]
#define MMA865x_CTRL_REG1_F_READ_NORMAL
Definition: mma865x.h:1556
#define MMA865x_CTRL_REG1_DR_100HZ
Definition: mma865x.h:1551
void mma865x_isr_callback(void *pUserData)
This is the Sensor Event ISR implementation.