F439_CPP_MPL3115A2_Project_01 1.0
STM32F439 MPL3115A2 pressure and temperature tutorial
Loading...
Searching...
No Matches
mpl3115a2.c
Go to the documentation of this file.
1
11#include "mpl3115a2.h"
12
14static I2C_HandleTypeDef *i2c;
15
17static uint16_t devAddr;
18
40HAL_StatusTypeDef MPL3115A2_Init(I2C_HandleTypeDef *hi2c, uint16_t devAddress)
41{
42 HAL_StatusTypeDef halStatus = HAL_OK;
43 uint8_t whoAmI = 0x00;
44 uint8_t standbyCmd = 0x00;
45 uint8_t configData[2];
46
47 i2c = hi2c;
48 devAddr = devAddress;
49
50 do {
51 halStatus = HAL_I2C_IsDeviceReady(i2c, devAddr, 5, 10);
52 if (halStatus != HAL_OK) {
53 reset_I2C();
54 }
55 } while (halStatus != HAL_OK);
56
57 if (halStatus == HAL_OK) {
58 halStatus = HAL_I2C_Mem_Read(i2c, devAddr, MPL3115A2_WHOAMI,
59 I2C_MEMADD_SIZE_8BIT, &whoAmI, 1, 1000);
60
61 if (halStatus == HAL_OK && whoAmI != 0xC4) {
63 }
64 }
65
66 if (halStatus == HAL_OK) {
67 /* Enter standby before changing sensor configuration registers. */
68 halStatus = HAL_I2C_Mem_Write(i2c, devAddr, MPL3115A2_CTRL_REG1,
69 I2C_MEMADD_SIZE_8BIT, &standbyCmd, 1, 100);
70 }
71
72 if (halStatus == HAL_OK) {
73 /* Enable pressure and temperature data-ready event flags. */
74 configData[0] = MPL3115A2_PT_DATA_CFG;
75 configData[1] = MPL3115A2_PT_DATA_CFG_TDEFE |
76 MPL3115A2_PT_DATA_CFG_PDEFE |
77 MPL3115A2_PT_DATA_CFG_DREM;
78 halStatus = HAL_I2C_Master_Transmit(i2c, devAddr, configData, 2,
79 HAL_MAX_DELAY);
80 }
81
82 if (halStatus == HAL_OK) {
83 /* Use barometer mode, OSR=128, and active mode. */
84 configData[0] = MPL3115A2_CTRL_REG1;
85 configData[1] = MPL3115A2_CTRL_REG1_OS128 | MPL3115A2_CTRL_REG1_SBYB;
86 halStatus = HAL_I2C_Master_Transmit(i2c, devAddr, configData, 2,
87 HAL_MAX_DELAY);
88 }
89
90 return halStatus;
91}
92
99HAL_StatusTypeDef MPL3115A2_GetStatus(uint8_t *retVal)
100{
101 HAL_StatusTypeDef halStatus = HAL_OK;
102 uint8_t data;
103
104 halStatus = HAL_I2C_Mem_Read(i2c, devAddr, MPL3115A2_REGISTER_STATUS,
105 I2C_MEMADD_SIZE_8BIT, &data, 1, HAL_MAX_DELAY);
106
107 *retVal = data;
108
109 return halStatus;
110}
111
125HAL_StatusTypeDef MPL3115A2_ReadTemperature(float *celsius)
126{
127 uint8_t rawData[2];
128 int16_t tempRaw;
129 HAL_StatusTypeDef halStatus = HAL_OK;
130
131 halStatus = HAL_I2C_Mem_Read(i2c, devAddr, MPL3115A2_REGISTER_TEMP_MSB,
132 I2C_MEMADD_SIZE_8BIT, rawData, 2, HAL_MAX_DELAY);
133
134 if (halStatus == HAL_OK) {
135 /* OUT_T_MSB:OUT_T_LSB is a signed Q8.4 value (1/256 C per LSB of the pair). */
136 tempRaw = (int16_t)((rawData[0] << 8) | rawData[1]);
137 *celsius = (float)tempRaw / 256.0f;
138 } else {
139 *celsius = -99.99f;
140 }
141
142 return halStatus;
143}
144
156HAL_StatusTypeDef MPL3115A2_ReadPressure(float *pressure_inHg)
157{
158 uint8_t pressureData[3];
159 uint32_t pressureQ18_2;
160 float pressurePa;
161 HAL_StatusTypeDef halStatus = HAL_OK;
162
163 halStatus = HAL_I2C_Mem_Read(i2c, devAddr,
164 MPL3115A2_REGISTER_PRESSURE_MSB, I2C_MEMADD_SIZE_8BIT,
165 pressureData, 3, HAL_MAX_DELAY);
166
167 if (halStatus == HAL_OK) {
168 /*
169 * Pressure is returned as a 20-bit unsigned Q18.2 value left-aligned
170 * across OUT_P_MSB, OUT_P_CSB, and OUT_P_LSB.
171 */
172 pressureQ18_2 = ((uint32_t)pressureData[0] << 16) |
173 ((uint32_t)pressureData[1] << 8) |
174 (uint32_t)pressureData[2];
175 pressureQ18_2 >>= 4;
176
177 pressurePa = (float)pressureQ18_2 / 4.0f;
178 *pressure_inHg = pressurePa / 3386.389f;
179 } else {
180 *pressure_inHg = -99.99f;
181 }
182
183 return halStatus;
184}
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition main.c:416
void reset_I2C(void)
Recover and reinitialize the I2C1 bus.
Definition main.c:97
static I2C_HandleTypeDef * i2c
HAL I2C handle used for all MPL3115A2 transactions.
Definition mpl3115a2.c:14
HAL_StatusTypeDef MPL3115A2_Init(I2C_HandleTypeDef *hi2c, uint16_t devAddress)
Initialize and configure the MPL3115A2 sensor.
Definition mpl3115a2.c:40
HAL_StatusTypeDef MPL3115A2_ReadPressure(float *pressure_inHg)
Read and convert the MPL3115A2 pressure measurement.
Definition mpl3115a2.c:156
static uint16_t devAddr
STM32 HAL-formatted MPL3115A2 I2C address.
Definition mpl3115a2.c:17
HAL_StatusTypeDef MPL3115A2_GetStatus(uint8_t *retVal)
Read the MPL3115A2 STATUS register.
Definition mpl3115a2.c:99
HAL_StatusTypeDef MPL3115A2_ReadTemperature(float *celsius)
Read and convert the MPL3115A2 temperature measurement.
Definition mpl3115a2.c:125