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. */
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 ODR to 100Hz. */
53 {MMA865x_CTRL_REG1, MMA865x_CTRL_REG1_DR_100HZ, MMA865x_CTRL_REG1_DR_MASK},
54 /*! Configure the MMA865x to set High Resolution mode. */
55 {MMA865x_CTRL_REG2, MMA865x_CTRL_REG2_MODS_HR, MMA865x_CTRL_REG2_MODS_MASK},
56 /*! Configure the MMA865x to set interrupt polarity as Active High. */
57 {MMA865x_CTRL_REG3, MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH, MMA865x_CTRL_REG3_IPOL_MASK},
58 /*! Configure the MMA865x to enable Interrupts for Data Ready. */
59 {MMA865x_CTRL_REG4, MMA865x_CTRL_REG4_INT_EN_FF_MT_EN, MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK},
60 /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
61 {MMA865x_CTRL_REG5, MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1, MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK},
62 /*! Configure the MMA865x to set freefall Mode and enable all XYZ axis events and event latching. */
63 {MMA865x_FF_MT_CFG, MMA865x_FF_MT_CFG_ELE_EN | MMA865x_FF_MT_CFG_OAE_FREEFALL | MMA865x_FF_MT_CFG_XEFE_EN |
64 MMA865x_FF_MT_CFG_YEFE_EN | MMA865x_FF_MT_CFG_ZEFE_EN,
65 MMA865x_FF_MT_CFG_ELE_MASK | MMA865x_FF_MT_CFG_OAE_MASK | MMA865x_FF_MT_CFG_XEFE_MASK |
66 MMA865x_FF_MT_CFG_YEFE_MASK | MMA865x_FF_MT_CFG_ZEFE_MASK},
67 /*! Configure the MMA865x to set Debounce counter to be cleared on favourable events and the thresholds . */
68 {MMA865x_FF_MT_THS, MMA865x_FF_MT_THS_DBCNTM_INC_CLR | FF_MT_THS_VALUE,
69 MMA865x_FF_MT_THS_DBCNTM_MASK | MMA865x_FF_MT_THS_THS_MASK},
70 /*! Configure the MMA865x to set Debounce counter value. */
71 {MMA865x_FF_MT_COUNT, FF_MT_WT_DBCNT, 0},
72 __END_WRITE_DATA__};
73
74 /*! @brief Address of Freefall Status Register. */
75 const registerreadlist_t cMma865xFreeFallEvent[] = {{.readFrom = MMA865x_FF_MT_SRC, .numBytes = 1}, __END_READ_DATA__};
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. */
mma865x_int_event_ready_callback(void * pUserData)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 */
main(void)94 int main(void)
95 {
96 int32_t status;
97 uint8_t dataReady;
98 ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
99 mma865x_i2c_sensorhandle_t mma865xDriver;
100 GENERIC_DRIVER_GPIO *pGpioDriver = &Driver_GPIO_KSDK;
101
102 BOARD_InitPins();
103 BOARD_BootClockRUN();
104 BOARD_InitDebugConsole();
105
106 PRINTF("\r\n ISSDK MMA865x sensor driver example for Freefall Detection. \r\n");
107
108 /*! Initialize MMA865x pin used by FRDM board */
109 pGpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_int_event_ready_callback, NULL);
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. */
136 status = MMA865x_I2C_Initialize(&mma865xDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MMA8652_I2C_ADDR,
137 MMA8652_WHOAMI_VALUE);
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. */
146 MMA865x_I2C_SetIdleTask(&mma865xDriver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
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. */
166 SMC_SetPowerModeWait(SMC);
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
182 if (MMA865x_FF_MT_SRC_EA_NONE == (dataReady & MMA865x_FF_MT_SRC_EA_MASK))
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 ////////////////////////////////////////////////////////////////////////////////
194