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 fxpq3115_fifo_interrupt.c
11  * @brief The fxpq3115_fifo_interrupt.c file implements the ISSDK FXPQ3115BV sensor driver
12  *        example demonstration with fifo in 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 "fxpq3115_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 /* Application Configurations Selections */
39 #define FIFO_WMRK_SIZE (8)     /* Buffer 8 Samples. */
40 #define FXPQ3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
41 /*! In FXPQ3115 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 FXPQ3115_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 B3115ConfigFIFOInt[] = {
51     /* Set FIFO Mode and set FIFO Watermark Level. */
52     {FXPQ3115_F_SETUP, FXPQ3115_F_SETUP_F_MODE_STOP_MODE | FIFO_WMRK_SIZE,
53      FXPQ3115_F_SETUP_F_MODE_MASK | FXPQ3115_F_SETUP_F_WMRK_MASK},
54     /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
55     {FXPQ3115_PT_DATA_CFG,
56      FXPQ3115_PT_DATA_CFG_TDEFE_ENABLED | FXPQ3115_PT_DATA_CFG_PDEFE_ENABLED | FXPQ3115_PT_DATA_CFG_DREM_ENABLED,
57      FXPQ3115_PT_DATA_CFG_TDEFE_MASK | FXPQ3115_PT_DATA_CFG_PDEFE_MASK | FXPQ3115_PT_DATA_CFG_DREM_MASK},
58     /* Set Over Sampling Ratio to 128. */
59     {FXPQ3115_CTRL_REG1, FXPQ3115_CTRL_REG1_OS_OSR_128, FXPQ3115_CTRL_REG1_OS_MASK},
60     {FXPQ3115_CTRL_REG2, FXPQ3115_SAMPLING_EXPONENT, FXPQ3115_CTRL_REG2_ST_MASK},
61     /* Set INT1 Active High. */
62     {FXPQ3115_CTRL_REG3, FXPQ3115_CTRL_REG3_IPOL1_HIGH, FXPQ3115_CTRL_REG3_IPOL1_MASK},
63     /* Enable Interrupts for FIFO Events. */
64     {FXPQ3115_CTRL_REG4, FXPQ3115_CTRL_REG4_INT_EN_FIFO_INTENABLED, FXPQ3115_CTRL_REG4_INT_EN_FIFO_MASK},
65     /* Route Interrupt to INT1. */
66     {FXPQ3115_CTRL_REG5, FXPQ3115_CTRL_REG5_INT_CFG_FIFO_INT1, FXPQ3115_CTRL_REG5_INT_CFG_FIFO_MASK},
67     __END_WRITE_DATA__};
68 
69 /*! @brief Address of Status Register. */
70 const registerreadlist_t B3115Status[] = {{.readFrom = FXPQ3115_STATUS, .numBytes = 1}, __END_READ_DATA__};
71 
72 /*! @brief Address and size of Pressure+Temperature Data in FIFO Mode. */
73 const registerreadlist_t B3115OutputFIFO[] = {
74     {.readFrom = FXPQ3115_OUT_P_MSB, .numBytes = FXPQ3115_DATA_SIZE * FIFO_WMRK_SIZE}, __END_READ_DATA__};
75 
76 //-----------------------------------------------------------------------
77 // Global Variables
78 //-----------------------------------------------------------------------
79 volatile uint8_t gB3115DataReady;
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  *  -----------------------------------------------------------------------*/
fxpq3115_int_data_ready_callback(void * pUserData)92 void fxpq3115_int_data_ready_callback(void *pUserData)
93 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
94     gB3115DataReady = 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[FXPQ3115_DATA_SIZE * FIFO_WMRK_SIZE];
112     fxpq3115_pressuredata_t rawData;
113 
114     ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
115     fxpq3115_i2c_sensorhandle_t fxpq3115Driver;
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 FXPQ3115 sensor driver example demonstration with interrupt mode.\r\n");
124 
125     /*! Initialize FXPQ3115 pin used by FRDM board */
126     pGpioDriver->pin_init(&FXPQ3115_INT1, GPIO_DIRECTION_IN, NULL, &fxpq3115_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 FXPQ3115 sensor driver. */
156     status = FXPQ3115_I2C_Initialize(&fxpq3115Driver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, FXPQ3115_I2C_ADDR,
157                                      FXPQ3115_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     FXPQ3115_I2C_SetIdleTask(&fxpq3115Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
167 
168     gB3115DataReady = false;
169 
170     /*! Configure the FXPQ3115 sensor driver. */
171     status = FXPQ3115_I2C_Configure(&fxpq3115Driver, B3115ConfigFIFOInt);
172     if (SENSOR_ERROR_NONE != status)
173     {
174         PRINTF("\r\n FXPQ3115 Sensor Configuration Failed, Err = %d\r\n", status);
175         return -1;
176     }
177     PRINTF("\r\n Successfully Applied FXPQ3115 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 == gB3115DataReady)
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             gB3115DataReady = 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 FXPQ3115, where we have to read F_STATUS to clear
193              * the sensor's internal INT.  */
194             FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115Status, (uint8_t *)&status);
195         }
196 
197         /*! Read new raw sensor data from the FXPQ3115. */
198         status = FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115OutputFIFO, 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 * FXPQ3115_DATA_SIZE + 0]) << 16) |
212                                ((data[i * FXPQ3115_DATA_SIZE + 1]) << 8) | ((data[i * FXPQ3115_DATA_SIZE + 2]));
213             pressureInPascals += rawData.pressure / FXPQ3115_PRESSURE_CONV_FACTOR;
214 
215             rawData.temperature =
216                 (int16_t)((data[i * FXPQ3115_DATA_SIZE + 3]) << 8) | (data[i * FXPQ3115_DATA_SIZE + 4]);
217             tempInDegrees += rawData.temperature / FXPQ3115_TEMPERATURE_CONV_FACTOR;
218         }
219         /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
220         PRINTF("\r\n Average Pressure    = %d Pa\r\n", pressureInPascals / FIFO_WMRK_SIZE);
221         PRINTF("\r\n Average Temperature = %d degC\r\n", tempInDegrees / FIFO_WMRK_SIZE);
222         ASK_USER_TO_RESUME(16 / FIFO_WMRK_SIZE); /* Ask for user input after processing 16 samples. */
223     }
224 }
225