ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_normal.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file mpl3115_normal.c
10  * @brief The mpl3115_normal.c file implements the ISSDK MPL3115 sensor driver
11  * example demonstration with polling mode.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "pin_mux.h"
18 #include "clock_config.h"
19 #include "board.h"
20 #include "fsl_debug_console.h"
21 #include "fsl_irqsteer.h"
22 
23 /* CMSIS Includes */
24 #include "Driver_I2C.h"
25 
26 /* ISSDK Includes */
27 #include "issdk_hal.h"
28 #include "mpl3115_drv.h"
29 
30 //-----------------------------------------------------------------------
31 // Macros
32 //-----------------------------------------------------------------------
33 #define MPL3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
34 /*! In MPL3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
35  * SAMPLING_EXPONENT).
36  * This gives a range of 1 second to 2^15 seconds (9 hours). */
37 #define MPL3115_SAMPLING_EXPONENT (1) /* 2 seconds */
38 
39 //-----------------------------------------------------------------------
40 // Constants
41 //-----------------------------------------------------------------------
42 /*! @brief Register settings for Normal (non buffered) mode. */
44  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
48  /* Set Over Sampling Ratio to 128. */
50  /* Set Auto acquisition time step. */
53 
54 /*! @brief Address of Status Register. */
56 
57 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
60 
61 //-----------------------------------------------------------------------
62 // Functions
63 //-----------------------------------------------------------------------
64 /*!
65  * @brief Main function
66  */
67 int main(void)
68 {
69  int16_t tempInDegrees;
70  uint32_t pressureInPascals;
72  uint8_t dataReady;
73  uint8_t data[MPL3115_DATA_SIZE];
75  sc_ipc_t ipc;
76  uint32_t freq;
77 
78  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
79  mpl3115_i2c_sensorhandle_t mpl3115Driver;
80 
81  ipc = BOARD_InitRpc();
82  BOARD_InitMemory();
83 
86 
88 
89  PRINTF("\r\n ----------------------------------------------------------------\r\n");
90  PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with POLL mode\r\n");
91  PRINTF("\r\n ----------------------------------------------------------------\r\n");
92 
93  /* Power on LPI2C. */
94  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
95  {
96  PRINTF("Error: Failed to power on LPI2C\r\n");
97  }
98 
99  /* Set LPI2C clock */
100  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
101  if (freq == 0)
102  {
103  PRINTF("Error: Failed to set LPI2C frequency\r\n");
104  }
105 
106  /* Power on IRQSTEER. */
107 #if (MIMX8QM_CM4_CORE0)
108  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
109  {
110  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
111  }
112 #elif (MIMX8QM_CM4_CORE1)
113  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
114  {
115  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
116  }
117 #endif
118 
119  /* Enable interrupt in irqsteer */
120  IRQSTEER_Init(IRQSTEER);
121  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
122 
123 
124  /*! Initialize the I2C driver. */
125  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
126  if (ARM_DRIVER_OK != status)
127  {
128  PRINTF("\r\n I2C Initialization Failed\r\n");
129  return -1;
130  }
131 
132  /*! Set the I2C Power mode. */
133  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
134  if (ARM_DRIVER_OK != status)
135  {
136  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
137  return -1;
138  }
139 
140  /*! Set the I2C bus speed. */
141  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
142  if (ARM_DRIVER_OK != status)
143  {
144  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
145  return -1;
146  }
147 
148  /*! Initialize MPL3115 sensor driver. */
151  if (SENSOR_ERROR_NONE != status)
152  {
153  PRINTF("\r\n Sensor Initialization Failed\r\n");
154  return -1;
155  }
156  PRINTF("\r\n Successfully Initiliazed MPL3115 Sensor\r\n");
157  PRINTF("\r\n Device Address is 0x%x and WHOAMI Value is 0x%x\r\n", MPL3115_I2C_ADDR, MPL3115_WHOAMI_VALUE);
158 
159  /*! Configure the MPL3115 sensor. */
160  status = MPL3115_I2C_Configure(&mpl3115Driver, cMpl3115ConfigNormal);
161  if (SENSOR_ERROR_NONE != status)
162  {
163  PRINTF("\r\nMPL3115 configuration failed...\r\n");
164  return -1;
165  }
166  PRINTF("\r\n Successfully Applied MPL3115 Sensor Configuration\r\n");
167 
168  PRINTF("\r\n Reading MPL3115 Pressure & Temperature Values\r\n");
169 
170  for (;;) /* Forever loop */
171  {
172  /*! Wait for data ready from the MPL3115. */
173  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115Status, &dataReady);
174  if (0 == (dataReady & MPL3115_DR_STATUS_PTDR_MASK))
175  { /* Loop, if new sample is not available. */
176  continue;
177  }
178 
179  /*! Read new raw sensor data from the MPL3115. */
180  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
181  if (ARM_DRIVER_OK != status)
182  {
183  PRINTF("\r\n Read Failed. \r\n");
184  return -1;
185  }
186 
187  /*! Process the sample and convert the raw sensor data. */
188  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
189  pressureInPascals = rawData.pressure / MPL3115_PRESSURE_CONV_FACTOR;
190 
191  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
192  tempInDegrees = rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
193 
194  PRINTF("\r\n Pressure = %d Pa", pressureInPascals);
195  PRINTF("\t Temperature = %d degC\r\n", tempInDegrees);
196  ASK_USER_TO_RESUME(8); /* Ask for user input after processing 8 samples. */
197  }
198 }
#define MPL3115_DATA_SIZE
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
int main(void)
Main function.
#define MPL3115_PRESSURE_CONV_FACTOR
Definition: mpl3115_drv.h:28
int32_t MPL3115_I2C_ReadData(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mpl3115_drv.c:104
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
#define MPL3115_CTRL_REG2_ST_MASK
Definition: mpl3115.h:916
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define MPL3115_PT_DATA_CFG_TDEFE_MASK
Definition: mpl3115.h:550
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define MPL3115_DR_STATUS_PTDR_MASK
Definition: mpl3115.h:195
const registerwritelist_t cMpl3115ConfigNormal[]
Register settings for Normal (non buffered) mode.
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define MPL3115_TEMPERATURE_CONV_FACTOR
Definition: mpl3115_drv.h:30
This structure defines the mpl3115 data buffer in Pressure Mode.
Definition: mpl3115_drv.h:45
#define MPL3115_PT_DATA_CFG_DREM_ENABLED
Definition: mpl3115.h:570
uint8_t data[FXLS8962_DATA_SIZE]
#define MPL3115_PT_DATA_CFG_PDEFE_MASK
Definition: mpl3115.h:553
#define __END_READ_DATA__
Definition: sensor_drv.h:51
This defines the sensor specific information.
Definition: mpl3115_drv.h:36
#define MPL3115_CTRL_REG1_OS_OSR_128
Definition: mpl3115.h:884
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
int32_t MPL3115_I2C_Initialize(mpl3115_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: mpl3115_drv.c:22
uint16_t readFrom
Definition: sensor_drv.h:80
#define MPL3115_PT_DATA_CFG_DREM_MASK
Definition: mpl3115.h:556
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
This structure defines the Read command List.
Definition: sensor_drv.h:78
const registerreadlist_t cMpl3115Status[]
Address of Status Register.
#define MPL3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: mpl3115.h:564
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define MPL3115_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:15
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
#define MPL3115_SAMPLING_EXPONENT
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
int32_t MPL3115_I2C_Configure(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mpl3115_drv.c:61
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855