From aeceefea59ad38e1b188b450c891dc90fcc12473 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 17 May 2022 12:24:50 +0100 Subject: [PATCH] VL53L5CX: Sideload firmware from user-supplied .bin --- drivers/vl53l5cx/platform.h | 1 + drivers/vl53l5cx/src | 2 +- drivers/vl53l5cx/vl53l5cx.hpp | 5 +++-- examples/breakout_vl53l5cx/vl53l5cx_demo.cpp | 5 +++-- .../examples/breakout_vl53l5cx/vl53l5cx_demo.py | 2 +- .../examples/breakout_vl53l5cx/vl53l5cx_motion.py | 2 +- .../breakout_vl53l5cx/vl53l5cx_object_tracking.py | 2 +- micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp | 14 +++++++++++--- micropython/modules/micropython.cmake | 2 +- 9 files changed, 23 insertions(+), 12 deletions(-) diff --git a/drivers/vl53l5cx/platform.h b/drivers/vl53l5cx/platform.h index 9a4548e9..16f492a2 100644 --- a/drivers/vl53l5cx/platform.h +++ b/drivers/vl53l5cx/platform.h @@ -84,6 +84,7 @@ typedef struct /* Example for most standard platform : I2C address of sensor */ uint16_t address; i2c_inst_t *i2c; + uint8_t *firmware; } VL53L5CX_Platform; diff --git a/drivers/vl53l5cx/src b/drivers/vl53l5cx/src index 834fa4e5..80de451b 160000 --- a/drivers/vl53l5cx/src +++ b/drivers/vl53l5cx/src @@ -1 +1 @@ -Subproject commit 834fa4e53119b987ae9357afc9dfacc6ef8261f8 +Subproject commit 80de451b3c50ef998e8a4a745e9fe8ef29314650 diff --git a/drivers/vl53l5cx/vl53l5cx.hpp b/drivers/vl53l5cx/vl53l5cx.hpp index 9198c58f..daa02bed 100644 --- a/drivers/vl53l5cx/vl53l5cx.hpp +++ b/drivers/vl53l5cx/vl53l5cx.hpp @@ -33,11 +33,12 @@ namespace pimoroni { // 7-bit version of the default address (0x52) static const uint8_t DEFAULT_ADDRESS = VL53L5CX_DEFAULT_I2C_ADDRESS >> 1; - VL53L5CX(I2C *i2c, uint8_t i2c_addr=DEFAULT_ADDRESS) { + VL53L5CX(I2C *i2c, uint8_t *firmware, uint8_t i2c_addr=DEFAULT_ADDRESS) { configuration = new VL53L5CX_Configuration{ .platform = VL53L5CX_Platform{ .address = i2c_addr, - .i2c = i2c->get_i2c() + .i2c = i2c->get_i2c(), + .firmware = firmware }, }; motion_configuration = new VL53L5CX_Motion_Configuration{}; diff --git a/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp b/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp index c0eb829d..acb7e66c 100644 --- a/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp +++ b/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp @@ -1,14 +1,15 @@ #include #include "pico/stdlib.h" #include "hardware/i2c.h" -#include "drivers/vl53l5cx/vl53l5cx.hpp" +#include "vl53l5cx.hpp" +#include "src/vl53l5cx_firmware.h" #include "common/pimoroni_i2c.hpp" using namespace pimoroni; I2C i2c(4, 5); -VL53L5CX vl53l5cx(&i2c); +VL53L5CX vl53l5cx(&i2c, (uint8_t *)&vl53l5cx_firmware_bin); int main() { stdio_init_all(); diff --git a/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py b/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py index ff0f1edd..616fed42 100644 --- a/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py +++ b/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py @@ -11,7 +11,7 @@ i2c = pimoroni_i2c.PimoroniI2C(**PINS_BREAKOUT_GARDEN, baudrate=2_000_000) print("Starting up sensor...") t_sta = time.ticks_ms() -sensor = breakout_vl53l5cx.VL53L5CX(i2c) +sensor = breakout_vl53l5cx.VL53L5CX(i2c, firmware=open("vl53l5cx_firmware.bin").read()) t_end = time.ticks_ms() print("Done in {}ms...".format(t_end - t_sta)) diff --git a/micropython/examples/breakout_vl53l5cx/vl53l5cx_motion.py b/micropython/examples/breakout_vl53l5cx/vl53l5cx_motion.py index 10ffec38..42da1fab 100644 --- a/micropython/examples/breakout_vl53l5cx/vl53l5cx_motion.py +++ b/micropython/examples/breakout_vl53l5cx/vl53l5cx_motion.py @@ -12,7 +12,7 @@ i2c = pimoroni_i2c.PimoroniI2C(**PINS_BREAKOUT_GARDEN, baudrate=2_000_000) print("Starting up sensor...") t_sta = time.ticks_ms() -sensor = breakout_vl53l5cx.VL53L5CX(i2c) +sensor = breakout_vl53l5cx.VL53L5CX(i2c, firmware=open("vl53l5cx_firmware.bin").read()) t_end = time.ticks_ms() print("Done in {}ms...".format(t_end - t_sta)) diff --git a/micropython/examples/breakout_vl53l5cx/vl53l5cx_object_tracking.py b/micropython/examples/breakout_vl53l5cx/vl53l5cx_object_tracking.py index 8f05d28a..3d4361a2 100644 --- a/micropython/examples/breakout_vl53l5cx/vl53l5cx_object_tracking.py +++ b/micropython/examples/breakout_vl53l5cx/vl53l5cx_object_tracking.py @@ -24,7 +24,7 @@ i2c = pimoroni_i2c.PimoroniI2C(**PINS_BREAKOUT_GARDEN, baudrate=2_000_000) print("Starting up sensor...") t_sta = time.ticks_ms() -sensor = breakout_vl53l5cx.VL53L5CX(i2c) +sensor = breakout_vl53l5cx.VL53L5CX(i2c, firmware=open("vl53l5cx_firmware.bin").read()) t_end = time.ticks_ms() print("Done in {}ms...".format(t_end - t_sta)) diff --git a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp index 662a982f..c37d231f 100644 --- a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp +++ b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp @@ -61,11 +61,13 @@ mp_obj_t VL53L5CX_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw enum { ARG_i2c, - ARG_addr + ARG_addr, + ARG_firmware, }; static const mp_arg_t allowed_args[] = { { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, - { MP_QSTR_addr, MP_ARG_INT, {.u_int = pimoroni::VL53L5CX::DEFAULT_ADDRESS} } + { MP_QSTR_addr, MP_ARG_INT, {.u_int = pimoroni::VL53L5CX::DEFAULT_ADDRESS} }, + { MP_QSTR_firmware, MP_ARG_OBJ | MP_ARG_REQUIRED } }; // Parse args. @@ -77,13 +79,19 @@ mp_obj_t VL53L5CX_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw return mp_const_none; } + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_firmware].u_obj, &bufinfo, MP_BUFFER_READ); + if(bufinfo.len != (size_t)(84 * 1024)) { // firmware blob is always 84K + mp_raise_ValueError("Supplied firmware should be 84k bytes!"); + } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); int addr = args[ARG_addr].u_int; self = m_new_obj_with_finaliser(_VL53L5CX_obj_t); self->base.type = &VL53L5CX_type; self->i2c = i2c; - self->breakout = new pimoroni::VL53L5CX(i2c->i2c, addr); + self->breakout = new pimoroni::VL53L5CX(i2c->i2c, (uint8_t *)bufinfo.buf, addr); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "VL53L5CX: error initialising"); diff --git a/micropython/modules/micropython.cmake b/micropython/modules/micropython.cmake index dc6461e8..49089b9e 100644 --- a/micropython/modules/micropython.cmake +++ b/micropython/modules/micropython.cmake @@ -29,7 +29,7 @@ include(breakout_bme280/micropython) include(breakout_bmp280/micropython) include(breakout_icp10125/micropython) include(breakout_scd41/micropython) -# include(breakout_vl53l5cx/micropython) +include(breakout_vl53l5cx/micropython) include(pico_scroll/micropython) include(pico_rgb_keypad/micropython)