From e963cff72de0efbd4e9c0b177caab88363f67d2e Mon Sep 17 00:00:00 2001 From: DavePutz Date: Mon, 13 Jul 2020 22:27:06 -0500 Subject: [PATCH 01/19] Issue #2949 Run background checks during long multiplications --- py/mpz.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/py/mpz.c b/py/mpz.c index 7800cdcc45..8b8541c737 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -443,8 +443,11 @@ STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t * *id++ = carry; } - ilen = id - oidig; + ilen = id - oidig; + // check to prevent usb starvation + RUN_BACKGROUND_TASKS; } + return ilen; } From 30361f6f2a9cea4ae2aeb6dc41f5872d1f6fadee Mon Sep 17 00:00:00 2001 From: DavePutz Date: Mon, 13 Jul 2020 22:39:30 -0500 Subject: [PATCH 02/19] Fix formatting --- py/mpz.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/py/mpz.c b/py/mpz.c index 8b8541c737..05827d1045 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -443,12 +443,11 @@ STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t * *id++ = carry; } - ilen = id - oidig; - // check to prevent usb starvation - RUN_BACKGROUND_TASKS; + ilen = id - oidig; + // check to prevent usb starvation + RUN_BACKGROUND_TASKS; } - return ilen; } From 14799f9ee6e634d73463a9ed401b5cc9a3219749 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Mon, 13 Jul 2020 22:43:46 -0500 Subject: [PATCH 03/19] more formatting fix --- py/mpz.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/mpz.c b/py/mpz.c index 05827d1045..4ef00f73c9 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -447,7 +447,7 @@ STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t * // check to prevent usb starvation RUN_BACKGROUND_TASKS; } - + return ilen; } From e5f7adcf5d1257cefd33831d541b829ca8267aca Mon Sep 17 00:00:00 2001 From: DavePutz Date: Mon, 13 Jul 2020 22:54:52 -0500 Subject: [PATCH 04/19] Fix to pass mpy-cross build --- py/mpz.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/py/mpz.c b/py/mpz.c index 4ef00f73c9..41b46f92ff 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -445,7 +445,9 @@ STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t * ilen = id - oidig; // check to prevent usb starvation + #ifdef RUN_BACKGROUND_TASKS RUN_BACKGROUND_TASKS; + #endif } return ilen; From 1160635608ebeaf3868a327803c0ca248f549123 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 16 Jul 2020 19:02:26 -0700 Subject: [PATCH 05/19] Enable PYSTACK to keep function state out of the heap --- main.c | 8 ++++++++ py/circuitpy_mpconfig.h | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/main.c b/main.c index f928d0d62f..f1dfa63c6f 100755 --- a/main.c +++ b/main.c @@ -97,6 +97,10 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { } } +#if MICROPY_ENABLE_PYSTACK +static size_t PLACE_IN_DTCM_BSS(_pystack)[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]; +#endif + void start_mp(supervisor_allocation* heap) { reset_status_led(); autoreload_stop(); @@ -125,6 +129,10 @@ void start_mp(supervisor_allocation* heap) { // Clear the readline history. It references the heap we're about to destroy. readline_init0(); + #if MICROPY_ENABLE_PYSTACK + mp_pystack_init(_pystack, _pystack + sizeof(_pystack)); + #endif + #if MICROPY_ENABLE_GC gc_init(heap->ptr, heap->ptr + heap->length / 4); #endif diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index d05a246fce..a7853fabda 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -119,6 +119,7 @@ #define MICROPY_QSTR_BYTES_IN_HASH (1) #define MICROPY_REPL_AUTO_INDENT (1) #define MICROPY_REPL_EVENT_DRIVEN (0) +#define MICROPY_ENABLE_PYSTACK (1) #define MICROPY_STACK_CHECK (1) #define MICROPY_STREAMS_NON_BLOCK (1) #ifndef MICROPY_USE_INTERNAL_PRINTF @@ -785,6 +786,10 @@ void supervisor_run_background_tasks_if_tick(void); #define CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS 1000 #endif +#ifndef CIRCUITPY_PYSTACK_SIZE +#define CIRCUITPY_PYSTACK_SIZE 1024 +#endif + #define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" #define CIRCUITPY_VERBOSE_BLE 0 From 610e0171aac69f7cd7b7c46eb776a3624ce224a3 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 17 Jul 2020 17:43:02 -0700 Subject: [PATCH 06/19] Disable existing native networking. We're moving towards a co-processor model and a Wiznet library is already available. New native APIs will replace these for chips with networking like the ESP32S2 but they won't be these. --- .../aloriumtech_evo_m51/mpconfigboard.mk | 2 -- .../circuitbrains_deluxe_m4/mpconfigboard.mk | 2 -- .../boards/kicksat-sprite/mpconfigboard.mk | 1 - .../boards/pewpew_m4/mpconfigboard.mk | 18 +++++++----------- .../mpconfigboard.mk | 2 -- .../mpconfigboard.mk | 1 - .../boards/winterbloom_sol/mpconfigboard.mk | 1 - ports/atmel-samd/mpconfigport.mk | 3 +-- .../nrf/boards/particle_argon/mpconfigboard.mk | 4 ---- .../nrf/boards/particle_boron/mpconfigboard.mk | 4 ---- .../nrf/boards/particle_xenon/mpconfigboard.mk | 4 ---- shared-bindings/network/__init__.c | 3 +++ shared-bindings/socket/__init__.c | 3 +++ shared-bindings/wiznet/__init__.c | 7 ++++++- 14 files changed, 20 insertions(+), 35 deletions(-) diff --git a/ports/atmel-samd/boards/aloriumtech_evo_m51/mpconfigboard.mk b/ports/atmel-samd/boards/aloriumtech_evo_m51/mpconfigboard.mk index 28b1434fb7..34301da3f0 100644 --- a/ports/atmel-samd/boards/aloriumtech_evo_m51/mpconfigboard.mk +++ b/ports/atmel-samd/boards/aloriumtech_evo_m51/mpconfigboard.mk @@ -13,6 +13,4 @@ EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = GD25Q16C LONGINT_IMPL = MPZ -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 diff --git a/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk b/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk index 7a9e5a7808..13ae59de46 100755 --- a/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk @@ -11,6 +11,4 @@ EXTERNAL_FLASH_DEVICE_COUNT = 2 EXTERNAL_FLASH_DEVICES = "W25Q64JV_IQ, S25FL064L" LONGINT_IMPL = MPZ -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 diff --git a/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk b/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk index febd2a6c36..9c7fe3398a 100644 --- a/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk +++ b/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk @@ -13,7 +13,6 @@ LONGINT_IMPL = MPZ CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_FRAMEBUFFERIO = 0 CIRCUITPY_DISPLAYIO = 0 -CIRCUITPY_NETWORK = 0 CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_PS2IO = 0 CIRCUITPY_AUDIOMP3 = 0 diff --git a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk index b6490433e5..c322897628 100644 --- a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk @@ -14,34 +14,30 @@ CIRCUITPY_FULL_BUILD = 0 # when frozen module gets smaller. CIRCUITPY_ANALOGIO = 0 CIRCUITPY_AUDIOBUSIO = 0 +CIRCUITPY_AUDIOPWMIO = 0 +CIRCUITPY_AUDIOMP3 = 0 CIRCUITPY_BITBANGIO = 0 +CIRCUITPY_BITBANG_APA102 = 0 CIRCUITPY_FREQUENCYIO = 0 +CIRCUITPY_GAMEPADSHIFT = 0 CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NEOPIXEL_WRITE = 0 -CIRCUITPY_NETWORK = 0 CIRCUITPY_PIXELBUF = 0 CIRCUITPY_PS2IO = 0 +CIRCUITPY_ROTARYIO = 0 CIRCUITPY_RTC = 0 +CIRCUITPY_SAMD = 0 CIRCUITPY_TOUCHIO = 0 CIRCUITPY_USB_HID = 0 CIRCUITPY_USB_MIDI = 0 -CIRCUITPY_AUDIOPWMIO = 0 -CIRCUITPY_AUDIOMP3 = 0 -CIRCUITPY_BITBANG_APA102 = 0 -CIRCUITPY_BLEIO = 0 -CIRCUITPY_GAMEPADSHIFT = 0 -CIRCUITPY_NETWORK = 0 -CIRCUITPY_ROTARYIO = 0 -CIRCUITPY_SAMD = 0 -CIRCUITPY_TOUCHIO = 0 CIRCUITPY_VECTORIO = 0 CIRCUITPY_AUDIOMIXER = 1 CIRCUITPY_AUDIOIO = 1 CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 -CIRCUITPY_STAGE = 1 CIRCUITPY_MATH = 1 +CIRCUITPY_STAGE = 1 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pewpew_m4 CIRCUITPY_DISPLAY_FONT = $(TOP)/ports/atmel-samd/boards/ugame10/brutalist-6.bdf diff --git a/ports/atmel-samd/boards/sparkfun_samd51_thing_plus/mpconfigboard.mk b/ports/atmel-samd/boards/sparkfun_samd51_thing_plus/mpconfigboard.mk index 13863e565f..567f1db205 100644 --- a/ports/atmel-samd/boards/sparkfun_samd51_thing_plus/mpconfigboard.mk +++ b/ports/atmel-samd/boards/sparkfun_samd51_thing_plus/mpconfigboard.mk @@ -12,6 +12,4 @@ EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = AT25SF041A LONGINT_IMPL = MPZ -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 diff --git a/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk b/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk index e06a2af7ee..7ab0ccfc9d 100644 --- a/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk +++ b/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk @@ -24,7 +24,6 @@ CIRCUITPY_BLEIO = 0 CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_GAMEPAD = 0 CIRCUITPY_I2CPERIPHERAL = 0 -CIRCUITPY_NETWORK = 0 CIRCUITPY_TOUCHIO = 0 CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_PS2IO = 0 diff --git a/ports/atmel-samd/boards/winterbloom_sol/mpconfigboard.mk b/ports/atmel-samd/boards/winterbloom_sol/mpconfigboard.mk index 11592073f9..9217cdf23d 100644 --- a/ports/atmel-samd/boards/winterbloom_sol/mpconfigboard.mk +++ b/ports/atmel-samd/boards/winterbloom_sol/mpconfigboard.mk @@ -23,7 +23,6 @@ CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_FRAMEBUFFERIO = 0 CIRCUITPY_GAMEPAD = 0 CIRCUITPY_I2CPERIPHERAL = 0 -CIRCUITPY_NETWORK = 0 CIRCUITPY_TOUCHIO = 0 CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_PS2IO = 0 diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index face79fad7..e4cf9ca833 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -54,8 +54,7 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0 # The ifndef's allow overriding in mpconfigboard.mk. ifndef CIRCUITPY_NETWORK -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 +CIRCUITPY_NETWORK = 0 endif ifndef CIRCUITPY_PS2IO diff --git a/ports/nrf/boards/particle_argon/mpconfigboard.mk b/ports/nrf/boards/particle_argon/mpconfigboard.mk index 2ca08b9829..f8d3d2aca2 100644 --- a/ports/nrf/boards/particle_argon/mpconfigboard.mk +++ b/ports/nrf/boards/particle_argon/mpconfigboard.mk @@ -8,7 +8,3 @@ MCU_CHIP = nrf52840 QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = "MX25L3233F" - -# Support for the Ethernet FeatherWing -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 diff --git a/ports/nrf/boards/particle_boron/mpconfigboard.mk b/ports/nrf/boards/particle_boron/mpconfigboard.mk index f03f43a7bc..eada97a730 100644 --- a/ports/nrf/boards/particle_boron/mpconfigboard.mk +++ b/ports/nrf/boards/particle_boron/mpconfigboard.mk @@ -8,7 +8,3 @@ MCU_CHIP = nrf52840 QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = "MX25L3233F" - -# Support for the Ethernet FeatherWing -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 diff --git a/ports/nrf/boards/particle_xenon/mpconfigboard.mk b/ports/nrf/boards/particle_xenon/mpconfigboard.mk index 44309bbca2..6062da378f 100644 --- a/ports/nrf/boards/particle_xenon/mpconfigboard.mk +++ b/ports/nrf/boards/particle_xenon/mpconfigboard.mk @@ -8,7 +8,3 @@ MCU_CHIP = nrf52840 QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = "MX25L3233F" - -# Support for the Ethernet FeatherWing -CIRCUITPY_NETWORK = 1 -MICROPY_PY_WIZNET5K = 5500 diff --git a/shared-bindings/network/__init__.c b/shared-bindings/network/__init__.c index 58aa13473e..4bd543a0a5 100644 --- a/shared-bindings/network/__init__.c +++ b/shared-bindings/network/__init__.c @@ -40,6 +40,9 @@ //| """Network Interface Management //| +//| .. warning:: This module is disabled in 6.x and will removed in 7.x. Please use networking +//| libraries instead. +//| //| This module provides a registry of configured NICs. //| It is used by the 'socket' module to look up a suitable //| NIC when a socket is created.""" diff --git a/shared-bindings/socket/__init__.c b/shared-bindings/socket/__init__.c index 0ded0218bb..2485d8ef8c 100644 --- a/shared-bindings/socket/__init__.c +++ b/shared-bindings/socket/__init__.c @@ -39,6 +39,9 @@ //| """TCP, UDP and RAW socket support //| +//| .. warning:: This module is disabled in 6.x and will removed in 7.x. Please use networking +//| libraries instead. (Native networking will provide a socket compatible class.) +//| //| Create TCP, UDP and RAW sockets for communicating over the Internet.""" //| diff --git a/shared-bindings/wiznet/__init__.c b/shared-bindings/wiznet/__init__.c index bc7ff150fe..8df06b5467 100644 --- a/shared-bindings/wiznet/__init__.c +++ b/shared-bindings/wiznet/__init__.c @@ -35,7 +35,12 @@ #include "shared-module/network/__init__.h" -//| """Support for WizNet hardware, including the WizNet 5500 Ethernet adaptor.""" +//| """Support for WizNet hardware, including the WizNet 5500 Ethernet adaptor. +//| +//| +//| .. warning:: This module is disabled in 6.x and will removed in 7.x. Please use networking +//| libraries instead. +//| """ //| extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k; From 8d692f33a8b9638e37ba6e311432eeb96c0ec825 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 18 Jul 2020 19:30:40 +0200 Subject: [PATCH 07/19] pewpew_m4: Enable analogio and usb_hid The newest version for the Stage library for PewPewM4 no longer contains embedded graphics, which frees enough space in flash to enabled back AnalogIO and also add USB_HID. There is still ~192 bytes left free. If new additions to CircuitPython make it grow further, we can disable USB_HID again. --- frozen/circuitpython-stage | 2 +- ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/frozen/circuitpython-stage b/frozen/circuitpython-stage index 0d2c083a2f..9596a5904e 160000 --- a/frozen/circuitpython-stage +++ b/frozen/circuitpython-stage @@ -1 +1 @@ -Subproject commit 0d2c083a2fb57a1562d4806775f45273abbfbfae +Subproject commit 9596a5904ed757e6fbffcf03e7aa77ae9ecf5223 diff --git a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk index b6490433e5..b1a8f7c72f 100644 --- a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk @@ -10,20 +10,15 @@ INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 -# TODO: Turn off analogio for now for space reasons, but restore it -# when frozen module gets smaller. -CIRCUITPY_ANALOGIO = 0 CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_BITBANGIO = 0 CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NEOPIXEL_WRITE = 0 -CIRCUITPY_NETWORK = 0 CIRCUITPY_PIXELBUF = 0 CIRCUITPY_PS2IO = 0 CIRCUITPY_RTC = 0 CIRCUITPY_TOUCHIO = 0 -CIRCUITPY_USB_HID = 0 CIRCUITPY_USB_MIDI = 0 CIRCUITPY_AUDIOPWMIO = 0 CIRCUITPY_AUDIOMP3 = 0 @@ -33,7 +28,6 @@ CIRCUITPY_GAMEPADSHIFT = 0 CIRCUITPY_NETWORK = 0 CIRCUITPY_ROTARYIO = 0 CIRCUITPY_SAMD = 0 -CIRCUITPY_TOUCHIO = 0 CIRCUITPY_VECTORIO = 0 CIRCUITPY_AUDIOMIXER = 1 @@ -42,6 +36,8 @@ CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 CIRCUITPY_STAGE = 1 CIRCUITPY_MATH = 1 +CIRCUITPY_ANALOGIO = 1 +CIRCUITPY_USB_HID = 1 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pewpew_m4 CIRCUITPY_DISPLAY_FONT = $(TOP)/ports/atmel-samd/boards/ugame10/brutalist-6.bdf From 11ef43b071f275ee470d1339cc7a2392281b9528 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 18 Jul 2020 20:09:30 +0200 Subject: [PATCH 08/19] pewpew_m4: Disable USB_HID back German translation fails to build, so I have to disabled USB_HID after all. --- ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk index b1a8f7c72f..8a67f127fb 100644 --- a/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk @@ -19,6 +19,7 @@ CIRCUITPY_PIXELBUF = 0 CIRCUITPY_PS2IO = 0 CIRCUITPY_RTC = 0 CIRCUITPY_TOUCHIO = 0 +CIRCUITPY_USB_HID = 0 CIRCUITPY_USB_MIDI = 0 CIRCUITPY_AUDIOPWMIO = 0 CIRCUITPY_AUDIOMP3 = 0 @@ -37,7 +38,6 @@ CIRCUITPY_GAMEPAD = 1 CIRCUITPY_STAGE = 1 CIRCUITPY_MATH = 1 CIRCUITPY_ANALOGIO = 1 -CIRCUITPY_USB_HID = 1 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pewpew_m4 CIRCUITPY_DISPLAY_FONT = $(TOP)/ports/atmel-samd/boards/ugame10/brutalist-6.bdf From 7ba89f5330fb2a7d48efb56f1b5920e8dfffedef Mon Sep 17 00:00:00 2001 From: Thea Flowers Date: Sat, 18 Jul 2020 22:33:06 -0700 Subject: [PATCH 09/19] Add _bhb user module for Big Honking Button BHB needs better accuracy from the ADC readings. To avoid changing the ADC configuration for all boards or adding complexity to AnalogIn, I implemented a custom user module to allow the BHB to talk to the ADC in the way that it needs to. I'm open to other approaches here, but this seemed like the least invasive and complex option. --- .../mpconfigboard.mk | 4 + .../usermods/_bhb/bhb.c | 120 ++++++++++++++++++ .../usermods/_bhb/micropython.mk | 6 + 3 files changed, 130 insertions(+) create mode 100644 ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/bhb.c create mode 100644 ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/micropython.mk diff --git a/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk b/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk index 466e345911..35a72819b8 100644 --- a/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk +++ b/ports/atmel-samd/boards/winterbloom_big_honking_button/mpconfigboard.mk @@ -32,3 +32,7 @@ CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_PS2IO = 0 CIRCUITPY_USB_HID = 0 CIRCUITPY_RTC = 0 + +# Enable board-specific modules +USER_C_MODULES = boards/winterbloom_big_honking_button/usermods +CFLAGS += -DMODULE_BHB_ENABLED=1 diff --git a/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/bhb.c b/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/bhb.c new file mode 100644 index 0000000000..54ce173d81 --- /dev/null +++ b/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/bhb.c @@ -0,0 +1,120 @@ +#include "py/obj.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "samd/pins.h" +#include "sam.h" + +STATIC mp_obj_t _bhb_read_adc(void); + +STATIC mp_obj_t _bhb_init_adc(void) { + claim_pin(&pin_PB08); + common_hal_never_reset_pin(&pin_PB08); + + /* Enable the APB clock for the ADC. */ + PM->APBCMASK.reg |= PM_APBCMASK_ADC; + + /* Enable GCLK0 for the ADC */ + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | + GCLK_CLKCTRL_GEN_GCLK0 | + GCLK_CLKCTRL_ID_ADC; + + /* Wait for bus synchronization. */ + while (GCLK->STATUS.bit.SYNCBUSY) {}; + + uint32_t bias = (*((uint32_t *) ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; + uint32_t linearity = (*((uint32_t *) ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; + linearity |= ((*((uint32_t *) ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5; + + /* Wait for bus synchronization. */ + while (ADC->STATUS.bit.SYNCBUSY) {}; + + /* Write the calibration data. */ + ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity); + + /* Use the internal VCC reference. This is 1/2 of what's on VCCA. + since VCCA is 3.3v, this is 1.65v. + */ + ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INTVCC1; + + /* Capture 64 samples. */ + ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_64 | ADC_AVGCTRL_ADJRES(4); + + /* Set the clock prescaler to 32, which is the same as the CircuitPython default. + Set the resolution to 16 for averaging + */ + ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV32 | + ADC_CTRLB_RESSEL_16BIT; + + /* Configure the input parameters. + + - GAIN_DIV2 means that the input voltage is halved. This is important + because the voltage reference is 1/2 of VCCA. So if you want to + measure 0-3.3v, you need to halve the input as well. + + - MUXNEG_GND means that the ADC should compare the input value to GND. + + - MUXPOS_PIN3 means that the ADC should read from AIN2, or PB08. + */ + ADC->INPUTCTRL.reg = ADC_INPUTCTRL_GAIN_DIV2 | + ADC_INPUTCTRL_MUXNEG_GND | + ADC_INPUTCTRL_MUXPOS_PIN2; + + + /* Set PB08 as an input pin. */ + PORT->Group[1].DIRCLR.reg = PORT_PB08; + + /* Enable the peripheral multiplexer for PB08. */ + PORT->Group[1].PINCFG[8].reg |= PORT_PINCFG_PMUXEN; + + /* Set PB08 to function B which is analog input. */ + PORT->Group[1].PMUX[4].reg |= PORT_PMUX_PMUXE_B; + + /* Wait for bus synchronization. */ + while (ADC->STATUS.bit.SYNCBUSY) {}; + + /* Enable the ADC. */ + ADC->CTRLA.bit.ENABLE = true; + + /* Make one read and throw it away, as per the datasheet. */ + _bhb_read_adc(); + + return mp_const_none; +} + +STATIC mp_obj_t _bhb_read_adc(void) { + /* Wait for bus synchronization. */ + while (ADC->STATUS.bit.SYNCBUSY) {}; + + /* Start the ADC using a software trigger. */ + ADC->SWTRIG.bit.START = true; + + /* Wait for the result ready flag to be set. */ + while (ADC->INTFLAG.bit.RESRDY == 0); + + /* Clear the flag. */ + ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY; + + /* Read the value. */ + uint32_t result = ADC->RESULT.reg; + + return MP_OBJ_NEW_SMALL_INT(result); +} + + +STATIC MP_DEFINE_CONST_FUN_OBJ_0(_bhb_init_adc_obj, _bhb_init_adc); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(_bhb_read_adc_obj, _bhb_read_adc); + +STATIC const mp_rom_map_elem_t _bhb_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bhb) }, + { MP_ROM_QSTR(MP_QSTR_init_adc), MP_ROM_PTR(&_bhb_init_adc_obj) }, + { MP_ROM_QSTR(MP_QSTR_read_adc), MP_ROM_PTR(&_bhb_read_adc_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(_bhb_module_globals, _bhb_module_globals_table); + +const mp_obj_module_t _bhb_user_cmodule = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&_bhb_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR__bhb, _bhb_user_cmodule, MODULE_BHB_ENABLED); diff --git a/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/micropython.mk b/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/micropython.mk new file mode 100644 index 0000000000..9374ec34ea --- /dev/null +++ b/ports/atmel-samd/boards/winterbloom_big_honking_button/usermods/_bhb/micropython.mk @@ -0,0 +1,6 @@ +USERMODULES_DIR := $(USERMOD_DIR) + +# Add all C files to SRC_USERMOD. +SRC_USERMOD += $(USERMODULES_DIR)/bhb.c + +CFLAGS_USERMOD += -I$(USERMODULES_DIR) From efeae0d84fa1745595d26e66590c1944269b6aae Mon Sep 17 00:00:00 2001 From: Kenny <3454741+WarriorOfWire@users.noreply.github.com> Date: Sun, 19 Jul 2020 12:27:35 -0700 Subject: [PATCH 10/19] fix 3169: Polygon.points property The getter for vectorio.Polygon#points was not updated with the data type change of the stored points list. This moves the implementation to shared_module and updates the data type to reflect the actual state. --- shared-bindings/vectorio/Polygon.c | 15 +-------------- shared-module/vectorio/Polygon.c | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/shared-bindings/vectorio/Polygon.c b/shared-bindings/vectorio/Polygon.c index 3443d9e426..1aca4611ea 100644 --- a/shared-bindings/vectorio/Polygon.c +++ b/shared-bindings/vectorio/Polygon.c @@ -49,20 +49,7 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar //| STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) { vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t list = mp_obj_new_list(0, NULL); - - size_t len = 0; - mp_obj_t *items; - mp_obj_list_get(common_hal_vectorio_polygon_get_points(self), &len, &items); - - for (size_t i = 0; i < len; i += 2) { - mp_obj_t tuple[] = { items[i], items[i+1] }; - mp_obj_list_append( - list, - mp_obj_new_tuple(2, tuple) - ); - } - return list; + return common_hal_vectorio_polygon_get_points(self); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_points_obj, vectorio_polygon_obj_get_points); diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c index 0025d4bfc4..aeeaf02240 100644 --- a/shared-module/vectorio/Polygon.c +++ b/shared-module/vectorio/Polygon.c @@ -20,7 +20,7 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple size_t len = 0; mp_obj_t *items; mp_obj_list_get(points_tuple_list, &len, &items); - VECTORIO_POLYGON_DEBUG("polygon_points_list len: %d\n", len); + VECTORIO_POLYGON_DEBUG(" self.len: %d, len: %d, ", self->len, len); if ( len < 3 ) { mp_raise_TypeError_varg(translate("Polygon needs at least 3 points")); @@ -28,9 +28,11 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple if ( self->len < 2*len ) { if ( self->points_list != NULL ) { + VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list)); gc_free( self->points_list ); } self->points_list = gc_alloc( 2 * len * sizeof(int), false, false ); + VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(int)); } self->len = 2*len; @@ -56,22 +58,35 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) { - VECTORIO_POLYGON_DEBUG("%p polygon_construct\n", self); + VECTORIO_POLYGON_DEBUG("%p polygon_construct: ", self); self->points_list = NULL; self->len = 0; self->on_dirty.obj = NULL; _clobber_points_list( self, points_list ); + VECTORIO_POLYGON_DEBUG("\n"); } mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) { - return self->points_list; + VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_get_points {len: %d, points_list: %p}\n", self, self->len, self->points_list); + mp_obj_t list = mp_obj_new_list(0, NULL); + + for (size_t i = 0; i < self->len; i += 2) { + mp_obj_t tuple[] = { mp_obj_new_int(self->points_list[i]), mp_obj_new_int(self->points_list[i+1]) }; + mp_obj_list_append( + list, + mp_obj_new_tuple(2, tuple) + ); + } + return list; } void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) { + VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_set_points: ", self); _clobber_points_list( self, points_list ); if (self->on_dirty.obj != NULL) { self->on_dirty.event(self->on_dirty.obj); } + VECTORIO_POLYGON_DEBUG("\n"); } void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) { From db43c56f794508689b07d931c16cadd3e98aad87 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 20 Jul 2020 08:44:39 -0500 Subject: [PATCH 11/19] background callbacks: Clear any callbacks that were queued Before this, a background callback that was on the list when background_callback_reset was called could have ended up in a state that made it "un-queueable": its "prev" pointer could have been non-NULL. --- supervisor/shared/background_callback.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index d10579c4f9..8e12dd3625 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include + #include "py/gc.h" #include "py/mpconfig.h" #include "supervisor/background_callback.h" @@ -101,6 +103,12 @@ void background_callback_end_critical_section() { void background_callback_reset() { CALLBACK_CRITICAL_BEGIN; + background_callback_t *cb = (background_callback_t*)callback_head; + while(cb) { + background_callback_t *next = cb->next; + memset(cb, 0, sizeof(*cb)); + cb = next; + } callback_head = NULL; callback_tail = NULL; in_background_callback = false; From 9fd10322fedc4a7736118fdb6cb1480ad8da94f1 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 20 Jul 2020 08:45:31 -0500 Subject: [PATCH 12/19] supervisor: rename some locals for clarity It's perfectly OK for these variables with static linkage to have the same name, but it's inconvenient for humans like me. --- supervisor/shared/tick.c | 4 ++-- supervisor/shared/usb/usb.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index bc270030f3..4af59f78e3 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -66,7 +66,7 @@ static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks); -static background_callback_t callback; +static background_callback_t tick_callback; volatile uint64_t last_finished_tick = 0; @@ -119,7 +119,7 @@ void supervisor_tick(void) { #endif } #endif - background_callback_add(&callback, supervisor_background_tasks, NULL); + background_callback_add(&tick_callback, supervisor_background_tasks, NULL); } uint64_t supervisor_ticks_ms64() { diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index 472be96d52..36b5ec05d6 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -64,8 +64,8 @@ void usb_init(void) { tusb_init(); #if MICROPY_KBD_EXCEPTION - // Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received - // This callback always got invoked regardless of mp_interrupt_char value since we only set it once here + // Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() usb_callback will be invoked when Ctrl+C is received + // This usb_callback always got invoked regardless of mp_interrupt_char value since we only set it once here tud_cdc_set_wanted_char(CHAR_CTRL_C); #endif @@ -83,14 +83,14 @@ void usb_background(void) { } } -static background_callback_t callback; +static background_callback_t usb_callback; static void usb_background_do(void* unused) { usb_background(); } void usb_irq_handler(void) { tud_int_handler(0); - background_callback_add(&callback, usb_background_do, NULL); + background_callback_add(&usb_callback, usb_background_do, NULL); } //--------------------------------------------------------------------+ From c243c13f026a4bd915f241e5fff5400aabea11ad Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 20 Jul 2020 08:52:35 -0500 Subject: [PATCH 13/19] framebufferio: Handle auto refresh flag at reset if we don't set the flag via accessor fn the tick enable might become wrong --- shared-module/displayio/__init__.c | 4 +--- shared-module/framebufferio/FramebufferDisplay.c | 5 +++++ shared-module/framebufferio/FramebufferDisplay.h | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index c898bbb98f..efc28470fb 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -190,9 +190,7 @@ void reset_displays(void) { common_hal_displayio_epaperdisplay_show(display, NULL); #if CIRCUITPY_FRAMEBUFFERIO } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { - framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display; - display->auto_refresh = true; - common_hal_framebufferio_framebufferdisplay_show(display, NULL); + framebufferio_framebufferdisplay_reset(&displays[i].framebuffer_display); #endif } } diff --git a/shared-module/framebufferio/FramebufferDisplay.c b/shared-module/framebufferio/FramebufferDisplay.c index 7d09e0baeb..2a90fa0d4a 100644 --- a/shared-module/framebufferio/FramebufferDisplay.c +++ b/shared-module/framebufferio/FramebufferDisplay.c @@ -318,3 +318,8 @@ void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisp gc_collect_ptr(self->framebuffer); displayio_display_core_collect_ptrs(&self->core); } + +void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t* self) { + common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, true); + common_hal_framebufferio_framebufferdisplay_show(self, NULL); +} diff --git a/shared-module/framebufferio/FramebufferDisplay.h b/shared-module/framebufferio/FramebufferDisplay.h index 1b68d2ab02..ca1ab984a3 100644 --- a/shared-module/framebufferio/FramebufferDisplay.h +++ b/shared-module/framebufferio/FramebufferDisplay.h @@ -55,6 +55,7 @@ typedef struct { void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self); void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self); void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self); +void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t* self); void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t* self); From e2d252a6d5f4b2425215e0a79f78b8cbfa657f9a Mon Sep 17 00:00:00 2001 From: Kenny <3454741+WarriorOfWire@users.noreply.github.com> Date: Mon, 20 Jul 2020 10:12:29 -0700 Subject: [PATCH 14/19] pre-allocate list of known size --- shared-module/vectorio/Polygon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c index aeeaf02240..c44c13fd73 100644 --- a/shared-module/vectorio/Polygon.c +++ b/shared-module/vectorio/Polygon.c @@ -69,7 +69,7 @@ void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t po mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) { VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_get_points {len: %d, points_list: %p}\n", self, self->len, self->points_list); - mp_obj_t list = mp_obj_new_list(0, NULL); + mp_obj_t list = mp_obj_new_list(self->len/2, NULL); for (size_t i = 0; i < self->len; i += 2) { mp_obj_t tuple[] = { mp_obj_new_int(self->points_list[i]), mp_obj_new_int(self->points_list[i+1]) }; From 389c81341dc7267a7f51c468d5e90602b76569ab Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 20 Jul 2020 18:16:20 -0700 Subject: [PATCH 15/19] Tweak declaration for boards with TCM --- main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index f1dfa63c6f..a571c547ab 100755 --- a/main.c +++ b/main.c @@ -98,7 +98,7 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { } #if MICROPY_ENABLE_PYSTACK -static size_t PLACE_IN_DTCM_BSS(_pystack)[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]; +static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]); #endif void start_mp(supervisor_allocation* heap) { @@ -130,7 +130,7 @@ void start_mp(supervisor_allocation* heap) { readline_init0(); #if MICROPY_ENABLE_PYSTACK - mp_pystack_init(_pystack, _pystack + sizeof(_pystack)); + mp_pystack_init(_pystack, _pystack + (sizeof(_pystack) / sizeof(size_t))); #endif #if MICROPY_ENABLE_GC From e977b427aa6783858ecccd4a69e17d08f039122c Mon Sep 17 00:00:00 2001 From: Kenny <3454741+WarriorOfWire@users.noreply.github.com> Date: Mon, 20 Jul 2020 20:25:31 -0700 Subject: [PATCH 16/19] vectorio: fix VectorShape non-transposed pixel placement Fixes https://github.com/adafruit/circuitpython/issues/3170 The absolute transform needs to be subtracted in all cases, not only when the coordinate system is transposed. --- shared-module/vectorio/VectorShape.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index bfe8a7b259..81f46b3fa0 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -223,8 +223,8 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ pixel_to_get_x = (input_pixel.y - self->absolute_transform->dy * self->x - self->absolute_transform->y) / self->absolute_transform->dy; pixel_to_get_y = (input_pixel.x - self->absolute_transform->dx * self->y - self->absolute_transform->x) / self->absolute_transform->dx; } else { - pixel_to_get_x = (input_pixel.x - self->absolute_transform->dx * self->x) / self->absolute_transform->dx; - pixel_to_get_y = (input_pixel.y - self->absolute_transform->dy * self->y) / self->absolute_transform->dy; + pixel_to_get_x = (input_pixel.x - self->absolute_transform->dx * self->x - self->absolute_transform->x) / self->absolute_transform->dx; + pixel_to_get_y = (input_pixel.y - self->absolute_transform->dy * self->y - self->absolute_transform->y) / self->absolute_transform->dy; } VECTORIO_SHAPE_PIXEL_DEBUG(" get_pixel %p (%3d, %3d) -> ( %3d, %3d )", self->ishape.shape, input_pixel.x, input_pixel.y, pixel_to_get_x, pixel_to_get_y); #ifdef VECTORIO_PERF From a29105fefd4fdf791376edf2df1a5d8b4c976974 Mon Sep 17 00:00:00 2001 From: Taku Fukada Date: Wed, 22 Jul 2020 00:37:22 +0900 Subject: [PATCH 17/19] Improve .pyi generation --- .github/workflows/build.yml | 2 +- tools/extract_pyi.py | 142 ++++++++++++++++++++++++++++-------- 2 files changed, 112 insertions(+), 32 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d5ccfb71c8..d809ba8cb6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,7 +37,7 @@ jobs: run: | sudo apt-get install -y eatmydata sudo eatmydata apt-get install -y gettext librsvg2-bin mingw-w64 - pip install requests sh click setuptools cpp-coveralls "Sphinx<4" sphinx-rtd-theme recommonmark sphinx-autoapi sphinxcontrib-svg2pdfconverter polib pyyaml astroid + pip install requests sh click setuptools cpp-coveralls "Sphinx<4" sphinx-rtd-theme recommonmark sphinx-autoapi sphinxcontrib-svg2pdfconverter polib pyyaml astroid isort - name: Versions run: | gcc --version diff --git a/tools/extract_pyi.py b/tools/extract_pyi.py index b9366f6bab..e5da610352 100644 --- a/tools/extract_pyi.py +++ b/tools/extract_pyi.py @@ -2,24 +2,91 @@ # # SPDX-License-Identifier: MIT +import ast import os +import re import sys -import astroid import traceback -top_level = sys.argv[1].strip("/") -stub_directory = sys.argv[2] +import isort + + +IMPORTS_IGNORE = frozenset({'int', 'float', 'bool', 'str', 'bytes', 'tuple', 'list', 'set', 'dict', 'bytearray', 'file', 'buffer'}) +IMPORTS_TYPING = frozenset({'Any', 'Optional', 'Union', 'Tuple', 'List', 'Sequence'}) +IMPORTS_TYPESHED = frozenset({'ReadableBuffer', 'WritableBuffer'}) + + +def is_any(node): + node_type = type(node) + if node is None: + return True + if node_type == ast.Name and node.id == "Any": + return True + if (node_type == ast.Attribute and type(node.value) == ast.Name + and node.value.id == "typing" and node.attr == "Any"): + return True + return False + + +def report_missing_annotations(tree): + for node in ast.walk(tree): + node_type = type(node) + if node_type == ast.AnnAssign: + if is_any(node.annotation): + print(f"Missing attribute type on line {node.lineno}") + elif node_type == ast.arg: + if is_any(node.annotation) and node.arg != "self": + print(f"Missing argument type: {node.arg} on line {node.lineno}") + elif node_type == ast.FunctionDef: + if is_any(node.returns) and node.name != "__init__": + print(f"Missing return type: {node.name} on line {node.lineno}") + + +def extract_imports(tree): + modules = set() + typing = set() + typeshed = set() + + def collect_annotations(anno_tree): + if anno_tree is None: + return + for node in ast.walk(anno_tree): + node_type = type(node) + if node_type == ast.Name: + if node.id in IMPORTS_IGNORE: + continue + elif node.id in IMPORTS_TYPING: + typing.add(node.id) + elif node.id in IMPORTS_TYPESHED: + typeshed.add(node.id) + elif not node.id[0].isupper(): + modules.add(node.id) + + for node in ast.walk(tree): + node_type = type(node) + if (node_type == ast.AnnAssign) or (node_type == ast.arg): + collect_annotations(node.annotation) + elif node_type == ast.FunctionDef: + collect_annotations(node.returns) + + return { + "modules": sorted(modules), + "typing": sorted(typing), + "typeshed": sorted(typeshed), + } + def convert_folder(top_level, stub_directory): ok = 0 total = 0 filenames = sorted(os.listdir(top_level)) pyi_lines = [] + for filename in filenames: full_path = os.path.join(top_level, filename) file_lines = [] if os.path.isdir(full_path): - mok, mtotal = convert_folder(full_path, os.path.join(stub_directory, filename)) + (mok, mtotal) = convert_folder(full_path, os.path.join(stub_directory, filename)) ok += mok total += mtotal elif filename.endswith(".c"): @@ -44,44 +111,57 @@ def convert_folder(top_level, stub_directory): pyi_lines.extend(file_lines) if not pyi_lines: - return ok, total + return (ok, total) stub_filename = os.path.join(stub_directory, "__init__.pyi") print(stub_filename) stub_contents = "".join(pyi_lines) - os.makedirs(stub_directory, exist_ok=True) - with open(stub_filename, "w") as f: - f.write(stub_contents) # Validate that the module is a parseable stub. total += 1 try: - tree = astroid.parse(stub_contents) - for i in tree.body: - if 'name' in i.__dict__: - print(i.__dict__['name']) - for j in i.body: - if isinstance(j, astroid.scoped_nodes.FunctionDef): - if None in j.args.__dict__['annotations']: - print(f"Missing parameter type: {j.__dict__['name']} on line {j.__dict__['lineno']}\n") - if j.returns: - if 'Any' in j.returns.__dict__.values(): - print(f"Missing return type: {j.__dict__['name']} on line {j.__dict__['lineno']}") - elif isinstance(j, astroid.node_classes.AnnAssign): - if 'name' in j.__dict__['annotation'].__dict__: - if j.__dict__['annotation'].__dict__['name'] == 'Any': - print(f"missing attribute type on line {j.__dict__['lineno']}") - + tree = ast.parse(stub_contents) + imports = extract_imports(tree) + report_missing_annotations(tree) ok += 1 - except astroid.exceptions.AstroidSyntaxError as e: - e = e.__cause__ + except SyntaxError as e: traceback.print_exception(type(e), e, e.__traceback__) + return (ok, total) + + # Add import statements + import_lines = ["from __future__ import annotations"] + import_lines.extend(f"import {m}" for m in imports["modules"]) + import_lines.append("from typing import " + ", ".join(imports["typing"])) + import_lines.append("from _typeshed import " + ", ".join(imports["typeshed"])) + import_body = "\n".join(import_lines) + m = re.match(r'(\s*""".*?""")', stub_contents, flags=re.DOTALL) + if m: + stub_contents = m.group(1) + "\n\n" + import_body + "\n\n" + stub_contents[m.end():] + else: + stub_contents = import_body + "\n\n" + stub_contents + stub_contents = isort.code(stub_contents) + + # Adjust blank lines + stub_contents = re.sub(r"\n+class", "\n\n\nclass", stub_contents) + stub_contents = re.sub(r"\n+def", "\n\n\ndef", stub_contents) + stub_contents = re.sub(r"\n+^(\s+)def", lambda m: f"\n\n{m.group(1)}def", stub_contents, flags=re.M) + stub_contents = stub_contents.strip() + "\n" + + os.makedirs(stub_directory, exist_ok=True) + with open(stub_filename, "w") as f: + f.write(stub_contents) + print() - return ok, total + return (ok, total) -ok, total = convert_folder(top_level, stub_directory) -print(f"{ok} ok out of {total}") +if __name__ == "__main__": + top_level = sys.argv[1].strip("/") + stub_directory = sys.argv[2] -if ok != total: - sys.exit(total - ok) + (ok, total) = convert_folder(top_level, stub_directory) + + print(f"Parsing .pyi files: {total - ok} failed, {ok} passed") + + if ok != total: + sys.exit(total - ok) From 678faff464fa7810dfc12e75f73029d8e3106f0c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 21 Jul 2020 11:02:23 -0700 Subject: [PATCH 18/19] Be more aggressive with uChip build --- ports/atmel-samd/boards/uchip/mpconfigboard.mk | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ports/atmel-samd/boards/uchip/mpconfigboard.mk b/ports/atmel-samd/boards/uchip/mpconfigboard.mk index 196068a1e0..0e5eda0b8d 100644 --- a/ports/atmel-samd/boards/uchip/mpconfigboard.mk +++ b/ports/atmel-samd/boards/uchip/mpconfigboard.mk @@ -10,9 +10,5 @@ INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 -# Tweak inlining depending on language. -ifeq ($(TRANSLATION), zh_Latn_pinyin) +# Always use aggressive inlining CFLAGS_INLINE_LIMIT = 45 -else -CFLAGS_INLINE_LIMIT = 70 -endif From 5e3a853db4ad735e3ae1c115cab3ef213a338f0d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 21 Jul 2020 12:51:59 -0700 Subject: [PATCH 19/19] Turn off GC opt on uchip board --- ports/atmel-samd/boards/uchip/mpconfigboard.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/atmel-samd/boards/uchip/mpconfigboard.mk b/ports/atmel-samd/boards/uchip/mpconfigboard.mk index 0e5eda0b8d..d08690a15a 100644 --- a/ports/atmel-samd/boards/uchip/mpconfigboard.mk +++ b/ports/atmel-samd/boards/uchip/mpconfigboard.mk @@ -12,3 +12,5 @@ CIRCUITPY_FULL_BUILD = 0 # Always use aggressive inlining CFLAGS_INLINE_LIMIT = 45 + +SUPEROPT_GC = 0