Merge pull request #183 from pimoroni/driver/icp10125

Driver for the ICP10125 pressure & temperature sensor
This commit is contained in:
Philip Howard 2021-11-09 17:02:50 +00:00 committed by GitHub
commit e249113fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 597 additions and 0 deletions

View File

@ -22,3 +22,4 @@ add_subdirectory(bme280)
add_subdirectory(button)
add_subdirectory(plasma)
add_subdirectory(rgbled)
add_subdirectory(icp10125)

View File

@ -0,0 +1 @@
include(icp10125.cmake)

View File

@ -0,0 +1,10 @@
set(DRIVER_NAME icp10125)
add_library(${DRIVER_NAME} INTERFACE)
target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp)
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_i2c pimoroni_i2c)

View File

@ -0,0 +1,180 @@
#include <cstdlib>
#include <math.h>
#include <map>
#include <vector>
#include <cstring>
#include "icp10125.hpp"
namespace pimoroni {
enum command {
SOFT_RESET = 0x805D,
READ_ID = 0xEFC8,
MOVE_ADDRESS_PTR = 0xC595,
READ_OTP = 0xC7F7
};
#pragma pack(push, 1)
struct alignas(1) uint16_result {
uint16_t data;
uint8_t crc8;
};
#pragma pack(pop)
struct conversion_constants {
float A;
float B;
float C;
};
bool ICP10125::init() {
reset();
uint8_t id = chip_id();
if(id != CHIP_ID) return false;
if(!read_otp()) return false;
return true;
}
void ICP10125::reset() {
uint16_t command = SOFT_RESET;
i2c->write_blocking(address, (uint8_t *)&command, 2, false);
sleep_ms(10); // Soft reset time is 170us but you can never be too sure...
}
ICP10125::reading ICP10125::measure(meas_command cmd) {
uint16_t command = __bswap16(cmd);
reading result = {0.0f, 0.0f, OK};
uint16_result results[3];
i2c->write_blocking(address, (uint8_t *)&command, 2, false);
switch(cmd) {
case NORMAL:
sleep_ms(7); // 5.6 - 6.3ms
break;
case LOW_POWER:
sleep_ms(2); // 1.6 - 1.8ms
break;
case LOW_NOISE:
sleep_ms(24); // 20.8 - 23.8ms
break;
case ULTRA_LOW_NOISE:
sleep_ms(95); // 83.2 - 94.5ms
break;
}
// Can probably just run this until it succeeds rather than the switch/sleep above.
// The datasheet implies polling and ignoring NACKs would work.
i2c->read_blocking(address, (uint8_t *)&results, 9, false);
if(results[0].crc8 != crc8((uint8_t *)&results[0].data, 2)) {result.status = CRC_FAIL; return result;};
if(results[1].crc8 != crc8((uint8_t *)&results[1].data, 2)) {result.status = CRC_FAIL; return result;};
if(results[2].crc8 != crc8((uint8_t *)&results[2].data, 2)) {result.status = CRC_FAIL; return result;};
int temperature = __bswap16(results[0].data);
// Due to all the byte swapping nonsense I'm not sure if I've discarded the LLSB or LMSB here...
int pressure = ((int32_t)__bswap16(results[1].data) << 8) | (__bswap16(results[2].data >> 8)); // LLSB is discarded
process_data(pressure, temperature, &result.pressure, &result.temperature);
return result;
}
int ICP10125::chip_id() {
uint16_result result;
uint16_t command = __bswap16(READ_ID);
i2c->write_blocking(address, (uint8_t *)&command, 2, false);
i2c->read_blocking(address, (uint8_t *)&result, 3, false);
if(result.crc8 != crc8((uint8_t *)&result.data, 2)) {
return -1;
}
return __bswap16(result.data) & 0x3f;
}
bool ICP10125::read_otp() {
uint16_result result[4];
uint16_t command = __bswap16(READ_OTP);
uint8_t move_address_ptr[] = {
MOVE_ADDRESS_PTR >> 8, MOVE_ADDRESS_PTR & 0xff,
0x00,
0x66,
0x9c // Address CRC8
};
i2c->write_blocking(address, move_address_ptr, sizeof(move_address_ptr), false);
for(auto x = 0u; x < 4; x++) {
i2c->write_blocking(address, (uint8_t *)&command, 2, false);
i2c->read_blocking(address, (uint8_t *)&result[x], 3, false);
if(result[x].crc8 != crc8((uint8_t *)&result[x].data, 2)) {
return false;
}
sensor_constants[x] = (float)__bswap16(result[x].data);
}
return true;
}
void ICP10125::process_data(const int p_LSB, const int T_LSB, float *pressure, float *temperature) {
float t;
float s1, s2, s3;
float in[3];
float out[3];
float A, B, C;
t = (float)(T_LSB - 32768);
s1 = LUT_lower + (float)(sensor_constants[0] * t * t) * quadr_factor;
s2 = offst_factor * sensor_constants[3] + (float)(sensor_constants[1] * t * t) * quadr_factor;
s3 = LUT_upper + (float)(sensor_constants[2] * t * t) * quadr_factor;
in[0] = s1;
in[1] = s2;
in[2] = s3;
calculate_conversion_constants(p_Pa_calib, in, out);
A = out[0];
B = out[1];
C = out[2];
*pressure = A + B / (C + p_LSB);
*temperature = -45.f + 175.f / 65536.f * T_LSB;
}
void ICP10125::calculate_conversion_constants(const float *p_Pa, const float *p_LUT, float *out) {
float A, B, C;
C = (p_LUT[0] * p_LUT[1] * (p_Pa[0] - p_Pa[1]) +
p_LUT[1] * p_LUT[2] * (p_Pa[1] - p_Pa[2]) +
p_LUT[2] * p_LUT[0] * (p_Pa[2] - p_Pa[0])) /
(p_LUT[2] * (p_Pa[0] - p_Pa[1]) +
p_LUT[0] * (p_Pa[1] - p_Pa[2]) +
p_LUT[1] * (p_Pa[2] - p_Pa[0]));
A = (p_Pa[0] * p_LUT[0] - p_Pa[1] * p_LUT[1] - (p_Pa[1] - p_Pa[0]) * C) / (p_LUT[0] - p_LUT[1]);
B = (p_Pa[0] - A) * (p_LUT[0] + C);
out[0] = A;
out[1] = B;
out[2] = C;
}
uint8_t ICP10125::crc8(uint8_t *bytes, size_t length, uint8_t polynomial) {
uint8_t result = 0xff;
for (auto byte = 0u; byte < length; byte++) {
result ^= bytes[byte];
for (auto bit = 0u; bit < 8; bit++) {
if (result & 0x80) {
result <<= 1;
result ^= polynomial;
} else {
result <<= 1;
}
}
}
return result;
}
}

View File

@ -0,0 +1,68 @@
#pragma once
#include <string>
#include "hardware/i2c.h"
#include "hardware/gpio.h"
#include "common/pimoroni_common.hpp"
#include "common/pimoroni_i2c.hpp"
namespace pimoroni {
class ICP10125 {
public:
enum meas_command {
NORMAL = 0x6825,
LOW_POWER = 0x609C,
LOW_NOISE = 0x70DF,
ULTRA_LOW_NOISE = 0x7866,
/*NORMAL_T_FIRST = 0x6825,
NORMAL_P_FIRST = 0x48A3,
LOW_POWER_T_FIRST = 0x609C,
LOW_POWER_P_FIRST = 0x401A,
LOW_NOISE_T_FIRST = 0x70DF,
LOW_NOISE_P_FIRST = 0x5059,
ULN_T_FIRST = 0x7866,
ULN_P_FIRST = 0x58E0*/
};
enum reading_status {
OK = 0,
CRC_FAIL = 1,
};
struct reading {
float temperature;
float pressure;
reading_status status;
};
static const uint8_t DEFAULT_I2C_ADDRESS = 0x63;
static const uint8_t CHIP_ID = 0x08;
ICP10125() : ICP10125(new I2C()) {};
ICP10125(I2C *i2c) : i2c(i2c) {}
bool init();
int chip_id();
void reset();
reading measure(meas_command cmd=NORMAL);
private:
I2C *i2c;
int8_t address = DEFAULT_I2C_ADDRESS;
float sensor_constants[4];
const float p_Pa_calib[3] = {45000.0f, 80000.0f, 105000.0f};
const float LUT_lower = 3.5 * (1 << 20);
const float LUT_upper = 11.5 * (1 << 20);
const float quadr_factor = 1.0 / 16777216.0;
const float offst_factor = 2048.0;
bool read_otp();
void process_data(const int p_LSB, const int T_LSB, float *pressure, float *temperature);
void calculate_conversion_constants(const float *p_Pa, const float *p_LUT, float *out);
uint8_t crc8(uint8_t *bytes, size_t length, uint8_t polynomial = 0x31);
};
}

View File

@ -19,6 +19,7 @@ add_subdirectory(breakout_bmp280)
add_subdirectory(breakout_bme280)
add_subdirectory(breakout_as7262)
add_subdirectory(breakout_bh1745)
add_subdirectory(breakout_icp10125)
add_subdirectory(pico_display)
add_subdirectory(pico_display_2)

View File

@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/basic_demo.cmake")

View File

@ -0,0 +1,16 @@
set(OUTPUT_NAME icp10125_basic_demo)
add_executable(
${OUTPUT_NAME}
basic_demo.cpp
)
# enable usb output, disable uart output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
pico_enable_stdio_uart(${OUTPUT_NAME} 1)
# Pull in pico libraries that we need
target_link_libraries(${OUTPUT_NAME} pico_stdlib icp10125)
# create map/bin/hex file etc.
pico_add_extra_outputs(${OUTPUT_NAME})

View File

@ -0,0 +1,24 @@
#include "pico/stdlib.h"
#include "common/pimoroni_common.hpp"
#include "icp10125.hpp"
using namespace pimoroni;
I2C i2c(BOARD::BREAKOUT_GARDEN);
ICP10125 icp10125(&i2c);
int main() {
stdio_init_all();
icp10125.init();
printf("init()\n");
while(true) {
auto result = icp10125.measure(ICP10125::NORMAL);
printf("%fc %fPa %d\n", result.temperature, result.pressure, result.status);
sleep_ms(500);
}
return 0;
}

View File

@ -0,0 +1,14 @@
import time
import breakout_icp10125
import pimoroni_i2c
i2c = pimoroni_i2c.PimoroniI2C(4, 5)
icp10125 = breakout_icp10125.BreakoutICP10125(i2c)
while True:
t, p, status = icp10125.measure(icp10125.NORMAL)
if status == icp10125.STATUS_OK:
print(t, p)
time.sleep(1.0)

View File

@ -0,0 +1,89 @@
# ICP1025 High-accuracy Barometric Pressure & Temperature Sensor <!-- omit in toc -->
The ICP1025 library is intended to drive the TDK InvenSense ICP10125 temperature and pressure sensor.
- [Getting Started](#getting-started)
- [Taking A Measurement](#taking-a-measurement)
- [Measurement Types](#measurement-types)
- [Normal](#normal)
- [Low Power](#low-power)
- [Low Noise](#low-noise)
- [Ultra Low Noise](#ultra-low-noise)
## Getting Started
Construct a new PimoroniI2C instance for your specific board. Breakout Garden uses pins 4 & 5 and Pico Explorer uses pins 20 & 21.
```python
import breakout_icp10125
import pimoroni_i2c
i2c = pimoroni_i2c.PimoroniI2C(4, 5)
icp10125 = breakout_icp10125.BreakoutICP10125(i2c)
```
## Taking A Measurement
The `measure` method triggers a measurement, blocks for enough time for the measurement to complete, and returns the result as a tuple with three values:
1. Temperature (degrees C)
2. Pressure (Pa)
3. Status
If the `status` is `icp10125.STATUS_OK` then the reading is valid. Otherwise you should probably discard it or attempt another reading.
For example, the following code will continuously poll the ICP10125 for `NORMAL` readings and print them out if they are valid:
```python
import breakout_icp10125
import pimoroni_i2c
i2c = pimoroni_i2c.PimoroniI2C(4, 5)
icp10125 = breakout_icp10125.BreakoutICP10125(i2c)
while True:
t, p, status = icp10125.measure()
if status == icp10125.STATUS_OK:
print(t, p)
time.sleep(1.0)
```
## Measurement Types
The ICP1025 has eight measurement commands. Four are supported by this library since the remaining four are identical save for the order of Temperature/Pressure data readout being reversed.
Each measurement type has a fixed duration.
### Normal
Normal measurements are the default type, and take 7ms to complete offering a good balance of stability and speed.
```python
result = icp10125.measure(icp10125.NORMAL)
```
### Low Power
Low-power measurements take just 2ms and trade stability for speed/power efficiency.
```python
result = icp10125.measure(icp10125.LOW_POWER)
```
### Low Noise
Low-noise measurements take 24ms (roughly 3.5x as long as normal) and trade speed for stability.
```python
result = icp10125.measure(icp10125.LOW_NOISE)
```
### Ultra Low Noise
Ultra Low-noise measurements take 95ms
```python
result = icp10125.measure(icp10125.ULTRA_LOW_NOISE)
```

View File

@ -0,0 +1,54 @@
#include "breakout_icp10125.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// BreakoutICP10125 Class
////////////////////////////////////////////////////////////////////////////////////////////////////
/***** Methods *****/
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutICP10125_measure_obj, 1, BreakoutICP10125_measure);
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutICP10125_soft_reset_obj, BreakoutICP10125_soft_reset);
/***** Binding of Methods *****/
STATIC const mp_rom_map_elem_t BreakoutICP10125_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_measure), MP_ROM_PTR(&BreakoutICP10125_measure_obj) },
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&BreakoutICP10125_soft_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_NORMAL), MP_ROM_INT(NORMAL) },
{ MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(LOW_POWER) },
{ MP_ROM_QSTR(MP_QSTR_LOW_NOISE), MP_ROM_INT(LOW_NOISE) },
{ MP_ROM_QSTR(MP_QSTR_ULTRA_LOW_NOISE), MP_ROM_INT(ULTRA_LOW_NOISE) },
{ MP_ROM_QSTR(MP_QSTR_STATUS_OK), MP_ROM_INT(OK) },
{ MP_ROM_QSTR(MP_QSTR_STATUS_CRC_FAIL), MP_ROM_INT(CRC_FAIL) },
};
STATIC MP_DEFINE_CONST_DICT(BreakoutICP10125_locals_dict, BreakoutICP10125_locals_dict_table);
/***** Class Definition *****/
const mp_obj_type_t breakout_icp10125_BreakoutICP10125_type = {
{ &mp_type_type },
.name = MP_QSTR_breakout_matrix11x7,
.print = BreakoutICP10125_print,
.make_new = BreakoutICP10125_make_new,
.locals_dict = (mp_obj_dict_t*)&BreakoutICP10125_locals_dict,
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// breakout_icp10125 Module
////////////////////////////////////////////////////////////////////////////////////////////////////
/***** Globals Table *****/
STATIC const mp_map_elem_t breakout_icp10125_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_breakout_icp10125) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutICP10125), (mp_obj_t)&breakout_icp10125_BreakoutICP10125_type },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_icp10125_globals, breakout_icp10125_globals_table);
/***** Module Definition *****/
const mp_obj_module_t breakout_icp10125_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_breakout_icp10125_globals,
};
////////////////////////////////////////////////////////////////////////////////////////////////////
MP_REGISTER_MODULE(MP_QSTR_breakout_icp10125, breakout_icp10125_user_cmodule, MODULE_BREAKOUT_SGP30_ENABLED);
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,95 @@
#include "drivers/icp10125/icp10125.hpp"
#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o))
using namespace pimoroni;
extern "C" {
#include "breakout_icp10125.h"
#include "pimoroni_i2c.h"
/***** I2C Struct *****/
typedef struct _PimoroniI2C_obj_t {
mp_obj_base_t base;
I2C *i2c;
} _PimoroniI2C_obj_t;
/***** Variables Struct *****/
typedef struct _breakout_icp10125_BreakoutICP10125_obj_t {
mp_obj_base_t base;
ICP10125 *breakout;
} breakout_icp10125_BreakoutICP10125_obj_t;
/***** Print *****/
void BreakoutICP10125_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind; //Unused input parameter
(void)self_in;
// breakout_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_icp10125_BreakoutICP10125_obj_t);
// ICP10125* breakout = self->breakout;
// TODO put something useful here? There's no point printing I2C info since that's handled by the I2C object now
mp_print_str(print, "BreakoutICP10125()");
}
/***** Constructor *****/
mp_obj_t BreakoutICP10125_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_icp10125_BreakoutICP10125_obj_t *self = nullptr;
enum { ARG_i2c };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) {
mp_raise_ValueError(MP_ERROR_TEXT("BreakoutICP10125: Bad i2C object"));
return mp_const_none;
}
_PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj);
self = m_new_obj(breakout_icp10125_BreakoutICP10125_obj_t);
self->base.type = &breakout_icp10125_BreakoutICP10125_type;
self->breakout = new ICP10125(i2c->i2c);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "BreakoutICP10125: breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}
/***** Methods *****/
mp_obj_t BreakoutICP10125_measure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_command };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_command, MP_ARG_INT, {.u_int = NORMAL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
breakout_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_icp10125_BreakoutICP10125_obj_t);
auto reading = self->breakout->measure((ICP10125::meas_command)args[ARG_command].u_int);
mp_obj_t tuple[3];
tuple[0] = mp_obj_new_float(reading.temperature);
tuple[1] = mp_obj_new_float(reading.pressure);
tuple[2] = mp_obj_new_int(reading.status);
return mp_obj_new_tuple(3, tuple);
}
mp_obj_t BreakoutICP10125_soft_reset(mp_obj_t self_in) {
breakout_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_icp10125_BreakoutICP10125_obj_t);
self->breakout->reset();
return mp_const_none;
}
}

View File

@ -0,0 +1,23 @@
// Include MicroPython API.
#include "py/runtime.h"
/***** Extern of Class Definition *****/
extern const mp_obj_type_t breakout_icp10125_BreakoutICP10125_type;
enum meas_command {
NORMAL = 0x6825,
LOW_POWER = 0x609C,
LOW_NOISE = 0x70DF,
ULTRA_LOW_NOISE = 0x7866,
};
enum reading_status {
OK = 0,
CRC_FAIL = 1,
};
/***** Extern of Class Methods *****/
extern void BreakoutICP10125_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
extern mp_obj_t BreakoutICP10125_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
extern mp_obj_t BreakoutICP10125_soft_reset(mp_obj_t self_in);
extern mp_obj_t BreakoutICP10125_measure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);

View File

@ -0,0 +1,19 @@
set(MOD_NAME breakout_icp10125)
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
add_library(usermod_${MOD_NAME} INTERFACE)
target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/icp10125/icp10125.cpp
)
target_include_directories(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
target_compile_definitions(usermod_${MOD_NAME} INTERFACE
-DMODULE_${MOD_NAME_UPPER}_ENABLED=1
)
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})

View File

@ -27,6 +27,7 @@ include(breakout_bh1745/micropython)
include(breakout_bme68x/micropython)
include(breakout_bme280/micropython)
include(breakout_bmp280/micropython)
include(breakout_icp10125/micropython)
include(pico_scroll/micropython)
include(pico_rgb_keypad/micropython)