F439_CPP_MPL3115A2_Project_01 1.0
STM32F439 MPL3115A2 pressure and temperature tutorial
Loading...
Searching...
No Matches

MPL3115A2 pressure and temperature sensor driver implementation. More...

#include "mpl3115a2.h"

Go to the source code of this file.

Functions

HAL_StatusTypeDef MPL3115A2_Init (I2C_HandleTypeDef *hi2c, uint16_t devAddress)
 Initialize and configure the MPL3115A2 sensor.
 
HAL_StatusTypeDef MPL3115A2_GetStatus (uint8_t *retVal)
 Read the MPL3115A2 STATUS register.
 
HAL_StatusTypeDef MPL3115A2_ReadTemperature (float *celsius)
 Read and convert the MPL3115A2 temperature measurement.
 
HAL_StatusTypeDef MPL3115A2_ReadPressure (float *pressure_inHg)
 Read and convert the MPL3115A2 pressure measurement.
 

Variables

static I2C_HandleTypeDef * i2c
 HAL I2C handle used for all MPL3115A2 transactions.
 
static uint16_t devAddr
 STM32 HAL-formatted MPL3115A2 I2C address.
 

Detailed Description

MPL3115A2 pressure and temperature sensor driver implementation.

The driver is intentionally small for tutorial use. It stores one global I2C handle and device address, initializes the sensor for barometer operation, and exposes blocking read helpers for temperature and pressure.

Definition in file mpl3115a2.c.

Function Documentation

◆ MPL3115A2_GetStatus()

HAL_StatusTypeDef MPL3115A2_GetStatus ( uint8_t * retVal)

Read the MPL3115A2 STATUS register.

Parameters
retValPointer that receives the raw STATUS register value.
Returns
HAL_OK when the I2C register read succeeds, otherwise the HAL error status.

Definition at line 99 of file mpl3115a2.c.

References devAddr, and i2c.

Referenced by main().

◆ MPL3115A2_Init()

HAL_StatusTypeDef MPL3115A2_Init ( I2C_HandleTypeDef * hi2c,
uint16_t devAddress )

Initialize and configure the MPL3115A2 sensor.

Initialize the MPL3115A2 sensor for barometer and temperature reads.

Stores the I2C handle and device address, then blocks until the sensor acknowledges on the bus, retrying through reset_I2C() on each failed probe. Once the device responds, verifies WHO_AM_I, places the sensor in standby, enables the pressure and temperature data-ready event flags, and returns it to active barometer mode with OSR=128.

The bus-probe phase does not return on failure; it retries indefinitely until the sensor answers. A WHO_AM_I mismatch is treated as a fatal hardware fault and invokes Error_Handler() (which does not return). After the identity check, the first failing configuration transaction stops setup and is reported through the return value.

Parameters
hi2cPointer to the STM32 HAL I2C handle connected to the sensor.
devAddressSTM32 HAL-formatted (left-shifted) I2C device address.
Returns
HAL_OK on success, or the failing HAL_StatusTypeDef if a configuration transaction fails. Does not return if WHO_AM_I fails to report the expected MPL3115A2 identity (0xC4).

Definition at line 40 of file mpl3115a2.c.

References devAddr, Error_Handler(), i2c, and reset_I2C().

Referenced by main().

◆ MPL3115A2_ReadPressure()

HAL_StatusTypeDef MPL3115A2_ReadPressure ( float * pressure_inHg)

Read and convert the MPL3115A2 pressure measurement.

Read pressure from the MPL3115A2.

Reads the three pressure output registers, converts the Q18.2 pressure value to pascals, and returns the result in inches of mercury.

Parameters
pressure_inHgPointer that receives the converted pressure in inHg. On an I2C read failure it is set to the sentinel value -99.99f.
Returns
HAL_OK when the I2C register read succeeds, otherwise the HAL error status.

Definition at line 156 of file mpl3115a2.c.

References devAddr, and i2c.

Referenced by main().

◆ MPL3115A2_ReadTemperature()

HAL_StatusTypeDef MPL3115A2_ReadTemperature ( float * celsius)

Read and convert the MPL3115A2 temperature measurement.

Read temperature from the MPL3115A2.

Reads OUT_T_MSB and OUT_T_LSB, which together form a signed Q8.4 fixed-point value (1/256 C per LSB of the register pair), and converts it to degrees Celsius. The signed cast preserves the full measurement range, including sub-zero temperatures.

Parameters
celsiusPointer that receives the converted temperature in degrees Celsius. On an I2C read failure it is set to the sentinel value -99.99f.
Returns
HAL_OK when the I2C register read succeeds, otherwise the HAL error status.

Definition at line 125 of file mpl3115a2.c.

References devAddr, and i2c.

Referenced by main().

Variable Documentation

◆ devAddr

uint16_t devAddr
static

STM32 HAL-formatted MPL3115A2 I2C address.

Definition at line 17 of file mpl3115a2.c.

Referenced by MPL3115A2_GetStatus(), MPL3115A2_Init(), MPL3115A2_ReadPressure(), and MPL3115A2_ReadTemperature().

◆ i2c

I2C_HandleTypeDef* i2c
static

HAL I2C handle used for all MPL3115A2 transactions.

Definition at line 14 of file mpl3115a2.c.

Referenced by MPL3115A2_GetStatus(), MPL3115A2_Init(), MPL3115A2_ReadPressure(), and MPL3115A2_ReadTemperature().