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.c
11  * @brief The fxpq3115_fifo.c file implements the ISSDK FXPQ3115BV sensor driver
12  *        example demonstration with fifo 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 /* ISSDK Includes */
29 #include "issdk_hal.h"
30 #include "fxpq3115_drv.h"
31 
32 //-----------------------------------------------------------------------
33 // Macros
34 //-----------------------------------------------------------------------
35 #define FIFO_WMRK_SIZE (8)     /* Buffer 8 Samples. */
36 #define FXPQ3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
37 /*! In FXPQ3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
38  *  SAMPLING_EXPONENT).
39  *  This gives a range of 1 second to 2^15 seconds (9 hours). */
40 #define FXPQ3115_SAMPLING_EXPONENT (0) /* 1 second. */
41 
42 //-----------------------------------------------------------------------
43 // Constants
44 //-----------------------------------------------------------------------
45 /*! @brief Register settings for FIFO (buffered) mode @ default 1 sample per second. */
46 const registerwritelist_t B3115ConfigFIFO[] = {
47     /* Set FIFO Mode and set FIFO Watermark Level. */
48     {FXPQ3115_F_SETUP, FXPQ3115_F_SETUP_F_MODE_STOP_MODE | FIFO_WMRK_SIZE,
49      FXPQ3115_F_SETUP_F_MODE_MASK | FXPQ3115_F_SETUP_F_WMRK_MASK},
50     /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
51     {FXPQ3115_PT_DATA_CFG,
52      FXPQ3115_PT_DATA_CFG_TDEFE_ENABLED | FXPQ3115_PT_DATA_CFG_PDEFE_ENABLED | FXPQ3115_PT_DATA_CFG_DREM_ENABLED,
53      FXPQ3115_PT_DATA_CFG_TDEFE_MASK | FXPQ3115_PT_DATA_CFG_PDEFE_MASK | FXPQ3115_PT_DATA_CFG_DREM_MASK},
54     /* Set Over Sampling Ratio to 128. */
55     {FXPQ3115_CTRL_REG1, FXPQ3115_CTRL_REG1_OS_OSR_128, FXPQ3115_CTRL_REG1_OS_MASK},
56     {FXPQ3115_CTRL_REG2, FXPQ3115_SAMPLING_EXPONENT, FXPQ3115_CTRL_REG2_ST_MASK},
57     __END_WRITE_DATA__};
58 
59 /*! @brief Address of Status Register. */
60 const registerreadlist_t B3115Status[] = {{.readFrom = FXPQ3115_STATUS, .numBytes = 1}, __END_READ_DATA__};
61 
62 /*! @brief Address and size of Pressure+Temperature Data in FIFO Mode. */
63 const registerreadlist_t B3115OutputFIFO[] = {
64     {.readFrom = FXPQ3115_OUT_P_MSB, .numBytes = FXPQ3115_DATA_SIZE * FIFO_WMRK_SIZE}, __END_READ_DATA__};
65 
66 //-----------------------------------------------------------------------
67 // Functions
68 //-----------------------------------------------------------------------
69 /*!
70  * @brief Main function
71  */
main(void)72 int main(void)
73 {
74     int16_t tempInDegrees;
75     uint32_t pressureInPascals;
76     int32_t status;
77     uint8_t dataReady;
78     uint8_t data[FXPQ3115_DATA_SIZE * FIFO_WMRK_SIZE];
79     fxpq3115_pressuredata_t rawData;
80 
81     ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
82     fxpq3115_i2c_sensorhandle_t fxpq3115Driver;
83 
84     BOARD_InitPins();
85     BOARD_BootClockRUN();
86     BOARD_InitDebugConsole();
87 
88     PRINTF("\r\n ISSDK FXPQ3115 sensor driver example demonstration with fifo mode\r\n");
89 
90     /*! Initialize the I2C driver. */
91     status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
92     if (ARM_DRIVER_OK != status)
93     {
94         PRINTF("\r\n I2C Initialization Failed\r\n");
95         return -1;
96     }
97 
98     /*! Set the I2C Power mode. */
99     status = I2Cdrv->PowerControl(ARM_POWER_FULL);
100     if (ARM_DRIVER_OK != status)
101     {
102         PRINTF("\r\n I2C Power Mode setting Failed\r\n");
103         return -1;
104     }
105 
106     /*! Set the I2C bus speed. */
107     status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
108     if (ARM_DRIVER_OK != status)
109     {
110         PRINTF("\r\n I2C Control Mode setting Failed\r\n");
111         return -1;
112     }
113 
114     /*! Initialize FXPQ3115 sensor driver. */
115     status = FXPQ3115_I2C_Initialize(&fxpq3115Driver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, FXPQ3115_I2C_ADDR,
116                                      FXPQ3115_WHOAMI_VALUE);
117     if (SENSOR_ERROR_NONE != status)
118     {
119         PRINTF("\r\n Sensor Initialization Failed\r\n");
120         return -1;
121     }
122     PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
123 
124     /*!  Set the task to be executed while waiting for I2C transactions to complete. */
125     FXPQ3115_I2C_SetIdleTask(&fxpq3115Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
126 
127     /*! Configure the FXPQ3115 sensor. */
128     status = FXPQ3115_I2C_Configure(&fxpq3115Driver, B3115ConfigFIFO);
129     if (SENSOR_ERROR_NONE != status)
130     {
131         PRINTF("\r\n FXPQ3115 sensor configuration failed.\r\n");
132         return -1;
133     }
134     PRINTF("\r\n Successfully Applied FXPQ3115 Sensor Configuration\r\n");
135 
136     for (;;) /* Forever loop */
137     {
138         /*! Wait for data ready from the FXPQ3115. */
139         status = FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115Status, &dataReady);
140         if (0 == (dataReady & FXPQ3115_F_STATUS_F_WMKF_FLAG_MASK))
141         { /* Loop, if new sample is not available. */
142             continue;
143         }
144 
145         /*! Read new raw sensor data from the FXPQ3115. */
146         status = FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115OutputFIFO, data);
147         if (ARM_DRIVER_OK != status)
148         {
149             PRINTF("\r\n Read Failed. \r\n");
150             return -1;
151         }
152 
153         /* Reset Counters */
154         pressureInPascals = 0;
155         tempInDegrees = 0;
156 
157         for (uint8_t i = 0; i < FIFO_WMRK_SIZE; i++)
158         { /*! Process all samples and convert the raw sensor data. */
159             rawData.pressure = (uint32_t)((data[i * FXPQ3115_DATA_SIZE + 0]) << 16) |
160                                ((data[i * FXPQ3115_DATA_SIZE + 1]) << 8) | ((data[i * FXPQ3115_DATA_SIZE + 2]));
161             pressureInPascals += rawData.pressure / FXPQ3115_PRESSURE_CONV_FACTOR;
162 
163             rawData.temperature =
164                 (int16_t)((data[i * FXPQ3115_DATA_SIZE + 3]) << 8) | (data[i * FXPQ3115_DATA_SIZE + 4]);
165             tempInDegrees += rawData.temperature / FXPQ3115_TEMPERATURE_CONV_FACTOR;
166         }
167         PRINTF("\r\n Average Pressure    = %d Pa\r\n", pressureInPascals / FIFO_WMRK_SIZE);
168         PRINTF("\r\n Average Temperature = %d degC\r\n", tempInDegrees / FIFO_WMRK_SIZE);
169         ASK_USER_TO_RESUME(16 / FIFO_WMRK_SIZE); /* Ask for user input after processing 16 samples. */
170     }
171 }
172