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. */
49 const registerwritelist_t cMma865xConfigFreeFall[] ={
50 /*! Configure the MMA865x to set FS Range as 2g. */
51 {MMA865x_XYZ_DATA_CFG, MMA865x_XYZ_DATA_CFG_FS_2G, MMA865x_XYZ_DATA_CFG_FS_MASK},
52 /*! Configure the MMA865x to set Debounce counter value. */
53 {MMA865x_FF_MT_COUNT, FF_MT_WT_DBCNT, 0},
54 /*! Configure the MMA865x to set Debounce counter to be cleared on favourable events and the thresholds . */
55 {MMA865x_FF_MT_THS, MMA865x_FF_MT_THS_DBCNTM_INC_CLR | FF_MT_THS_VALUE,
56 MMA865x_FF_MT_THS_DBCNTM_MASK | MMA865x_FF_MT_THS_THS_MASK},
57 /*! Configure the MMA865x to set freefall Mode and enable all XYZ axis events and event latching. */
58 {MMA865x_FF_MT_CFG, MMA865x_FF_MT_CFG_OAE_FREEFALL | MMA865x_FF_MT_CFG_XEFE_EN |
59 MMA865x_FF_MT_CFG_YEFE_EN | MMA865x_FF_MT_CFG_ZEFE_EN,MMA865x_FF_MT_CFG_OAE_MASK | MMA865x_FF_MT_CFG_XEFE_MASK |
60 MMA865x_FF_MT_CFG_YEFE_MASK | MMA865x_FF_MT_CFG_ZEFE_MASK},
61 /*! Configure the MMA865x to set interrupt polarity as Active High. */
62 {MMA865x_CTRL_REG3, MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH, MMA865x_CTRL_REG3_IPOL_MASK},
63 /*! Configure the MMA865x to enable Interrupts for Data Ready. */
64 {MMA865x_CTRL_REG4, MMA865x_CTRL_REG4_INT_EN_FF_MT_EN, MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK},
65 /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
66 {MMA865x_CTRL_REG5, MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1, MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK},
67 /*! Configure the MMA865x to set ODR to 100Hz. */
68 {MMA865x_CTRL_REG1, MMA865x_CTRL_REG1_DR_200HZ | MMA865x_CTRL_REG1_ACTIVE_ACTIVATED, MMA865x_CTRL_REG1_DR_MASK | MMA865x_CTRL_REG1_ACTIVE_MASK},
69 __END_WRITE_DATA__};
70
71 /*! @brief Address of Freefall Status Register. */
72 const registerreadlist_t cMma865xFreeFallEvent[] = {{.readFrom = MMA865x_FF_MT_SRC, .numBytes = 1}, __END_READ_DATA__};
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 * -----------------------------------------------------------------------*/
mma865x_isr_callback(void * pUserData)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 * -----------------------------------------------------------------------*/
main(void)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;
111 GENERIC_DRIVER_GPIO *pGpioDriver = &Driver_GPIO_KSDK;
112
113 BOARD_InitPins();
114 BOARD_BootClockRUN();
115 BOARD_InitDebugConsole();
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. */
147 status = MMA865x_I2C_Initialize(&mma865xDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MMA8652_I2C_ADDR,
148 MMA8652_WHOAMI_VALUE);
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. */
157 MMA865x_I2C_SetIdleTask(&mma865xDriver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
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. */
174 SMC_SetPowerModeWait(SMC);
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 }
186