1 /*
2  * Copyright (c) 2015-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 mpl3115_fifo_interrupt.c
11  * @brief The mpl3115_fifo_interrupt.c file implements the ISSDK MPL3115 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 "mpl3115_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 /* Application Configurations Selections */
39 #define FIFO_WMRK_SIZE (8)    /* Buffer 8 Samples. */
40 #define MPL3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
41 /*! In MPL3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
42  *  SAMPLING_EXPONENT).
43  *  This gives a range of 1 second to 2^15 seconds (9 hours). */
44 #define MPL3115_SAMPLING_EXPONENT (0) /* 1 seconds. */
45 
46 //-----------------------------------------------------------------------
47 // Constants
48 //-----------------------------------------------------------------------
49 /*! @brief Register settings for FIFO (buffered) mode @ default 1 sample per second with Interrupts. */
50 const registerwritelist_t cMpl3115ConfigFIFO[] = {
51     /* Set FIFO Mode and set FIFO Watermark Level. */
52     {MPL3115_F_SETUP, MPL3115_F_SETUP_F_MODE_STOP_MODE | FIFO_WMRK_SIZE,
53      MPL3115_F_SETUP_F_MODE_MASK | MPL3115_F_SETUP_F_WMRK_MASK},
54     /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
55     {MPL3115_PT_DATA_CFG,
56      MPL3115_PT_DATA_CFG_TDEFE_ENABLED | MPL3115_PT_DATA_CFG_PDEFE_ENABLED | MPL3115_PT_DATA_CFG_DREM_ENABLED,
57      MPL3115_PT_DATA_CFG_TDEFE_MASK | MPL3115_PT_DATA_CFG_PDEFE_MASK | MPL3115_PT_DATA_CFG_DREM_MASK},
58     /* Set Over Sampling Ratio to 128. */
59     {MPL3115_CTRL_REG1, MPL3115_CTRL_REG1_OS_OSR_128, MPL3115_CTRL_REG1_OS_MASK},
60     {MPL3115_CTRL_REG2, MPL3115_SAMPLING_EXPONENT, MPL3115_CTRL_REG2_ST_MASK},
61     /* Set INT1 Active High. */
62     {MPL3115_CTRL_REG3, MPL3115_CTRL_REG3_IPOL1_HIGH, MPL3115_CTRL_REG3_IPOL1_MASK},
63     /* Enable Interrupts for FIFO Events. */
64     {MPL3115_CTRL_REG4, MPL3115_CTRL_REG4_INT_EN_FIFO_INTENABLED, MPL3115_CTRL_REG4_INT_EN_FIFO_MASK},
65     /* Route Interrupt to INT1. */
66     {MPL3115_CTRL_REG5, MPL3115_CTRL_REG5_INT_CFG_FIFO_INT1, MPL3115_CTRL_REG5_INT_CFG_FIFO_MASK},
67     __END_WRITE_DATA__};
68 
69 /*! @brief Address of Status Register. */
70 const registerreadlist_t cMpl3115Status[] = {{.readFrom = MPL3115_STATUS, .numBytes = 1}, __END_READ_DATA__};
71 
72 /*! @brief Address and size of Pressure+Temperature Data in FIFO Mode. */
73 const registerreadlist_t cMpl3115OutputFIFO[] = {
74     {.readFrom = MPL3115_OUT_P_MSB, .numBytes = MPL3115_DATA_SIZE * FIFO_WMRK_SIZE}, __END_READ_DATA__};
75 
76 //-----------------------------------------------------------------------
77 // Global Variables
78 //-----------------------------------------------------------------------
79 volatile uint8_t gMpl3115DataReady;
80 
81 //-----------------------------------------------------------------------
82 // Functions
83 //-----------------------------------------------------------------------
84 /*! -----------------------------------------------------------------------
85  *  @brief       This is the Sensor Data Ready ISR implementation.
86  *  @details     This function sets the flag which indicates if a new sample(s) is available for reading.
87  *  @param[in]   pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
88  *  @return      void  There is no return value.
89  *  @constraints None
90  *  @reeentrant  Yes
91  *  -----------------------------------------------------------------------*/
mpl3115_int_data_ready_callback(void * pUserData)92 void mpl3115_int_data_ready_callback(void *pUserData)
93 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
94     gMpl3115DataReady = true;
95 }
96 
97 /*! -----------------------------------------------------------------------
98  *  @brief       This is the The main function implementation.
99  *  @details     This function invokes board initializes routines, then then brings up the sensor and
100  *               finally enters an endless loop to continuously read available samples.
101  *  @param[in]   void This is no input parameter.
102  *  @return      void  There is no return value.
103  *  @constraints None
104  *  @reeentrant  No
105  *  -----------------------------------------------------------------------*/
main(void)106 int main(void)
107 {
108     int16_t tempInDegrees;
109     uint32_t pressureInPascals;
110     int32_t status;
111     uint8_t data[MPL3115_DATA_SIZE * FIFO_WMRK_SIZE];
112     mpl3115_pressuredata_t rawData;
113 
114     ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
115     mpl3115_i2c_sensorhandle_t mpl3115Driver;
116     GENERIC_DRIVER_GPIO *pGpioDriver = &Driver_GPIO_KSDK;
117 
118     /*! Initialize the MCU hardware. */
119     BOARD_InitPins();
120     BOARD_BootClockRUN();
121     BOARD_InitDebugConsole();
122 
123     PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with interrupt mode.\r\n");
124 
125     /*! Initialize MAG3110 pin used by FRDM board */
126     pGpioDriver->pin_init(&MPL3115_INT1, GPIO_DIRECTION_IN, NULL, &mpl3115_int_data_ready_callback, NULL);
127 
128     /*! Initialize RGB LED pin used by FRDM board */
129     pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
130 
131     /*! Initialize the I2C driver. */
132     status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
133     if (ARM_DRIVER_OK != status)
134     {
135         PRINTF("\r\n I2C Initialization Failed\r\n");
136         return -1;
137     }
138 
139     /*! Set the I2C Power mode. */
140     status = I2Cdrv->PowerControl(ARM_POWER_FULL);
141     if (ARM_DRIVER_OK != status)
142     {
143         PRINTF("\r\n I2C Power Mode setting Failed\r\n");
144         return -1;
145     }
146 
147     /*! Set the I2C bus speed. */
148     status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
149     if (ARM_DRIVER_OK != status)
150     {
151         PRINTF("\r\n I2C Control Mode setting Failed\r\n");
152         return -1;
153     }
154 
155     /*! Initialize MPL3115 sensor driver. */
156     status = MPL3115_I2C_Initialize(&mpl3115Driver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MPL3115_I2C_ADDR,
157                                     MPL3115_WHOAMI_VALUE);
158     if (SENSOR_ERROR_NONE != status)
159     {
160         PRINTF("\r\n Sensor Initialization Failed\r\n");
161         return -1;
162     }
163     PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
164 
165     /*!  Set the task to be executed while waiting for I2C transactions to complete. */
166     MPL3115_I2C_SetIdleTask(&mpl3115Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
167 
168     gMpl3115DataReady = false;
169 
170     /*! Configure the MAG3110 sensor driver. */
171     status = MPL3115_I2C_Configure(&mpl3115Driver, cMpl3115ConfigFIFO);
172     if (SENSOR_ERROR_NONE != status)
173     {
174         PRINTF("\r\nMPL3115 configuration failed...\r\n");
175         return -1;
176     }
177     PRINTF("\r\n Successfully Applied MAG3110 Sensor Configuration\r\n");
178 
179     for (;;) /* Forever loop (with logic optimized for FIFO Mode). */
180     {        /* In ISR Mode we do not need to check Data Ready Register.
181               * The receipt of interrupt will indicate data is ready. */
182         if (false == gMpl3115DataReady)
183         { /* Loop, if new sample is not available. */
184             SMC_SetPowerModeWait(SMC);
185             continue;
186         }
187         else
188         { /*! Clear the data ready flag, it will be set again by the ISR. */
189             gMpl3115DataReady = false;
190             pGpioDriver->toggle_pin(&GREEN_LED);
191             /* Read FIFO status, to clear sensor's INT.
192              * Note: This is a special step in FIFO Mode particular to MPL3115, where we have to read F_STATUS to clear
193              * the sensor's internal INT.  */
194             MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115Status, (uint8_t *)&status);
195         }
196 
197         /*! Read new raw sensor data from the MPL3115. */
198         status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputFIFO, data);
199         if (ARM_DRIVER_OK != status)
200         {
201             PRINTF("\r\n Read Failed. \r\n");
202             return -1;
203         }
204 
205         /* Reset Counters */
206         pressureInPascals = 0;
207         tempInDegrees = 0;
208 
209         for (uint8_t i = 0; i < FIFO_WMRK_SIZE; i++)
210         { /*! Process all samples and convert the raw sensor data. */
211             rawData.pressure = (uint32_t)((data[i * MPL3115_DATA_SIZE + 0]) << 16) |
212                                ((data[i * MPL3115_DATA_SIZE + 1]) << 8) | ((data[i * MPL3115_DATA_SIZE + 2]));
213             pressureInPascals += rawData.pressure / MPL3115_PRESSURE_CONV_FACTOR;
214 
215             rawData.temperature = (int16_t)((data[i * MPL3115_DATA_SIZE + 3]) << 8) | (data[i * MPL3115_DATA_SIZE + 4]);
216             tempInDegrees += rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
217         }
218         /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
219         PRINTF("\r\nAverage Pressure    = %d Pa\r\n", pressureInPascals / FIFO_WMRK_SIZE);
220         PRINTF("\r\nAverage Temperature = %d degC\r\n", tempInDegrees / FIFO_WMRK_SIZE);
221         ASK_USER_TO_RESUME(16 / FIFO_WMRK_SIZE); /* Ask for user input after processing 16 samples. */
222     }
223 }
224