Skip to content

Commit 04a1e58

Browse files
author
Alasdair Allan
authored
Merge pull request #136 from matiasilva/mpl3115a2-altimeter
Add example for MPL3115A2 altimeter
2 parents b9f1eff + 2199a83 commit 04a1e58

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ App|Description
7777
---|---
7878
[bus_scan](i2c/bus_scan) | Scan the I2C bus for devices and display results.
7979
[lcd_1602_i2c](i2c/lcd_1602_i2c) | Display some text on a generic 16x2 character LCD display, via I2C.
80+
[mpl3115a2_i2c](i2c/mpl3115a2_i2c) | Interface with an MPL3115A2 altimeter, exploring interrupts and advanced board features, via I2C.
8081
[mpu6050_i2c](i2c/mpu6050_i2c) | Read acceleration and angular rate values from a MPU6050 accelerometer/gyro, attached to an I2C bus.
8182

8283
### Interpolator

i2c/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
if (NOT PICO_NO_HARDWARE)
22
add_subdirectory(bus_scan)
33
add_subdirectory(lcd_1602_i2c)
4+
add_subdirectory(mpl3115a2_i2c)
45
add_subdirectory(mpu6050_i2c)
56
endif ()

i2c/mpl3115a2_i2c/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(mpl3115a2_i2c
2+
mpl3115a2_i2c.c
3+
)
4+
5+
# pull in common dependencies and additional i2c hardware support
6+
target_link_libraries(mpl3115a2_i2c pico_stdlib hardware_i2c)
7+
8+
# create map/bin/hex file etc.
9+
pico_add_extra_outputs(mpl3115a2_i2c)
10+
11+
# add url via pico_set_program_url
12+
example_auto_set_url(mpl3115a2_i2c)

i2c/mpl3115a2_i2c/README.adoc

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
= Attaching an MPL3115A2 altimeter via I2C
2+
3+
This example code shows how to interface the Raspberry Pi Pico to an MPL3115A2 altimeter via I2C. The MPL3115A2 has onboard pressure and temperature sensors which are used to estimate the altitude. In comparison to the BMP- family of pressure and temperature sensors, the MPL3115A2 has two interrupt pins for ultra low power operation and takes care of the sensor reading compensation on the board! It also has multiple modes of operation and impressive operating conditions.
4+
5+
The board used in this example https://www.adafruit.com/product/1893[comes from Adafruit], but any MPL3115A2 breakouts should work similarly.
6+
7+
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.
8+
9+
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.
10+
11+
TIP: Choosing the right sensor for your project among so many choices can be hard! There are multiple factors you may have to consider in addition to any constraints imposed on you. Cost, operating temperature, sensor resolution, power consumption, ease of use, communication protocols and supply voltage are all but a few factors that can play a role in sensor choice. For most hobbyist purposes though, the majority of sensors out there will do just fine!
12+
13+
== Wiring information
14+
15+
Wiring up the device requires 5 jumpers, to connect VCC (3.3v), GND, INT1, SDA and SCL. The example here uses I2C port 0, which is assigned to GPIO 4 (SDA) and GPIO 5 (SCL) by default. Power is supplied from the 3.3V pin.
16+
17+
NOTE: The MPL3115A2 has a 1.6-3.6V voltage supply range. This means it can work with the Pico's 3.3v pins out of the box but our Adafruit breakout has an onboard voltage regulator for good measure. This may not always be true of other sensors, though.
18+
19+
[[mpl3115a2_i2c_wiring]]
20+
[pdfwidth=75%]
21+
.Wiring Diagram for MPL3115A2 altimeter.
22+
image::mpl3115a2_i2c_bb.png[]
23+
24+
== List of Files
25+
26+
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
27+
mpl3115a2_i2c.c:: The example code.
28+
29+
== Bill of Materials
30+
31+
.A list of materials required for the example
32+
[[mpl3115a2-i2c-bom-table]]
33+
[cols=3]
34+
|===
35+
| *Item* | *Quantity* | Details
36+
| Breadboard | 1 | generic part
37+
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
38+
| MPL3115A2 altimeter | 1 | https://www.adafruit.com/product/1893[Adafruit]
39+
| M/M Jumper wires | 5 | generic part
40+
|===

i2c/mpl3115a2_i2c/mpl3115a2_i2c.c

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "pico/binary_info.h"
10+
#include "hardware/gpio.h"
11+
#include "hardware/i2c.h"
12+
13+
/* Example code to talk to an MPL3115A2 altimeter sensor via I2C
14+
15+
See accompanying documentation in README.adoc or the C++ SDK booklet.
16+
17+
Connections on Raspberry Pi Pico board, other boards may vary.
18+
19+
GPIO PICO_DEFAULT_I2C_SDA_PIN (On Pico this is 4 (pin 6)) -> SDA on MPL3115A2 board
20+
GPIO PICO_DEFAULT_I2C_SCK_PIN (On Pico this is 5 (pin 7)) -> SCL on MPL3115A2 board
21+
GPIO 16 -> INT1 on MPL3115A2 board
22+
3.3v (pin 36) -> VCC on MPL3115A2 board
23+
GND (pin 38) -> GND on MPL3115A2 board
24+
*/
25+
26+
// 7-bit address
27+
#define ADDR 0x60
28+
#define INT1_PIN _u(16)
29+
30+
// following definitions only valid for F_MODE > 0 (ie. if FIFO enabled)
31+
#define MPL3115A2_F_DATA _u(0x01)
32+
#define MPL3115A2_F_STATUS _u(0x00)
33+
#define MPL3115A2_F_SETUP _u(0x0F)
34+
#define MPL3115A2_INT_SOURCE _u(0x12)
35+
#define MPL3115A2_CTRLREG1 _u(0x26)
36+
#define MPL3115A2_CTRLREG2 _u(0x27)
37+
#define MPL3115A2_CTRLREG3 _u(0x28)
38+
#define MPL3115A2_CTRLREG4 _u(0x29)
39+
#define MPL3115A2_CTRLREG5 _u(0x2A)
40+
#define MPL3115A2_PT_DATA_CFG _u(0x13)
41+
#define MPL3115A2_OFF_P _u(0x2B)
42+
#define MPL3115A2_OFF_T _u(0x2C)
43+
#define MPL3115A2_OFF_H _u(0x2D)
44+
45+
#define MPL3115A2_FIFO_DISABLED _u(0x00)
46+
#define MPL3115A2_FIFO_STOP_ON_OVERFLOW _u(0x80)
47+
#define MPL3115A2_FIFO_SIZE 32
48+
#define MPL3115A2_DATA_BATCH_SIZE 5
49+
#define MPL3115A2_ALTITUDE_NUM_REGS 3
50+
#define MPL3115A2_ALTITUDE_INT_SIZE 20
51+
#define MPL3115A2_TEMPERATURE_INT_SIZE 12
52+
#define MPL3115A2_NUM_FRAC_BITS 4
53+
54+
#define PARAM_ASSERTIONS_ENABLE_I2C 1
55+
56+
volatile uint8_t fifo_data[MPL3115A2_FIFO_SIZE * MPL3115A2_DATA_BATCH_SIZE];
57+
volatile bool has_new_data = false;
58+
59+
struct mpl3115a2_data_t {
60+
// Q8.4 fixed point
61+
float temperature;
62+
// Q16.4 fixed-point
63+
float altitude;
64+
};
65+
66+
void copy_to_vbuf(uint8_t buf1[], volatile uint8_t buf2[], int buflen) {
67+
for (size_t i = 0; i < buflen; i++) {
68+
buf2[i] = buf1[i];
69+
}
70+
}
71+
72+
#ifdef i2c_default
73+
void mpl3115a2_read_fifo(volatile uint8_t fifo_buf[]) {
74+
// drains the 160 byte FIFO
75+
uint8_t reg = MPL3115A2_F_DATA;
76+
uint8_t buf[MPL3115A2_FIFO_SIZE * MPL3115A2_DATA_BATCH_SIZE];
77+
i2c_write_blocking(i2c_default, ADDR, &reg, 1, true);
78+
// burst read 160 bytes from fifo
79+
i2c_read_blocking(i2c_default, ADDR, buf, MPL3115A2_FIFO_SIZE * MPL3115A2_DATA_BATCH_SIZE, false);
80+
copy_to_vbuf(buf, fifo_buf, MPL3115A2_FIFO_SIZE * MPL3115A2_DATA_BATCH_SIZE);
81+
}
82+
83+
uint8_t mpl3115a2_read_reg(uint8_t reg) {
84+
uint8_t read;
85+
i2c_write_blocking(i2c_default, ADDR, &reg, 1, true); // keep control of bus
86+
i2c_read_blocking(i2c_default, ADDR, &read, 1, false);
87+
return read;
88+
}
89+
90+
void mpl3115a2_init() {
91+
// set as altimeter with oversampling ratio of 128
92+
uint8_t buf[] = { MPL3115A2_CTRLREG1, 0xB8 };
93+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
94+
95+
// set data refresh every 2 seconds, 0 next bits as we're not using those interrupts
96+
buf[0] = MPL3115A2_CTRLREG2, buf[1] = 0x00;
97+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
98+
99+
// set both interrupts pins to active low and enable internal pullups
100+
buf[0] = MPL3115A2_CTRLREG3, buf[1] = 0x01;
101+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
102+
103+
// enable FIFO interrupt
104+
buf[0] = MPL3115A2_CTRLREG4, buf[1] = 0x40;
105+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
106+
107+
// tie FIFO interrupt to pin INT1
108+
buf[0] = MPL3115A2_CTRLREG5, buf[1] = 0x40;
109+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
110+
111+
// set p, t and h offsets here if needed
112+
// eg. 2's complement number: 0xFF subtracts 1 meter
113+
//buf[0] = MPL3115A2_OFF_H, buf[1] = 0xFF;
114+
//i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
115+
116+
// do not accept more data on FIFO overflow
117+
buf[0] = MPL3115A2_F_SETUP, buf[1] = MPL3115A2_FIFO_STOP_ON_OVERFLOW;
118+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
119+
120+
// set device active
121+
buf[0] = MPL3115A2_CTRLREG1, buf[1] = 0xB9;
122+
i2c_write_blocking(i2c_default, ADDR, buf, 2, false);
123+
}
124+
125+
void gpio_callback(uint gpio, uint32_t events) {
126+
// if we had enabled more than 2 interrupts on same pin, then we should read
127+
// INT_SOURCE reg to find out which interrupt triggered
128+
129+
// we can filter by which GPIO was triggered
130+
if (gpio == INT1_PIN) {
131+
// FIFO overflow interrupt
132+
// watermark bits set to 0 in F_SETUP reg, so only possible event is an overflow
133+
// otherwise, we would read F_STATUS to confirm it was an overflow
134+
printf("FIFO overflow!\n");
135+
// drain the fifo
136+
mpl3115a2_read_fifo(fifo_data);
137+
// read status register to clear interrupt bit
138+
mpl3115a2_read_reg(MPL3115A2_F_STATUS);
139+
has_new_data = true;
140+
}
141+
}
142+
#endif
143+
144+
void mpl3115a2_convert_fifo_batch(uint8_t start, volatile uint8_t buf[], struct mpl3115a2_data_t* data) {
145+
// convert a batch of fifo data into temperature and altitude data
146+
147+
// 3 altitude registers: MSB (8 bits), CSB (8 bits) and LSB (4 bits, starting from MSB)
148+
// first two are integer bits (2's complement) and LSB is fractional bits -> makes 20 bit signed integer
149+
int32_t h = (int32_t)((uint32_t)buf[start] << 24 | buf[start + 1] << 16 | buf[start + 2] << 8);
150+
data->altitude = h * 1.f / 65536;
151+
152+
// 2 temperature registers: MSB (8 bits) and LSB (4 bits, starting from MSB)
153+
// first 8 are integer bits with sign and LSB is fractional bits -> 12 bit signed integer
154+
int16_t t = (int16_t)(((uint16_t)buf[start + 3]) << 8 | buf[start + 4]);
155+
data->temperature = t * 1.f / 256;
156+
}
157+
158+
int main() {
159+
stdio_init_all();
160+
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
161+
#warning i2c / mpl3115a2_i2c example requires a board with I2C pins
162+
puts("Default I2C pins were not defined");
163+
#else
164+
printf("Hello, MPL3115A2. Waiting for something to interrupt me!...\n");
165+
166+
// use default I2C0 at 400kHz, I2C is active low
167+
i2c_init(i2c_default, 400 * 1000);
168+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
169+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
170+
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
171+
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
172+
173+
gpio_init(INT1_PIN);
174+
gpio_pull_up(INT1_PIN); // pull it up even more!
175+
176+
// add program information for picotool
177+
bi_decl(bi_program_name("Example in the pico-examples library for the MPL3115A2 altimeter"));
178+
bi_decl(bi_1pin_with_name(16, "Interrupt pin 1"));
179+
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
180+
181+
mpl3115a2_init();
182+
183+
gpio_set_irq_enabled_with_callback(INT1_PIN, GPIO_IRQ_LEVEL_LOW, true, &gpio_callback);
184+
185+
while (1) {
186+
// as interrupt data comes in, let's print the 32 sample average
187+
if (has_new_data) {
188+
float tsum = 0, hsum = 0;
189+
struct mpl3115a2_data_t data;
190+
for (int i = 0; i < MPL3115A2_FIFO_SIZE; i++) {
191+
mpl3115a2_convert_fifo_batch(i * MPL3115A2_DATA_BATCH_SIZE, fifo_data, &data);
192+
tsum += data.temperature;
193+
hsum += data.altitude;
194+
}
195+
printf("%d sample average -> t: %.4f C, h: %.4f m\n", MPL3115A2_FIFO_SIZE, tsum / MPL3115A2_FIFO_SIZE, hsum / MPL3115A2_FIFO_SIZE);
196+
has_new_data = false;
197+
}
198+
sleep_ms(10);
199+
};
200+
201+
#endif
202+
return 0;
203+
}

i2c/mpl3115a2_i2c/mpl3115a2_i2c.fzz

66 KB
Binary file not shown.
165 KB
Loading

0 commit comments

Comments
 (0)