ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_oneshot.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_oneshot.c
10  * @brief The mpl3115_oneshot.c file implements the ISSDK MPL3115 sensor driver
11  * example demonstration with one shot 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 
35 //-----------------------------------------------------------------------
36 // Constants
37 //-----------------------------------------------------------------------
38 /*! @brief Register settings for Altitude readings in One-Shot mode. */
40  /* Enable Altitude output. */
43 
44 /*! @brief Register settings for Triggring One-Shot Sampling. */
46  /* Set the One ShoT Bit. */
49 
50 /*! @brief Address of Register containing OST Bit. */
52 
53 /*! @brief Address and size of Raw Altitude+Temperature Data. */
56 
57 //-----------------------------------------------------------------------
58 // Functions
59 //-----------------------------------------------------------------------
60 /*!
61  * @brief Main function
62  */
63 int main(void)
64 {
65  int16_t tempInDegrees;
66  int32_t altitudeInMeters;
68  uint8_t dataReady;
69  uint8_t data[MPL3115_DATA_SIZE];
71  sc_ipc_t ipc;
72  uint32_t freq;
73 
74  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
75  mpl3115_i2c_sensorhandle_t mpl3115Driver;
76  registerDeviceInfo_t deviceInfo;
77 
78  ipc = BOARD_InitRpc();
79  BOARD_InitMemory();
82 
84 
85  PRINTF("\r\n -------------------------------------------------------------------\r\n");
86  PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with OneShot mode\r\n");
87  PRINTF("\r\n -------------------------------------------------------------------\r\n");
88 
89  /* Power on LPI2C. */
90  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
91  {
92  PRINTF("Error: Failed to power on LPI2C\r\n");
93  }
94 
95  /* Set LPI2C clock */
96  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
97  if (freq == 0)
98  {
99  PRINTF("Error: Failed to set LPI2C frequency\r\n");
100  }
101 
102  /* Power on IRQSTEER. */
103 #if (MIMX8QM_CM4_CORE0)
104  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
105  {
106  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
107  }
108 #elif (MIMX8QM_CM4_CORE1)
109  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
110  {
111  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
112  }
113 #endif
114 
115  /* Enable interrupt in irqsteer */
116  IRQSTEER_Init(IRQSTEER);
117  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
118 
119  /*! Initialize the I2C driver. */
120  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
121  if (ARM_DRIVER_OK != status)
122  {
123  PRINTF("\r\n I2C Initialization Failed\r\n");
124  return -1;
125  }
126 
127  /*! Set the I2C Power mode. */
128  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
129  if (ARM_DRIVER_OK != status)
130  {
131  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
132  return -1;
133  }
134 
135  /*! Set the I2C bus speed. */
136  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
137  if (ARM_DRIVER_OK != status)
138  {
139  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
140  return -1;
141  }
142 
143  /*! Initialize MPL3115 sensor driver. */
146  if (SENSOR_ERROR_NONE != status)
147  {
148  PRINTF("\r\n Sensor Initialization Failed\r\n");
149  return -1;
150  }
151  PRINTF("\r\n Successfully Initiliazed MPL3115 Sensor\r\n");
152  PRINTF("\r\n Device Address is 0x%x and WHOAMI Value is 0x%x\r\n", MPL3115_I2C_ADDR, MPL3115_WHOAMI_VALUE);
153  /*! We do not need to call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
154  * Instead we directly write register settings for One-Shot Mode... */
155  deviceInfo.deviceInstance = I2C_S_DEVICE_INDEX;
156  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &deviceInfo, mpl3115Driver.slaveAddress, cMpl3115ConfigAltitude);
157  if (ARM_DRIVER_OK != status)
158  {
159  PRINTF("\r\nWrite Failed.\r\n");
160  return -1;
161  }
162  PRINTF("\r\n MPL3115 triggers acquisition of one sample and then read the data...\r\n");
163 
164  /*! The code contained withing the braces illustrate steps to read one-sample.
165  * These can be repated at desired intervals to acquire more samples. */
166  { /* Trigger acquisition of One Sample. */
167  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &deviceInfo, mpl3115Driver.slaveAddress, cMpl3115SetOST);
168  if (ARM_DRIVER_OK != status)
169  {
170  PRINTF("\r\nWrite Failed.\r\n");
171  return SENSOR_ERROR_WRITE;
172  }
173 
174  do /*! Keep checking the OST FLAG for completion. */
175  {
176  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115GetOST, &dataReady);
177  if (ARM_DRIVER_OK != status)
178  {
179  PRINTF("\r\nRead Failed.\r\n");
180  return -1;
181  }
182  } /* Loop, untill sample acquisition is not completed. */
183  while (0 != (dataReady & MPL3115_CTRL_REG1_OST_MASK));
184 
185  /*! Read raw sensor data from the MPL3115. */
186  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
187  if (ARM_DRIVER_OK != status)
188  {
189  PRINTF("\r\n Read Failed. \r\n");
190  return -1;
191  }
192 
193  /*! Process the sample and convert the raw sensor data. */
194  rawData.altitude = (int32_t)((data[0]) << 24) | ((data[1]) << 16) | ((data[2]) << 8);
195  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
196  altitudeInMeters = rawData.altitude / MPL3115_ALTITUDE_CONV_FACTOR;
197  tempInDegrees = rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
198 
199  PRINTF("\r\n Altitude = %d Meters", altitudeInMeters);
200  PRINTF("\t Temperature = %d degC\r\n", tempInDegrees);
201  ASK_USER_TO_RESUME(1); /* Ask for user input after processing 8 samples. */
202  }
203 
204  return 0;
205 }
int main(void)
Main function.
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:102
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
This structure defines the mpl3115 data buffer in Altitude Mode.
Definition: mpl3115_drv.h:54
const registerwritelist_t cMpl3115ConfigAltitude[]
Register settings for Altitude readings in One-Shot mode.
const registerreadlist_t cMpl3115GetOST[]
Address of Register containing OST Bit.
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 __END_WRITE_DATA__
Definition: sensor_drv.h:45
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
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:71
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define MPL3115_TEMPERATURE_CONV_FACTOR
Definition: mpl3115_drv.h:30
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Altitude+Temperature Data.
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define MPL3115_CTRL_REG1_OST_MASK
Definition: mpl3115.h:849
This defines the sensor specific information.
Definition: mpl3115_drv.h:36
#define MPL3115_CTRL_REG1_ALT_MASK
Definition: mpl3115.h:861
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_WHOAMI_VALUE
Definition: mpl3115.h:65
ARM_DRIVER_I2C * pCommDrv
Definition: mpl3115_drv.h:39
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define MPL3115_DATA_SIZE
#define MPL3115_CTRL_REG1_OST_SET
Definition: mpl3115.h:871
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define MPL3115_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define MPL3115_CTRL_REG1_ALT_ALT
Definition: mpl3115.h:888
#define MPL3115_ALTITUDE_CONV_FACTOR
Definition: mpl3115_drv.h:29