|
| 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, ®, 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, ®, 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 | +} |
0 commit comments