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_interrupt.c
11 *   @brief The mma865x_interrupt.c file implements the ISSDK MMA865x sensor driver
12 *        example demonstration with Interrupt mode.
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 #define MMA865x_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! Prepare the register write list to configure MMA865x in non-FIFO and ISR mode. */
44 const registerwritelist_t cMma865xConfigInterrupt[] =
45     {/*! Configure the MMA865x to set FS Range as 2g. */
46      {MMA865x_XYZ_DATA_CFG, MMA865x_XYZ_DATA_CFG_FS_2G, MMA865x_XYZ_DATA_CFG_FS_MASK},
47      /*! Configure the MMA865x to set ODR to 6.25Hz. */
48      {MMA865x_CTRL_REG1, MMA865x_CTRL_REG1_DR_6_25HZ, MMA865x_CTRL_REG1_DR_MASK},
49      /*! Configure the MMA865x to set High Resolution mode. */
50      {MMA865x_CTRL_REG2, MMA865x_CTRL_REG2_MODS_HR, MMA865x_CTRL_REG2_MODS_MASK},
51      /*! Configure the MMA865x to set interrupt polarity as Active High. */
52      {MMA865x_CTRL_REG3, MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH, MMA865x_CTRL_REG3_IPOL_MASK},
53      /*! Configure the MMA865x to enable Interrupts for Data Ready. */
54      {MMA865x_CTRL_REG4, MMA865x_CTRL_REG4_INT_EN_DRDY_EN, MMA865x_CTRL_REG4_INT_EN_DRDY_MASK},
55      /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
56      {MMA865x_CTRL_REG5, MMA865x_CTRL_REG5_INT_CFG_DRDY_INT1, MMA865x_CTRL_REG5_INT_CFG_DRDY_MASK},
57      __END_WRITE_DATA__};
58 
59 /*! Prepare the register read list to read the raw Accel data from MMA865x. */
60 const registerreadlist_t cMma865xOutputValues[] = {{.readFrom = MMA865x_OUT_X_MSB, .numBytes = MMA865x_ACCEL_DATA_SIZE},
61                                                    __END_READ_DATA__};
62 
63 //-----------------------------------------------------------------------
64 // Global Variables
65 //-----------------------------------------------------------------------
66 volatile bool gMma865xDataReady = false;
67 
68 //-----------------------------------------------------------------------
69 // Functions
70 //-----------------------------------------------------------------------
71 /*! -----------------------------------------------------------------------
72  *  @brief       This is the Sensor Data Ready ISR implementation.
73  *  @details     This function sets the flag which indicates if a new sample(s) is available for reading.
74  *  @param[in]   pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
75  *  @return      void  There is no return value.
76  *  @constraints None
77  *  @reeentrant  Yes
78  *  -----------------------------------------------------------------------*/
mma865x_int_data_ready_callback(void * pUserData)79 void mma865x_int_data_ready_callback(void *pUserData)
80 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
81     gMma865xDataReady = true;
82 }
83 
84 /*! -----------------------------------------------------------------------
85  *  @brief       This is the The main function implementation.
86  *  @details     This function invokes board initializes routines, then then brings up the sensor and
87  *               finally enters an endless loop to continuously read available samples.
88  *  @param[in]   void This is no input parameter.
89  *  @return      void  There is no return value.
90  *  @constraints None
91  *  @reeentrant  No
92  *  -----------------------------------------------------------------------*/
main(void)93 int main(void)
94 {
95     int32_t status;
96     uint8_t data[MMA865x_ACCEL_DATA_SIZE];
97     mma865x_acceldata_t rawData;
98 
99     ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
100     mma865x_i2c_sensorhandle_t mma865xDriver;
101     GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
102 
103     /*! Initialize the MCU hardware. */
104     BOARD_InitPins();
105     BOARD_BootClockRUN();
106     BOARD_InitDebugConsole();
107 
108     PRINTF("\r\n ISSDK MMA865x sensor driver example for Interrupt Mode.\r\n");
109 
110     /*! Initialize MMA865x pin used by FRDM board */
111     gpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_int_data_ready_callback, NULL);
112 
113     /*! Initialize RGB LED pin used by FRDM board */
114     gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
115 
116     /*! Initialize the I2C driver. */
117     status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
118     if (ARM_DRIVER_OK != status)
119     {
120         PRINTF("\r\n I2C Initialization Failed\r\n");
121         return -1;
122     }
123 
124     /*! Set the I2C Power mode. */
125     status = I2Cdrv->PowerControl(ARM_POWER_FULL);
126     if (ARM_DRIVER_OK != status)
127     {
128         PRINTF("\r\n I2C Power Mode setting Failed\r\n");
129         return -1;
130     }
131 
132     /*! Set the I2C bus speed. */
133     status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
134     if (ARM_DRIVER_OK != status)
135     {
136         PRINTF("\r\n I2C Control Mode setting Failed\r\n");
137         return -1;
138     }
139 
140     /*! Initialize the MMA865x sensor driver. */
141     status = MMA865x_I2C_Initialize(&mma865xDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MMA8652_I2C_ADDR,
142                                     MMA8652_WHOAMI_VALUE);
143     if (SENSOR_ERROR_NONE != status)
144     {
145         PRINTF("\r\n Sensor Initialization Failed\r\n");
146         return -1;
147     }
148     PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
149 
150     /*!  Set the task to be executed while waiting for I2C transactions to complete. */
151     MMA865x_I2C_SetIdleTask(&mma865xDriver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
152 
153     /*! Configure the MMA865x sensor driver with No FIFO mode. */
154     status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigInterrupt);
155     if (SENSOR_ERROR_NONE != status)
156     {
157         PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
158         return -1;
159     }
160     PRINTF("\r\n Successfully Applied MMA865x Sensor Configuration\r\n");
161 
162     for (;;) /* Forever loop */
163     {        /* In ISR Mode we do not need to check Data Ready Register.
164               * The receipt of interrupt will indicate data is ready. */
165         if (false == gMma865xDataReady)
166         { /* Loop, if new sample is not available. */
167             SMC_SetPowerModeWait(SMC);
168             continue;
169         }
170         else
171         { /*! Clear the data ready flag, it will be set again by the ISR. */
172             gMma865xDataReady = false;
173             gpioDriver->toggle_pin(&GREEN_LED);
174         }
175 
176         /*! Read the raw sensor data from the MMA865x. */
177         status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutputValues, data);
178         if (ARM_DRIVER_OK != status)
179         {
180             PRINTF("\r\n Read Failed. \r\n");
181             return -1;
182         }
183 
184         /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
185         rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
186         rawData.accel[0] /= 16;
187         rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
188         rawData.accel[1] /= 16;
189         rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
190         rawData.accel[2] /= 16;
191 
192         /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
193         PRINTF("\r\n Accel X = %d  Y = %d  Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
194         ASK_USER_TO_RESUME(50); /* Ask for user input after processing 100 samples. */
195     }
196 }
197