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