Skip to content

#622 i2c altimeter issue fix #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions i2c/mpl3115a2_i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ add_executable(mpl3115a2_i2c
mpl3115a2_i2c.c
)

target_include_directories(mpl3115a2_i2c PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(mpl3115a2_i2c
pico_stdlib
hardware_i2c
)

# pull in common dependencies and additional i2c hardware support
target_link_libraries(mpl3115a2_i2c pico_stdlib hardware_i2c)

Expand Down
2 changes: 1 addition & 1 deletion i2c/mpl3115a2_i2c/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example code shows how to interface the Raspberry Pi Pico to an MPL3115A2 a

The board used in this example https://www.adafruit.com/product/1893[comes from Adafruit], but any MPL3115A2 breakouts should work similarly.

The MPL3115A2 makes available two ways of reading its temperature and pressure data. The first is known as polling, where the Pico will continuously read data out of a set of auto-incrementing registers which are refreshed with new data every so often. The second, which this example will demonstrate, uses a 160-byte first-in-first-out (FIFO) queue and configurable interrupts to tell the Pico when to read data. More information regarding when the interrupts can be triggered available https://www.nxp.com/docs/en/data-sheet/MPL3115A2.pdf[in the datasheet]. This example waits for the 32 sample FIFO to overflow, detects this via an interrupt pin, and then averages the 32 samples taken. The sensor is configured to take a sample every second.
The MPL3115A2 makes available two ways of reading its temperature and pressure data. The first is known as polling, where the Pico will continuously read data out of a set of auto-incrementing registers which are refreshed with new data every so often. The second, which this example will demonstrate, uses a 160-byte first-in-first-out (FIFO) queue and configurable interrupts to tell the Pico when to read data. More information regarding when the interrupts can be triggered is available at https://www.nxp.com/docs/en/data-sheet/MPL3115A2.pdf[in the datasheet]. This example waits for the 32 sample FIFO to overflow, detects this via an interrupt pin, and then averages the 32 samples taken. The sensor is configured to take a sample every second.

Bit math is used to convert the temperature and altitude data from the raw bits collected in the registers. Take the temperature calculation as an example: it is a 12-bit signed number with 8 integer bits and 4 fractional bits. First, we read the 2 8-bit registers and store them in a buffer. Then, we concatenate them into one unsigned 16-bit integer starting with the OUT_T_MSB register, thus making sure that the last bit of this register is aligned with the MSB in our 16 bit unsigned integer so it is correctly interpreted as the signed bit when we later cast this to a signed 16-bit integer. Finally, the entire number is converted to a float implicitly when we multiply it by 1/2^8 to shift it 8 bits to the right of the decimal point. Though only the last 4 bits of the OUT_T_LSB register hold data, this does not matter as the remaining 4 are held at zero and "disappear" when we shift the decimal point left by 8. Similar logic is applied to the altitude calculation.

Expand Down
38 changes: 31 additions & 7 deletions i2c/mpl3115a2_i2c/mpl3115a2_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pico/binary_info.h"
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "mpl3115a2_i2c.h"

/* Example code to talk to an MPL3115A2 altimeter sensor via I2C

Expand Down Expand Up @@ -42,6 +43,11 @@
#define MPL3115A2_OFF_T _u(0x2C)
#define MPL3115A2_OFF_H _u(0x2D)

/*** Sea-level pressure registers ***/
#define MPL3115A2_BAR_IN_MSB _u(0x14)
#define MPL3115A2_BAR_IN_LSB _u(0x15)


#define MPL3115A2_FIFO_DISABLED _u(0x00)
#define MPL3115A2_FIFO_STOP_ON_OVERFLOW _u(0x80)
#define MPL3115A2_FIFO_SIZE 32
Expand All @@ -56,12 +62,24 @@
volatile uint8_t fifo_data[MPL3115A2_FIFO_SIZE * MPL3115A2_DATA_BATCH_SIZE];
volatile bool has_new_data = false;

struct mpl3115a2_data_t {
// Q8.4 fixed point
float temperature;
// Q16.4 fixed-point
float altitude;
};

/*** Sea-level pressure functions ***/
// Set sea-level pressure in hectopascals (hPa)
void mpl3115a2_set_sealevel_pressure(float hPa) {
uint16_t bars = (uint16_t)(hPa * 50); // Convert hPa to BAR_IN value (2 Pa/LSB)
uint8_t buf[] = {MPL3115A2_BAR_IN_MSB, (bars >> 8) & 0xFF, bars & 0xFF};
i2c_write_blocking(i2c_default, ADDR, buf, 3, false);
}

// Get current sea-level pressure setting in hPa
float mpl3115a2_get_sealevel_pressure() {
uint8_t reg = MPL3115A2_BAR_IN_MSB;
uint8_t buf[2];
i2c_write_blocking(i2c_default, ADDR, &reg, 1, true);
i2c_read_blocking(i2c_default, ADDR, buf, 2, false);
uint16_t bars = (buf[0] << 8) | buf[1];
return (float)bars / 50.0f; // Convert back to hPa
}

void copy_to_vbuf(uint8_t buf1[], volatile uint8_t buf2[], uint buflen) {
for (size_t i = 0; i < buflen; i++) {
Expand Down Expand Up @@ -109,6 +127,9 @@ void mpl3115a2_init() {
buf[0] = MPL3115A2_CTRLREG5, buf[1] = 0x40;
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);

/*** Default sea-level pressure (1013.25 hPa) ***/
mpl3115a2_set_sealevel_pressure(1013.25f);

// set p, t and h offsets here if needed
// eg. 2's complement number: 0xFF subtracts 1 meter
//buf[0] = MPL3115A2_OFF_H, buf[1] = 0xFF;
Expand All @@ -132,7 +153,6 @@ void gpio_callback(uint gpio, __unused uint32_t events) {
// FIFO overflow interrupt
// watermark bits set to 0 in F_SETUP reg, so only possible event is an overflow
// otherwise, we would read F_STATUS to confirm it was an overflow
printf("FIFO overflow!\n");
// drain the fifo
mpl3115a2_read_fifo(fifo_data);
// read status register to clear interrupt bit
Expand Down Expand Up @@ -186,6 +206,9 @@ int main() {

mpl3115a2_init();

// Uncomment to overwrite default sea-level pressure:
// mpl3115a2_set_sealevel_pressure(1020.0f); // Local weather pressure

gpio_set_irq_enabled_with_callback(INT1_PIN, GPIO_IRQ_LEVEL_LOW, true, &gpio_callback);

while (1) {
Expand All @@ -200,6 +223,7 @@ int main() {
}
printf("%d sample average -> t: %.4f C, h: %.4f m\n", MPL3115A2_FIFO_SIZE, tsum / MPL3115A2_FIFO_SIZE,
hsum / MPL3115A2_FIFO_SIZE);
mpl3115a2_get_sealevel_pressure(); // Show current setting
has_new_data = false;
}
sleep_ms(10);
Expand Down
30 changes: 30 additions & 0 deletions i2c/mpl3115a2_i2c/mpl3115a2_i2c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// mpl3115a2.h
#ifndef _MPL3115A2_H
#define _MPL3115A2_H

#include "pico/stdlib.h"

#ifdef __cplusplus
extern "C" {
#endif

struct mpl3115a2_data_t {
// Q8.4 fixed point
float temperature;
// Q16.4 fixed-point
float altitude;
};

void mpl3115a2_init(void);
void mpl3115a2_read_fifo(volatile uint8_t* fifo_buf);
void mpl3115a2_convert_fifo_batch(uint8_t start, volatile uint8_t* buf, struct mpl3115a2_data_t* data);

// Add NEW prototypes
void mpl3115a2_set_sealevel_pressure(float hPa);
float mpl3115a2_get_sealevel_pressure(void);

#ifdef __cplusplus
}
#endif

#endif // _MPL3115A2_H