From dfa13f2a3922a59b7193fe427fce6e8dc92239d9 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 1 Aug 2022 16:53:44 +0100 Subject: [PATCH 1/8] MicroPython: Early wakeup GPIO latch module. --- micropython/modules/micropython-picow.cmake | 1 + micropython/modules/wakeup/micropython.cmake | 24 ++++++++++++ micropython/modules/wakeup/wakeup.c | 20 ++++++++++ micropython/modules/wakeup/wakeup.cpp | 41 ++++++++++++++++++++ micropython/modules/wakeup/wakeup.h | 4 ++ 5 files changed, 90 insertions(+) create mode 100644 micropython/modules/wakeup/micropython.cmake create mode 100644 micropython/modules/wakeup/wakeup.c create mode 100644 micropython/modules/wakeup/wakeup.cpp create mode 100644 micropython/modules/wakeup/wakeup.h diff --git a/micropython/modules/micropython-picow.cmake b/micropython/modules/micropython-picow.cmake index 14449723a..4f14d4ec4 100644 --- a/micropython/modules/micropython-picow.cmake +++ b/micropython/modules/micropython-picow.cmake @@ -43,6 +43,7 @@ include(pcf85063a/micropython) # Utility include(adcfft/micropython) +include(wakeup/micropython) # LEDs & Matrices include(plasma/micropython) diff --git a/micropython/modules/wakeup/micropython.cmake b/micropython/modules/wakeup/micropython.cmake new file mode 100644 index 000000000..4be8830f1 --- /dev/null +++ b/micropython/modules/wakeup/micropython.cmake @@ -0,0 +1,24 @@ +set(MOD_NAME wakeup) +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 +) + +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}) + +set_source_files_properties( + ${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c + PROPERTIES COMPILE_FLAGS + "-Wno-discarded-qualifiers" +) diff --git a/micropython/modules/wakeup/wakeup.c b/micropython/modules/wakeup/wakeup.c new file mode 100644 index 000000000..5c4c326b9 --- /dev/null +++ b/micropython/modules/wakeup/wakeup.c @@ -0,0 +1,20 @@ +#include "wakeup.h" + +STATIC MP_DEFINE_CONST_FUN_OBJ_0(Wakeup_get_gpio_state_obj, Wakeup_get_gpio_state); + +STATIC const mp_map_elem_t wakeup_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_wakeup) }, + { MP_ROM_QSTR(MP_QSTR_get_gpio_state), MP_ROM_PTR(&Wakeup_get_gpio_state_obj) } +}; +STATIC MP_DEFINE_CONST_DICT(mp_module_wakeup_globals, wakeup_globals_table); + +const mp_obj_module_t wakeup_user_cmodule = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_wakeup_globals, +}; + +#if MICROPY_VERSION <= 70144 +MP_REGISTER_MODULE(MP_QSTR_wakeup, wakeup_user_cmodule, MODULE_WAKEUP_ENABLED); +#else +MP_REGISTER_MODULE(MP_QSTR_wakeup, wakeup_user_cmodule); +#endif \ No newline at end of file diff --git a/micropython/modules/wakeup/wakeup.cpp b/micropython/modules/wakeup/wakeup.cpp new file mode 100644 index 000000000..6fcf4bc81 --- /dev/null +++ b/micropython/modules/wakeup/wakeup.cpp @@ -0,0 +1,41 @@ +#include "hardware/gpio.h" + +#ifndef WAKEUP_PIN_MASK +#define WAKEUP_PIN_MASK ((0b1 << 2) | (0b1 << 6)) +#endif + +#ifndef WAKEUP_PIN_DIR +#define WAKEUP_PIN_DIR ((0b1 << 2) | (0b1 << 6)) +#endif + +#ifndef WAKEUP_PIN_VALUE +#define WAKEUP_PIN_VALUE ((0b1 << 2) | (0b1 << 6)) +#endif + +namespace { + struct Wakeup { + public: + uint32_t wakeup_gpio_state; + + Wakeup() { + // Assert wakeup pins (indicator LEDs, VSYS hold etc) + gpio_init_mask(WAKEUP_PIN_MASK); + gpio_set_dir_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_DIR); + gpio_put_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_VALUE); + + // Latch GPIO state into variable + Wakeup::wakeup_gpio_state = gpio_get_all(); + } + }; + + Wakeup wakeup __attribute__ ((init_priority (101))); +}; + +extern "C" { +#include "wakeup.h" + +mp_obj_t Wakeup_get_gpio_state() { + return mp_obj_new_int(wakeup.wakeup_gpio_state); +} + +} \ No newline at end of file diff --git a/micropython/modules/wakeup/wakeup.h b/micropython/modules/wakeup/wakeup.h new file mode 100644 index 000000000..bb2a58760 --- /dev/null +++ b/micropython/modules/wakeup/wakeup.h @@ -0,0 +1,4 @@ +#include "py/runtime.h" +#include "py/objstr.h" + +extern mp_obj_t Wakeup_get_gpio_state(); \ No newline at end of file From a4b6d86ac74062110db3fb798718e4664d3f4533 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 2 Aug 2022 09:46:04 +0100 Subject: [PATCH 2/8] MicroPython: Port wakeup GPIO latch to asm. --- micropython/modules/wakeup/micropython.cmake | 1 + micropython/modules/wakeup/wakeup.S | 26 ++++++++++++++++++++ micropython/modules/wakeup/wakeup.cpp | 9 +++---- 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 micropython/modules/wakeup/wakeup.S diff --git a/micropython/modules/wakeup/micropython.cmake b/micropython/modules/wakeup/micropython.cmake index 4be8830f1..da1d8759e 100644 --- a/micropython/modules/wakeup/micropython.cmake +++ b/micropython/modules/wakeup/micropython.cmake @@ -5,6 +5,7 @@ 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}/${MOD_NAME}.S ) target_include_directories(usermod_${MOD_NAME} INTERFACE diff --git a/micropython/modules/wakeup/wakeup.S b/micropython/modules/wakeup/wakeup.S new file mode 100644 index 000000000..a7fc2c379 --- /dev/null +++ b/micropython/modules/wakeup/wakeup.S @@ -0,0 +1,26 @@ + +.syntax unified +.cpu cortex-m0plus +.thumb + +#include "pico/asm_helper.S" + +// This macro tells the pico runtime to call __wakeup_gpio_latch very early in boot +__pre_init __wakeup_gpio_latch, 00001 + +.section .data.wakeup_gpio_latch +.global wakeup_gpio_state +.align 4 +wakeup_gpio_state: +.word 0x00000000 + +.section .text +.thumb_func +__wakeup_gpio_latch: + // Read GPIO state for front buttons and store + movs r3, 0xd0 // Load 0xd0 into r3 + lsls r3, r3, 24 // Shift left 24 to get 0xd0000000 + ldr r1, [r3, 4] // Load GPIO state (0xd0000004) into r1 + ldr r2, =wakeup_gpio_state // Load output var addr into r2 + str r1, [r2] // Store r1 to r2 + bx lr // Return diff --git a/micropython/modules/wakeup/wakeup.cpp b/micropython/modules/wakeup/wakeup.cpp index 6fcf4bc81..d0be90910 100644 --- a/micropython/modules/wakeup/wakeup.cpp +++ b/micropython/modules/wakeup/wakeup.cpp @@ -12,19 +12,16 @@ #define WAKEUP_PIN_VALUE ((0b1 << 2) | (0b1 << 6)) #endif +extern uint32_t wakeup_gpio_state; + namespace { struct Wakeup { public: - uint32_t wakeup_gpio_state; - Wakeup() { // Assert wakeup pins (indicator LEDs, VSYS hold etc) gpio_init_mask(WAKEUP_PIN_MASK); gpio_set_dir_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_DIR); gpio_put_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_VALUE); - - // Latch GPIO state into variable - Wakeup::wakeup_gpio_state = gpio_get_all(); } }; @@ -35,7 +32,7 @@ extern "C" { #include "wakeup.h" mp_obj_t Wakeup_get_gpio_state() { - return mp_obj_new_int(wakeup.wakeup_gpio_state); + return mp_obj_new_int(wakeup_gpio_state); } } \ No newline at end of file From fb246441bd72e0c5ddca064dd82b3d3d80f500b5 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 2 Aug 2022 14:28:43 +0100 Subject: [PATCH 3/8] MicroPython: Add picow_enviro build. --- .github/workflows/micropython-picow.yml | 2 + .../picow_enviro/PICO_W_ENVIRO/board.json | 21 ++++ .../picow_enviro/PICO_W_ENVIRO/manifest.py | 8 ++ .../PICO_W_ENVIRO/mpconfigboard.cmake | 8 ++ .../PICO_W_ENVIRO/mpconfigboard.h | 25 +++++ .../_board/picow_enviro/pico_w_enviro.h | 102 ++++++++++++++++++ micropython/modules/micropython-picow.cmake | 1 - .../modules/micropython-picow_enviro.cmake | 59 ++++++++++ 8 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 micropython/_board/picow_enviro/PICO_W_ENVIRO/board.json create mode 100644 micropython/_board/picow_enviro/PICO_W_ENVIRO/manifest.py create mode 100644 micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.cmake create mode 100644 micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.h create mode 100644 micropython/_board/picow_enviro/pico_w_enviro.h create mode 100644 micropython/modules/micropython-picow_enviro.cmake diff --git a/.github/workflows/micropython-picow.yml b/.github/workflows/micropython-picow.yml index 067b2e10e..98888c206 100644 --- a/.github/workflows/micropython-picow.yml +++ b/.github/workflows/micropython-picow.yml @@ -68,6 +68,8 @@ jobs: include: - name: picow board: PICO_W + - name: picow_enviro + board: PICO_W_ENVIRO env: # MicroPython version will be contained in github.event.release.tag_name for releases diff --git a/micropython/_board/picow_enviro/PICO_W_ENVIRO/board.json b/micropython/_board/picow_enviro/PICO_W_ENVIRO/board.json new file mode 100644 index 000000000..755ba1037 --- /dev/null +++ b/micropython/_board/picow_enviro/PICO_W_ENVIRO/board.json @@ -0,0 +1,21 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [ + "Breadboard friendly", + "Castellated Pads", + "Micro USB", + "WiFi" + ], + "id": "rp2-pico-w", + "images": [ + "rp2-pico-w.jpg" + ], + "mcu": "rp2040", + "product": "Pico W", + "thumbnail": "", + "url": "https://www.raspberrypi.com/products/raspberry-pi-pico/", + "vendor": "Raspberry Pi" +} diff --git a/micropython/_board/picow_enviro/PICO_W_ENVIRO/manifest.py b/micropython/_board/picow_enviro/PICO_W_ENVIRO/manifest.py new file mode 100644 index 000000000..731cecb35 --- /dev/null +++ b/micropython/_board/picow_enviro/PICO_W_ENVIRO/manifest.py @@ -0,0 +1,8 @@ +include("../manifest.py") + +freeze("$(MPY_DIR)/tools", "upip.py") +freeze("$(MPY_DIR)/tools", "upip_utarfile.py") + +if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): + freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") + freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt") diff --git a/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.cmake b/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.cmake new file mode 100644 index 000000000..c4cafabf5 --- /dev/null +++ b/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.cmake @@ -0,0 +1,8 @@ +# cmake file for Raspberry Pi Pico W +set(MICROPY_BOARD PICO_W) + +set(MICROPY_PY_LWIP ON) +set(MICROPY_PY_NETWORK_CYW43 ON) + +# Board specific version of the frozen manifest +set(MICROPY_FROZEN_MANIFEST ${CMAKE_SOURCE_DIR}/boards/PICO_W_ENVIRO/manifest.py) diff --git a/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.h b/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.h new file mode 100644 index 000000000..43a9fbafa --- /dev/null +++ b/micropython/_board/picow_enviro/PICO_W_ENVIRO/mpconfigboard.h @@ -0,0 +1,25 @@ +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico W" + +// todo: We need something to check our binary size +#define MICROPY_HW_FLASH_STORAGE_BYTES (848 * 1024) + +// Enable networking. +#define MICROPY_PY_NETWORK 1 + +// CYW43 driver configuration. +#define CYW43_USE_SPI (1) +#define CYW43_LWIP (1) +#define CYW43_GPIO (1) +#define CYW43_SPI_PIO (1) + +// For debugging mbedtls - also set +// Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose +// #define MODUSSL_MBEDTLS_DEBUG_LEVEL 1 + +#define MICROPY_HW_PIN_CYW43_COUNT CYW43_WL_GPIO_COUNT +#ifdef CYW43_WL_GPIO_LED_PIN +#define MICROPY_HW_PIN_CYW43_LED_PIN_NUM CYW43_WL_GPIO_LED_PIN +#endif + +#define MICROPY_HW_PIN_RESERVED(i) ((i) == CYW43_PIN_WL_HOST_WAKE || (i) == CYW43_PIN_WL_REG_ON) diff --git a/micropython/_board/picow_enviro/pico_w_enviro.h b/micropython/_board/picow_enviro/pico_w_enviro.h new file mode 100644 index 000000000..b4a8bd7ae --- /dev/null +++ b/micropython/_board/picow_enviro/pico_w_enviro.h @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +// ----------------------------------------------------- +// NOTE: THIS HEADER IS ALSO INCLUDED BY ASSEMBLER SO +// SHOULD ONLY CONSIST OF PREPROCESSOR DIRECTIVES +// ----------------------------------------------------- + +// This header may be included by other board headers as "boards/pico.h" + +#ifndef _BOARDS_PICO_W_H +#define _BOARDS_PICO_W_H + +// For board detection +#define RASPBERRYPI_PICO_W + +// --- UART --- +#ifndef PICO_DEFAULT_UART +#define PICO_DEFAULT_UART 0 +#endif +#ifndef PICO_DEFAULT_UART_TX_PIN +#define PICO_DEFAULT_UART_TX_PIN 0 +#endif +#ifndef PICO_DEFAULT_UART_RX_PIN +#define PICO_DEFAULT_UART_RX_PIN 1 +#endif + +// --- LED --- +// no PICO_DEFAULT_LED_PIN - LED is on Wireless chip +// no PICO_DEFAULT_WS2812_PIN + +// --- I2C --- +#ifndef PICO_DEFAULT_I2C +#define PICO_DEFAULT_I2C 0 +#endif +#ifndef PICO_DEFAULT_I2C_SDA_PIN +#define PICO_DEFAULT_I2C_SDA_PIN 4 +#endif +#ifndef PICO_DEFAULT_I2C_SCL_PIN +#define PICO_DEFAULT_I2C_SCL_PIN 5 +#endif + +// --- SPI --- +#ifndef PICO_DEFAULT_SPI +#define PICO_DEFAULT_SPI 0 +#endif +#ifndef PICO_DEFAULT_SPI_SCK_PIN +#define PICO_DEFAULT_SPI_SCK_PIN 18 +#endif +#ifndef PICO_DEFAULT_SPI_TX_PIN +#define PICO_DEFAULT_SPI_TX_PIN 19 +#endif +#ifndef PICO_DEFAULT_SPI_RX_PIN +#define PICO_DEFAULT_SPI_RX_PIN 16 +#endif +#ifndef PICO_DEFAULT_SPI_CSN_PIN +#define PICO_DEFAULT_SPI_CSN_PIN 17 +#endif + +// --- FLASH --- + +#define PICO_BOOT_STAGE2_CHOOSE_W25Q080 1 + +#ifndef PICO_FLASH_SPI_CLKDIV +#define PICO_FLASH_SPI_CLKDIV 2 +#endif + +#ifndef PICO_FLASH_SIZE_BYTES +#define PICO_FLASH_SIZE_BYTES (2 * 1024 * 1024) +#endif + +// note the SMSP mode pin is on WL_GPIO1 +// #define PICO_SMPS_MODE_PIN + +#ifndef PICO_RP2040_B0_SUPPORTED +#define PICO_RP2040_B0_SUPPORTED 0 +#endif + +#ifndef PICO_RP2040_B1_SUPPORTED +#define PICO_RP2040_B1_SUPPORTED 0 +#endif + +#ifndef CYW43_PIN_WL_HOST_WAKE +#define CYW43_PIN_WL_HOST_WAKE 24 +#endif + +#ifndef CYW43_PIN_WL_REG_ON +#define CYW43_PIN_WL_REG_ON 23 +#endif + +#ifndef CYW43_WL_GPIO_COUNT +#define CYW43_WL_GPIO_COUNT 3 +#endif + +#ifndef CYW43_WL_GPIO_LED_PIN +#define CYW43_WL_GPIO_LED_PIN 0 +#endif + +#endif diff --git a/micropython/modules/micropython-picow.cmake b/micropython/modules/micropython-picow.cmake index 4f14d4ec4..14449723a 100644 --- a/micropython/modules/micropython-picow.cmake +++ b/micropython/modules/micropython-picow.cmake @@ -43,7 +43,6 @@ include(pcf85063a/micropython) # Utility include(adcfft/micropython) -include(wakeup/micropython) # LEDs & Matrices include(plasma/micropython) diff --git a/micropython/modules/micropython-picow_enviro.cmake b/micropython/modules/micropython-picow_enviro.cmake new file mode 100644 index 000000000..b3fcf252e --- /dev/null +++ b/micropython/modules/micropython-picow_enviro.cmake @@ -0,0 +1,59 @@ +include_directories(${CMAKE_CURRENT_LIST_DIR}/../../) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../") +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../") + +# Essential +include(pimoroni_i2c/micropython) +include(pimoroni_bus/micropython) + +# Pico Graphics Essential +include(hershey_fonts/micropython) +include(bitmap_fonts/micropython) +include(picographics/micropython) + +# Pico Graphics Extra +include(jpegdec/micropython) +include(qrcode/micropython/micropython) + +# Sensors & Breakouts +include(breakout_dotmatrix/micropython) +include(breakout_encoder/micropython) +include(breakout_ioexpander/micropython) +include(breakout_ltr559/micropython) +include(breakout_as7262/micropython) +include(breakout_rgbmatrix5x5/micropython) +include(breakout_matrix11x7/micropython) +include(breakout_msa301/micropython) +include(breakout_pmw3901/micropython) +include(breakout_mics6814/micropython) +include(breakout_potentiometer/micropython) +include(breakout_rtc/micropython) +include(breakout_trackball/micropython) +include(breakout_sgp30/micropython) +include(breakout_bh1745/micropython) +include(breakout_bme68x/micropython) +include(breakout_bme280/micropython) +include(breakout_bmp280/micropython) +include(breakout_icp10125/micropython) +include(breakout_scd41/micropython) +include(breakout_vl53l5cx/micropython) +include(pcf85063a/micropython) + +# Utility +include(adcfft/micropython) +include(wakeup/micropython) + +# LEDs & Matrices +include(plasma/micropython) + +# Servos & Motors +include(pwm/micropython) +include(servo/micropython) +include(encoder/micropython) +include(motor/micropython) + +# include(micropython-common) + +include(modules_py/modules_py) \ No newline at end of file From 9ec77c2f10e798013f1ac18f20c5ef3b9509aadc Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 3 Aug 2022 09:32:59 +0100 Subject: [PATCH 4/8] LTR559: Lux as float. Avoid truncating Lux to uint16_t. --- drivers/ltr559/ltr559.cpp | 2 +- drivers/ltr559/ltr559.hpp | 2 +- examples/breakout_ltr559/demo.cpp | 2 +- micropython/modules/breakout_ltr559/breakout_ltr559.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/ltr559/ltr559.cpp b/drivers/ltr559/ltr559.cpp index 796176c87..43b36a058 100644 --- a/drivers/ltr559/ltr559.cpp +++ b/drivers/ltr559/ltr559.cpp @@ -148,7 +148,7 @@ namespace pimoroni { float lux = ((int32_t)data.als0 * ch0_c[ch_idx]) - ((int32_t)data.als1 * ch1_c[ch_idx]); lux /= (float)this->data.integration_time / 100.0f; lux /= (float)this->data.gain; - data.lux = (uint16_t)(lux / 10000.0f); + data.lux = lux / 10000.0f; } return has_updated; diff --git a/drivers/ltr559/ltr559.hpp b/drivers/ltr559/ltr559.hpp index e9511d016..a79270551 100644 --- a/drivers/ltr559/ltr559.hpp +++ b/drivers/ltr559/ltr559.hpp @@ -94,7 +94,7 @@ namespace pimoroni { uint16_t integration_time; uint16_t gain; float ratio; - uint16_t lux; + float lux; } ltr559_reading; class lookup { diff --git a/examples/breakout_ltr559/demo.cpp b/examples/breakout_ltr559/demo.cpp index 276f6690d..16b418db8 100644 --- a/examples/breakout_ltr559/demo.cpp +++ b/examples/breakout_ltr559/demo.cpp @@ -25,7 +25,7 @@ int main() { while(true){ bool new_data = ltr559.get_reading(); if(new_data) { - printf("Lux: %d Prox: %d\n", ltr559.data.lux, ltr559.data.proximity); + printf("Lux: %.2f Prox: %d\n", ltr559.data.lux, ltr559.data.proximity); } sleep_ms(100); }; diff --git a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp index 8b03efe24..4d30734a8 100644 --- a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp +++ b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp @@ -72,7 +72,7 @@ mp_obj_t BreakoutLTR559_get_reading(mp_obj_t self_in) { tuple[INTEGRATION_TIME] = mp_obj_new_int(data.integration_time); tuple[GAIN] = mp_obj_new_int(data.gain); tuple[RATIO] = mp_obj_new_float(data.ratio); - tuple[LUX] = mp_obj_new_int(data.lux); + tuple[LUX] = mp_obj_new_float(data.lux); return mp_obj_new_tuple(7, tuple); } From 898e2a1b32960054620f7a78b91a664ca8eb8537 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 3 Aug 2022 10:29:43 +0100 Subject: [PATCH 5/8] Wakeup: Optionally turn off RTC CLOCK_OUT. --- micropython/modules/wakeup/wakeup.config.hpp | 36 ++++++++++++++++++++ micropython/modules/wakeup/wakeup.cpp | 32 ++++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 micropython/modules/wakeup/wakeup.config.hpp diff --git a/micropython/modules/wakeup/wakeup.config.hpp b/micropython/modules/wakeup/wakeup.config.hpp new file mode 100644 index 000000000..44f9704f4 --- /dev/null +++ b/micropython/modules/wakeup/wakeup.config.hpp @@ -0,0 +1,36 @@ +#include "hardware/i2c.h" + +// Pins to toggle on wakeup +#ifndef WAKEUP_PIN_MASK +#define WAKEUP_PIN_MASK ((0b1 << 2) | (0b1 << 6)) +#endif + +// Direction +#ifndef WAKEUP_PIN_DIR +#define WAKEUP_PIN_DIR ((0b1 << 2) | (0b1 << 6)) +#endif + +// Value +#ifndef WAKEUP_PIN_VALUE +#define WAKEUP_PIN_VALUE ((0b1 << 2) | (0b1 << 6)) +#endif + +#ifndef WAKEUP_HAS_RTC +#define WAKEUP_HAS_RTC (1) +#endif + +#ifndef WAKEUP_RTC_SDA +#define WAKEUP_RTC_SDA (4) +#endif + +#ifndef WAKEUP_RTC_SCL +#define WAKEUP_RTC_SCL (5) +#endif + +#ifndef WAKEUP_RTC_I2C_ADDR +#define WAKEUP_RTC_I2C_ADDR 0x51 +#endif + +#ifndef WAKEUP_RTC_I2C_INST +#define WAKEUP_RTC_I2C_INST i2c0 +#endif \ No newline at end of file diff --git a/micropython/modules/wakeup/wakeup.cpp b/micropython/modules/wakeup/wakeup.cpp index d0be90910..3c68cb598 100644 --- a/micropython/modules/wakeup/wakeup.cpp +++ b/micropython/modules/wakeup/wakeup.cpp @@ -1,16 +1,5 @@ #include "hardware/gpio.h" - -#ifndef WAKEUP_PIN_MASK -#define WAKEUP_PIN_MASK ((0b1 << 2) | (0b1 << 6)) -#endif - -#ifndef WAKEUP_PIN_DIR -#define WAKEUP_PIN_DIR ((0b1 << 2) | (0b1 << 6)) -#endif - -#ifndef WAKEUP_PIN_VALUE -#define WAKEUP_PIN_VALUE ((0b1 << 2) | (0b1 << 6)) -#endif +#include "wakeup.config.hpp" extern uint32_t wakeup_gpio_state; @@ -22,6 +11,25 @@ namespace { gpio_init_mask(WAKEUP_PIN_MASK); gpio_set_dir_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_DIR); gpio_put_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_VALUE); + +#if WAKEUP_HAS_RTC==1 + // Set up RTC I2C pins and send reset command + i2c_init(WAKEUP_RTC_I2C_INST, 100000); + gpio_init(WAKEUP_RTC_SDA); + gpio_init(WAKEUP_RTC_SCL); + gpio_set_function(WAKEUP_RTC_SDA, GPIO_FUNC_I2C); gpio_pull_up(WAKEUP_RTC_SDA); + gpio_set_function(WAKEUP_RTC_SCL, GPIO_FUNC_I2C); gpio_pull_up(WAKEUP_RTC_SCL); + + // Turn off CLOCK_OUT by writing 0b111 to CONTROL_2 (0x01) register + uint8_t data[] = {0x01, 0b111}; + i2c_write_blocking(WAKEUP_RTC_I2C_INST, WAKEUP_RTC_I2C_ADDR, data, 2, false); + + i2c_deinit(WAKEUP_RTC_I2C_INST); + + // Cleanup + gpio_init(WAKEUP_RTC_SDA); + gpio_init(WAKEUP_RTC_SCL); +#endif } }; From f7781e2696d3a795033e8af39d8d2cc054b4aa57 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 4 Aug 2022 12:16:08 +0100 Subject: [PATCH 6/8] Wakeup: Fixup crt0.S and runtime.c. Assert the VSYS_EN pin before copying SRAM and zeroing BSS. Saves ~142ms of startup time before GPIO input is latched and pin is asserted. --- micropython/_board/board-fixup.sh | 5 + micropython/_board/picow_enviro/fixup.sh | 6 + .../_board/picow_enviro/wakeup_gpio.patch | 136 ++++++++++++++++++ micropython/modules/wakeup/micropython.cmake | 2 +- micropython/modules/wakeup/wakeup.S | 12 +- micropython/modules/wakeup/wakeup.cpp | 10 +- 6 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 micropython/_board/picow_enviro/fixup.sh create mode 100644 micropython/_board/picow_enviro/wakeup_gpio.patch diff --git a/micropython/_board/board-fixup.sh b/micropython/_board/board-fixup.sh index 7335535f6..ad62502c4 100755 --- a/micropython/_board/board-fixup.sh +++ b/micropython/_board/board-fixup.sh @@ -13,4 +13,9 @@ fi if [[ ! -d "$MPY_DIR/boards/$BOARD" ]] && [[ -d "$FIXUP_DIR/$NAME/$BOARD/" ]]; then echo "Missing board dir. Copying: $FIXUP_DIR/$NAME/$BOARD/ to $MPY_DIR/boards/" cp -r "$FIXUP_DIR/$NAME/$BOARD/" "$MPY_DIR/boards/" +fi + +if [[ -f "$FIXUP_DIR/$NAME/fixup.sh" ]]; then + echo "Running custom fixup[.sh" + bash "$FIXUP_DIR/$NAME/fixup.sh" "$FIXUP_DIR/$NAME" "$MPY_DIR" fi \ No newline at end of file diff --git a/micropython/_board/picow_enviro/fixup.sh b/micropython/_board/picow_enviro/fixup.sh new file mode 100644 index 000000000..6acb551d8 --- /dev/null +++ b/micropython/_board/picow_enviro/fixup.sh @@ -0,0 +1,6 @@ +SRC_DIR=$1 +DST_DIR=$2 + +echo "Applying wakeup_gpio.patch" +cd "$DST_DIR/../../lib/pico-sdk" +git apply "$SRC_DIR/wakeup_gpio.patch" \ No newline at end of file diff --git a/micropython/_board/picow_enviro/wakeup_gpio.patch b/micropython/_board/picow_enviro/wakeup_gpio.patch new file mode 100644 index 000000000..4a630a65f --- /dev/null +++ b/micropython/_board/picow_enviro/wakeup_gpio.patch @@ -0,0 +1,136 @@ +diff --git a/src/rp2_common/pico_runtime/runtime.c b/src/rp2_common/pico_runtime/runtime.c +index 70dd3bb..b8c1ed0 100644 +--- a/src/rp2_common/pico_runtime/runtime.c ++++ b/src/rp2_common/pico_runtime/runtime.c +@@ -17,6 +17,7 @@ + #include "hardware/clocks.h" + #include "hardware/irq.h" + #include "hardware/resets.h" ++#include "hardware/gpio.h" + + #include "pico/mutex.h" + #include "pico/time.h" +@@ -32,6 +33,21 @@ + #include "pico/bootrom.h" + #endif + ++// Pins to toggle on wakeup ++#ifndef PICO_WAKEUP_PIN_MASK ++#define PICO_WAKEUP_PIN_MASK ((0b1 << 2) | (0b1 << 6)) ++#endif ++ ++// Direction ++#ifndef PICO_WAKEUP_PIN_DIR ++#define PICO_WAKEUP_PIN_DIR ((0b1 << 2) | (0b1 << 6)) ++#endif ++ ++// Value ++#ifndef PICO_WAKEUP_PIN_VALUE ++#define PICO_WAKEUP_PIN_VALUE ((0b1 << 2) | (0b1 << 6)) ++#endif ++ + extern char __StackLimit; /* Set by linker. */ + + uint32_t __attribute__((section(".ram_vector_table"))) ram_vector_table[48]; +@@ -61,11 +77,18 @@ void runtime_install_stack_guard(void *stack_bottom) { + | 0x10000000; // XN = disable instruction fetch; no other bits means no permissions + } + +-void runtime_init(void) { ++void runtime_user_init(void) { ++ gpio_init_mask(PICO_WAKEUP_PIN_MASK); ++ gpio_set_dir_masked(PICO_WAKEUP_PIN_MASK, PICO_WAKEUP_PIN_DIR); ++ gpio_put_masked(PICO_WAKEUP_PIN_MASK, PICO_WAKEUP_PIN_VALUE); ++} ++ ++void runtime_reset_peripherals(void) { + // Reset all peripherals to put system into a known state, + // - except for QSPI pads and the XIP IO bank, as this is fatal if running from flash + // - and the PLLs, as this is fatal if clock muxing has not been reset on this boot + // - and USB, syscfg, as this disturbs USB-to-SWD on core 1 ++ + reset_block(~( + RESETS_RESET_IO_QSPI_BITS | + RESETS_RESET_PADS_QSPI_BITS | +@@ -86,7 +109,9 @@ void runtime_init(void) { + RESETS_RESET_UART1_BITS | + RESETS_RESET_USBCTRL_BITS + )); ++} + ++void runtime_init(void) { + // pre-init runs really early since we need it even for memcpy and divide! + // (basically anything in aeabi that uses bootrom) + +diff --git a/src/rp2_common/pico_standard_link/crt0.S b/src/rp2_common/pico_standard_link/crt0.S +index b2992f6..80367da 100644 +--- a/src/rp2_common/pico_standard_link/crt0.S ++++ b/src/rp2_common/pico_standard_link/crt0.S +@@ -9,6 +9,7 @@ + #include "hardware/regs/addressmap.h" + #include "hardware/regs/sio.h" + #include "pico/binary_info/defs.h" ++#include "hardware/regs/resets.h" + + #ifdef NDEBUG + #ifndef COLLAPSE_IRQS +@@ -225,6 +226,17 @@ _reset_handler: + cmp r0, #0 + bne hold_non_core0_in_bootrom + ++ ldr r1, =runtime_reset_peripherals ++ blx r1 ++ ++ ldr r1, =runtime_user_init ++ blx r1 ++ ++ // Read GPIO state for front buttons and store ++ movs r3, 0xd0 // Load 0xd0 into r3 ++ lsls r3, r3, 24 // Shift left 24 to get 0xd0000000 ++ ldr r6, [r3, 4] // Load GPIO state (0xd0000004) into r6 ++ + // In a NO_FLASH binary, don't perform .data copy, since it's loaded + // in-place by the SRAM load. Still need to clear .bss + #if !PICO_NO_FLASH +@@ -251,6 +263,10 @@ bss_fill_test: + cmp r1, r2 + bne bss_fill_loop + ++ // runtime_wakeup_gpio_state gets zero init above ++ ldr r2, =runtime_wakeup_gpio_state // Load output var addr into r2 ++ str r6, [r2] // Store r6 to r2 ++ + platform_entry: // symbol for stack traces + // Use 32-bit jumps, in case these symbols are moved out of branch range + // (e.g. if main is in SRAM and crt0 in flash) +@@ -314,6 +330,19 @@ data_cpy_table: + runtime_init: + bx lr + ++.weak runtime_user_init ++.type runtime_user_init,%function ++.thumb_func ++runtime_user_init: ++ bx lr ++ ++.weak runtime_reset_peripherals ++.type runtime_reset_peripherals,%function ++.thumb_func ++runtime_reset_peripherals: ++ bx lr ++ ++ + // ---------------------------------------------------------------------------- + // If core 1 somehow gets into crt0 due to a spectacular VTOR mishap, we need to + // catch it and send back to the sleep-and-launch code in the bootrom. Shouldn't +@@ -345,3 +374,9 @@ __get_current_exception: + .align 2 + .equ HeapSize, PICO_HEAP_SIZE + .space HeapSize ++ ++.section .data._reset_handler ++.global runtime_wakeup_gpio_state ++.align 4 ++runtime_wakeup_gpio_state: ++.word 0x00000000 +\ No newline at end of file diff --git a/micropython/modules/wakeup/micropython.cmake b/micropython/modules/wakeup/micropython.cmake index da1d8759e..d5e781ea3 100644 --- a/micropython/modules/wakeup/micropython.cmake +++ b/micropython/modules/wakeup/micropython.cmake @@ -5,7 +5,7 @@ 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}/${MOD_NAME}.S + #${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.S ) target_include_directories(usermod_${MOD_NAME} INTERFACE diff --git a/micropython/modules/wakeup/wakeup.S b/micropython/modules/wakeup/wakeup.S index a7fc2c379..4e7620498 100644 --- a/micropython/modules/wakeup/wakeup.S +++ b/micropython/modules/wakeup/wakeup.S @@ -6,7 +6,7 @@ #include "pico/asm_helper.S" // This macro tells the pico runtime to call __wakeup_gpio_latch very early in boot -__pre_init __wakeup_gpio_latch, 00001 +__pre_init __wakeup_gpio_latch, 00000 .section .data.wakeup_gpio_latch .global wakeup_gpio_state @@ -23,4 +23,14 @@ __wakeup_gpio_latch: ldr r1, [r3, 4] // Load GPIO state (0xd0000004) into r1 ldr r2, =wakeup_gpio_state // Load output var addr into r2 str r1, [r2] // Store r1 to r2 + + // Enable 3v3 pin on the badger + ldr r1, =0x40014054 // GPIO control register 10 + movs r2, 5 // SIO function + str r2, [r1] // Set Enable 3v3 to SIO // https://github.com/raspberrypi/pico-sdk/blob/2e6142b15b8a75c1227dd3edbe839193b2bf9041/src/rp2_common/hardware_gpio/include/hardware/gpio.h#L96 + str r2, [r1, 120] // Also set LED (25) to SIO + ldr r2, =0x02000400 // Pins 25 and 10 + str r2, [r3, 36] // Enable pins out + str r2, [r3, 20] // Set pins high + bx lr // Return diff --git a/micropython/modules/wakeup/wakeup.cpp b/micropython/modules/wakeup/wakeup.cpp index 3c68cb598..86934093f 100644 --- a/micropython/modules/wakeup/wakeup.cpp +++ b/micropython/modules/wakeup/wakeup.cpp @@ -1,16 +1,16 @@ #include "hardware/gpio.h" #include "wakeup.config.hpp" -extern uint32_t wakeup_gpio_state; +extern uint32_t runtime_wakeup_gpio_state; namespace { struct Wakeup { public: Wakeup() { // Assert wakeup pins (indicator LEDs, VSYS hold etc) - gpio_init_mask(WAKEUP_PIN_MASK); - gpio_set_dir_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_DIR); - gpio_put_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_VALUE); + //gpio_init_mask(WAKEUP_PIN_MASK); + //gpio_set_dir_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_DIR); + //gpio_put_masked(WAKEUP_PIN_MASK, WAKEUP_PIN_VALUE); #if WAKEUP_HAS_RTC==1 // Set up RTC I2C pins and send reset command @@ -40,7 +40,7 @@ extern "C" { #include "wakeup.h" mp_obj_t Wakeup_get_gpio_state() { - return mp_obj_new_int(wakeup_gpio_state); + return mp_obj_new_int(runtime_wakeup_gpio_state); } } \ No newline at end of file From ac449ff640a69c40d17539ff39ee76e57a1718cf Mon Sep 17 00:00:00 2001 From: Mike Bell Date: Thu, 4 Aug 2022 15:01:15 +0100 Subject: [PATCH 7/8] Wakeup: Early ROSC to ~48MHz. Speed up MicroPython Pico W startup to init_priority(101) execution from ~160ms to ~32ms. --- .../_board/picow_enviro/wakeup_gpio.patch | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/micropython/_board/picow_enviro/wakeup_gpio.patch b/micropython/_board/picow_enviro/wakeup_gpio.patch index 4a630a65f..cb876a384 100644 --- a/micropython/_board/picow_enviro/wakeup_gpio.patch +++ b/micropython/_board/picow_enviro/wakeup_gpio.patch @@ -63,21 +63,28 @@ index 70dd3bb..b8c1ed0 100644 // (basically anything in aeabi that uses bootrom) diff --git a/src/rp2_common/pico_standard_link/crt0.S b/src/rp2_common/pico_standard_link/crt0.S -index b2992f6..80367da 100644 +index b2992f6..6091e70 100644 --- a/src/rp2_common/pico_standard_link/crt0.S +++ b/src/rp2_common/pico_standard_link/crt0.S -@@ -9,6 +9,7 @@ +@@ -9,6 +9,8 @@ #include "hardware/regs/addressmap.h" #include "hardware/regs/sio.h" #include "pico/binary_info/defs.h" +#include "hardware/regs/resets.h" ++#include "hardware/regs/rosc.h" #ifdef NDEBUG #ifndef COLLAPSE_IRQS -@@ -225,6 +226,17 @@ _reset_handler: +@@ -225,6 +227,23 @@ _reset_handler: cmp r0, #0 bne hold_non_core0_in_bootrom ++ // Increase ROSC frequency to ~48MHz (range 14.4 - 96) ++ // Startup drops from ~160ms to ~32ms on Pico W MicroPython ++ ldr r0, =(ROSC_BASE + ROSC_DIV_OFFSET) ++ ldr r1, =0xaa2 ++ str r1, [r0] ++ + ldr r1, =runtime_reset_peripherals + blx r1 + @@ -92,7 +99,7 @@ index b2992f6..80367da 100644 // In a NO_FLASH binary, don't perform .data copy, since it's loaded // in-place by the SRAM load. Still need to clear .bss #if !PICO_NO_FLASH -@@ -251,6 +263,10 @@ bss_fill_test: +@@ -251,6 +270,10 @@ bss_fill_test: cmp r1, r2 bne bss_fill_loop @@ -103,7 +110,7 @@ index b2992f6..80367da 100644 platform_entry: // symbol for stack traces // Use 32-bit jumps, in case these symbols are moved out of branch range // (e.g. if main is in SRAM and crt0 in flash) -@@ -314,6 +330,19 @@ data_cpy_table: +@@ -314,6 +337,19 @@ data_cpy_table: runtime_init: bx lr @@ -123,7 +130,7 @@ index b2992f6..80367da 100644 // ---------------------------------------------------------------------------- // If core 1 somehow gets into crt0 due to a spectacular VTOR mishap, we need to // catch it and send back to the sleep-and-launch code in the bootrom. Shouldn't -@@ -345,3 +374,9 @@ __get_current_exception: +@@ -345,3 +381,9 @@ __get_current_exception: .align 2 .equ HeapSize, PICO_HEAP_SIZE .space HeapSize From 42a29dbe7b35801e4125a56c48644a6988e1d147 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 8 Aug 2022 11:21:59 +0100 Subject: [PATCH 8/8] Wakeup: Port to Badger 2040. --- .github/workflows/micropython-badger2040.yml | 2 +- micropython/_board/badger2040/fixup.sh | 6 + .../_board/badger2040/wakeup_gpio.patch | 143 ++++++++++++++++++ micropython/modules/badger2040/badger2040.cpp | 11 +- micropython/modules/badger2040/badgerinit.S | 38 ----- .../modules/badger2040/micropython.cmake | 1 - 6 files changed, 156 insertions(+), 45 deletions(-) create mode 100644 micropython/_board/badger2040/fixup.sh create mode 100644 micropython/_board/badger2040/wakeup_gpio.patch delete mode 100644 micropython/modules/badger2040/badgerinit.S diff --git a/.github/workflows/micropython-badger2040.yml b/.github/workflows/micropython-badger2040.yml index 2144cc4a0..cdd8bfe24 100644 --- a/.github/workflows/micropython-badger2040.yml +++ b/.github/workflows/micropython-badger2040.yml @@ -7,7 +7,7 @@ on: types: [created] env: - MICROPYTHON_VERSION: v1.19 + MICROPYTHON_VERSION: 9dfabcd6d3d080aced888e8474e921f11dc979bb BOARD_TYPE: PIMORONI_BADGER2040 # MicroPython version will be contained in github.event.release.tag_name for releases RELEASE_FILE: pimoroni-badger2040-${{github.event.release.tag_name || github.sha}}-micropython diff --git a/micropython/_board/badger2040/fixup.sh b/micropython/_board/badger2040/fixup.sh new file mode 100644 index 000000000..6acb551d8 --- /dev/null +++ b/micropython/_board/badger2040/fixup.sh @@ -0,0 +1,6 @@ +SRC_DIR=$1 +DST_DIR=$2 + +echo "Applying wakeup_gpio.patch" +cd "$DST_DIR/../../lib/pico-sdk" +git apply "$SRC_DIR/wakeup_gpio.patch" \ No newline at end of file diff --git a/micropython/_board/badger2040/wakeup_gpio.patch b/micropython/_board/badger2040/wakeup_gpio.patch new file mode 100644 index 000000000..abaef7f05 --- /dev/null +++ b/micropython/_board/badger2040/wakeup_gpio.patch @@ -0,0 +1,143 @@ +diff --git a/src/rp2_common/pico_runtime/runtime.c b/src/rp2_common/pico_runtime/runtime.c +index 70dd3bb..b8c1ed0 100644 +--- a/src/rp2_common/pico_runtime/runtime.c ++++ b/src/rp2_common/pico_runtime/runtime.c +@@ -17,6 +17,7 @@ + #include "hardware/clocks.h" + #include "hardware/irq.h" + #include "hardware/resets.h" ++#include "hardware/gpio.h" + + #include "pico/mutex.h" + #include "pico/time.h" +@@ -32,6 +33,21 @@ + #include "pico/bootrom.h" + #endif + ++// Pins to toggle on wakeup ++#ifndef PICO_WAKEUP_PIN_MASK ++#define PICO_WAKEUP_PIN_MASK ((0b1 << 10) | (0b1 << 25)) ++#endif ++ ++// Direction ++#ifndef PICO_WAKEUP_PIN_DIR ++#define PICO_WAKEUP_PIN_DIR ((0b1 << 10) | (0b1 << 25)) ++#endif ++ ++// Value ++#ifndef PICO_WAKEUP_PIN_VALUE ++#define PICO_WAKEUP_PIN_VALUE ((0b1 << 10) | (0b1 << 25)) ++#endif ++ + extern char __StackLimit; /* Set by linker. */ + + uint32_t __attribute__((section(".ram_vector_table"))) ram_vector_table[48]; +@@ -61,11 +77,18 @@ void runtime_install_stack_guard(void *stack_bottom) { + | 0x10000000; // XN = disable instruction fetch; no other bits means no permissions + } + +-void runtime_init(void) { ++void runtime_user_init(void) { ++ gpio_init_mask(PICO_WAKEUP_PIN_MASK); ++ gpio_set_dir_masked(PICO_WAKEUP_PIN_MASK, PICO_WAKEUP_PIN_DIR); ++ gpio_put_masked(PICO_WAKEUP_PIN_MASK, PICO_WAKEUP_PIN_VALUE); ++} ++ ++void runtime_reset_peripherals(void) { + // Reset all peripherals to put system into a known state, + // - except for QSPI pads and the XIP IO bank, as this is fatal if running from flash + // - and the PLLs, as this is fatal if clock muxing has not been reset on this boot + // - and USB, syscfg, as this disturbs USB-to-SWD on core 1 ++ + reset_block(~( + RESETS_RESET_IO_QSPI_BITS | + RESETS_RESET_PADS_QSPI_BITS | +@@ -86,7 +109,9 @@ void runtime_init(void) { + RESETS_RESET_UART1_BITS | + RESETS_RESET_USBCTRL_BITS + )); ++} + ++void runtime_init(void) { + // pre-init runs really early since we need it even for memcpy and divide! + // (basically anything in aeabi that uses bootrom) + +diff --git a/src/rp2_common/pico_standard_link/crt0.S b/src/rp2_common/pico_standard_link/crt0.S +index b2992f6..6091e70 100644 +--- a/src/rp2_common/pico_standard_link/crt0.S ++++ b/src/rp2_common/pico_standard_link/crt0.S +@@ -9,6 +9,8 @@ + #include "hardware/regs/addressmap.h" + #include "hardware/regs/sio.h" + #include "pico/binary_info/defs.h" ++#include "hardware/regs/resets.h" ++#include "hardware/regs/rosc.h" + + #ifdef NDEBUG + #ifndef COLLAPSE_IRQS +@@ -225,6 +227,23 @@ _reset_handler: + cmp r0, #0 + bne hold_non_core0_in_bootrom + ++ // Increase ROSC frequency to ~48MHz (range 14.4 - 96) ++ // Startup drops from ~160ms to ~32ms on Pico W MicroPython ++ ldr r0, =(ROSC_BASE + ROSC_DIV_OFFSET) ++ ldr r1, =0xaa2 ++ str r1, [r0] ++ ++ ldr r1, =runtime_reset_peripherals ++ blx r1 ++ ++ ldr r1, =runtime_user_init ++ blx r1 ++ ++ // Read GPIO state for front buttons and store ++ movs r3, 0xd0 // Load 0xd0 into r3 ++ lsls r3, r3, 24 // Shift left 24 to get 0xd0000000 ++ ldr r6, [r3, 4] // Load GPIO state (0xd0000004) into r6 ++ + // In a NO_FLASH binary, don't perform .data copy, since it's loaded + // in-place by the SRAM load. Still need to clear .bss + #if !PICO_NO_FLASH +@@ -251,6 +270,10 @@ bss_fill_test: + cmp r1, r2 + bne bss_fill_loop + ++ // runtime_wakeup_gpio_state gets zero init above ++ ldr r2, =runtime_wakeup_gpio_state // Load output var addr into r2 ++ str r6, [r2] // Store r6 to r2 ++ + platform_entry: // symbol for stack traces + // Use 32-bit jumps, in case these symbols are moved out of branch range + // (e.g. if main is in SRAM and crt0 in flash) +@@ -314,6 +337,19 @@ data_cpy_table: + runtime_init: + bx lr + ++.weak runtime_user_init ++.type runtime_user_init,%function ++.thumb_func ++runtime_user_init: ++ bx lr ++ ++.weak runtime_reset_peripherals ++.type runtime_reset_peripherals,%function ++.thumb_func ++runtime_reset_peripherals: ++ bx lr ++ ++ + // ---------------------------------------------------------------------------- + // If core 1 somehow gets into crt0 due to a spectacular VTOR mishap, we need to + // catch it and send back to the sleep-and-launch code in the bootrom. Shouldn't +@@ -345,3 +381,9 @@ __get_current_exception: + .align 2 + .equ HeapSize, PICO_HEAP_SIZE + .space HeapSize ++ ++.section .data._reset_handler ++.global runtime_wakeup_gpio_state ++.align 4 ++runtime_wakeup_gpio_state: ++.word 0x00000000 +\ No newline at end of file diff --git a/micropython/modules/badger2040/badger2040.cpp b/micropython/modules/badger2040/badger2040.cpp index 55acd0a9e..b6577e457 100644 --- a/micropython/modules/badger2040/badger2040.cpp +++ b/micropython/modules/badger2040/badger2040.cpp @@ -9,20 +9,21 @@ extern "C" { #include "py/builtin.h" #include "py/mpthread.h" -extern uint32_t badger_buttons_on_wake; +extern uint32_t runtime_wakeup_gpio_state; +const uint32_t BUTTON_MASK = 0b11111 << 11; static bool _Badger2040_wake_state_any() { - return badger_buttons_on_wake > 0; + return (runtime_wakeup_gpio_state & BUTTON_MASK) > 0; } static bool _Badger2040_wake_state_get(uint32_t pin) { - return badger_buttons_on_wake & (0b1 << pin); + return runtime_wakeup_gpio_state & BUTTON_MASK & (0b1 << pin); } static bool _Badger2040_wake_state_get_once(uint32_t pin) { uint32_t mask = 0b1 << pin; - bool value = badger_buttons_on_wake & mask; - badger_buttons_on_wake &= ~mask; + bool value = runtime_wakeup_gpio_state & BUTTON_MASK & mask; + runtime_wakeup_gpio_state &= ~mask; return value; } diff --git a/micropython/modules/badger2040/badgerinit.S b/micropython/modules/badger2040/badgerinit.S deleted file mode 100644 index 82142929d..000000000 --- a/micropython/modules/badger2040/badgerinit.S +++ /dev/null @@ -1,38 +0,0 @@ - -.syntax unified -.cpu cortex-m0plus -.thumb - -#include "pico/asm_helper.S" - -// This macro tells the pico runtime to call __badger_init very early in boot -__pre_init __badger_init, 00003 - -.section .data.badger_init -.global badger_buttons_on_wake -.align 4 -badger_buttons_on_wake: -.word 0x00000000 - -.section .text -.thumb_func -__badger_init: - // Read GPIO state for front buttons and store - movs r3, 0xd0 - lsls r3, r3, 24 - ldr r1, [r3, 4] // Read all GPIOs - movs r2, 0b11111 // Mask the front buttons (pins 15-11) - lsls r2, 11 - ands r1, r1, r2 - ldr r2, =badger_buttons_on_wake - str r1, [r2] - - // Enable 3v3 pin on the badger - ldr r1, =0x40014054 // GPIO control register 10 - movs r2, 5 // SIO function - str r2, [r1] // Set Enable 3v3 to SIO - str r2, [r1, 120] // Also set LED (25) to SIO - ldr r2, =0x02000400 // Pins 25 and 10 - str r2, [r3, 36] // Enable pins out - str r2, [r3, 20] // Set pins high - bx lr diff --git a/micropython/modules/badger2040/micropython.cmake b/micropython/modules/badger2040/micropython.cmake index 2bbe2bf40..ded851991 100644 --- a/micropython/modules/badger2040/micropython.cmake +++ b/micropython/modules/badger2040/micropython.cmake @@ -5,7 +5,6 @@ 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}/badgerinit.S ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/badger2040/badger2040.cpp ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/uc8151_legacy/uc8151_legacy.cpp )