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 //-----------------------------------------------------------------------
51 const registerwritelist_t cMma865xConfigDoubleTap[] =
52     {/* HP_FILTER_CUTOFF to be set to 0x00 */
53      {MMA865x_HP_FILTER_CUTOFF, MMA865x_HP_FILTER_CUTOFF_PULSE_HPF_BYP_ENABLED, MMA865x_HP_FILTER_CUTOFF_PULSE_HPF_BYP_MASK},
54      /* Enable double-pulse event on X, Y & Z axis */
55      {MMA865x_PULSE_CFG, MMA865x_PULSE_CFG_XDPEFE_EN | MMA865x_PULSE_CFG_YDPEFE_EN | MMA865x_PULSE_CFG_ZDPEFE_EN,
56       MMA865x_PULSE_CFG_XDPEFE_MASK | MMA865x_PULSE_CFG_YDPEFE_MASK | MMA865x_PULSE_CFG_ZDPEFE_MASK},
57      /* Set thresholds to be used by the system to start the pulse-event detection procedure */
58      {MMA865x_PULSE_THSX, PULSE_THX, MMA865x_PULSE_THSX_THSX_MASK},
59      {MMA865x_PULSE_THSY, PULSE_THY, MMA865x_PULSE_THSY_THSY_MASK},
60      {MMA865x_PULSE_THSZ, PULSE_THZ, MMA865x_PULSE_THSZ_THSZ_MASK},
61      /* Set Pulse time limit threshold to PULSE_TL */
62      {MMA865x_PULSE_TMLT, PULSE_TL, 0},
63      /* Set Pulse latency time threshold to PULSE_LT */
64      {MMA865x_PULSE_LTCY, PULSE_LT, 0},
65      /* Set Pulse window time threshold to PULSE_WT */
66      {MMA865x_PULSE_WIND, PULSE_WT, 0},
67      /* Configure MMA8652 in standby mode, normal read and noise mode, 100Hz ODR and auto-wake frequency of 50Hz */
68      {MMA865x_CTRL_REG1, MMA865x_CTRL_REG1_ACTIVE_STANDBY | MMA865x_CTRL_REG1_F_READ_NORMAL | MMA865x_CTRL_REG1_DR_100HZ | MMA865x_CTRL_REG1_ASLP_RATE_50HZ,
69      MMA865x_CTRL_REG1_ACTIVE_MASK | MMA865x_CTRL_REG1_F_READ_MASK | MMA865x_CTRL_REG1_DR_MASK | MMA865x_CTRL_REG1_ASLP_RATE_MASK},
70      /* Configure MMA8652 in low power wake mode */
71      {MMA865x_CTRL_REG2, MMA865x_CTRL_REG2_MODS_LP, MMA865x_CTRL_REG2_MODS_MASK},
72      /* Configure INT1/INT2 interrupt logic polarity to Active high */
73      {MMA865x_CTRL_REG3, MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH, MMA865x_CTRL_REG3_IPOL_MASK},
74      {MMA865x_CTRL_REG4, MMA865x_CTRL_REG4_INT_EN_PULSE_EN, MMA865x_CTRL_REG4_INT_EN_PULSE_MASK}, /*! Pulse Detection Event. */
75      {MMA865x_CTRL_REG5, MMA865x_CTRL_REG5_INT_CFG_PULSE_INT1, MMA865x_CTRL_REG5_INT_CFG_PULSE_MASK}, /*! INT1 Pin  */
76      __END_WRITE_DATA__};
77 
78 /*! @brief Read register list for MMA8652 to read status bits for the pulse detection function. */
79 const registerreadlist_t gMma865xReadPulseSrc[] = {{.readFrom = MMA865x_PULSE_SRC, .numBytes = 1}, __END_READ_DATA__};
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  *  -----------------------------------------------------------------------*/
mma865x_isr_callback(void * pUserData)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  *  -----------------------------------------------------------------------*/
main(void)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();
122     BOARD_BootClockRUN();
123     BOARD_InitDebugConsole();
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. */
158     status = MMA865x_I2C_Initialize(&mma865xDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MMA8652_I2C_ADDR,
159                                     MMA8652_WHOAMI_VALUE);
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. */
168     MMA865x_I2C_SetIdleTask(&mma865xDriver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
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. */
185             SMC_SetPowerModeWait(SMC);
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 }
208