From 7f486314db9a0a8dbb77974f7c75b6a987d367dc Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 22 Jul 2021 18:36:33 +0100 Subject: [PATCH 1/2] Driver for ICP10125 temperature/pressure sensor --- drivers/CMakeLists.txt | 1 + drivers/icp10125/CMakeLists.txt | 1 + drivers/icp10125/icp10125.cmake | 10 ++ drivers/icp10125/icp10125.cpp | 179 ++++++++++++++++++++ drivers/icp10125/icp10125.hpp | 69 ++++++++ examples/CMakeLists.txt | 1 + examples/breakout_icp10125/CMakeLists.txt | 1 + examples/breakout_icp10125/basic_demo.cmake | 16 ++ examples/breakout_icp10125/basic_demo.cpp | 24 +++ 9 files changed, 302 insertions(+) create mode 100644 drivers/icp10125/CMakeLists.txt create mode 100644 drivers/icp10125/icp10125.cmake create mode 100644 drivers/icp10125/icp10125.cpp create mode 100644 drivers/icp10125/icp10125.hpp create mode 100644 examples/breakout_icp10125/CMakeLists.txt create mode 100644 examples/breakout_icp10125/basic_demo.cmake create mode 100644 examples/breakout_icp10125/basic_demo.cpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 948b56ed..55e6b4d0 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -21,3 +21,4 @@ add_subdirectory(bme280) add_subdirectory(button) add_subdirectory(plasma) add_subdirectory(rgbled) +add_subdirectory(icp10125) diff --git a/drivers/icp10125/CMakeLists.txt b/drivers/icp10125/CMakeLists.txt new file mode 100644 index 00000000..0153926c --- /dev/null +++ b/drivers/icp10125/CMakeLists.txt @@ -0,0 +1 @@ +include(icp10125.cmake) \ No newline at end of file diff --git a/drivers/icp10125/icp10125.cmake b/drivers/icp10125/icp10125.cmake new file mode 100644 index 00000000..b3e0bfcc --- /dev/null +++ b/drivers/icp10125/icp10125.cmake @@ -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) diff --git a/drivers/icp10125/icp10125.cpp b/drivers/icp10125/icp10125.cpp new file mode 100644 index 00000000..68e9a087 --- /dev/null +++ b/drivers/icp10125/icp10125.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include +#include + +#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); + 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; + } + +} \ No newline at end of file diff --git a/drivers/icp10125/icp10125.hpp b/drivers/icp10125/icp10125.hpp new file mode 100644 index 00000000..4bd0cee7 --- /dev/null +++ b/drivers/icp10125/icp10125.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include + +#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(); + bool read_otp(); + 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; + + void reset(); + 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); + }; + +} \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d514d21d..52afb30e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -18,6 +18,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_unicorn) diff --git a/examples/breakout_icp10125/CMakeLists.txt b/examples/breakout_icp10125/CMakeLists.txt new file mode 100644 index 00000000..88e0f0e4 --- /dev/null +++ b/examples/breakout_icp10125/CMakeLists.txt @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/basic_demo.cmake") \ No newline at end of file diff --git a/examples/breakout_icp10125/basic_demo.cmake b/examples/breakout_icp10125/basic_demo.cmake new file mode 100644 index 00000000..c772a442 --- /dev/null +++ b/examples/breakout_icp10125/basic_demo.cmake @@ -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}) diff --git a/examples/breakout_icp10125/basic_demo.cpp b/examples/breakout_icp10125/basic_demo.cpp new file mode 100644 index 00000000..07aa9b14 --- /dev/null +++ b/examples/breakout_icp10125/basic_demo.cpp @@ -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; +} From 759868e787fcec143d7096c0af504983436ea15e Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 23 Jul 2021 12:18:17 +0100 Subject: [PATCH 2/2] Python bindings & docs for ICP10125 --- drivers/icp10125/icp10125.cpp | 1 + drivers/icp10125/icp10125.hpp | 5 +- .../examples/breakout_icp10125/normal_demo.py | 14 +++ .../modules/breakout_icp10125/README.md | 89 +++++++++++++++++ .../breakout_icp10125/breakout_icp10125.c | 54 +++++++++++ .../breakout_icp10125/breakout_icp10125.cpp | 95 +++++++++++++++++++ .../breakout_icp10125/breakout_icp10125.h | 23 +++++ .../breakout_icp10125/micropython.cmake | 19 ++++ micropython/modules/micropython.cmake | 1 + 9 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 micropython/examples/breakout_icp10125/normal_demo.py create mode 100644 micropython/modules/breakout_icp10125/README.md create mode 100644 micropython/modules/breakout_icp10125/breakout_icp10125.c create mode 100644 micropython/modules/breakout_icp10125/breakout_icp10125.cpp create mode 100644 micropython/modules/breakout_icp10125/breakout_icp10125.h create mode 100644 micropython/modules/breakout_icp10125/micropython.cmake diff --git a/drivers/icp10125/icp10125.cpp b/drivers/icp10125/icp10125.cpp index 68e9a087..5a2cf9a3 100644 --- a/drivers/icp10125/icp10125.cpp +++ b/drivers/icp10125/icp10125.cpp @@ -75,6 +75,7 @@ namespace pimoroni { 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); diff --git a/drivers/icp10125/icp10125.hpp b/drivers/icp10125/icp10125.hpp index 4bd0cee7..ea90b31d 100644 --- a/drivers/icp10125/icp10125.hpp +++ b/drivers/icp10125/icp10125.hpp @@ -46,7 +46,7 @@ namespace pimoroni { bool init(); int chip_id(); - bool read_otp(); + void reset(); reading measure(meas_command cmd=NORMAL); private: @@ -59,8 +59,7 @@ namespace pimoroni { const float LUT_upper = 11.5 * (1 << 20); const float quadr_factor = 1.0 / 16777216.0; const float offst_factor = 2048.0; - - void reset(); + 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); diff --git a/micropython/examples/breakout_icp10125/normal_demo.py b/micropython/examples/breakout_icp10125/normal_demo.py new file mode 100644 index 00000000..96adc50e --- /dev/null +++ b/micropython/examples/breakout_icp10125/normal_demo.py @@ -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) diff --git a/micropython/modules/breakout_icp10125/README.md b/micropython/modules/breakout_icp10125/README.md new file mode 100644 index 00000000..804bbeef --- /dev/null +++ b/micropython/modules/breakout_icp10125/README.md @@ -0,0 +1,89 @@ +# ICP1025 High-accuracy Barometric Pressure & Temperature Sensor + +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) +``` diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.c b/micropython/modules/breakout_icp10125/breakout_icp10125.c new file mode 100644 index 00000000..6c254ffe --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.c @@ -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); +//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.cpp b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp new file mode 100644 index 00000000..c02ad8ab --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp @@ -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; +} + +} \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.h b/micropython/modules/breakout_icp10125/breakout_icp10125.h new file mode 100644 index 00000000..607dfed6 --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.h @@ -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); \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/micropython.cmake b/micropython/modules/breakout_icp10125/micropython.cmake new file mode 100644 index 00000000..3897a67b --- /dev/null +++ b/micropython/modules/breakout_icp10125/micropython.cmake @@ -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}) \ No newline at end of file diff --git a/micropython/modules/micropython.cmake b/micropython/modules/micropython.cmake index 30044027..4ddde4cc 100644 --- a/micropython/modules/micropython.cmake +++ b/micropython/modules/micropython.cmake @@ -25,6 +25,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)