From 77fcb8a5a3c7bb8163c4ed8f739410af268a237d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 22 Sep 2022 12:18:16 -0500 Subject: [PATCH 01/18] disable rainbowio on this arduino_nano_33_iot The gpio changes took away enough code space that it doesn't fit. --- ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk index f289aad065..de9819a895 100644 --- a/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk @@ -11,3 +11,4 @@ CIRCUITPY_BUILD_EXTENSIONS = bin,uf2 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 +CIRCUITPY_RAINBOWIO = 0 From 9caa65cf28956344023b28a3c4e94f57419eb289 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Aug 2022 13:59:52 -0500 Subject: [PATCH 02/18] allow a board to override link.ld --- ports/raspberrypi/Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 67de654255..9f7d7dfc93 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -276,12 +276,13 @@ SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_M all: $(BUILD)/firmware.uf2 -$(BUILD)/firmware.elf: $(OBJ) link.ld +LINK_LD := $(firstword $(wildcard boards/$(BOARD)/link.ld link.ld)) +$(BUILD)/firmware.elf: $(OBJ) $(LINK_LD) $(STEPECHO) "LINK $@" $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags - $(Q)$(CC) -o $@ $(CFLAGS) @$(BUILD)/firmware.ldflags -Wl,-T,link.ld -Wl,-Map=$@.map -Wl,-cref -Wl,--gc-sections @$(BUILD)/firmware.objs - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py link.ld + $(Q)$(CC) -o $@ $(CFLAGS) @$(BUILD)/firmware.ldflags -Wl,-T,$(LINK_LD) -Wl,-Map=$@.map -Wl,-cref -Wl,--gc-sections @$(BUILD)/firmware.objs + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(LINK_LD) $(BUILD)/firmware.bin: $(BUILD)/firmware.elf $(STEPECHO) "Create $@" From 22b04aef22cadd5ba02f5f723588e07c780fbea8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Aug 2022 14:04:47 -0500 Subject: [PATCH 03/18] Reindent a block --- ports/raspberrypi/Makefile | 97 +++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 9f7d7dfc93..09824f7c43 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -57,54 +57,55 @@ CROSS_COMPILE = arm-none-eabi- HAL_DIR=hal/$(MCU_SERIES) -INC += -I. \ - -I../.. \ - -I../lib/mp-readline \ - -I../shared/timeutils \ - -Iboards/$(BOARD) \ - -Iboards/ \ - -isystem sdk/ \ - -isystem sdk/src/common/pico_base/include/ \ - -isystem sdk/src/common/pico_binary_info/include/ \ - -isystem sdk/src/common/pico_stdlib/include/ \ - -isystem sdk/src/common/pico_sync/include/ \ - -isystem sdk/src/common/pico_time/include/ \ - -isystem sdk/src/common/pico_util/include/ \ - -isystem sdk/src/rp2040/hardware_regs/include/ \ - -isystem sdk/src/rp2040/hardware_structs/include/ \ - -isystem sdk/src/rp2_common/hardware_adc/include/ \ - -isystem sdk/src/rp2_common/hardware_base/include/ \ - -isystem sdk/src/rp2_common/hardware_claim/include/ \ - -isystem sdk/src/rp2_common/hardware_clocks/include/ \ - -isystem sdk/src/rp2_common/hardware_divider/include/ \ - -isystem sdk/src/rp2_common/hardware_dma/include/ \ - -isystem sdk/src/rp2_common/hardware_flash/include/ \ - -isystem sdk/src/rp2_common/hardware_gpio/include/ \ - -isystem sdk/src/rp2_common/hardware_irq/include/ \ - -isystem sdk/src/rp2_common/hardware_i2c/include/ \ - -isystem sdk/src/rp2_common/hardware_pio/include/ \ - -isystem sdk/src/rp2_common/hardware_pll/include/ \ - -isystem sdk/src/rp2_common/hardware_resets/include/ \ - -isystem sdk/src/rp2_common/hardware_rtc/include/ \ - -isystem sdk/src/rp2_common/hardware_spi/include/ \ - -isystem sdk/src/rp2_common/hardware_sync/include/ \ - -isystem sdk/src/rp2_common/hardware_timer/include/ \ - -isystem sdk/src/rp2_common/hardware_uart/include/ \ - -isystem sdk/src/rp2_common/hardware_watchdog/include/ \ - -isystem sdk/src/rp2_common/hardware_xosc/include/ \ - -isystem sdk/src/rp2_common/pico_multicore/include/ \ - -isystem sdk/src/rp2_common/pico_fix/rp2040_usb_device_enumeration/include/ \ - -isystem sdk/src/rp2_common/pico_stdio/include/ \ - -isystem sdk/src/rp2_common/pico_printf/include/ \ - -isystem sdk/src/rp2_common/pico_float/include/ \ - -isystem sdk/src/rp2_common/pico_platform/include/ \ - -isystem sdk/src/rp2_common/pico_runtime/printf/include/ \ - -isystem sdk/src/rp2_common/pico_bootrom/include/ \ - -isystem sdk/src/rp2_common/pico_unique_id/include/ \ - -Isdk_config \ - -I../../lib/tinyusb/src \ - -I../../supervisor/shared/usb \ - -I$(BUILD) +INC += \ + -I. \ + -I../.. \ + -I../lib/mp-readline \ + -I../shared/timeutils \ + -Iboards/$(BOARD) \ + -Iboards/ \ + -isystem sdk/ \ + -isystem sdk/src/common/pico_base/include/ \ + -isystem sdk/src/common/pico_binary_info/include/ \ + -isystem sdk/src/common/pico_stdlib/include/ \ + -isystem sdk/src/common/pico_sync/include/ \ + -isystem sdk/src/common/pico_time/include/ \ + -isystem sdk/src/common/pico_util/include/ \ + -isystem sdk/src/rp2040/hardware_regs/include/ \ + -isystem sdk/src/rp2040/hardware_structs/include/ \ + -isystem sdk/src/rp2_common/hardware_adc/include/ \ + -isystem sdk/src/rp2_common/hardware_base/include/ \ + -isystem sdk/src/rp2_common/hardware_claim/include/ \ + -isystem sdk/src/rp2_common/hardware_clocks/include/ \ + -isystem sdk/src/rp2_common/hardware_divider/include/ \ + -isystem sdk/src/rp2_common/hardware_dma/include/ \ + -isystem sdk/src/rp2_common/hardware_flash/include/ \ + -isystem sdk/src/rp2_common/hardware_gpio/include/ \ + -isystem sdk/src/rp2_common/hardware_irq/include/ \ + -isystem sdk/src/rp2_common/hardware_i2c/include/ \ + -isystem sdk/src/rp2_common/hardware_pio/include/ \ + -isystem sdk/src/rp2_common/hardware_pll/include/ \ + -isystem sdk/src/rp2_common/hardware_resets/include/ \ + -isystem sdk/src/rp2_common/hardware_rtc/include/ \ + -isystem sdk/src/rp2_common/hardware_spi/include/ \ + -isystem sdk/src/rp2_common/hardware_sync/include/ \ + -isystem sdk/src/rp2_common/hardware_timer/include/ \ + -isystem sdk/src/rp2_common/hardware_uart/include/ \ + -isystem sdk/src/rp2_common/hardware_watchdog/include/ \ + -isystem sdk/src/rp2_common/hardware_xosc/include/ \ + -isystem sdk/src/rp2_common/pico_multicore/include/ \ + -isystem sdk/src/rp2_common/pico_fix/rp2040_usb_device_enumeration/include/ \ + -isystem sdk/src/rp2_common/pico_stdio/include/ \ + -isystem sdk/src/rp2_common/pico_printf/include/ \ + -isystem sdk/src/rp2_common/pico_float/include/ \ + -isystem sdk/src/rp2_common/pico_platform/include/ \ + -isystem sdk/src/rp2_common/pico_runtime/printf/include/ \ + -isystem sdk/src/rp2_common/pico_bootrom/include/ \ + -isystem sdk/src/rp2_common/pico_unique_id/include/ \ + -Isdk_config \ + -I../../lib/tinyusb/src \ + -I../../supervisor/shared/usb \ + -I$(BUILD) # Pico specific configuration CFLAGS += -DRASPBERRYPI -DPICO_ON_DEVICE=1 -DPICO_NO_BINARY_INFO=0 -DPICO_TIME_DEFAULT_ALARM_POOL_DISABLED=0 -DPICO_DIVIDER_CALL_IDIV0=0 -DPICO_DIVIDER_CALL_LDIV0=0 -DPICO_DIVIDER_HARDWARE=1 -DPICO_DOUBLE_ROM=1 -DPICO_FLOAT_ROM=1 -DPICO_MULTICORE=1 -DPICO_BITS_IN_RAM=0 -DPICO_DIVIDER_IN_RAM=0 -DPICO_DOUBLE_PROPAGATE_NANS=0 -DPICO_DOUBLE_IN_RAM=0 -DPICO_MEM_IN_RAM=0 -DPICO_FLOAT_IN_RAM=0 -DPICO_FLOAT_PROPAGATE_NANS=1 -DPICO_NO_FLASH=0 -DPICO_COPY_TO_RAM=0 -DPICO_DISABLE_SHARED_IRQ_HANDLERS=0 -DPICO_NO_BI_BOOTSEL_VIA_DOUBLE_RESET=0 From 346fff2e7cdaa985f64fd1e7848ed05fbc24a884 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Aug 2022 14:01:14 -0500 Subject: [PATCH 04/18] cyw43 basic gpio support, hwaddr in boot_out --- locale/circuitpython.pot | 7 +- main.c | 1 + .../common-hal/digitalio/DigitalInOut.c | 6 +- .../common-hal/digitalio/DigitalInOut.c | 6 +- .../cxd56/common-hal/digitalio/DigitalInOut.c | 6 +- .../common-hal/digitalio/DigitalInOut.c | 6 +- .../litex/common-hal/digitalio/DigitalInOut.c | 6 +- .../common-hal/digitalio/DigitalInOut.c | 6 +- ports/nrf/common-hal/digitalio/DigitalInOut.c | 6 +- ports/raspberrypi/Makefile | 74 ++++- ports/raspberrypi/bindings/cyw43/__init__.c | 70 +++++ ports/raspberrypi/bindings/cyw43/__init__.h | 34 ++ .../boards/raspberry_pi_pico_w/board.c | 40 +++ .../boards/raspberry_pi_pico_w/link.ld | 293 ++++++++++++++++++ .../raspberry_pi_pico_w/mpconfigboard.h | 5 + .../raspberry_pi_pico_w/mpconfigboard.mk | 20 ++ .../pico-sdk-configboard.h | 1 + .../boards/raspberry_pi_pico_w/pins.c | 54 ++++ .../common-hal/digitalio/DigitalInOut.c | 58 +++- .../common-hal/microcontroller/Pin.c | 36 +++ .../common-hal/microcontroller/Pin.h | 3 + .../common-hal/microcontroller/__init__.c | 5 + ports/raspberrypi/peripherals/pins.c | 11 + ports/raspberrypi/peripherals/pins.h | 6 + ports/raspberrypi/supervisor/port.c | 39 +++ ports/stm/common-hal/digitalio/DigitalInOut.c | 6 +- py/circuitpy_defns.mk | 3 + py/circuitpy_mpconfig.h | 9 + py/circuitpy_mpconfig.mk | 3 + shared-bindings/digitalio/DigitalInOut.c | 35 ++- shared-bindings/digitalio/DigitalInOut.h | 12 +- shared-bindings/microcontroller/Pin.c | 4 +- shared-bindings/microcontroller/Pin.h | 2 + supervisor/port.h | 4 + supervisor/shared/port.c | 3 + 35 files changed, 848 insertions(+), 32 deletions(-) create mode 100644 ports/raspberrypi/bindings/cyw43/__init__.c create mode 100644 ports/raspberrypi/bindings/cyw43/__init__.h create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/board.c create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/link.ld create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.h create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/pico-sdk-configboard.h create mode 100644 ports/raspberrypi/boards/raspberry_pi_pico_w/pins.c diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 52f1bdb5ad..01db150b3f 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -95,6 +95,7 @@ msgstr "" msgid "%q failure: %d" msgstr "" +#: shared-bindings/digitalio/DigitalInOut.c #: shared-bindings/microcontroller/Pin.c msgid "%q in use" msgstr "" @@ -916,6 +917,10 @@ msgstr "" msgid "Expected a %q" msgstr "" +#: ports/raspberrypi/bindings/cyw43/__init__.c +msgid "Expected a %q or %q" +msgstr "" + #: shared-bindings/alarm/__init__.c msgid "Expected an %q" msgstr "" @@ -1177,7 +1182,7 @@ msgstr "" msgid "Internal watchdog timer expired." msgstr "" -#: py/argcheck.c +#: py/argcheck.c shared-bindings/digitalio/DigitalInOut.c msgid "Invalid %q" msgstr "" diff --git a/main.c b/main.c index 04e8777b9a..d8542bcb57 100644 --- a/main.c +++ b/main.c @@ -784,6 +784,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) { mp_printf(&mp_plat_print, "%02X", raw_id[i]); } mp_printf(&mp_plat_print, "\n"); + port_boot_info(); #endif bool found_boot = maybe_run_list(boot_py_filenames); diff --git a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c index 8358ac4d87..dc770a2de3 100644 --- a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c +++ b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c @@ -66,11 +66,12 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->output = false; // This also sets direction to input. common_hal_digitalio_digitalinout_set_pull(self, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -151,7 +152,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { enum gpio_pull_mode asf_pull = GPIO_PULL_OFF; switch (pull) { @@ -168,6 +169,7 @@ void common_hal_digitalio_digitalinout_set_pull( // Must set pull after setting direction. gpio_set_pin_direction(self->pin->number, GPIO_DIRECTION_IN); gpio_set_pin_pull_mode(self->pin->number, asf_pull); + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/broadcom/common-hal/digitalio/DigitalInOut.c b/ports/broadcom/common-hal/digitalio/DigitalInOut.c index d8ac3472de..0dd74a8412 100644 --- a/ports/broadcom/common-hal/digitalio/DigitalInOut.c +++ b/ports/broadcom/common-hal/digitalio/DigitalInOut.c @@ -63,11 +63,12 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->output = false; // This also sets direction to input. common_hal_digitalio_digitalinout_set_pull(self, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -134,7 +135,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { const uint8_t pin = self->pin->number; BP_PULL_Enum bp_pull = BP_PULL_NONE; @@ -144,6 +145,7 @@ void common_hal_digitalio_digitalinout_set_pull( bp_pull = BP_PULL_DOWN; } gpio_set_pull(pin, bp_pull); + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/cxd56/common-hal/digitalio/DigitalInOut.c b/ports/cxd56/common-hal/digitalio/DigitalInOut.c index cfb39301df..c1f247d0a9 100644 --- a/ports/cxd56/common-hal/digitalio/DigitalInOut.c +++ b/ports/cxd56/common-hal/digitalio/DigitalInOut.c @@ -64,11 +64,12 @@ bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *se return self->pin == NULL; } -void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->input = true; self->pull = pull; board_gpio_write(self->pin->number, -1); board_gpio_config(self->pin->number, 0, true, true, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { @@ -124,10 +125,11 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitali return self->open_drain ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; } -void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->pull = pull; board_gpio_write(self->pin->number, -1); board_gpio_config(self->pin->number, 0, true, true, pull); + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t *self) { diff --git a/ports/espressif/common-hal/digitalio/DigitalInOut.c b/ports/espressif/common-hal/digitalio/DigitalInOut.c index 63c8ec1a41..61671dadb9 100644 --- a/ports/espressif/common-hal/digitalio/DigitalInOut.c +++ b/ports/espressif/common-hal/digitalio/DigitalInOut.c @@ -82,10 +82,11 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { common_hal_digitalio_digitalinout_set_pull(self, pull); gpio_set_direction(self->pin->number, GPIO_MODE_INPUT); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -140,7 +141,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( return DRIVE_MODE_PUSH_PULL; } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { gpio_num_t number = self->pin->number; gpio_pullup_dis(number); @@ -150,6 +151,7 @@ void common_hal_digitalio_digitalinout_set_pull( } else if (pull == PULL_DOWN) { gpio_pulldown_en(number); } + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/litex/common-hal/digitalio/DigitalInOut.c b/ports/litex/common-hal/digitalio/DigitalInOut.c index a48d698fcb..7545722967 100644 --- a/ports/litex/common-hal/digitalio/DigitalInOut.c +++ b/ports/litex/common-hal/digitalio/DigitalInOut.c @@ -58,10 +58,11 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { (void)pull; touch_oe_write(touch_oe_read() & ~(1 << self->pin->number)); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -111,10 +112,11 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { (void)self; (void)pull; + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c b/ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c index dde7b3badd..7639204bc6 100644 --- a/ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c +++ b/ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c @@ -88,12 +88,13 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->output = false; // This also sets direction to input. common_hal_digitalio_digitalinout_set_pull(self, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -150,7 +151,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { self->pull = pull; @@ -158,6 +159,7 @@ void common_hal_digitalio_digitalinout_set_pull( const gpio_pin_config_t config = { kGPIO_DigitalInput, 0, kGPIO_NoIntmode }; GPIO_PinInit(self->pin->gpio, self->pin->number, &config); + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/nrf/common-hal/digitalio/DigitalInOut.c b/ports/nrf/common-hal/digitalio/DigitalInOut.c index 95e488f3b7..a6440228f1 100644 --- a/ports/nrf/common-hal/digitalio/DigitalInOut.c +++ b/ports/nrf/common-hal/digitalio/DigitalInOut.c @@ -59,10 +59,11 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL); common_hal_digitalio_digitalinout_set_pull(self, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -120,7 +121,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL; @@ -137,6 +138,7 @@ void common_hal_digitalio_digitalinout_set_pull( } nrf_gpio_cfg_input(self->pin->number, hal_pull); + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 09824f7c43..313005932c 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -57,6 +57,71 @@ CROSS_COMPILE = arm-none-eabi- HAL_DIR=hal/$(MCU_SERIES) +ifeq ($(CIRCUITPY_CYW43),1) +INC_CYW43 := \ + -isystem sdk/src/rp2_common/pico_cyw43_arch/include/ \ + -isystem sdk/lib/cyw43-driver/src \ + -isystem sdk/lib/cyw43-driver/firmware \ + +CFLAGS_CYW43 := -DCYW43_LWIP=0 -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 -DCYW43_USE_SPI -DIGNORE_GPIO25 +SRC_SDK_CYW43 := \ + lib/cyw43-driver/src/cyw43_ctrl.c \ + lib/cyw43-driver/src/cyw43_ll.c \ + lib/cyw43-driver/src/cyw43_lwip.c \ + lib/cyw43-driver/src/cyw43_stats.c \ + src/common/pico_sync/sem.c \ + src/rp2_common/pico_cyw43_arch/cyw43_arch.c \ + src/rp2_common/pico_cyw43_arch/cyw43_arch_threadsafe_background.c \ + src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.c \ + +SRC_CYW43 := $(wildcard bindings/cyw43/*.c) + +PIOASM = $(BUILD)/sdk/pioasm/pioasm +.PHONY: PioasmBuild +PioasmBuild: $(PIOASM) +$(PIOASM): + $(Q)cmake -S sdk -B $(BUILD)/sdk + $(Q)make -C $(BUILD)/sdk PioasmBuild + +$(BUILD)/cyw43_bus_pio_spi.pio.h: sdk/src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.pio $(PIOASM) + $(Q)$(PIOASM) -o c-sdk $< $@ +$(BUILD)/sdk/src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.o: $(BUILD)/cyw43_bus_pio_spi.pio.h + +CYW43_FIRMWARE_BIN = 43439A0-7.95.49.00.combined + +$(BUILD)/cyw43_resource.o: sdk/lib/cyw43-driver/firmware/$(CYW43_FIRMWARE_BIN) + $(Q)$(OBJCOPY) -I binary -O elf32-littlearm -B arm \ + --readonly-text \ + --rename-section .data=.big_const,contents,alloc,load,readonly,data \ + --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_start=fw_43439A0_7_95_49_00_start \ + --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_size=fw_43439A0_7_95_49_00_size \ + --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_end=fw_43439A0_7_95_49_00_end \ + $< $@ +OBJ_CYW43 := $(BUILD)/cyw43_resource.o +# need to do the equivalent of this in cmake +### # cyw43_resource.o contains the WiFi and BT firmware as a binary blob +### add_custom_command( +### OUTPUT ${CYW43_FIRMWARE_OBJ} +### DEPENDS ${PICO_CYW43_DRIVER_PATH}/firmware/${CYW43_FIRMWARE_BIN} +### WORKING_DIRECTORY ${PICO_CYW43_DRIVER_PATH}/firmware +### COMMAND ${CMAKE_OBJCOPY} -I binary -O elf32-littlearm -B arm +### --readonly-text +### --rename-section .data=${RESOURCE_SECNAME},${RESOURCE_SECFLAGS} +### --redefine-sym _binary_${CYW43_FIRMWARE_BIN_}_start=${CYW43_FIRMWARE_PRETTY}_start +### --redefine-sym _binary_${CYW43_FIRMWARE_BIN_}_end=${CYW43_FIRMWARE_PRETTY}_end +### --redefine-sym _binary_${CYW43_FIRMWARE_BIN_}_size=${CYW43_FIRMWARE_PRETTY}_size +### ${CYW43_FIRMWARE_BIN} ${CYW43_FIRMWARE_OBJ} +### ) +### +else +$(error y u no y fi) +INC_CYW43 := +CFLAGS_CYW43 := +SRC_SDK_CYW43 := +SRC_CYW43 := +OBJ_CYW43 := +endif + INC += \ -I. \ -I../.. \ @@ -102,6 +167,7 @@ INC += \ -isystem sdk/src/rp2_common/pico_runtime/printf/include/ \ -isystem sdk/src/rp2_common/pico_bootrom/include/ \ -isystem sdk/src/rp2_common/pico_unique_id/include/ \ + $(INC_CYW43) \ -Isdk_config \ -I../../lib/tinyusb/src \ -I../../supervisor/shared/usb \ @@ -116,6 +182,9 @@ CFLAGS += -DTUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX=1 -DCFG_TUSB_MCU=OPT_MCU_R # option to override default optimization level, set in boards/$(BOARD)/mpconfigboard.mk CFLAGS += $(OPTIMIZATION_FLAGS) +# flags specific to wifi / cyw43 + +CFLAGS += $(CFLAGS_CYW43) #Debugging/Optimization ifeq ($(DEBUG), 1) CFLAGS += -ggdb3 -O3 @@ -133,7 +202,7 @@ endif # Remove -Wno-stringop-overflow after we can test with CI's GCC 10. Mac's looks weird. DISABLE_WARNINGS = -Wno-stringop-overflow -Wno-unused-function -Wno-unused-variable -Wno-strict-overflow -Wno-cast-align -Wno-strict-prototypes -Wno-nested-externs -Wno-double-promotion -Wno-sign-compare -CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes +CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes -Wno-error=unused-result CFLAGS += \ -march=armv6-m \ @@ -186,6 +255,7 @@ SRC_SDK := \ src/rp2_common/pico_runtime/runtime.c \ src/rp2_common/pico_stdio/stdio.c \ src/rp2_common/pico_unique_id/unique_id.c \ + $(SRC_SDK_CYW43) \ SRC_SDK := $(addprefix sdk/, $(SRC_SDK)) $(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK)): CFLAGS += -Wno-missing-prototypes -Wno-undef @@ -205,6 +275,7 @@ SRC_C += \ lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c \ lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c \ mphalport.c \ + $(SRC_CYW43) \ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ @@ -244,6 +315,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) OBJ += $(BUILD)/boot2_padded_checksummed.o +OBJ += $(OBJ_CYW43) $(BUILD)/boot2_padded_checksummed.o: $(BUILD)/boot2_padded_checksummed.S $(STEPECHO) "CC $<" diff --git a/ports/raspberrypi/bindings/cyw43/__init__.c b/ports/raspberrypi/bindings/cyw43/__init__.c new file mode 100644 index 0000000000..2d5479a5d2 --- /dev/null +++ b/ports/raspberrypi/bindings/cyw43/__init__.c @@ -0,0 +1,70 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" + +#include "shared-bindings/board/__init__.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "bindings/cyw43/__init__.h" + +const mp_obj_type_t cyw43_pin_type = { + { &mp_type_type }, + .flags = MP_TYPE_FLAG_EXTENDED, + .name = MP_QSTR_CywPin, + .print = shared_bindings_microcontroller_pin_print, + MP_TYPE_EXTENDED_FIELDS( + .unary_op = mp_generic_unary_op, + ) +}; + +const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj) { + if (!mp_obj_is_type(obj, &mcu_pin_type) && !mp_obj_is_type(obj, &cyw43_pin_type)) { + mp_raise_TypeError_varg(translate("Expected a %q or %q"), mcu_pin_type.name, cyw43_pin_type.name); + } + return MP_OBJ_TO_PTR(obj); +} + +const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj) { + const mcu_pin_obj_t *pin = validate_obj_is_pin_including_cyw43(obj); + assert_pin_free(pin); + return pin; +} + +STATIC const mp_rom_map_elem_t cyw43_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cyw43) }, + { MP_ROM_QSTR(MP_QSTR_CywPin), MP_ROM_QSTR(MP_QSTR_CywPin) }, +}; + +STATIC MP_DEFINE_CONST_DICT(cyw43_module_globals, cyw43_module_globals_table); + +const mp_obj_module_t cyw43_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&cyw43_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_cyw43, cyw43_module, CIRCUITPY_CYW43); diff --git a/ports/raspberrypi/bindings/cyw43/__init__.h b/ports/raspberrypi/bindings/cyw43/__init__.h new file mode 100644 index 0000000000..5dcdfba52b --- /dev/null +++ b/ports/raspberrypi/bindings/cyw43/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +extern const mp_obj_type_t cyw43_pin_type; +const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj); +const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj); diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c new file mode 100644 index 0000000000..de6e424ed9 --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "supervisor/board.h" + +void board_init(void) { +} + +bool board_requests_safe_mode(void) { + return false; +} + +void reset_board(void) { +} + +void board_deinit(void) { +} diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/link.ld b/ports/raspberrypi/boards/raspberry_pi_pico_w/link.ld new file mode 100644 index 0000000000..7d0a44ce51 --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/link.ld @@ -0,0 +1,293 @@ +/* Based on GCC ARM embedded samples. + Defines the following symbols for use by code: + __exidx_start + __exidx_end + __etext + __data_start__ + __preinit_array_start + __preinit_array_end + __init_array_start + __init_array_end + __fini_array_start + __fini_array_end + __data_end__ + __bss_start__ + __bss_end__ + __end__ + end + __HeapLimit + __StackLimit + __StackTop + __stack (== StackTop) +*/ + +MEMORY +{ + FLASH_FIRMWARE (rx) : ORIGIN = 0x10000000, LENGTH = 1788k + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 256k + SCRATCH_X (rwx) : ORIGIN = 0x20040000, LENGTH = 4k + SCRATCH_Y (rwx) : ORIGIN = 0x20041000, LENGTH = 4k +} + +ENTRY(_entry_point) + +SECTIONS +{ + /* Second stage bootloader is prepended to the image. It must be 256 bytes big + and checksummed. It is usually built by the boot_stage2 target + in the Pico SDK + */ + + .flash_begin : { + __flash_binary_start = .; + } > FLASH_FIRMWARE + + .boot2 : { + __boot2_start__ = .; + KEEP (*(.boot2)) + __boot2_end__ = .; + } > FLASH_FIRMWARE + + ASSERT(__boot2_end__ - __boot2_start__ == 256, + "ERROR: Pico second stage bootloader must be 256 bytes in size") + + /* The second stage will always enter the image at the start of .text. + The debugger will use the ELF entry point, which is the _entry_point + symbol if present, otherwise defaults to start of .text. + This can be used to transfer control back to the bootrom on debugger + launches only, to perform proper flash setup. + */ + + .text : { + __logical_binary_start = .; + KEEP (*(.vectors)) + KEEP (*(.binary_info_header)) + __binary_info_header_end = .; + KEEP (*(.reset)) + /* TODO revisit this now memset/memcpy/float in ROM */ + /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from + * FLASH ... we will include any thing excluded here in .data below by default */ + *(.init) + + __property_getter_start = .; + *(.property_getter) + __property_getter_end = .; + __property_getset_start = .; + *(.property_getset) + __property_getset_end = .; + + *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*) + *(.fini) + /* Pull all c'tors into .text */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + /* Followed by destructors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(.eh_frame*) + . = ALIGN(4); + } > FLASH_FIRMWARE + + .rodata : { + *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*) + . = ALIGN(4); + *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*))) + . = ALIGN(4); + } > FLASH_FIRMWARE + + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } > FLASH_FIRMWARE + + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } > FLASH_FIRMWARE + __exidx_end = .; + + /* Machine inspectable binary information */ + . = ALIGN(4); + __binary_info_start = .; + .binary_info : + { + KEEP(*(.binary_info.keep.*)) + *(.binary_info.*) + } > FLASH_FIRMWARE + __binary_info_end = .; + . = ALIGN(4); + + /* End of .text-like segments */ + __etext = .; + + .ram_vector_table (COPY): { + *(.ram_vector_table) + } > RAM + + .data : { + __data_start__ = .; + *(vtable) + + *(.time_critical*) + + /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */ + *(.text*) + . = ALIGN(4); + *(.rodata*) + . = ALIGN(4); + + *(.data*) + + . = ALIGN(4); + *(.after_data.*) + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__mutex_array_start = .); + KEEP(*(SORT(.mutex_array.*))) + KEEP(*(.mutex_array)) + PROVIDE_HIDDEN (__mutex_array_end = .); + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(SORT(.preinit_array.*))) + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + *(SORT(.fini_array.*)) + *(.fini_array) + PROVIDE_HIDDEN (__fini_array_end = .); + + *(.jcr) + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + } > RAM AT> FLASH_FIRMWARE + + .itcm : + { + . = ALIGN(4); + *(.itcm.*) + + . = ALIGN(4); + } > RAM AT> FLASH_FIRMWARE + _ld_itcm_destination = ADDR(.itcm); + _ld_itcm_flash_copy = LOADADDR(.itcm); + _ld_itcm_size = SIZEOF(.itcm); + + .dtcm_data : + { + . = ALIGN(4); + + *(.dtcm_data.*) + + . = ALIGN(4); + } > RAM AT> FLASH_FIRMWARE + _ld_dtcm_data_destination = ADDR(.dtcm_data); + _ld_dtcm_data_flash_copy = LOADADDR(.dtcm_data); + _ld_dtcm_data_size = SIZEOF(.dtcm_data); + + .dtcm_bss : + { + . = ALIGN(4); + + *(.dtcm_bss.*) + + . = ALIGN(4); + } > RAM AT> RAM + _ld_dtcm_bss_start = ADDR(.dtcm_bss); + _ld_dtcm_bss_size = SIZEOF(.dtcm_bss); + + .uninitialized_data (COPY): { + . = ALIGN(4); + *(.uninitialized_data*) + } > RAM + + /* Start and end symbols must be word-aligned */ + .scratch_x : { + __scratch_x_start__ = .; + *(.scratch_x.*) + . = ALIGN(4); + __scratch_x_end__ = .; + } > SCRATCH_X AT > FLASH_FIRMWARE + __scratch_x_source__ = LOADADDR(.scratch_x); + + .scratch_y : { + __scratch_y_start__ = .; + *(.scratch_y.*) + . = ALIGN(4); + __scratch_y_end__ = .; + } > SCRATCH_Y AT > FLASH_FIRMWARE + __scratch_y_source__ = LOADADDR(.scratch_y); + + .bss : { + . = ALIGN(4); + __bss_start__ = .; + *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*))) + *(COMMON) + . = ALIGN(4); + __bss_end__ = .; + } > RAM + + .heap (COPY): + { + __end__ = .; + end = __end__; + *(.heap*) + __HeapLimit = .; + } > RAM + + /* .stack*_dummy section doesn't contains any symbols. It is only + * used for linker to calculate size of stack sections, and assign + * values to stack symbols later + * + * stack1 section may be empty/missing if platform_launch_core1 is not used */ + + /* by default we put core 0 stack at the end of scratch Y, so that if core 1 + * stack is not used then all of SCRATCH_X is free. + */ + .stack1_dummy (COPY): + { + *(.stack1*) + } > SCRATCH_X + .stack_dummy (COPY): + { + *(.stack*) + } > SCRATCH_Y + + .flash_end : { + __flash_binary_end = .; + } > FLASH_FIRMWARE + + /* stack limit is poorly named, but historically is maximum heap ptr */ + __StackLimit = ORIGIN(RAM) + LENGTH(RAM); + __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X); + __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y); + __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy); + __StackBottom = __StackTop - SIZEOF(.stack_dummy); + PROVIDE(__stack = __StackTop); + + /* Check if data + heap + stack exceeds RAM limit */ + ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed") + + ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary") + /* todo assert on extra code */ +} diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.h b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.h new file mode 100644 index 0000000000..58a291b18b --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.h @@ -0,0 +1,5 @@ +#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico W" +#define MICROPY_HW_MCU_NAME "rp2040" + +#define CIRCUITPY_DIGITALIO_HAVE_INVALID_PULL (1) +#define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (1) diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk new file mode 100644 index 0000000000..0b3ce5ef00 --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk @@ -0,0 +1,20 @@ +USB_VID = 0x239A +USB_PID = 0x8120 +USB_PRODUCT = "Pico W" +USB_MANUFACTURER = "Raspberry Pi" + +CHIP_VARIANT = RP2040 +CHIP_FAMILY = rp2 + +EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ" + +CIRCUITPY__EVE = 1 + +CIRCUITPY_CYW43 = 1 +CIRCUITPY_SSL = 0 +CIRCUITPY_HASHLIB = 0 +CIRCUITPY_WEB_WORKFLOW = 0 +CIRCUITPY_MDNS = 0 +CIRCUITPY_SOCKETPOOL = 0 + +CFLAGS += -DCYW43_PIN_WL_HOST_WAKE=24 -DCYW43_PIN_WL_REG_ON=23 -DCYW43_WL_GPIO_COUNT=3 -DCYW43_WL_GPIO_LED_PIN=0 diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/pico-sdk-configboard.h b/ports/raspberrypi/boards/raspberry_pi_pico_w/pico-sdk-configboard.h new file mode 100644 index 0000000000..36da55d457 --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/pico-sdk-configboard.h @@ -0,0 +1 @@ +// Put board-specific pico-sdk definitions here. This file must exist. diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/pins.c b/ports/raspberrypi/boards/raspberry_pi_pico_w/pins.c new file mode 100644 index 0000000000..fb3570cc8a --- /dev/null +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/pins.c @@ -0,0 +1,54 @@ +#include "shared-bindings/board/__init__.h" + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + { MP_ROM_QSTR(MP_QSTR_GP0), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_GP1), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_GP2), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_GP3), MP_ROM_PTR(&pin_GPIO3) }, + { MP_ROM_QSTR(MP_QSTR_GP4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_GP5), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_GP6), MP_ROM_PTR(&pin_GPIO6) }, + { MP_ROM_QSTR(MP_QSTR_GP7), MP_ROM_PTR(&pin_GPIO7) }, + { MP_ROM_QSTR(MP_QSTR_GP8), MP_ROM_PTR(&pin_GPIO8) }, + { MP_ROM_QSTR(MP_QSTR_GP9), MP_ROM_PTR(&pin_GPIO9) }, + { MP_ROM_QSTR(MP_QSTR_GP10), MP_ROM_PTR(&pin_GPIO10) }, + { MP_ROM_QSTR(MP_QSTR_GP11), MP_ROM_PTR(&pin_GPIO11) }, + { MP_ROM_QSTR(MP_QSTR_GP12), MP_ROM_PTR(&pin_GPIO12) }, + { MP_ROM_QSTR(MP_QSTR_GP13), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_GP14), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_GP15), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_GP16), MP_ROM_PTR(&pin_GPIO16) }, + { MP_ROM_QSTR(MP_QSTR_GP17), MP_ROM_PTR(&pin_GPIO17) }, + { MP_ROM_QSTR(MP_QSTR_GP18), MP_ROM_PTR(&pin_GPIO18) }, + { MP_ROM_QSTR(MP_QSTR_GP19), MP_ROM_PTR(&pin_GPIO19) }, + { MP_ROM_QSTR(MP_QSTR_GP20), MP_ROM_PTR(&pin_GPIO20) }, + { MP_ROM_QSTR(MP_QSTR_GP21), MP_ROM_PTR(&pin_GPIO21) }, + { MP_ROM_QSTR(MP_QSTR_GP22), MP_ROM_PTR(&pin_GPIO22) }, + + { MP_ROM_QSTR(MP_QSTR_SMPS_MODE), MP_ROM_PTR(&pin_GPIO23) }, + { MP_ROM_QSTR(MP_QSTR_GP23), MP_ROM_PTR(&pin_GPIO23) }, + + { MP_ROM_QSTR(MP_QSTR_VBUS_SENSE), MP_ROM_PTR(&pin_GPIO24) }, + { MP_ROM_QSTR(MP_QSTR_GP24), MP_ROM_PTR(&pin_GPIO24) }, + + { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_CYW0) }, + { MP_ROM_QSTR(MP_QSTR_CYW0), MP_ROM_PTR(&pin_CYW0) }, + + { MP_ROM_QSTR(MP_QSTR_GP26_A0), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_GP26), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_GPIO26) }, + + { MP_ROM_QSTR(MP_QSTR_GP27_A1), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_GP27), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_GPIO27) }, + + { MP_ROM_QSTR(MP_QSTR_GP28_A2), MP_ROM_PTR(&pin_GPIO28) }, + { MP_ROM_QSTR(MP_QSTR_GP28), MP_ROM_PTR(&pin_GPIO28) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO28) }, + + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO29) }, + { MP_ROM_QSTR(MP_QSTR_VOLTAGE_MONITOR), MP_ROM_PTR(&pin_GPIO29) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c index ef431d8506..1702254f9f 100644 --- a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c +++ b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c @@ -31,11 +31,18 @@ #include "py/mphal.h" #include "common-hal/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "supervisor/shared/translate/translate.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" +#if CIRCUITPY_CYW43 +#include "pico/cyw43_arch.h" +#include "bindings/cyw43/__init__.h" +#define IS_CYW(self) ((self)->pin->base.type == &cyw43_pin_type) +#endif + digitalinout_result_t common_hal_digitalio_digitalinout_construct( digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { claim_pin(pin); @@ -61,20 +68,31 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self if (common_hal_digitalio_digitalinout_deinited(self)) { return; } - reset_pin_number(self->pin->number); + common_hal_reset_pin(self->pin); self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - self->output = false; // This also sets direction to input. - common_hal_digitalio_digitalinout_set_pull(self, pull); + return common_hal_digitalio_digitalinout_set_pull(self, pull); } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { + + #if CIRCUITPY_CYW43 + if (IS_CYW(self)) { + if (drive_mode != DRIVE_MODE_PUSH_PULL) { + return DIGITALINOUT_INVALID_DRIVE_MODE; + } + cyw43_arch_gpio_put(self->pin->number, value); + self->output = true; + self->open_drain = false; + return DIGITALINOUT_OK; + } + #endif const uint8_t pin = self->pin->number; gpio_disable_pulls(pin); @@ -99,6 +117,12 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t *self, bool value) { const uint8_t pin = self->pin->number; + #if CIRCUITPY_CYW43 + if (IS_CYW(self)) { + cyw43_arch_gpio_put(pin, value); + return; + } + #endif if (self->open_drain && value) { // If true and open-drain, set the direction -before- setting // the pin value, to to avoid a high glitch on the pin before @@ -122,6 +146,13 @@ bool common_hal_digitalio_digitalinout_get_value( digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { + #if CIRCUITPY_CYW43 + if (IS_CYW(self)) { + if (drive_mode != DRIVE_MODE_PUSH_PULL) { + return DIGITALINOUT_INVALID_DRIVE_MODE; + } + } + #endif const uint8_t pin = self->pin->number; bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; @@ -142,11 +173,23 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( } } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + #if CIRCUITPY_CYW43 + if (IS_CYW(self)) { + if (pull != PULL_NONE) { + return DIGITALINOUT_INVALID_PULL; + } + cyw43_arch_gpio_get(self->pin->number); + self->output = false; + return DIGITALINOUT_OK; + } + #endif const uint8_t pin = self->pin->number; gpio_set_pulls(pin, pull == PULL_UP, pull == PULL_DOWN); gpio_set_dir(pin, GPIO_IN); + self->output = false; + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( @@ -170,6 +213,11 @@ bool common_hal_digitalio_has_reg_op(digitalinout_reg_op_t op) { } volatile uint32_t *common_hal_digitalio_digitalinout_get_reg(digitalio_digitalinout_obj_t *self, digitalinout_reg_op_t op, uint32_t *mask) { + #if CIRCUITPY_CYW43 + if (IS_CYW(self)) { + return NULL; + } + #endif const uint8_t pin = self->pin->number; *mask = 1u << pin; diff --git a/ports/raspberrypi/common-hal/microcontroller/Pin.c b/ports/raspberrypi/common-hal/microcontroller/Pin.c index d40c1f43b5..59adb5395f 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Pin.c +++ b/ports/raspberrypi/common-hal/microcontroller/Pin.c @@ -28,11 +28,19 @@ #include "common-hal/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" +#include "bindings/cyw43/__init__.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" +#if CIRCUITPY_CYW43 +#include "pico/cyw43_arch.h" +#endif + STATIC uint32_t never_reset_pins; +bool cyw_ever_init; +static uint32_t cyw_pin_claimed; + void reset_all_pins(void) { for (size_t i = 0; i < TOTAL_GPIO_COUNT; i++) { if ((never_reset_pins & (1 << i)) != 0) { @@ -40,6 +48,14 @@ void reset_all_pins(void) { } reset_pin_number(i); } + #if CIRCUITPY_CYW43 + if (cyw_ever_init) { + for (size_t i = 0; i < 1; i++) { + cyw43_arch_gpio_put(i, 0); + } + } + cyw_pin_claimed = 0; + #endif } void never_reset_pin_number(uint8_t pin_number) { @@ -66,15 +82,30 @@ void reset_pin_number(uint8_t pin_number) { hw_set_bits(&padsbank0_hw->io[pin_number], PADS_BANK0_GPIO0_OD_BITS); } +void reset_pin_number_cyw(uint8_t pin_no) { + cyw_pin_claimed &= ~(1 << pin_no); +} + void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) { never_reset_pin_number(pin->number); } void common_hal_reset_pin(const mcu_pin_obj_t *pin) { + #if CIRCUITPY_CYW43 + if (pin->base.type == &cyw43_pin_type) { + reset_pin_number_cyw(pin->number); + return; + } + #endif reset_pin_number(pin->number); } void claim_pin(const mcu_pin_obj_t *pin) { + #if CIRCUITPY_CYW43 + if (pin->base.type == &cyw43_pin_type) { + cyw_pin_claimed |= (1 << pin->number); + } + #endif // Nothing to do because all changes will set the GPIO settings. } @@ -89,6 +120,11 @@ bool pin_number_is_free(uint8_t pin_number) { } bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { + #if CIRCUITPY_CYW43 + if (pin->base.type == &cyw43_pin_type) { + return !(cyw_pin_claimed & (1 << pin->number)); + } + #endif return pin_number_is_free(pin->number); } diff --git a/ports/raspberrypi/common-hal/microcontroller/Pin.h b/ports/raspberrypi/common-hal/microcontroller/Pin.h index 3e2287dc5d..5042dd7d66 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Pin.h +++ b/ports/raspberrypi/common-hal/microcontroller/Pin.h @@ -34,10 +34,13 @@ #include "peripherals/pins.h" +extern bool cyw_ever_init; + void reset_all_pins(void); // reset_pin_number takes the pin number instead of the pointer so that objects don't // need to store a full pointer. void reset_pin_number(uint8_t pin_number); +void reset_pin_number_cyw(uint8_t pin_number); void never_reset_pin_number(uint8_t pin_number); void claim_pin(const mcu_pin_obj_t *pin); bool pin_number_is_free(uint8_t pin_number); diff --git a/ports/raspberrypi/common-hal/microcontroller/__init__.c b/ports/raspberrypi/common-hal/microcontroller/__init__.c index 3b656edc07..5e79fbd0ce 100644 --- a/ports/raspberrypi/common-hal/microcontroller/__init__.c +++ b/ports/raspberrypi/common-hal/microcontroller/__init__.c @@ -175,10 +175,15 @@ const mp_rom_map_elem_t mcu_pin_global_dict_table[TOTAL_GPIO_COUNT] = { { MP_ROM_QSTR(MP_QSTR_GPIO22), MP_ROM_PTR(&pin_GPIO22) }, { MP_ROM_QSTR(MP_QSTR_GPIO23), MP_ROM_PTR(&pin_GPIO23) }, { MP_ROM_QSTR(MP_QSTR_GPIO24), MP_ROM_PTR(&pin_GPIO24) }, + #if !defined(IGNORE_GPIO25) { MP_ROM_QSTR(MP_QSTR_GPIO25), MP_ROM_PTR(&pin_GPIO25) }, + #endif { MP_ROM_QSTR(MP_QSTR_GPIO26), MP_ROM_PTR(&pin_GPIO26) }, { MP_ROM_QSTR(MP_QSTR_GPIO27), MP_ROM_PTR(&pin_GPIO27) }, { MP_ROM_QSTR(MP_QSTR_GPIO28), MP_ROM_PTR(&pin_GPIO28) }, { MP_ROM_QSTR(MP_QSTR_GPIO29), MP_ROM_PTR(&pin_GPIO29) }, + #if CIRCUITPY_CYW43 + { MP_ROM_QSTR(MP_QSTR_CYW0), MP_ROM_PTR(&pin_CYW0) }, + #endif }; MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table); diff --git a/ports/raspberrypi/peripherals/pins.c b/ports/raspberrypi/peripherals/pins.c index 1b5fe91cac..e6a197f3be 100644 --- a/ports/raspberrypi/peripherals/pins.c +++ b/ports/raspberrypi/peripherals/pins.c @@ -27,6 +27,7 @@ #include "pins.h" #include "shared-bindings/microcontroller/Pin.h" +#include "bindings/cyw43/__init__.h" // This macro is used to simplify pin definition in boards//pins.c #define PIN(p_number) \ @@ -34,6 +35,11 @@ { &mcu_pin_type }, \ .number = p_number \ } +#define CYW_PIN(p_number) \ + const mcu_pin_obj_t pin_CYW##p_number = { \ + { &cyw43_pin_type }, \ + .number = p_number \ + } PIN(0); PIN(1); @@ -60,8 +66,13 @@ PIN(21); PIN(22); PIN(23); PIN(24); +#if !defined(IGNORE_GPIO25) PIN(25); +#endif PIN(26); PIN(27); PIN(28); PIN(29); +#if CIRCUITPY_CYW43 +CYW_PIN(0); +#endif diff --git a/ports/raspberrypi/peripherals/pins.h b/ports/raspberrypi/peripherals/pins.h index 99ab9cfe48..0a01b3959e 100644 --- a/ports/raspberrypi/peripherals/pins.h +++ b/ports/raspberrypi/peripherals/pins.h @@ -62,10 +62,16 @@ extern const mcu_pin_obj_t pin_GPIO21; extern const mcu_pin_obj_t pin_GPIO22; extern const mcu_pin_obj_t pin_GPIO23; extern const mcu_pin_obj_t pin_GPIO24; +#if !defined(IGNORE_GPIO25) extern const mcu_pin_obj_t pin_GPIO25; +#endif extern const mcu_pin_obj_t pin_GPIO26; extern const mcu_pin_obj_t pin_GPIO27; extern const mcu_pin_obj_t pin_GPIO28; extern const mcu_pin_obj_t pin_GPIO29; +#if CIRCUITPY_CYW43 +extern const mcu_pin_obj_t pin_CYW0; +#endif + #endif // MICROPY_INCLUDED_RASPBERRYPI_PERIPHERALS_PINS_H diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 5041250c0a..2e1fed7a25 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -53,12 +53,17 @@ #include "src/rp2_common/hardware_uart/include/hardware/uart.h" #include "src/rp2_common/hardware_sync/include/hardware/sync.h" #include "src/rp2_common/hardware_timer/include/hardware/timer.h" +#if CIRCUITPY_CYW43 +#include "pico/cyw43_arch.h" +#endif #include "src/common/pico_time/include/pico/time.h" #include "src/common/pico_binary_info/include/pico/binary_info.h" #include "pico/bootrom.h" #include "hardware/watchdog.h" +#include "supervisor/serial.h" + extern volatile bool mp_msc_enabled; STATIC void _tick_callback(uint alarm_num); @@ -122,6 +127,24 @@ safe_mode_t port_init(void) { // Check brownout. + #if CIRCUITPY_CYW43 + never_reset_pin_number(23); + never_reset_pin_number(24); + never_reset_pin_number(25); + never_reset_pin_number(29); + if (cyw43_arch_init()) { + serial_write("WiFi init failed\n"); + return -1; + } else { + cyw_ever_init = true; + for (int i = 3; i--;) { + cyw43_arch_gpio_put(0, 1); + sleep_ms(100); + cyw43_arch_gpio_put(0, 0); + sleep_ms(100); + } + } + #endif if (board_requests_safe_mode()) { return USER_SAFE_MODE; } @@ -261,3 +284,19 @@ __attribute__((used)) void HardFault_Handler(void) { asm ("nop;"); } } + +void port_yield() { + #if CIRCUITPY_CYW43 + cyw43_arch_poll(); + #endif +} + +void port_boot_info(void) { + #if CIRCUITPY_CYW43 + mp_printf(&mp_plat_print, "MAC"); + for (int i = 0; i < 6; i++) { + mp_printf(&mp_plat_print, ":%02X", cyw43_state.mac[i]); + } + mp_printf(&mp_plat_print, "\n"); + #endif +} diff --git a/ports/stm/common-hal/digitalio/DigitalInOut.c b/ports/stm/common-hal/digitalio/DigitalInOut.c index 3a0e27943b..58f81b43b6 100644 --- a/ports/stm/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm/common-hal/digitalio/DigitalInOut.c @@ -77,7 +77,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self self->pin = NULL; } -void common_hal_digitalio_digitalinout_switch_to_input( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -88,6 +88,7 @@ void common_hal_digitalio_digitalinout_switch_to_input( HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct); common_hal_digitalio_digitalinout_set_pull(self, pull); + return DIGITALINOUT_OK; } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( @@ -138,7 +139,7 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( == LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; } -void common_hal_digitalio_digitalinout_set_pull( +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { switch (pull) { @@ -154,6 +155,7 @@ void common_hal_digitalio_digitalinout_set_pull( default: break; } + return DIGITALINOUT_OK; } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 80377f1a65..b5c8047f99 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -170,6 +170,9 @@ endif ifeq ($(CIRCUITPY_COUNTIO),1) SRC_PATTERNS += countio/% endif +ifeq ($(CIRCUITPY_CYW43),1) +SRC_PATTERNS += cyw43/% +endif ifeq ($(CIRCUITPY_DIGITALIO),1) SRC_PATTERNS += digitalio/% endif diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index acdf08e6a4..7c497c6106 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -588,6 +588,15 @@ void supervisor_run_background_tasks_if_tick(void); #define MICROPY_WRAP_MP_EXECUTE_BYTECODE PLACE_IN_ITCM #endif +#ifndef CIRCUITPY_DIGITALIO_HAVE_INVALID_PULL +#define CIRCUITPY_DIGITALIO_HAVE_INVALID_PULL (0) +#endif + +#ifndef CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE +#define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (0) +#endif + + #define MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE (CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE) #endif // __INCLUDED_MPCONFIG_CIRCUITPY_H diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 0f4269c522..6bff102dd4 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -159,6 +159,9 @@ CFLAGS += -DCIRCUITPY_CAMERA=$(CIRCUITPY_CAMERA) CIRCUITPY_CANIO ?= 0 CFLAGS += -DCIRCUITPY_CANIO=$(CIRCUITPY_CANIO) +CIRCUITPY_CYW43 ?= 0 +CFLAGS += -DCIRCUITPY_CYW43=$(CIRCUITPY_CYW43) + CIRCUITPY_DIGITALIO ?= 1 CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO) diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c index ae72b5cb55..c4a1f853b5 100644 --- a/shared-bindings/digitalio/DigitalInOut.c +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -43,6 +43,29 @@ #include "shared-bindings/util.h" #include "supervisor/shared/translate/translate.h" +#if CIRCUITPY_CYW43 +#include "bindings/cyw43/__init__.h" +#endif + +STATIC void check_result(digitalinout_result_t result) { + switch (result) { + case DIGITALINOUT_OK: + return; + case DIGITALINOUT_PIN_BUSY: + mp_raise_ValueError_varg(translate("%q in use"), MP_QSTR_Pin); + case DIGITALINOUT_INPUT_ONLY: + mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_direction); + #if CIRCUITPY_DIGITALIO_HAVE_INVALID_PULL + case DIGITALINOUT_INVALID_PULL: + mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_pull); + #endif + #if CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE + case DIGITALINOUT_INVALID_DRIVE_MODE: + mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_drive_mode); + #endif + } +} + //| class DigitalInOut: //| """Digital input and output //| @@ -65,7 +88,11 @@ STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type, digitalio_digitalinout_obj_t *self = m_new_obj(digitalio_digitalinout_obj_t); self->base.type = &digitalio_digitalinout_type; + #if CIRCUITPY_CYW43 + const mcu_pin_obj_t *pin = validate_obj_is_free_pin_including_cyw43(args[0]); + #else const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]); + #endif common_hal_digitalio_digitalinout_construct(self, pin); return MP_OBJ_FROM_PTR(self); @@ -166,7 +193,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_o mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - common_hal_digitalio_digitalinout_switch_to_input(self, validate_pull(args[ARG_pull].u_rom_obj, MP_QSTR_pull)); + check_result(common_hal_digitalio_digitalinout_switch_to_input(self, validate_pull(args[ARG_pull].u_rom_obj, MP_QSTR_pull))); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digitalio_digitalinout_switch_to_input); @@ -200,7 +227,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_ob digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); if (value == MP_ROM_PTR(&digitalio_direction_input_obj)) { - common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE); + check_result(common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE)); } else if (value == MP_ROM_PTR(&digitalio_direction_output_obj)) { digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL); if (result == DIGITALINOUT_INPUT_ONLY) { @@ -276,7 +303,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_o if (drive_mode == MP_ROM_PTR(&digitalio_drive_mode_open_drain_obj)) { c_drive_mode = DRIVE_MODE_OPEN_DRAIN; } - common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode); + check_result(common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode)); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_drive_mode_obj, digitalio_digitalinout_obj_set_drive_mode); @@ -319,7 +346,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t p return mp_const_none; } - common_hal_digitalio_digitalinout_set_pull(self, validate_pull(pull_obj, MP_QSTR_pull)); + check_result(common_hal_digitalio_digitalinout_set_pull(self, validate_pull(pull_obj, MP_QSTR_pull))); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_pull_obj, digitalio_digitalinout_obj_set_pull); diff --git a/shared-bindings/digitalio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h index b6edbc63aa..9a751e93bb 100644 --- a/shared-bindings/digitalio/DigitalInOut.h +++ b/shared-bindings/digitalio/DigitalInOut.h @@ -38,7 +38,13 @@ extern const mp_obj_type_t digitalio_digitalinout_type; typedef enum { DIGITALINOUT_OK, DIGITALINOUT_PIN_BUSY, - DIGITALINOUT_INPUT_ONLY + DIGITALINOUT_INPUT_ONLY, + #if CIRCUITPY_DIGITALIO_HAVE_INVALID_PULL + DIGITALINOUT_INVALID_PULL, + #endif + #if CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE + DIGITALINOUT_INVALID_DRIVE_MODE, + #endif } digitalinout_result_t; typedef enum { @@ -52,14 +58,14 @@ typedef enum { digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin); void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self); bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self); -void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull); +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull); digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode); digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t *self); void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t *self, bool value); bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t *self); digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode); digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t *self); -void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull); +digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull); digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t *self); void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self); digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj); diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index 840f468e2a..1bf41e1e61 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -70,7 +70,7 @@ static void get_pin_name(const mcu_pin_obj_t *self, qstr *package, qstr *module, } } -STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +void shared_bindings_microcontroller_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mcu_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); qstr package = MP_QSTR_Pin; qstr module; @@ -88,7 +88,7 @@ const mp_obj_type_t mcu_pin_type = { { &mp_type_type }, .flags = MP_TYPE_FLAG_EXTENDED, .name = MP_QSTR_Pin, - .print = mcu_pin_print, + .print = shared_bindings_microcontroller_pin_print, MP_TYPE_EXTENDED_FIELDS( .unary_op = mp_generic_unary_op, ) diff --git a/shared-bindings/microcontroller/Pin.h b/shared-bindings/microcontroller/Pin.h index 12ff422a4a..b41b3c308a 100644 --- a/shared-bindings/microcontroller/Pin.h +++ b/shared-bindings/microcontroller/Pin.h @@ -55,6 +55,8 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin); void common_hal_mcu_pin_claim_number(uint8_t pin_no); void common_hal_mcu_pin_reset_number(uint8_t pin_no); +void shared_bindings_microcontroller_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); + #define COMMON_HAL_MCU_NO_PIN ((uint8_t)0xff) #endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H diff --git a/supervisor/port.h b/supervisor/port.h index 554baca2a4..2fb24385c1 100644 --- a/supervisor/port.h +++ b/supervisor/port.h @@ -120,4 +120,8 @@ void port_yield(void); // A default weak implementation is provided that does nothing. void port_post_boot_py(bool heap_valid); +// Some ports want to add information to boot_out.txt. +// A default weak implementation is provided that does nothing. +void port_boot_info(void); + #endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H diff --git a/supervisor/shared/port.c b/supervisor/shared/port.c index e78e6a931a..4070cf254e 100644 --- a/supervisor/shared/port.c +++ b/supervisor/shared/port.c @@ -34,3 +34,6 @@ MP_WEAK void port_wake_main_task_from_isr(void) { MP_WEAK void port_yield(void) { } + +MP_WEAK void port_boot_info(void) { +} From 6c3cdceb45677d0914d2411fe7e2f245ce465c5a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 31 Aug 2022 10:13:39 -0500 Subject: [PATCH 05/18] Implement scan, connect, ping My pings go out, and then they come back ```py import os import wifi import ipaddress wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD')) ipv4 = ipaddress.ip_address("8.8.4.4") print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000)) ``` --- frozen/circuitpython-stage | 2 +- locale/circuitpython.pot | 19 +- mpy-cross/Makefile | 2 + ports/espressif/esp32-camera | 2 +- ports/raspberrypi/Makefile | 22 +- .../raspberry_pi_pico_w/mpconfigboard.mk | 1 + ports/raspberrypi/common-hal/wifi/Monitor.c | 74 ++++ ports/raspberrypi/common-hal/wifi/Monitor.h | 40 ++ ports/raspberrypi/common-hal/wifi/Network.c | 81 ++++ ports/raspberrypi/common-hal/wifi/Network.h | 36 ++ ports/raspberrypi/common-hal/wifi/Radio.c | 311 ++++++++++++++ ports/raspberrypi/common-hal/wifi/Radio.h | 39 ++ .../common-hal/wifi/ScannedNetworks.c | 120 ++++++ .../common-hal/wifi/ScannedNetworks.h | 42 ++ ports/raspberrypi/common-hal/wifi/__init__.c | 124 ++++++ ports/raspberrypi/common-hal/wifi/__init__.h | 37 ++ ports/raspberrypi/lwip_inc/arch/sys_arch.h | 1 + ports/raspberrypi/lwip_inc/lwipopts.h | 92 +++++ ports/raspberrypi/lwip_src/ping.c | 390 ++++++++++++++++++ ports/raspberrypi/lwip_src/ping.h | 25 ++ ports/raspberrypi/mpconfigport.h | 8 + ports/raspberrypi/supervisor/port.c | 15 +- py/objgenerator.c | 3 +- shared-bindings/wifi/Monitor.h | 2 +- shared-bindings/wifi/Network.h | 2 +- shared-bindings/wifi/Radio.c | 2 +- shared-bindings/wifi/Radio.h | 2 +- tools/codeformat.py | 2 + tools/describe | 2 +- 29 files changed, 1472 insertions(+), 26 deletions(-) create mode 100644 ports/raspberrypi/common-hal/wifi/Monitor.c create mode 100644 ports/raspberrypi/common-hal/wifi/Monitor.h create mode 100644 ports/raspberrypi/common-hal/wifi/Network.c create mode 100644 ports/raspberrypi/common-hal/wifi/Network.h create mode 100644 ports/raspberrypi/common-hal/wifi/Radio.c create mode 100644 ports/raspberrypi/common-hal/wifi/Radio.h create mode 100644 ports/raspberrypi/common-hal/wifi/ScannedNetworks.c create mode 100644 ports/raspberrypi/common-hal/wifi/ScannedNetworks.h create mode 100644 ports/raspberrypi/common-hal/wifi/__init__.c create mode 100644 ports/raspberrypi/common-hal/wifi/__init__.h create mode 100644 ports/raspberrypi/lwip_inc/arch/sys_arch.h create mode 100644 ports/raspberrypi/lwip_inc/lwipopts.h create mode 100644 ports/raspberrypi/lwip_src/ping.c create mode 100644 ports/raspberrypi/lwip_src/ping.h diff --git a/frozen/circuitpython-stage b/frozen/circuitpython-stage index 4124dfbdaa..9a8338b3bd 160000 --- a/frozen/circuitpython-stage +++ b/frozen/circuitpython-stage @@ -1 +1 @@ -Subproject commit 4124dfbdaadce1966f457d7d6c6984e9832999bf +Subproject commit 9a8338b3bdaeac9eeb5b74d147107c67db33fdac diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 01db150b3f..c38548a90f 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -303,7 +303,7 @@ msgstr "" msgid "'%s' object doesn't support item deletion" msgstr "" -#: py/runtime.c +#: ports/raspberrypi/common-hal/wifi/Radio.c py/runtime.c msgid "'%s' object has no attribute '%q'" msgstr "" @@ -489,6 +489,7 @@ msgid "Already running" msgstr "" #: ports/espressif/common-hal/wifi/Radio.c +#: ports/raspberrypi/common-hal/wifi/Radio.c msgid "Already scanning for wifi networks" msgstr "" @@ -2208,6 +2209,11 @@ msgstr "" msgid "Unknown system firmware error: %d" msgstr "" +#: ports/raspberrypi/common-hal/wifi/__init__.c +#, c-format +msgid "Unkown error code %d" +msgstr "" + #: shared-bindings/adafruit_pixelbuf/PixelBuf.c #, c-format msgid "Unmatched number of items on RHS (expected %d, got %d)." @@ -4137,14 +4143,10 @@ msgstr "" msgid "type is not an acceptable base type" msgstr "" -#: py/runtime.c +#: py/objgenerator.c py/runtime.c msgid "type object '%q' has no attribute '%q'" msgstr "" -#: py/objgenerator.c -msgid "type object 'generator' has no attribute '__await__'" -msgstr "" - #: py/objtype.c msgid "type takes 1 or 3 arguments" msgstr "" @@ -4276,9 +4278,14 @@ msgid "width must be greater than zero" msgstr "" #: ports/espressif/common-hal/wifi/Radio.c +#: ports/raspberrypi/common-hal/wifi/Radio.c msgid "wifi is not enabled" msgstr "" +#: ports/raspberrypi/common-hal/wifi/Monitor.c +msgid "wifi.Monitor not available" +msgstr "" + #: shared-bindings/_bleio/Adapter.c msgid "window must be <= interval" msgstr "" diff --git a/mpy-cross/Makefile b/mpy-cross/Makefile index 6e4d7ef316..4858cb25c7 100644 --- a/mpy-cross/Makefile +++ b/mpy-cross/Makefile @@ -20,3 +20,5 @@ override undefine PROG endif include mpy-cross.mk +CFLAGS += -g +STRIP = : diff --git a/ports/espressif/esp32-camera b/ports/espressif/esp32-camera index 54c3f61c86..28804391c0 160000 --- a/ports/espressif/esp32-camera +++ b/ports/espressif/esp32-camera @@ -1 +1 @@ -Subproject commit 54c3f61c864c3d3ffd779042454554045b9dd9d2 +Subproject commit 28804391c002f6a3ea5ce6a55aee3b191be3ecde diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 313005932c..bdfdb9a9b7 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -59,20 +59,30 @@ HAL_DIR=hal/$(MCU_SERIES) ifeq ($(CIRCUITPY_CYW43),1) INC_CYW43 := \ - -isystem sdk/src/rp2_common/pico_cyw43_arch/include/ \ - -isystem sdk/lib/cyw43-driver/src \ -isystem sdk/lib/cyw43-driver/firmware \ + -isystem sdk/lib/cyw43-driver/src \ + -isystem sdk/lib/lwip/src/include \ + -isystem sdk/src/rp2_common/pico_cyw43_arch/include/ \ + -isystem sdk/src/rp2_common/pico_lwip/include/ \ -CFLAGS_CYW43 := -DCYW43_LWIP=0 -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 -DCYW43_USE_SPI -DIGNORE_GPIO25 +CFLAGS_CYW43 := -DCYW43_LWIP=1 -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 -DCYW43_USE_SPI -DIGNORE_GPIO25 SRC_SDK_CYW43 := \ lib/cyw43-driver/src/cyw43_ctrl.c \ lib/cyw43-driver/src/cyw43_ll.c \ lib/cyw43-driver/src/cyw43_lwip.c \ lib/cyw43-driver/src/cyw43_stats.c \ src/common/pico_sync/sem.c \ + src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.c \ src/rp2_common/pico_cyw43_arch/cyw43_arch.c \ src/rp2_common/pico_cyw43_arch/cyw43_arch_threadsafe_background.c \ - src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.c \ + src/rp2_common/pico_lwip/nosys.c \ + src/rp2_common/pico_lwip/random.c \ + +SRC_LWIP := \ + $(wildcard sdk/lib/lwip/src/core/*.c) \ + $(wildcard sdk/lib/lwip/src/core/ipv4/*.c) \ + sdk/lib/lwip/src/netif/ethernet.c \ + $(wildcard lwip_src/*.c) \ SRC_CYW43 := $(wildcard bindings/cyw43/*.c) @@ -114,16 +124,17 @@ OBJ_CYW43 := $(BUILD)/cyw43_resource.o ### ) ### else -$(error y u no y fi) INC_CYW43 := CFLAGS_CYW43 := SRC_SDK_CYW43 := SRC_CYW43 := OBJ_CYW43 := +SRC_LWIP := endif INC += \ -I. \ + -Ilwip_inc \ -I../.. \ -I../lib/mp-readline \ -I../shared/timeutils \ @@ -276,6 +287,7 @@ SRC_C += \ lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c \ mphalport.c \ $(SRC_CYW43) \ + $(SRC_LWIP) \ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk index 0b3ce5ef00..7acd728bcc 100644 --- a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk @@ -16,5 +16,6 @@ CIRCUITPY_HASHLIB = 0 CIRCUITPY_WEB_WORKFLOW = 0 CIRCUITPY_MDNS = 0 CIRCUITPY_SOCKETPOOL = 0 +CIRCUITPY_WIFI = 1 CFLAGS += -DCYW43_PIN_WL_HOST_WAKE=24 -DCYW43_PIN_WL_REG_ON=23 -DCYW43_WL_GPIO_COUNT=3 -DCYW43_WL_GPIO_LED_PIN=0 diff --git a/ports/raspberrypi/common-hal/wifi/Monitor.c b/ports/raspberrypi/common-hal/wifi/Monitor.c new file mode 100644 index 0000000000..f288cd1ea1 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Monitor.c @@ -0,0 +1,74 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/mpstate.h" +#include "py/runtime.h" + +#include "shared-bindings/wifi/Monitor.h" +#include "shared-bindings/wifi/Packet.h" + + +#define MONITOR_PAYLOAD_FCS_LEN (4) +#define MONITOR_QUEUE_TIMEOUT_TICK (0) + +typedef struct { +} monitor_packet_t; + +void common_hal_wifi_monitor_construct(wifi_monitor_obj_t *self, uint8_t channel, size_t queue) { + mp_raise_NotImplementedError(translate("wifi.Monitor not available")); +} + +bool common_hal_wifi_monitor_deinited(void) { + return true; +} + +void common_hal_wifi_monitor_deinit(wifi_monitor_obj_t *self) { +} + +void common_hal_wifi_monitor_set_channel(wifi_monitor_obj_t *self, uint8_t channel) { +} + +mp_obj_t common_hal_wifi_monitor_get_channel(wifi_monitor_obj_t *self) { + return MP_OBJ_NEW_SMALL_INT(0); +} + +mp_obj_t common_hal_wifi_monitor_get_queue(wifi_monitor_obj_t *self) { + return mp_obj_new_int_from_uint(0); +} + +mp_obj_t common_hal_wifi_monitor_get_lost(wifi_monitor_obj_t *self) { + return mp_obj_new_int_from_uint(0); +} + +mp_obj_t common_hal_wifi_monitor_get_queued(wifi_monitor_obj_t *self) { + return mp_obj_new_int_from_uint(0); +} + +mp_obj_t common_hal_wifi_monitor_get_packet(wifi_monitor_obj_t *self) { + return mp_const_none; +} diff --git a/ports/raspberrypi/common-hal/wifi/Monitor.h b/ports/raspberrypi/common-hal/wifi/Monitor.h new file mode 100644 index 0000000000..14ee685bbd --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Monitor.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 microDev + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H +#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t channel; + size_t lost; + size_t queue_length; +} wifi_monitor_obj_t; + +#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H diff --git a/ports/raspberrypi/common-hal/wifi/Network.c b/ports/raspberrypi/common-hal/wifi/Network.c new file mode 100644 index 0000000000..8db42e962c --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Network.c @@ -0,0 +1,81 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "shared-bindings/wifi/Network.h" +#include "shared-bindings/wifi/AuthMode.h" + +mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) { + const char *cstr = (const char *)self->record.ssid; + return mp_obj_new_str(cstr, self->record.ssid_len); +} + +#define MAC_ADDRESS_LENGTH 6 + +mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self) { + return mp_obj_new_bytes(self->record.bssid, MAC_ADDRESS_LENGTH); +} + +mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) { + return mp_obj_new_int(self->record.rssi); +} + +mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self) { + return mp_obj_new_int(self->record.channel); +} + +mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { + return (mp_obj_t)MP_QSTR_; +} + +mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { + uint8_t authmode_mask = 0; + if (self->record.auth_mode == 0) { + authmode_mask = (1 << AUTHMODE_OPEN); + } + if (self->record.auth_mode & 1) { + authmode_mask |= (1 << AUTHMODE_PSK); + } + ; + if (self->record.auth_mode & 2) { + authmode_mask |= (1 << AUTHMODE_WPA); + } + ; + if (self->record.auth_mode & 4) { + authmode_mask |= (1 << AUTHMODE_WPA2); + } + ; + mp_obj_t authmode_list = mp_obj_new_list(0, NULL); + if (authmode_mask != 0) { + for (uint8_t i = 0; i < 8; i++) { + if ((authmode_mask >> i) & 1) { + mp_obj_list_append(authmode_list, cp_enum_find(&wifi_authmode_type, i)); + } + } + } + return authmode_list; +} diff --git a/ports/raspberrypi/common-hal/wifi/Network.h b/ports/raspberrypi/common-hal/wifi/Network.h new file mode 100644 index 0000000000..8e4e7bd310 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Network.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +#include "pico/cyw43_arch.h" + +typedef struct { + mp_obj_base_t base; + cyw43_ev_scan_result_t record; +} wifi_network_obj_t; diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c new file mode 100644 index 0000000000..f614d3185f --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -0,0 +1,311 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "supervisor/port.h" +#include "shared-bindings/wifi/Radio.h" +#include "shared-bindings/wifi/Network.h" + +#include +#include + +#include "common-hal/wifi/__init__.h" +#include "shared/runtime/interrupt_char.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/ipaddress/IPv4Address.h" +#include "shared-bindings/wifi/ScannedNetworks.h" +#include "shared-bindings/wifi/AuthMode.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/ipaddress/__init__.h" + +#if CIRCUITPY_MDNS +#include "components/mdns/include/mdns.h" +#endif + +#include "lwip/dns.h" +#include "lwip/icmp.h" +#include "lwip/raw.h" +#include "lwip_src/ping.h" + +#ifndef PING_ID +#define PING_ID 0xAFAF +#endif + +#define MAC_ADDRESS_LENGTH 6 + +#define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA]) +#define NETIF_AP (&cyw43_state.netif[CYW43_ITF_AP]) + +NORETURN static void ro_attribute(int attr) { + mp_raise_msg_varg(&mp_type_AttributeError, MP_ERROR_TEXT("'%s' object has no attribute '%q'"), "Radio", attr); +} + +bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self) { + return true; +} + +void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) { + if (!enabled) { + ro_attribute(MP_QSTR_enabled); + } +} + +mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) { + if (!NETIF_STA->hostname) { + return mp_const_none; + } + return mp_obj_new_str(NETIF_STA->hostname, strlen(NETIF_STA->hostname)); +} + +void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) { + ro_attribute(MP_QSTR_hostname); +} + +mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) { + return mp_obj_new_bytes(cyw43_state.mac, MAC_ADDRESS_LENGTH); +} + +void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac) { + ro_attribute(MP_QSTR_mac_address); +} + +mp_float_t common_hal_wifi_radio_get_tx_power(wifi_radio_obj_t *self) { + return MICROPY_FLOAT_CONST(0.); +} + +void common_hal_wifi_radio_set_tx_power(wifi_radio_obj_t *self, const mp_float_t tx_power) { + ro_attribute(MP_QSTR_tx_power); + +} + +mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) { + return common_hal_wifi_radio_get_mac_address(self); +} + +void common_hal_wifi_radio_set_mac_address_ap(wifi_radio_obj_t *self, const uint8_t *mac) { + ro_attribute(MP_QSTR_mac_address_ap); +} + +mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) { + if (self->current_scan) { + mp_raise_RuntimeError(translate("Already scanning for wifi networks")); + } + if (!common_hal_wifi_radio_get_enabled(self)) { + mp_raise_RuntimeError(translate("wifi is not enabled")); + } + wifi_scannednetworks_obj_t *scan = m_new_obj(wifi_scannednetworks_obj_t); + scan->base.type = &wifi_scannednetworks_type; + mp_obj_t args[] = { mp_const_empty_tuple, MP_OBJ_NEW_SMALL_INT(16) }; + scan->results = mp_type_deque.make_new(&mp_type_deque, 2, 0, args); + self->current_scan = scan; + wifi_scannednetworks_start_scan(scan); + return scan; +} + +void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) { + self->current_scan = NULL; +} + +void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) { + cyw43_arch_enable_sta_mode(); +} + +void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) { +} + +void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode, uint8_t max_connections) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) { +} + +wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len) { + if (!common_hal_wifi_radio_get_enabled(self)) { + mp_raise_RuntimeError(translate("wifi is not enabled")); + } + unsigned timeout_ms = timeout <= 0 ? 8000 : (unsigned)MAX(0, MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000)); + // TODO use connect_async so we can service bg tasks & check for ctrl-c during + // connect + int result = cyw43_arch_wifi_connect_timeout_ms((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK, timeout_ms); + switch (result) { + case 0: + return WIFI_RADIO_ERROR_NONE; + // case CYW43_LINK_DOWN: + // case CYW43_LINK_JOIN: + // case CYW43_LINK_NOIP: + // case CYW43_LINK_UP: + case CYW43_LINK_FAIL: + return WIFI_RADIO_ERROR_CONNECTION_FAIL; + case CYW43_LINK_NONET: + return WIFI_RADIO_ERROR_NO_AP_FOUND; + case CYW43_LINK_BADAUTH: + return WIFI_RADIO_ERROR_AUTH_FAIL; + + default: + return WIFI_RADIO_ERROR_UNSPECIFIED; + } +} + +mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_STA)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_STA->gw.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_AP)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_AP->gw.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_STA)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_STA->netmask.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_AP)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_AP->netmask.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_STA)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_STA->ip_addr.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self) { + if (!netif_is_up(NETIF_AP)) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(NETIF_AP->ip_addr.addr); +} + +mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) { + uint32_t addr = dns_getserver(0)->addr; + if (!netif_is_up(NETIF_STA) || addr == 0) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(addr); +} + +void common_hal_wifi_radio_set_ipv4_dns(wifi_radio_obj_t *self, mp_obj_t ipv4_dns_addr) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_wifi_radio_start_dhcp_client(wifi_radio_obj_t *self) { +} + +void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway, mp_obj_t ipv4_dns) { + mp_raise_NotImplementedError(NULL); +} + +volatile bool ping_received; + +static u8_t +ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) { + struct icmp_echo_hdr *iecho; + LWIP_ASSERT("p != NULL", p != NULL); + + if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && + pbuf_remove_header(p, PBUF_IP_HLEN) == 0) { + iecho = (struct icmp_echo_hdr *)p->payload; + + if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { + LWIP_DEBUGF(PING_DEBUG, ("ping: recv ")); + ip_addr_debug_print(PING_DEBUG, addr); + LWIP_DEBUGF(PING_DEBUG, (" %"U32_F " ms\n", (sys_now() - ping_time))); + + /* do some ping result processing */ + ping_received = true; + pbuf_free(p); + return 1; /* eat the packet */ + } + /* not eaten, restore original packet */ + pbuf_add_header(p, PBUF_IP_HLEN); + } + return 0; /* don't eat the packet */ +} + +mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) { + ip_addr_t ping_addr; + ipaddress_ipaddress_to_lwip(ip_address, &ping_addr); + + struct raw_pcb *ping_pcb; + MICROPY_PY_LWIP_ENTER(); + ping_pcb = raw_new(IP_PROTO_ICMP); + if (!ping_pcb) { + MICROPY_PY_LWIP_EXIT(); + return -1; + } + raw_recv(ping_pcb, ping_recv, NULL); + raw_bind(ping_pcb, IP_ADDR_ANY); + MICROPY_PY_LWIP_EXIT(); + + ping_received = false; + ping_send(ping_pcb, &ping_addr); + size_t timeout_ms = (size_t)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000); + uint64_t start = port_get_raw_ticks(NULL); + uint64_t deadline = start + timeout_ms; + while (port_get_raw_ticks(NULL) < deadline && !ping_received) { + RUN_BACKGROUND_TASKS; + if (mp_hal_is_interrupted()) { + break; + } + } + mp_int_t result = -1; + if (ping_received) { + uint64_t now = port_get_raw_ticks(NULL); + result = now - start; + } + + MICROPY_PY_LWIP_ENTER(); + raw_remove(ping_pcb); + MICROPY_PY_LWIP_EXIT(); + + return result; +} + +void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self) { + // Only bother to scan the actual object references. + gc_collect_ptr(self->current_scan); +} diff --git a/ports/raspberrypi/common-hal/wifi/Radio.h b/ports/raspberrypi/common-hal/wifi/Radio.h new file mode 100644 index 0000000000..e919c5d718 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/Radio.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +#include "shared-bindings/wifi/ScannedNetworks.h" +#include "shared-bindings/wifi/Network.h" + +typedef struct { + mp_obj_base_t base; + wifi_scannednetworks_obj_t *current_scan; +} wifi_radio_obj_t; + +extern void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self); diff --git a/ports/raspberrypi/common-hal/wifi/ScannedNetworks.c b/ports/raspberrypi/common-hal/wifi/ScannedNetworks.c new file mode 100644 index 0000000000..fd180da3b1 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/ScannedNetworks.c @@ -0,0 +1,120 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * Copyright (c) 2018 Artur Pacholec + * Copyright (c) 2017 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "shared/runtime/interrupt_char.h" +#include "py/gc.h" +#include "py/objstr.h" +#include "py/runtime.h" +#include "common-hal/wifi/__init__.h" +#include "shared-bindings/wifi/__init__.h" +#include "shared-bindings/wifi/Network.h" +#include "shared-bindings/wifi/Radio.h" +#include "shared-bindings/wifi/ScannedNetworks.h" + +#define NUM_SCAN (16) +static cyw43_ev_scan_result_t scan_results[NUM_SCAN]; +static uint8_t scan_put, scan_get; +static bool scan_full; + + +static void scan_result_clear() { + scan_put = scan_get = 0; + scan_full = false; +} + +static void scan_result_put(const cyw43_ev_scan_result_t *result) { + if (!scan_full) { + scan_results[scan_put] = *result; + scan_put = (scan_put + 1) % NUM_SCAN; + scan_full = (scan_put == scan_get); + } +} + +static bool scan_result_available() { + return scan_full || (scan_get != scan_put); +} + +static cyw43_ev_scan_result_t *scan_result_get(cyw43_ev_scan_result_t *result) { + if (!scan_result_available()) { + return NULL; + } + + *result = scan_results[scan_get]; + scan_get = (scan_get + 1) % NUM_SCAN; + scan_full = false; + return result; +} + +// Note: It's not OK to allocate memory in here, we can be called at a bad time +// which messes up the gc allocator +static int scan_result(void *env, const cyw43_ev_scan_result_t *result) { + wifi_scannednetworks_obj_t *self = common_hal_wifi_radio_obj.current_scan; + // scan ended or something + if (!self) { + return 0; + } + + scan_result_put(result); + + return 0; +} + +mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) { + // no results available, wait for some + while (!scan_result_available(self) && cyw43_wifi_scan_active(&cyw43_state)) { + RUN_BACKGROUND_TASKS; + if (mp_hal_is_interrupted()) { + return mp_const_none; + } + cyw43_arch_poll(); + } + + if (!scan_result_available(self)) { + common_hal_wifi_radio_obj.current_scan = NULL; + return mp_const_none; + } + + + wifi_network_obj_t *entry = m_new_obj(wifi_network_obj_t); + entry->base.type = &wifi_network_type; + scan_result_get(&entry->record); + + return MP_OBJ_FROM_PTR(entry); +} + +void wifi_scannednetworks_deinit(wifi_scannednetworks_obj_t *self) { + // there's actually no way to stop an ongoing scan in cyw43! + common_hal_wifi_radio_obj.current_scan = NULL; +} + +void wifi_scannednetworks_start_scan(wifi_scannednetworks_obj_t *self) { + cyw43_wifi_scan_options_t scan_options = {0}; + CHECK_CYW_RESULT(cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result)); +} diff --git a/ports/raspberrypi/common-hal/wifi/ScannedNetworks.h b/ports/raspberrypi/common-hal/wifi/ScannedNetworks.h new file mode 100644 index 0000000000..eae783ca4e --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/ScannedNetworks.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * Copyright (c) 2018 Artur Pacholec + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include + +#include "py/obj.h" +#include "cyw43.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t results; + bool done; +} wifi_scannednetworks_obj_t; + +void wifi_scannednetworks_deinit(wifi_scannednetworks_obj_t *self); +void wifi_scannednetworks_start_scan(wifi_scannednetworks_obj_t *self); diff --git a/ports/raspberrypi/common-hal/wifi/__init__.c b/ports/raspberrypi/common-hal/wifi/__init__.c new file mode 100644 index 0000000000..9a42320cf5 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/__init__.c @@ -0,0 +1,124 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "common-hal/wifi/__init__.h" +#include "shared-bindings/wifi/__init__.h" + +#include "shared-bindings/ipaddress/IPv4Address.h" +#include "shared-bindings/wifi/Monitor.h" +#include "shared-bindings/wifi/Radio.h" + +#include "py/mperrno.h" +#include "py/mpstate.h" +#include "py/runtime.h" + +wifi_radio_obj_t common_hal_wifi_radio_obj; + +#include "supervisor/port.h" +#include "supervisor/shared/status_bar.h" +#include "supervisor/workflow.h" + +static bool wifi_inited; +static bool wifi_ever_inited; +static bool wifi_user_initiated; + +void common_hal_wifi_init(bool user_initiated) { + wifi_radio_obj_t *self = &common_hal_wifi_radio_obj; + + if (wifi_inited) { + if (user_initiated && !wifi_user_initiated) { + common_hal_wifi_radio_set_enabled(self, true); + } + return; + } + wifi_inited = true; + wifi_user_initiated = user_initiated; + common_hal_wifi_radio_obj.base.type = &wifi_radio_type; + common_hal_wifi_radio_obj.current_scan = NULL; + + if (!wifi_ever_inited) { + } + wifi_ever_inited = true; + + // set station mode to avoid the default SoftAP + common_hal_wifi_radio_start_station(self); + // start wifi + common_hal_wifi_radio_set_enabled(self, true); +} + +void wifi_user_reset(void) { + if (wifi_user_initiated) { + // wifi_reset(); + wifi_user_initiated = false; + } +} + +void wifi_reset(void) { + if (!wifi_inited) { + return; + } + common_hal_wifi_monitor_deinit(MP_STATE_VM(wifi_monitor_singleton)); + wifi_radio_obj_t *radio = &common_hal_wifi_radio_obj; + common_hal_wifi_radio_obj.current_scan = NULL; + // common_hal_wifi_radio_set_enabled(radio, false); + supervisor_workflow_request_background(); +} + +void common_hal_wifi_gc_collect(void) { + common_hal_wifi_radio_gc_collect(&common_hal_wifi_radio_obj); +} + +void raise_cyw_error(int err) { + int mp_errno; + switch (err) { + case -CYW43_EIO: + mp_errno = MP_EIO; + break; + case -CYW43_EPERM: + mp_errno = MP_EPERM; + break; + case -CYW43_EINVAL: + mp_errno = MP_EINVAL; + break; + case -CYW43_ETIMEDOUT: + mp_errno = MP_ETIMEDOUT; + break; + default: + mp_raise_OSError_msg_varg(translate("Unkown error code %d"), err); + } + mp_raise_OSError(mp_errno); +} + +void ipaddress_ipaddress_to_lwip(mp_obj_t ip_address, ip_addr_t *lwip_ip_address) { + if (!mp_obj_is_type(ip_address, &ipaddress_ipv4address_type)) { + mp_raise_ValueError(translate("Only IPv4 addresses supported")); + } + mp_obj_t packed = common_hal_ipaddress_ipv4address_get_packed(ip_address); + size_t len; + const char *bytes = mp_obj_str_get_data(packed, &len); + + IP_ADDR4(lwip_ip_address, bytes[0], bytes[1], bytes[2], bytes[3]); +} diff --git a/ports/raspberrypi/common-hal/wifi/__init__.h b/ports/raspberrypi/common-hal/wifi/__init__.h new file mode 100644 index 0000000000..d34d631089 --- /dev/null +++ b/ports/raspberrypi/common-hal/wifi/__init__.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" +#include "py/mpconfig.h" +#include "lwip/ip_addr.h" + +void wifi_reset(void); +NORETURN void raise_cyw_error(int err); +#define CHECK_CYW_RESULT(x) do { int res = (x); if (res != 0) raise_cyw_error(res); } while (0) + +void ipaddress_ipaddress_to_lwip(mp_obj_t ip_address, ip_addr_t *lwip_ip_address); diff --git a/ports/raspberrypi/lwip_inc/arch/sys_arch.h b/ports/raspberrypi/lwip_inc/arch/sys_arch.h new file mode 100644 index 0000000000..8b1a393741 --- /dev/null +++ b/ports/raspberrypi/lwip_inc/arch/sys_arch.h @@ -0,0 +1 @@ +// empty diff --git a/ports/raspberrypi/lwip_inc/lwipopts.h b/ports/raspberrypi/lwip_inc/lwipopts.h new file mode 100644 index 0000000000..993e675d6b --- /dev/null +++ b/ports/raspberrypi/lwip_inc/lwipopts.h @@ -0,0 +1,92 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#define MEM_SIZE 4000 +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#define LWIP_TIMEVAL_PRIVATE 0 + +#endif /* __LWIPOPTS_H__ */ diff --git a/ports/raspberrypi/lwip_src/ping.c b/ports/raspberrypi/lwip_src/ping.c new file mode 100644 index 0000000000..d5173cab96 --- /dev/null +++ b/ports/raspberrypi/lwip_src/ping.c @@ -0,0 +1,390 @@ +/** + * @file + * Ping sender module + * + */ + +/* + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + */ + +/** + * This is an example of a "ping" sender (with raw API and socket API). + * It can be used as a start point to maintain opened a network connection, or + * like a network "watchdog" for your device. + * + */ + +#include "lwip/opt.h" + +#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */ + +#include "ping.h" + +#include "lwip/mem.h" +#include "lwip/raw.h" +#include "lwip/icmp.h" +#include "lwip/netif.h" +#include "lwip/sys.h" +#include "lwip/timeouts.h" +#include "lwip/inet_chksum.h" +#include "lwip/prot/ip4.h" + +#if PING_USE_SOCKETS +#include "lwip/sockets.h" +#include "lwip/inet.h" +#include +#endif /* PING_USE_SOCKETS */ + + +/** + * PING_DEBUG: Enable debugging for PING. + */ +#ifndef PING_DEBUG +#define PING_DEBUG LWIP_DBG_ON +#endif + +/** ping receive timeout - in milliseconds */ +#ifndef PING_RCV_TIMEO +#define PING_RCV_TIMEO 1000 +#endif + +/** ping delay - in milliseconds */ +#ifndef PING_DELAY +#define PING_DELAY 1000 +#endif + +/** ping identifier - must fit on a u16_t */ +#ifndef PING_ID +#define PING_ID 0xAFAF +#endif + +/** ping additional data size to include in the packet */ +#ifndef PING_DATA_SIZE +#define PING_DATA_SIZE 32 +#endif + +/** ping result action - no default action */ +#ifndef PING_RESULT +#define PING_RESULT(ping_ok) +#endif + +/* ping variables */ +static const ip_addr_t *ping_target; +u16_t ping_seq_num; +#ifdef LWIP_DEBUG +static u32_t ping_time; +#endif /* LWIP_DEBUG */ +#if !PING_USE_SOCKETS +static struct raw_pcb *ping_pcb; +#endif /* PING_USE_SOCKETS */ + +/** Prepare a echo ICMP request */ +void +ping_prepare_echo(struct icmp_echo_hdr *iecho, u16_t len) { + size_t i; + size_t data_len = len - sizeof(struct icmp_echo_hdr); + + ICMPH_TYPE_SET(iecho, ICMP_ECHO); + ICMPH_CODE_SET(iecho, 0); + iecho->chksum = 0; + iecho->id = PING_ID; + iecho->seqno = lwip_htons(++ping_seq_num); + + /* fill the additional data buffer with some data */ + for (i = 0; i < data_len; i++) { + ((char *)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; + } + + iecho->chksum = inet_chksum(iecho, len); +} + +#if PING_USE_SOCKETS + +/* Ping using the socket ip */ +err_t +ping_send(int s, const ip_addr_t *addr) { + int err; + struct icmp_echo_hdr *iecho; + struct sockaddr_storage to; + size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; + LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); + + #if LWIP_IPV6 + if (IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) { + /* todo: support ICMP6 echo */ + return ERR_VAL; + } + #endif /* LWIP_IPV6 */ + + iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size); + if (!iecho) { + return ERR_MEM; + } + + ping_prepare_echo(iecho, (u16_t)ping_size); + + #if LWIP_IPV4 + if (IP_IS_V4(addr)) { + struct sockaddr_in *to4 = (struct sockaddr_in *)&to; + to4->sin_len = sizeof(*to4); + to4->sin_family = AF_INET; + inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr)); + } + #endif /* LWIP_IPV4 */ + + #if LWIP_IPV6 + if (IP_IS_V6(addr)) { + struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to; + to6->sin6_len = sizeof(*to6); + to6->sin6_family = AF_INET6; + inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr)); + } + #endif /* LWIP_IPV6 */ + + err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr *)&to, sizeof(to)); + + mem_free(iecho); + + return err ? ERR_OK : ERR_VAL; +} + +static void +ping_recv(int s) { + char buf[64]; + int len; + struct sockaddr_storage from; + int fromlen = sizeof(from); + + while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen)) > 0) { + if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) { + ip_addr_t fromaddr; + memset(&fromaddr, 0, sizeof(fromaddr)); + + #if LWIP_IPV4 + if (from.ss_family == AF_INET) { + struct sockaddr_in *from4 = (struct sockaddr_in *)&from; + inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr); + IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4); + } + #endif /* LWIP_IPV4 */ + + #if LWIP_IPV6 + if (from.ss_family == AF_INET6) { + struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from; + inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr); + IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6); + } + #endif /* LWIP_IPV6 */ + + LWIP_DEBUGF(PING_DEBUG, ("ping: recv ")); + ip_addr_debug_print_val(PING_DEBUG, fromaddr); + LWIP_DEBUGF(PING_DEBUG, (" %"U32_F " ms\n", (sys_now() - ping_time))); + + /* todo: support ICMP6 echo */ + #if LWIP_IPV4 + if (IP_IS_V4_VAL(fromaddr)) { + struct ip_hdr *iphdr; + struct icmp_echo_hdr *iecho; + + iphdr = (struct ip_hdr *)buf; + iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); + if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { + /* do some ping result processing */ + PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); + return; + } else { + LWIP_DEBUGF(PING_DEBUG, ("ping: drop\n")); + } + } + #endif /* LWIP_IPV4 */ + } + fromlen = sizeof(from); + } + + if (len == 0) { + LWIP_DEBUGF(PING_DEBUG, ("ping: recv - %"U32_F " ms - timeout\n", (sys_now() - ping_time))); + } + + /* do some ping result processing */ + PING_RESULT(0); +} + +static void +ping_thread(void *arg) { + int s; + int ret; + + #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD + int timeout = PING_RCV_TIMEO; + #else + struct timeval timeout; + timeout.tv_sec = PING_RCV_TIMEO / 1000; + timeout.tv_usec = (PING_RCV_TIMEO % 1000) * 1000; + #endif + LWIP_UNUSED_ARG(arg); + + #if LWIP_IPV6 + if (IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) { + s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP); + } else { + s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6); + } + #else + s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP); + #endif + if (s < 0) { + return; + } + + ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + LWIP_ASSERT("setting receive timeout failed", ret == 0); + LWIP_UNUSED_ARG(ret); + + while (1) { + if (ping_send(s, ping_target) == ERR_OK) { + LWIP_DEBUGF(PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, ping_target); + LWIP_DEBUGF(PING_DEBUG, ("\n")); + + #ifdef LWIP_DEBUG + ping_time = sys_now(); + #endif /* LWIP_DEBUG */ + ping_recv(s); + } else { + LWIP_DEBUGF(PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, ping_target); + LWIP_DEBUGF(PING_DEBUG, (" - error\n")); + } + sys_msleep(PING_DELAY); + } +} + +#else /* PING_USE_SOCKETS */ + +/* Ping using the raw ip */ +static u8_t +ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) { + struct icmp_echo_hdr *iecho; + LWIP_UNUSED_ARG(arg); + LWIP_UNUSED_ARG(pcb); + LWIP_UNUSED_ARG(addr); + LWIP_ASSERT("p != NULL", p != NULL); + + if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && + pbuf_remove_header(p, PBUF_IP_HLEN) == 0) { + iecho = (struct icmp_echo_hdr *)p->payload; + + if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { + LWIP_DEBUGF(PING_DEBUG, ("ping: recv ")); + ip_addr_debug_print(PING_DEBUG, addr); + LWIP_DEBUGF(PING_DEBUG, (" %"U32_F " ms\n", (sys_now() - ping_time))); + + /* do some ping result processing */ + PING_RESULT(1); + pbuf_free(p); + return 1; /* eat the packet */ + } + /* not eaten, restore original packet */ + pbuf_add_header(p, PBUF_IP_HLEN); + } + + return 0; /* don't eat the packet */ +} + +int +ping_send(struct raw_pcb *raw, const ip_addr_t *addr) { + struct pbuf *p; + struct icmp_echo_hdr *iecho; + size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; + + LWIP_DEBUGF(PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, addr); + LWIP_DEBUGF(PING_DEBUG, ("\n")); + LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff); + + p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM); + if (!p) { + return 0; + } + if ((p->len == p->tot_len) && (p->next == NULL)) { + iecho = (struct icmp_echo_hdr *)p->payload; + + ping_prepare_echo(iecho, (u16_t)ping_size); + + raw_sendto(raw, p, addr); + #ifdef LWIP_DEBUG + ping_time = sys_now(); + #endif /* LWIP_DEBUG */ + } + pbuf_free(p); + return 1; +} + +static void +ping_timeout(void *arg) { + struct raw_pcb *pcb = (struct raw_pcb *)arg; + + LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL); + + ping_send(pcb, ping_target); + + sys_timeout(PING_DELAY, ping_timeout, pcb); +} + +static void +ping_raw_init(void) { + if (ping_pcb) { + return; + } + ping_pcb = raw_new(IP_PROTO_ICMP); + LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); + + raw_recv(ping_pcb, ping_recv, NULL); + raw_bind(ping_pcb, IP_ADDR_ANY); + sys_timeout(PING_DELAY, ping_timeout, ping_pcb); +} + +static void +ping_send_now(void) { + LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); + ping_send(ping_pcb, ping_target); +} + +#endif /* PING_USE_SOCKETS */ + +void +ping_init(const ip_addr_t *ping_addr) { + ping_target = ping_addr; + + #if PING_USE_SOCKETS + sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); + #else /* PING_USE_SOCKETS */ + ping_raw_init(); + #endif /* PING_USE_SOCKETS */ +} + +#endif /* LWIP_RAW */ diff --git a/ports/raspberrypi/lwip_src/ping.h b/ports/raspberrypi/lwip_src/ping.h new file mode 100644 index 0000000000..abbae9d086 --- /dev/null +++ b/ports/raspberrypi/lwip_src/ping.h @@ -0,0 +1,25 @@ +#ifndef LWIP_PING_H +#define LWIP_PING_H + +#include "lwip/raw.h" +#include "lwip/ip_addr.h" +#include "lwip/prot/icmp.h" + +/** + * PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used + */ +#ifndef PING_USE_SOCKETS +#define PING_USE_SOCKETS LWIP_SOCKET +#endif + +void ping_init(const ip_addr_t *ping_addr); +void ping_prepare_echo(struct icmp_echo_hdr *iecho, u16_t len); + +extern u16_t ping_seq_num; +#if !PING_USE_SOCKETS +void ping_set_target(const ip_addr_t *ping_addr); +int ping_send(struct raw_pcb *raw, const ip_addr_t *addr); +// u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr); +#endif /* !PING_USE_SOCKETS */ + +#endif /* LWIP_PING_H */ diff --git a/ports/raspberrypi/mpconfigport.h b/ports/raspberrypi/mpconfigport.h index fd09d8a9ac..9bc97f6a78 100644 --- a/ports/raspberrypi/mpconfigport.h +++ b/ports/raspberrypi/mpconfigport.h @@ -49,4 +49,12 @@ mp_obj_t background_pio[NUM_DMA_CHANNELS]; \ CIRCUITPY_COMMON_ROOT_POINTERS; +#if CIRCUITPY_CYW43 +#include "pico/cyw43_arch.h" +#undef _L // interferes with ulab, indirectly defined in ctype.h, is ulab bug to use identifier _L +#define MICROPY_PY_LWIP_ENTER cyw43_arch_lwip_begin +#define MICROPY_PY_LWIP_REENTER MICROPY_PY_LWIP_ENTER +#define MICROPY_PY_LWIP_EXIT cyw43_arch_lwip_end +#endif + #endif // __INCLUDED_MPCONFIGPORT_H diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 2e1fed7a25..94e57c2028 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -41,6 +41,10 @@ #include "shared-bindings/rtc/__init__.h" #include "shared-bindings/pwmio/PWMOut.h" +#if CIRCUITPY_WIFI +#include "common-hal/wifi/__init__.h" +#endif + #include "common-hal/rtc/RTC.h" #include "common-hal/busio/UART.h" @@ -134,15 +138,8 @@ safe_mode_t port_init(void) { never_reset_pin_number(29); if (cyw43_arch_init()) { serial_write("WiFi init failed\n"); - return -1; } else { cyw_ever_init = true; - for (int i = 3; i--;) { - cyw43_arch_gpio_put(0, 1); - sleep_ms(100); - cyw43_arch_gpio_put(0, 0); - sleep_ms(100); - } } #endif if (board_requests_safe_mode()) { @@ -182,6 +179,10 @@ void reset_port(void) { audio_dma_reset(); #endif + #if CIRCUITPY_WIFI + wifi_reset(); + #endif + reset_all_pins(); } diff --git a/py/objgenerator.c b/py/objgenerator.c index b18101cfa1..c9af11fbb5 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -330,7 +330,8 @@ STATIC mp_obj_t gen_instance_await(mp_obj_t self_in) { if (!self->coroutine_generator) { // Pretend like a generator does not have this coroutine behavior. // Pay no attention to the dir() behind the curtain - mp_raise_AttributeError(MP_ERROR_TEXT("type object 'generator' has no attribute '__await__'")); + mp_raise_msg_varg(&mp_type_AttributeError, MP_ERROR_TEXT("type object '%q' has no attribute '%q'"), + MP_QSTR_generator, MP_QSTR___await__); } // You can directly call send on a coroutine generator or you can __await__ then send on the return of that. return self; diff --git a/shared-bindings/wifi/Monitor.h b/shared-bindings/wifi/Monitor.h index 38c52a05e7..b828616ddb 100644 --- a/shared-bindings/wifi/Monitor.h +++ b/shared-bindings/wifi/Monitor.h @@ -29,7 +29,7 @@ #include "common-hal/wifi/Monitor.h" -const mp_obj_type_t wifi_monitor_type; +extern const mp_obj_type_t wifi_monitor_type; void common_hal_wifi_monitor_construct(wifi_monitor_obj_t *self, uint8_t channel, size_t queue); diff --git a/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h index 0f07e7b555..574d690f37 100644 --- a/shared-bindings/wifi/Network.h +++ b/shared-bindings/wifi/Network.h @@ -33,7 +33,7 @@ #include "py/objstr.h" -const mp_obj_type_t wifi_network_type; +extern const mp_obj_type_t wifi_network_type; extern mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self); extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self); diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index bcd998d806..aecb1b561f 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -89,7 +89,7 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) mp_arg_validate_length_range(hostname.len, 1, 253, MP_QSTR_hostname); - #ifndef CONFIG_IDF_TARGET_ESP32C3 + #if 0 // ndef CONFIG_IDF_TARGET_ESP32C3 regex_t regex; // validate hostname according to RFC 1123 regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); if (regexec(®ex, hostname.buf, 0, NULL, 0)) { diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h index 312e9e5395..479ce1a1b3 100644 --- a/shared-bindings/wifi/Radio.h +++ b/shared-bindings/wifi/Radio.h @@ -33,7 +33,7 @@ #include "py/objstr.h" -const mp_obj_type_t wifi_radio_type; +extern const mp_obj_type_t wifi_radio_type; typedef enum { // 0 is circuitpython-specific; 1-53 are IEEE; 200+ are Espressif diff --git a/tools/codeformat.py b/tools/codeformat.py index edefbc8ddc..3965e838a2 100644 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -67,6 +67,8 @@ EXCLUSIONS = [ "tests/**/repl_*.py", # needs careful attention before applying automatic formatting "tests/basics/*.py", + # don't reindent this third-party code we vendored in + "ports/raspberrypi/lwip_src", ] # None of the standard Python path matching routines implement the matching diff --git a/tools/describe b/tools/describe index 7fd624abe3..e20c2ffa44 100755 --- a/tools/describe +++ b/tools/describe @@ -1,3 +1,3 @@ #!/bin/sh # Add any supplied arguments. -git describe --first-parent --dirty --tags --always --match "[1-9].*" "$@" +git describe --first-parent --dirty --tags --match "[1-9].*" "$@" From a7a1bd788001eca4e71fd49b4ae2a2d6e75071a9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 17 Sep 2022 09:01:31 -0500 Subject: [PATCH 06/18] Implement DNS resolution ``` >>> s = socketpool.SocketPool(wifi.radio) >>> s.getaddrinfo("google.com", 80) [(0, 0, 0, '', ('142.250.81.206', 80))] ``` --- ports/raspberrypi/Makefile | 1 + .../raspberry_pi_pico_w/mpconfigboard.mk | 2 +- .../common-hal/socketpool/Socket.c | 165 ++++++++++++++++++ .../common-hal/socketpool/Socket.h | 77 ++++++++ .../common-hal/socketpool/SocketPool.c | 97 ++++++++++ .../common-hal/socketpool/SocketPool.h | 33 ++++ .../common-hal/socketpool/__init__.c | 33 ++++ .../common-hal/socketpool/__init__.h | 27 +++ ports/raspberrypi/common-hal/wifi/Radio.c | 14 +- ports/raspberrypi/lwip_inc/lwipopts.h | 2 + ports/raspberrypi/mpconfigport.h | 5 +- shared-bindings/socketpool/Socket.c | 2 +- 12 files changed, 446 insertions(+), 12 deletions(-) create mode 100644 ports/raspberrypi/common-hal/socketpool/Socket.c create mode 100644 ports/raspberrypi/common-hal/socketpool/Socket.h create mode 100644 ports/raspberrypi/common-hal/socketpool/SocketPool.c create mode 100644 ports/raspberrypi/common-hal/socketpool/SocketPool.h create mode 100644 ports/raspberrypi/common-hal/socketpool/__init__.c create mode 100644 ports/raspberrypi/common-hal/socketpool/__init__.h diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index bdfdb9a9b7..28659e9cb2 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -79,6 +79,7 @@ SRC_SDK_CYW43 := \ src/rp2_common/pico_lwip/random.c \ SRC_LWIP := \ + shared/netutils/netutils.c \ $(wildcard sdk/lib/lwip/src/core/*.c) \ $(wildcard sdk/lib/lwip/src/core/ipv4/*.c) \ sdk/lib/lwip/src/netif/ethernet.c \ diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk index 7acd728bcc..2b114b3954 100644 --- a/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk @@ -15,7 +15,7 @@ CIRCUITPY_SSL = 0 CIRCUITPY_HASHLIB = 0 CIRCUITPY_WEB_WORKFLOW = 0 CIRCUITPY_MDNS = 0 -CIRCUITPY_SOCKETPOOL = 0 +CIRCUITPY_SOCKETPOOL = 1 CIRCUITPY_WIFI = 1 CFLAGS += -DCYW43_PIN_WL_HOST_WAKE=24 -DCYW43_PIN_WL_REG_ON=23 -DCYW43_WL_GPIO_COUNT=3 -DCYW43_WL_GPIO_LED_PIN=0 diff --git a/ports/raspberrypi/common-hal/socketpool/Socket.c b/ports/raspberrypi/common-hal/socketpool/Socket.c new file mode 100644 index 0000000000..a1b77533a2 --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/Socket.c @@ -0,0 +1,165 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Lucian Copeland for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/socketpool/Socket.h" + +#include "shared/runtime/interrupt_char.h" +#include "py/mperrno.h" +#include "py/runtime.h" +#include "shared-bindings/socketpool/SocketPool.h" +#include "supervisor/port.h" +#include "supervisor/shared/tick.h" +#include "supervisor/workflow.h" + +#include "lwip/err.h" +#include "lwip/sys.h" +#include "lwip/netdb.h" + +#define MAX_SOCKETS (8) + +STATIC mp_obj_t open_socket_objs[MAX_SOCKETS]; +STATIC bool user_socket[MAX_SOCKETS]; + +void socket_user_reset(void) { + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { + if (open_socket_objs[i] && user_socket[i]) { + // shut 'er down + } + } +} + +// The writes below send an event to the socket select task so that it redoes the +// select with the new open socket set. + +STATIC bool register_open_socket(mp_obj_t obj) { + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { + if (!open_socket_objs[i]) { + open_socket_objs[i] = obj; + user_socket[i] = false; + return true; + } + } + return false; +} + +STATIC void unregister_open_socket(mp_obj_t obj) { + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { + if (open_socket_objs[i] == obj) { + open_socket_objs[i] = NULL; + user_socket[i] = false; + } + } +} + +STATIC void mark_user_socket(mp_obj_t obj) { + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { + if (open_socket_objs[i] == obj) { + user_socket[i] = true; + return; + } + } +} + +bool socketpool_socket(socketpool_socketpool_obj_t *self, + socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type, + socketpool_socket_obj_t *sock) { + mp_raise_NotImplementedError(NULL); +} + +socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self, + socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) { + mp_raise_NotImplementedError(NULL); +} + +int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_t *port) { + mp_raise_NotImplementedError(NULL); +} + +socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_obj_t *self, + uint8_t *ip, uint32_t *port) { + mp_raise_NotImplementedError(NULL); +} + +bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self, + const char *host, size_t hostlen, uint32_t port) { + mp_raise_NotImplementedError(NULL); +} + +void socketpool_socket_close(socketpool_socket_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_socketpool_socket_close(socketpool_socket_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *self, + const char *host, size_t hostlen, uint32_t port) { + mp_raise_NotImplementedError(NULL); +} + +bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *self) { + mp_raise_NotImplementedError(NULL); +} + +bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t *self, int backlog) { + mp_raise_NotImplementedError(NULL); +} + +mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *self, + uint8_t *buf, uint32_t len, uint8_t *ip, uint32_t *port) { + mp_raise_NotImplementedError(NULL); +} + +int socketpool_socket_recv_into(socketpool_socket_obj_t *self, + const uint8_t *buf, uint32_t len) { + mp_raise_NotImplementedError(NULL); +} + +mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { + mp_raise_NotImplementedError(NULL); +} + +int socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { + mp_raise_NotImplementedError(NULL); +} + +mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { + mp_raise_NotImplementedError(NULL); +} + +mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *self, + const char *host, size_t hostlen, uint32_t port, const uint8_t *buf, uint32_t len) { + mp_raise_NotImplementedError(NULL); +} + +void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t *self, uint32_t timeout_ms) { + mp_raise_NotImplementedError(NULL); +} diff --git a/ports/raspberrypi/common-hal/socketpool/Socket.h b/ports/raspberrypi/common-hal/socketpool/Socket.h new file mode 100644 index 0000000000..f582b77a9b --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/Socket.h @@ -0,0 +1,77 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2019 Damien P. George + * Copyright (c) 2015 Galen Hazelwood + * Copyright (c) 2015-2017 Paul Sokolovsky + * Copyright (c) 2022 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +#include "common-hal/socketpool/SocketPool.h" + +typedef struct _lwip_socket_obj_t { + mp_obj_base_t base; + + volatile union { + struct tcp_pcb *tcp; + struct udp_pcb *udp; + struct raw_pcb *raw; + } pcb; + volatile union { + struct pbuf *pbuf; + struct { + uint8_t alloc; + uint8_t iget; + uint8_t iput; + union { + struct tcp_pcb *item; // if alloc == 0 + struct tcp_pcb **array; // if alloc != 0 + } tcp; + } connection; + } incoming; + mp_obj_t callback; + byte peer[4]; + mp_uint_t peer_port; + mp_uint_t timeout; + uint16_t recv_offset; + + uint8_t domain; + uint8_t type; + + #define STATE_NEW 0 + #define STATE_LISTENING 1 + #define STATE_CONNECTING 2 + #define STATE_CONNECTED 3 + #define STATE_PEER_CLOSED 4 + #define STATE_ACTIVE_UDP 5 + // Negative value is lwIP error + int8_t state; + + socketpool_socketpool_obj_t *pool; +} socketpool_socket_obj_t; + +void socket_user_reset(void); diff --git a/ports/raspberrypi/common-hal/socketpool/SocketPool.c b/ports/raspberrypi/common-hal/socketpool/SocketPool.c new file mode 100644 index 0000000000..723b753064 --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/SocketPool.c @@ -0,0 +1,97 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/socketpool/SocketPool.h" +#include "common-hal/socketpool/Socket.h" +#include "shared/runtime/interrupt_char.h" +#include "py/runtime.h" +#include "shared-bindings/wifi/__init__.h" + +#include "lwip/dns.h" +#include "lwip/inet.h" + +void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *self, mp_obj_t radio) { + if (radio != MP_OBJ_FROM_PTR(&common_hal_wifi_radio_obj)) { + mp_raise_ValueError(translate("SocketPool can only be used with wifi.radio")); + } +} + +// common_hal_socketpool_socket is in socketpool/Socket.c to centralize open socket tracking. + +typedef struct _getaddrinfo_state_t { + volatile int status; + volatile ip_addr_t ipaddr; +} getaddrinfo_state_t; + +STATIC void lwip_getaddrinfo_cb(const char *name, const ip_addr_t *ipaddr, void *arg) { + getaddrinfo_state_t *state = arg; + if (ipaddr != NULL) { + state->status = 1; + state->ipaddr = *ipaddr; + } else { + // error + state->status = -2; + } +} + +mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self, + const char *host) { + + getaddrinfo_state_t state; + state.status = 0; + + MICROPY_PY_LWIP_ENTER + err_t ret = dns_gethostbyname(host, (ip_addr_t *)&state.ipaddr, lwip_getaddrinfo_cb, &state); + MICROPY_PY_LWIP_EXIT + + switch (ret) { + case ERR_OK: + // cached + state.status = 1; + break; + case ERR_INPROGRESS: + while (state.status == 0) { + RUN_BACKGROUND_TASKS; + if (mp_hal_is_interrupted()) { + break; + } + } + break; + default: + state.status = ret; + } + + if (state.status < 0) { + // TODO: CPython raises gaierror, we raise with native lwIP negative error + // values, to differentiate from normal errno's at least in such way. + mp_raise_OSError(state.status); + } + + char ip_str[IP4ADDR_STRLEN_MAX]; + inet_ntoa_r(state.ipaddr, ip_str, IP4ADDR_STRLEN_MAX); + mp_obj_t ip_obj = mp_obj_new_str(ip_str, strlen(ip_str)); + return ip_obj; +} diff --git a/ports/raspberrypi/common-hal/socketpool/SocketPool.h b/ports/raspberrypi/common-hal/socketpool/SocketPool.h new file mode 100644 index 0000000000..ea0679b5eb --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/SocketPool.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} socketpool_socketpool_obj_t; diff --git a/ports/raspberrypi/common-hal/socketpool/__init__.c b/ports/raspberrypi/common-hal/socketpool/__init__.c new file mode 100644 index 0000000000..595977d24f --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/__init__.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/socketpool/__init__.h" + +#include "common-hal/socketpool/Socket.h" + +void socketpool_user_reset(void) { + socket_user_reset(); +} diff --git a/ports/raspberrypi/common-hal/socketpool/__init__.h b/ports/raspberrypi/common-hal/socketpool/__init__.h new file mode 100644 index 0000000000..32f1fe1dcb --- /dev/null +++ b/ports/raspberrypi/common-hal/socketpool/__init__.h @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index f614d3185f..7d9dcbee23 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -271,17 +271,17 @@ mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, ipaddress_ipaddress_to_lwip(ip_address, &ping_addr); struct raw_pcb *ping_pcb; - MICROPY_PY_LWIP_ENTER(); - ping_pcb = raw_new(IP_PROTO_ICMP); + MICROPY_PY_LWIP_ENTER + ping_pcb = raw_new(IP_PROTO_ICMP); if (!ping_pcb) { - MICROPY_PY_LWIP_EXIT(); + MICROPY_PY_LWIP_EXIT return -1; } raw_recv(ping_pcb, ping_recv, NULL); raw_bind(ping_pcb, IP_ADDR_ANY); - MICROPY_PY_LWIP_EXIT(); + MICROPY_PY_LWIP_EXIT - ping_received = false; + ping_received = false; ping_send(ping_pcb, &ping_addr); size_t timeout_ms = (size_t)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000); uint64_t start = port_get_raw_ticks(NULL); @@ -298,9 +298,9 @@ mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, result = now - start; } - MICROPY_PY_LWIP_ENTER(); + MICROPY_PY_LWIP_ENTER; raw_remove(ping_pcb); - MICROPY_PY_LWIP_EXIT(); + MICROPY_PY_LWIP_EXIT; return result; } diff --git a/ports/raspberrypi/lwip_inc/lwipopts.h b/ports/raspberrypi/lwip_inc/lwipopts.h index 993e675d6b..ac4dd1564b 100644 --- a/ports/raspberrypi/lwip_inc/lwipopts.h +++ b/ports/raspberrypi/lwip_inc/lwipopts.h @@ -89,4 +89,6 @@ #define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_NO_CTYPE_H 1 + #endif /* __LWIPOPTS_H__ */ diff --git a/ports/raspberrypi/mpconfigport.h b/ports/raspberrypi/mpconfigport.h index 9bc97f6a78..7f2b75a81b 100644 --- a/ports/raspberrypi/mpconfigport.h +++ b/ports/raspberrypi/mpconfigport.h @@ -51,10 +51,9 @@ #if CIRCUITPY_CYW43 #include "pico/cyw43_arch.h" -#undef _L // interferes with ulab, indirectly defined in ctype.h, is ulab bug to use identifier _L -#define MICROPY_PY_LWIP_ENTER cyw43_arch_lwip_begin +#define MICROPY_PY_LWIP_ENTER cyw43_arch_lwip_begin(); #define MICROPY_PY_LWIP_REENTER MICROPY_PY_LWIP_ENTER -#define MICROPY_PY_LWIP_EXIT cyw43_arch_lwip_end +#define MICROPY_PY_LWIP_EXIT cyw43_arch_lwip_end(); #endif #endif // __INCLUDED_MPCONFIGPORT_H diff --git a/shared-bindings/socketpool/Socket.c b/shared-bindings/socketpool/Socket.c index 716ff673ee..304ba22e33 100644 --- a/shared-bindings/socketpool/Socket.c +++ b/shared-bindings/socketpool/Socket.c @@ -184,7 +184,7 @@ STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t data_ mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_WRITE); byte ip[4]; - mp_uint_t port; + uint32_t port; mp_int_t ret = common_hal_socketpool_socket_recvfrom_into(self, (byte *)bufinfo.buf, bufinfo.len, ip, &port); mp_obj_t tuple_contents[2]; From ff7731491e420781712822d10befe95e0c0fb9ca Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 21 Sep 2022 15:11:52 -0500 Subject: [PATCH 07/18] Implement enough of socketpool to do ntp and non-https requests --- .gitmodules | 6 + ports/raspberrypi/Makefile | 41 +- .../common-hal/socketpool/Socket.c | 912 +++++++++++++++++- .../common-hal/socketpool/SocketPool.c | 19 +- .../common-hal/socketpool/SocketPool.h | 2 + ports/raspberrypi/lib/cyw43-driver | 1 + ports/raspberrypi/lib/lwip | 1 + ports/raspberrypi/pioasm/CMakeLists.txt | 8 + shared-bindings/digitalio/DigitalInOut.c | 10 +- 9 files changed, 937 insertions(+), 63 deletions(-) create mode 160000 ports/raspberrypi/lib/cyw43-driver create mode 160000 ports/raspberrypi/lib/lwip create mode 100644 ports/raspberrypi/pioasm/CMakeLists.txt diff --git a/.gitmodules b/.gitmodules index e0d0a262ca..875b03dab8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -310,3 +310,9 @@ [submodule "ports/espressif/esp32-camera"] path = ports/espressif/esp32-camera url = https://github.com/adafruit/esp32-camera/ +[submodule "ports/raspberrypi/lib/cyw43-driver"] + path = ports/raspberrypi/lib/cyw43-driver + url = https://github.com/georgerobotics/cyw43-driver.git +[submodule "ports/raspberrypi/lib/lwip"] + path = ports/raspberrypi/lib/lwip + url = https://github.com/lwip-tcpip/lwip.git diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 28659e9cb2..cf3967fee4 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -59,18 +59,14 @@ HAL_DIR=hal/$(MCU_SERIES) ifeq ($(CIRCUITPY_CYW43),1) INC_CYW43 := \ - -isystem sdk/lib/cyw43-driver/firmware \ - -isystem sdk/lib/cyw43-driver/src \ - -isystem sdk/lib/lwip/src/include \ + -isystem lib/cyw43-driver/firmware \ + -isystem lib/cyw43-driver/src \ + -isystem lib/lwip/src/include \ -isystem sdk/src/rp2_common/pico_cyw43_arch/include/ \ -isystem sdk/src/rp2_common/pico_lwip/include/ \ -CFLAGS_CYW43 := -DCYW43_LWIP=1 -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 -DCYW43_USE_SPI -DIGNORE_GPIO25 +CFLAGS_CYW43 := -DCYW43_LWIP=1 -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 -DCYW43_USE_SPI -DIGNORE_GPIO25 -DCYW43_LOGIC_DEBUG=0 SRC_SDK_CYW43 := \ - lib/cyw43-driver/src/cyw43_ctrl.c \ - lib/cyw43-driver/src/cyw43_ll.c \ - lib/cyw43-driver/src/cyw43_lwip.c \ - lib/cyw43-driver/src/cyw43_stats.c \ src/common/pico_sync/sem.c \ src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.c \ src/rp2_common/pico_cyw43_arch/cyw43_arch.c \ @@ -80,19 +76,24 @@ SRC_SDK_CYW43 := \ SRC_LWIP := \ shared/netutils/netutils.c \ - $(wildcard sdk/lib/lwip/src/core/*.c) \ - $(wildcard sdk/lib/lwip/src/core/ipv4/*.c) \ - sdk/lib/lwip/src/netif/ethernet.c \ + $(wildcard lib/lwip/src/core/*.c) \ + $(wildcard lib/lwip/src/core/ipv4/*.c) \ + lib/lwip/src/netif/ethernet.c \ $(wildcard lwip_src/*.c) \ -SRC_CYW43 := $(wildcard bindings/cyw43/*.c) +SRC_CYW43 := \ + $(wildcard bindings/cyw43/*.c) \ + lib/cyw43-driver/src/cyw43_stats.c \ + lib/cyw43-driver/src/cyw43_ctrl.c \ + lib/cyw43-driver/src/cyw43_ll.c \ + lib/cyw43-driver/src/cyw43_lwip.c \ -PIOASM = $(BUILD)/sdk/pioasm/pioasm +PIOASM = $(BUILD)/pioasm/pioasm/pioasm .PHONY: PioasmBuild PioasmBuild: $(PIOASM) $(PIOASM): - $(Q)cmake -S sdk -B $(BUILD)/sdk - $(Q)make -C $(BUILD)/sdk PioasmBuild + $(Q)cmake -S pioasm -B $(BUILD)/pioasm + $(Q)$(MAKE) -C $(BUILD)/pioasm PioasmBuild $(BUILD)/cyw43_bus_pio_spi.pio.h: sdk/src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.pio $(PIOASM) $(Q)$(PIOASM) -o c-sdk $< $@ @@ -100,13 +101,13 @@ $(BUILD)/sdk/src/rp2_common/cyw43_driver/cyw43_bus_pio_spi.o: $(BUILD)/cyw43_bus CYW43_FIRMWARE_BIN = 43439A0-7.95.49.00.combined -$(BUILD)/cyw43_resource.o: sdk/lib/cyw43-driver/firmware/$(CYW43_FIRMWARE_BIN) +$(BUILD)/cyw43_resource.o: lib/cyw43-driver/firmware/$(CYW43_FIRMWARE_BIN) $(Q)$(OBJCOPY) -I binary -O elf32-littlearm -B arm \ --readonly-text \ --rename-section .data=.big_const,contents,alloc,load,readonly,data \ - --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_start=fw_43439A0_7_95_49_00_start \ - --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_size=fw_43439A0_7_95_49_00_size \ - --redefine-sym _binary_sdk_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_end=fw_43439A0_7_95_49_00_end \ + --redefine-sym _binary_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_start=fw_43439A0_7_95_49_00_start \ + --redefine-sym _binary_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_size=fw_43439A0_7_95_49_00_size \ + --redefine-sym _binary_lib_cyw43_driver_firmware_43439A0_7_95_49_00_combined_end=fw_43439A0_7_95_49_00_end \ $< $@ OBJ_CYW43 := $(BUILD)/cyw43_resource.o # need to do the equivalent of this in cmake @@ -270,7 +271,7 @@ SRC_SDK := \ $(SRC_SDK_CYW43) \ SRC_SDK := $(addprefix sdk/, $(SRC_SDK)) -$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK)): CFLAGS += -Wno-missing-prototypes -Wno-undef +$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef SRC_C += \ boards/$(BOARD)/board.c \ diff --git a/ports/raspberrypi/common-hal/socketpool/Socket.c b/ports/raspberrypi/common-hal/socketpool/Socket.c index a1b77533a2..05c8212d69 100644 --- a/ports/raspberrypi/common-hal/socketpool/Socket.c +++ b/ports/raspberrypi/common-hal/socketpool/Socket.c @@ -3,7 +3,11 @@ * * The MIT License (MIT) * + * Copyright (c) 2013-2019 Damien P. George + * Copyright (c) 2015 Galen Hazelwood + * Copyright (c) 2015-2017 Paul Sokolovsky * Copyright (c) 2020 Lucian Copeland for Adafruit Industries + * Copyright (c) 2022 Jeff Epler for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,27 +30,582 @@ #include "shared-bindings/socketpool/Socket.h" -#include "shared/runtime/interrupt_char.h" +#include "py/gc.h" #include "py/mperrno.h" +#include "py/mphal.h" #include "py/runtime.h" +#include "py/stream.h" #include "shared-bindings/socketpool/SocketPool.h" +#include "shared/runtime/interrupt_char.h" #include "supervisor/port.h" #include "supervisor/shared/tick.h" #include "supervisor/workflow.h" +#include "lwip/dns.h" #include "lwip/err.h" -#include "lwip/sys.h" +#include "lwip/igmp.h" +#include "lwip/init.h" #include "lwip/netdb.h" +#include "lwip/priv/tcp_priv.h" +#include "lwip/raw.h" +#include "lwip/sys.h" +#include "lwip/tcp.h" +#include "lwip/timeouts.h" +#include "lwip/udp.h" + +#define MICROPY_PY_LWIP_SOCK_RAW (1) + +#if 0 // print debugging info +#define DEBUG_printf DEBUG_printf +#else // don't print debugging info +#define DEBUG_printf(...) (void)0 +#endif + +// Timeout between closing a TCP socket and doing a tcp_abort on that +// socket, if the connection isn't closed cleanly in that time. +#define MICROPY_PY_LWIP_TCP_CLOSE_TIMEOUT_MS (10000) + +// All socket options should be globally distinct, +// because we ignore option levels for efficiency. +#define IP_ADD_MEMBERSHIP 0x400 +#define IP_DROP_MEMBERSHIP 0x401 + +/******************************************************************************/ +// Table to convert lwIP err_t codes to socket errno codes, from the lwIP +// socket API. + +// Extension to lwIP error codes +// Matches lwIP 2.0.3 +#undef _ERR_BADF +#define _ERR_BADF -17 +static const int error_lookup_table[] = { + 0, /* ERR_OK 0 No error, everything OK */ + MP_ENOMEM, /* ERR_MEM -1 Out of memory error */ + MP_ENOBUFS, /* ERR_BUF -2 Buffer error */ + MP_EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */ + MP_EHOSTUNREACH, /* ERR_RTE -4 Routing problem */ + MP_EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */ + MP_EINVAL, /* ERR_VAL -6 Illegal value */ + MP_EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block */ + MP_EADDRINUSE, /* ERR_USE -8 Address in use */ + MP_EALREADY, /* ERR_ALREADY -9 Already connecting */ + MP_EALREADY, /* ERR_ISCONN -10 Conn already established */ + MP_ENOTCONN, /* ERR_CONN -11 Not connected */ + -1, /* ERR_IF -12 Low-level netif error */ + MP_ECONNABORTED, /* ERR_ABRT -13 Connection aborted */ + MP_ECONNRESET, /* ERR_RST -14 Connection reset */ + MP_ENOTCONN, /* ERR_CLSD -15 Connection closed */ + MP_EIO, /* ERR_ARG -16 Illegal argument. */ + MP_EBADF, /* _ERR_BADF -17 Closed socket (null pcb) */ +}; + +/*******************************************************************************/ +// The socket object provided by lwip.socket. + +#define MOD_NETWORK_AF_INET (SOCKETPOOL_AF_INET) +#define MOD_NETWORK_AF_INET6 (SOCKETPOOL_AF_INET6) + +#define MOD_NETWORK_SOCK_STREAM (SOCKETPOOL_SOCK_STREAM) +#define MOD_NETWORK_SOCK_DGRAM (SOCKETPOOL_SOCK_DGRAM) +#define MOD_NETWORK_SOCK_RAW (SOCKETPOOL_SOCK_RAW) #define MAX_SOCKETS (8) -STATIC mp_obj_t open_socket_objs[MAX_SOCKETS]; +static inline void poll_sockets(void) { + #ifdef MICROPY_EVENT_POLL_HOOK + MICROPY_EVENT_POLL_HOOK; + #else + mp_hal_delay_ms(1); + #endif +} + +STATIC struct tcp_pcb *volatile *lwip_socket_incoming_array(socketpool_socket_obj_t *socket) { + if (socket->incoming.connection.alloc == 0) { + return &socket->incoming.connection.tcp.item; + } else { + return &socket->incoming.connection.tcp.array[0]; + } +} + +STATIC void lwip_socket_free_incoming(socketpool_socket_obj_t *socket) { + bool socket_is_listener = + socket->type == MOD_NETWORK_SOCK_STREAM + && socket->pcb.tcp->state == LISTEN; + + if (!socket_is_listener) { + if (socket->incoming.pbuf != NULL) { + pbuf_free(socket->incoming.pbuf); + socket->incoming.pbuf = NULL; + } + } else { + uint8_t alloc = socket->incoming.connection.alloc; + struct tcp_pcb *volatile *tcp_array = lwip_socket_incoming_array(socket); + for (uint8_t i = 0; i < alloc; ++i) { + // Deregister callback and abort + if (tcp_array[i] != NULL) { + tcp_poll(tcp_array[i], NULL, 0); + tcp_abort(tcp_array[i]); + tcp_array[i] = NULL; + } + } + } +} + +/*******************************************************************************/ +// Callback functions for the lwIP raw API. + +static inline void exec_user_callback(socketpool_socket_obj_t *socket) { + #if 0 + if (socket->callback != MP_OBJ_NULL) { + // Schedule the user callback to execute outside the lwIP context + mp_sched_schedule(socket->callback, MP_OBJ_FROM_PTR(socket)); + } + #endif +} + +#if MICROPY_PY_LWIP_SOCK_RAW +// Callback for incoming raw packets. +#if LWIP_VERSION_MAJOR < 2 +STATIC u8_t _lwip_raw_incoming(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr) +#else +STATIC u8_t _lwip_raw_incoming(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) +#endif +{ + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + + if (socket->incoming.pbuf != NULL) { + pbuf_free(p); + } else { + socket->incoming.pbuf = p; + memcpy(&socket->peer, addr, sizeof(socket->peer)); + } + return 1; // we ate the packet +} +#endif + +// Callback for incoming UDP packets. We simply stash the packet and the source address, +// in case we need it for recvfrom. +#if LWIP_VERSION_MAJOR < 2 +STATIC void _lwip_udp_incoming(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port) +#else +STATIC void _lwip_udp_incoming(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) +#endif +{ + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + + if (socket->incoming.pbuf != NULL) { + // That's why they call it "unreliable". No room in the inn, drop the packet. + pbuf_free(p); + } else { + socket->incoming.pbuf = p; + socket->peer_port = (mp_uint_t)port; + memcpy(&socket->peer, addr, sizeof(socket->peer)); + } +} + +// Callback for general tcp errors. +STATIC void _lwip_tcp_error(void *arg, err_t err) { + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + + // Free any incoming buffers or connections that are stored + lwip_socket_free_incoming(socket); + // Pass the error code back via the connection variable. + socket->state = err; + // If we got here, the lwIP stack either has deallocated or will deallocate the pcb. + socket->pcb.tcp = NULL; +} + +// Callback for tcp connection requests. Error code err is unused. (See tcp.h) +STATIC err_t _lwip_tcp_connected(void *arg, struct tcp_pcb *tpcb, err_t err) { + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + + socket->state = STATE_CONNECTED; + return ERR_OK; +} + +// Handle errors (eg connection aborted) on TCP PCBs that have been put on the +// accept queue but are not yet actually accepted. +STATIC void _lwip_tcp_err_unaccepted(void *arg, err_t err) { + struct tcp_pcb *pcb = (struct tcp_pcb *)arg; + + // The ->connected entry is repurposed to store the parent socket; this is safe + // because it's only ever used by lwIP if tcp_connect is called on the TCP PCB. + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)pcb->connected; + + // Array is not volatile because thiss callback is executed within the lwIP context + uint8_t alloc = socket->incoming.connection.alloc; + struct tcp_pcb **tcp_array = (struct tcp_pcb **)lwip_socket_incoming_array(socket); + + // Search for PCB on the accept queue of the parent socket + struct tcp_pcb **shift_down = NULL; + uint8_t i = socket->incoming.connection.iget; + do { + if (shift_down == NULL) { + if (tcp_array[i] == pcb) { + shift_down = &tcp_array[i]; + } + } else { + *shift_down = tcp_array[i]; + shift_down = &tcp_array[i]; + } + if (++i >= alloc) { + i = 0; + } + } while (i != socket->incoming.connection.iput); + + // PCB found in queue, remove it + if (shift_down != NULL) { + *shift_down = NULL; + socket->incoming.connection.iput = shift_down - tcp_array; + } +} + +// By default, a child socket of listen socket is created with recv +// handler which discards incoming pbuf's. We don't want to do that, +// so set this handler which requests lwIP to keep pbuf's and deliver +// them later. We cannot cache pbufs in child socket on Python side, +// until it is created in accept(). +STATIC err_t _lwip_tcp_recv_unaccepted(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { + return ERR_BUF; +} + +// Callback for incoming tcp connections. +STATIC err_t _lwip_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err) { + // err can be ERR_MEM to notify us that there was no memory for an incoming connection + if (err != ERR_OK) { + return ERR_OK; + } + + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + tcp_recv(newpcb, _lwip_tcp_recv_unaccepted); + + // Search for an empty slot to store the new connection + struct tcp_pcb *volatile *slot = &lwip_socket_incoming_array(socket)[socket->incoming.connection.iput]; + if (*slot == NULL) { + // Have an empty slot to store waiting connection + *slot = newpcb; + if (++socket->incoming.connection.iput >= socket->incoming.connection.alloc) { + socket->incoming.connection.iput = 0; + } + + // Schedule user accept callback + exec_user_callback(socket); + + // Set the error callback to handle the case of a dropped connection before we + // have a chance to take it off the accept queue. + // The ->connected entry is repurposed to store the parent socket; this is safe + // because it's only ever used by lwIP if tcp_connect is called on the TCP PCB. + newpcb->connected = (void *)socket; + tcp_arg(newpcb, newpcb); + tcp_err(newpcb, _lwip_tcp_err_unaccepted); + + return ERR_OK; + } + + DEBUG_printf("_lwip_tcp_accept: No room to queue pcb waiting for accept\n"); + return ERR_BUF; +} + +// Callback for inbound tcp packets. +STATIC err_t _lwip_tcp_recv(void *arg, struct tcp_pcb *tcpb, struct pbuf *p, err_t err) { + socketpool_socket_obj_t *socket = (socketpool_socket_obj_t *)arg; + + if (p == NULL) { + // Other side has closed connection. + DEBUG_printf("_lwip_tcp_recv[%p]: other side closed connection\n", socket); + socket->state = STATE_PEER_CLOSED; + exec_user_callback(socket); + return ERR_OK; + } + + if (socket->incoming.pbuf == NULL) { + socket->incoming.pbuf = p; + } else { + #ifdef SOCKET_SINGLE_PBUF + return ERR_BUF; + #else + pbuf_cat(socket->incoming.pbuf, p); + #endif + } + + exec_user_callback(socket); + + return ERR_OK; +} + +/*******************************************************************************/ +// Functions for socket send/receive operations. Socket send/recv and friends call +// these to do the work. + +// Helper function for send/sendto to handle raw/UDP packets. +STATIC mp_uint_t lwip_raw_udp_send(socketpool_socket_obj_t *socket, const byte *buf, mp_uint_t len, ip_addr_t *dest, uint32_t port, int *_errno) { + if (len > 0xffff) { + // Any packet that big is probably going to fail the pbuf_alloc anyway, but may as well try + len = 0xffff; + } + + MICROPY_PY_LWIP_ENTER + + // FIXME: maybe PBUF_ROM? + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); + if (p == NULL) { + MICROPY_PY_LWIP_EXIT + *_errno = MP_ENOMEM; + return -1; + } + + memcpy(p->payload, buf, len); + + err_t err; + if (dest == NULL) { + #if MICROPY_PY_LWIP_SOCK_RAW + if (socket->type == MOD_NETWORK_SOCK_RAW) { + err = raw_send(socket->pcb.raw, p); + } else + #endif + { + err = udp_send(socket->pcb.udp, p); + } + } else { + #if MICROPY_PY_LWIP_SOCK_RAW + if (socket->type == MOD_NETWORK_SOCK_RAW) { + err = raw_sendto(socket->pcb.raw, p, dest); + } else + #endif + { + err = udp_sendto(socket->pcb.udp, p, dest, port); + } + } + + pbuf_free(p); + + MICROPY_PY_LWIP_EXIT + + // udp_sendto can return 1 on occasion for ESP8266 port. It's not known why + // but it seems that the send actually goes through without error in this case. + // So we treat such cases as a success until further investigation. + if (err != ERR_OK && err != 1) { + *_errno = error_lookup_table[-err]; + return -1; + } + + return len; +} + +// Helper function for recv/recvfrom to handle raw/UDP packets +STATIC mp_uint_t lwip_raw_udp_receive(socketpool_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, uint32_t *port, int *_errno) { + + if (socket->incoming.pbuf == NULL) { + if (socket->timeout == 0) { + // Non-blocking socket. + *_errno = MP_EAGAIN; + return -1; + } + + // Wait for data to arrive on UDP socket. + mp_uint_t start = mp_hal_ticks_ms(); + while (socket->incoming.pbuf == NULL) { + if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) { + *_errno = MP_ETIMEDOUT; + return -1; + } + poll_sockets(); + } + } + + if (ip != NULL) { + memcpy(ip, &socket->peer, sizeof(socket->peer)); + *port = socket->peer_port; + } + + struct pbuf *p = socket->incoming.pbuf; + + MICROPY_PY_LWIP_ENTER + + u16_t result = pbuf_copy_partial(p, buf, ((p->tot_len > len) ? len : p->tot_len), 0); + pbuf_free(p); + socket->incoming.pbuf = NULL; + + MICROPY_PY_LWIP_EXIT + + return (mp_uint_t)result; +} + +// For use in stream virtual methods +#define STREAM_ERROR_CHECK(socket) \ + if (socket->state < 0) { \ + *_errno = error_lookup_table[-socket->state]; \ + return MP_STREAM_ERROR; \ + } \ + assert(socket->pcb.tcp); + +// Version of above for use when lock is held +#define STREAM_ERROR_CHECK_WITH_LOCK(socket) \ + if (socket->state < 0) { \ + *_errno = error_lookup_table[-socket->state]; \ + MICROPY_PY_LWIP_EXIT \ + return MP_STREAM_ERROR; \ + } \ + assert(socket->pcb.tcp); + + +// Helper function for send/sendto to handle TCP packets +STATIC mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) { + // Check for any pending errors + STREAM_ERROR_CHECK(socket); + + MICROPY_PY_LWIP_ENTER + + u16_t available = tcp_sndbuf(socket->pcb.tcp); + + if (available == 0) { + // Non-blocking socket + if (socket->timeout == 0) { + MICROPY_PY_LWIP_EXIT + *_errno = MP_EAGAIN; + return MP_STREAM_ERROR; + } + + mp_uint_t start = mp_hal_ticks_ms(); + // Assume that STATE_PEER_CLOSED may mean half-closed connection, where peer closed it + // sending direction, but not receiving. Consequently, check for both STATE_CONNECTED + // and STATE_PEER_CLOSED as normal conditions and still waiting for buffers to be sent. + // If peer fully closed socket, we would have socket->state set to ERR_RST (connection + // reset) by error callback. + // Avoid sending too small packets, so wait until at least 16 bytes available + while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) { + MICROPY_PY_LWIP_EXIT + if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) { + *_errno = MP_ETIMEDOUT; + return MP_STREAM_ERROR; + } + poll_sockets(); + MICROPY_PY_LWIP_REENTER + } + + // While we waited, something could happen + STREAM_ERROR_CHECK_WITH_LOCK(socket); + } + + u16_t write_len = MIN(available, len); + + // If tcp_write returns ERR_MEM then there's currently not enough memory to + // queue the write, so wait and keep trying until it succeeds (with 10s limit). + // Note: if the socket is non-blocking then this code will actually block until + // there's enough memory to do the write, but by this stage we have already + // committed to being able to write the data. + err_t err; + for (int i = 0; i < 200; ++i) { + err = tcp_write(socket->pcb.tcp, buf, write_len, TCP_WRITE_FLAG_COPY); + if (err != ERR_MEM) { + break; + } + err = tcp_output(socket->pcb.tcp); + if (err != ERR_OK) { + break; + } + MICROPY_PY_LWIP_EXIT + mp_hal_delay_ms(50); + MICROPY_PY_LWIP_REENTER + } + + // If the output buffer is getting full then send the data to the lower layers + if (err == ERR_OK && tcp_sndbuf(socket->pcb.tcp) < TCP_SND_BUF / 4) { + err = tcp_output(socket->pcb.tcp); + } + + MICROPY_PY_LWIP_EXIT + + if (err != ERR_OK) { + *_errno = error_lookup_table[-err]; + return MP_STREAM_ERROR; + } + + return write_len; +} + +// Helper function for recv/recvfrom to handle TCP packets +STATIC mp_uint_t lwip_tcp_receive(socketpool_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) { + // Check for any pending errors + STREAM_ERROR_CHECK(socket); + + if (socket->incoming.pbuf == NULL) { + + // Non-blocking socket + if (socket->timeout == 0) { + if (socket->state == STATE_PEER_CLOSED) { + return 0; + } + *_errno = MP_EAGAIN; + return -1; + } + + mp_uint_t start = mp_hal_ticks_ms(); + while (socket->state == STATE_CONNECTED && socket->incoming.pbuf == NULL) { + if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) { + *_errno = MP_ETIMEDOUT; + return -1; + } + poll_sockets(); + } + + if (socket->state == STATE_PEER_CLOSED) { + if (socket->incoming.pbuf == NULL) { + // socket closed and no data left in buffer + return 0; + } + } else if (socket->state != STATE_CONNECTED) { + if (socket->state >= STATE_NEW) { + *_errno = MP_ENOTCONN; + } else { + *_errno = error_lookup_table[-socket->state]; + } + return -1; + } + } + + MICROPY_PY_LWIP_ENTER + + assert(socket->pcb.tcp != NULL); + + struct pbuf *p = socket->incoming.pbuf; + + mp_uint_t remaining = p->len - socket->recv_offset; + if (len > remaining) { + len = remaining; + } + + memcpy(buf, (byte *)p->payload + socket->recv_offset, len); + + remaining -= len; + if (remaining == 0) { + socket->incoming.pbuf = p->next; + // If we don't ref here, free() will free the entire chain, + // if we ref, it does what we need: frees 1st buf, and decrements + // next buf's refcount back to 1. + pbuf_ref(p->next); + pbuf_free(p); + socket->recv_offset = 0; + } else { + socket->recv_offset += len; + } + tcp_recved(socket->pcb.tcp, len); + + MICROPY_PY_LWIP_EXIT + + return len; +} + + +STATIC socketpool_socket_obj_t *open_socket_objs[MAX_SOCKETS]; STATIC bool user_socket[MAX_SOCKETS]; void socket_user_reset(void) { for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { if (open_socket_objs[i] && user_socket[i]) { - // shut 'er down + socketpool_socket_close(open_socket_objs[i]); + open_socket_objs[i] = NULL; + user_socket[i] = false; } } } @@ -54,48 +613,131 @@ void socket_user_reset(void) { // The writes below send an event to the socket select task so that it redoes the // select with the new open socket set. -STATIC bool register_open_socket(mp_obj_t obj) { +STATIC bool register_open_socket(socketpool_socket_obj_t *obj) { for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { if (!open_socket_objs[i]) { open_socket_objs[i] = obj; + DEBUG_printf("register_open_socket(%p) -> %d\n", obj, i); user_socket[i] = false; return true; } } + DEBUG_printf("register_open_socket(%p) fails due to full table\n", obj); return false; } -STATIC void unregister_open_socket(mp_obj_t obj) { +STATIC void unregister_open_socket(socketpool_socket_obj_t *obj) { for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { if (open_socket_objs[i] == obj) { + DEBUG_printf("unregister_open_socket(%p) clears %d\n", obj, i); open_socket_objs[i] = NULL; user_socket[i] = false; + return; } } + DEBUG_printf("unregister_open_socket(%p) fails due to missing entry\n", obj); } -STATIC void mark_user_socket(mp_obj_t obj) { +STATIC void mark_user_socket(socketpool_socket_obj_t *obj) { for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { if (open_socket_objs[i] == obj) { + DEBUG_printf("mark_user_socket(%p) -> %d\n", obj, i); user_socket[i] = true; return; } } + DEBUG_printf("mark_user_socket(%p) fails due to missing entry\n", obj); } bool socketpool_socket(socketpool_socketpool_obj_t *self, socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type, - socketpool_socket_obj_t *sock) { - mp_raise_NotImplementedError(NULL); + socketpool_socket_obj_t *socket) { + + if (!register_open_socket(socket)) { + DEBUG_printf("collecting garbage to open socket\n"); + gc_collect(); + if (!register_open_socket(socket)) { + return false; + } + } + + socket->timeout = -1; + socket->recv_offset = 0; + socket->domain = SOCKETPOOL_AF_INET; + socket->type = type; + socket->callback = MP_OBJ_NULL; + socket->state = STATE_NEW; + + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: + socket->pcb.tcp = tcp_new(); + socket->incoming.connection.alloc = 0; + socket->incoming.connection.tcp.item = NULL; + break; + case SOCKETPOOL_SOCK_DGRAM: + socket->pcb.udp = udp_new(); + socket->incoming.pbuf = NULL; + break; + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: { + socket->pcb.raw = raw_new(0); + break; + } + #endif + default: + return false; + } + + if (socket->pcb.tcp == NULL) { + return false; + } + + switch (socket->type) { + case MOD_NETWORK_SOCK_STREAM: { + // Register the socket object as our callback argument. + tcp_arg(socket->pcb.tcp, (void *)socket); + // Register our error callback. + tcp_err(socket->pcb.tcp, _lwip_tcp_error); + break; + } + case MOD_NETWORK_SOCK_DGRAM: { + socket->state = STATE_ACTIVE_UDP; + // Register our receive callback now. Since UDP sockets don't require binding or connection + // before use, there's no other good time to do it. + udp_recv(socket->pcb.udp, _lwip_udp_incoming, (void *)socket); + break; + } + #if MICROPY_PY_LWIP_SOCK_RAW + case MOD_NETWORK_SOCK_RAW: { + // Register our receive callback now. Since raw sockets don't require binding or connection + // before use, there's no other good time to do it. + raw_recv(socket->pcb.raw, _lwip_raw_incoming, (void *)socket); + break; + } + #endif + } + return true; } socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self, socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) { - mp_raise_NotImplementedError(NULL); + if (family != SOCKETPOOL_AF_INET) { + mp_raise_NotImplementedError(translate("Only IPv4 sockets supported")); + } + + // we must allocate sockets long-lived because we depend on their object-identity + socketpool_socket_obj_t *socket = m_new_ll_obj_with_finaliser(socketpool_socket_obj_t); + socket->base.type = &socketpool_socket_type; + + if (!socketpool_socket(self, family, type, socket)) { + mp_raise_RuntimeError(translate("Out of sockets")); + } + mark_user_socket(socket); + return socket; } int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_t *port) { - mp_raise_NotImplementedError(NULL); + return -MP_EBADF; } socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_obj_t *self, @@ -108,58 +750,264 @@ bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self, mp_raise_NotImplementedError(NULL); } -void socketpool_socket_close(socketpool_socket_obj_t *self) { - mp_raise_NotImplementedError(NULL); +STATIC err_t _lwip_tcp_close_poll(void *arg, struct tcp_pcb *pcb) { + // Connection has not been cleanly closed so just abort it to free up memory + tcp_poll(pcb, NULL, 0); + tcp_abort(pcb); + return ERR_OK; } -void common_hal_socketpool_socket_close(socketpool_socket_obj_t *self) { - mp_raise_NotImplementedError(NULL); +void socketpool_socket_close(socketpool_socket_obj_t *socket) { + unregister_open_socket(socket); + MICROPY_PY_LWIP_ENTER + if (socket->pcb.tcp == NULL) { // already closed + MICROPY_PY_LWIP_EXIT + return; + } + lwip_socket_free_incoming(socket); + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: { + // Deregister callback (pcb.tcp is set to NULL below so must deregister now) + tcp_arg(socket->pcb.tcp, NULL); + tcp_err(socket->pcb.tcp, NULL); + tcp_recv(socket->pcb.tcp, NULL); + + if (socket->pcb.tcp->state != LISTEN) { + // Schedule a callback to abort the connection if it's not cleanly closed after + // the given timeout. The callback must be set before calling tcp_close since + // the latter may free the pcb; if it doesn't then the callback will be active. + tcp_poll(socket->pcb.tcp, _lwip_tcp_close_poll, MICROPY_PY_LWIP_TCP_CLOSE_TIMEOUT_MS / 500); + } + if (tcp_close(socket->pcb.tcp) != ERR_OK) { + DEBUG_printf("lwip_close: had to call tcp_abort()\n"); + tcp_abort(socket->pcb.tcp); + } + break; + } + case SOCKETPOOL_SOCK_DGRAM: + udp_recv(socket->pcb.udp, NULL, NULL); + udp_remove(socket->pcb.udp); + break; + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: + raw_recv(socket->pcb.raw, NULL, NULL); + raw_remove(socket->pcb.raw); + break; + #endif + } + + socket->pcb.tcp = NULL; + socket->state = _ERR_BADF; + MICROPY_PY_LWIP_EXIT } -void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *self, +void common_hal_socketpool_socket_close(socketpool_socket_obj_t *socket) { + socketpool_socket_close(socket); +} + +void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *socket, const char *host, size_t hostlen, uint32_t port) { - mp_raise_NotImplementedError(NULL); + + if (socket->pcb.tcp == NULL) { + mp_raise_OSError(MP_EBADF); + } + + // get address + ip_addr_t dest; + int error = socketpool_resolve_host(socket->pool, host, &dest); + if (error != 0) { + mp_raise_OSError(EHOSTUNREACH); + } + + err_t err = ERR_ARG; + switch (socket->type) { + case MOD_NETWORK_SOCK_STREAM: { + if (socket->state != STATE_NEW) { + if (socket->state == STATE_CONNECTED) { + mp_raise_OSError(MP_EISCONN); + } else { + mp_raise_OSError(MP_EALREADY); + } + } + + // Register our receive callback. + MICROPY_PY_LWIP_ENTER + tcp_recv(socket->pcb.tcp, _lwip_tcp_recv); + socket->state = STATE_CONNECTING; + err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected); + if (err != ERR_OK) { + MICROPY_PY_LWIP_EXIT + socket->state = STATE_NEW; + mp_raise_OSError(error_lookup_table[-err]); + } + socket->peer_port = (mp_uint_t)port; + memcpy(socket->peer, &dest, sizeof(socket->peer)); + MICROPY_PY_LWIP_EXIT + + // And now we wait... + if (socket->timeout != -1) { + for (mp_uint_t retries = socket->timeout / 100; retries--;) { + mp_hal_delay_ms(100); + if (socket->state != STATE_CONNECTING) { + break; + } + } + if (socket->state == STATE_CONNECTING) { + mp_raise_OSError(MP_EINPROGRESS); + } + } else { + while (socket->state == STATE_CONNECTING) { + poll_sockets(); + } + } + if (socket->state == STATE_CONNECTED) { + err = ERR_OK; + } else { + err = socket->state; + } + break; + } + case MOD_NETWORK_SOCK_DGRAM: { + err = udp_connect(socket->pcb.udp, &dest, port); + break; + } + #if MICROPY_PY_LWIP_SOCK_RAW + case MOD_NETWORK_SOCK_RAW: { + err = raw_connect(socket->pcb.raw, &dest); + break; + } + #endif + } + + if (err != ERR_OK) { + mp_raise_OSError(error_lookup_table[-err]); + } } -bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t *self) { - mp_raise_NotImplementedError(NULL); +bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t *socket) { + return !socket->pcb.tcp; } -bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *self) { - mp_raise_NotImplementedError(NULL); +bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *socket) { + return socket->state == STATE_CONNECTED; } bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t *self, int backlog) { mp_raise_NotImplementedError(NULL); } -mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *self, +mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *socket, uint8_t *buf, uint32_t len, uint8_t *ip, uint32_t *port) { - mp_raise_NotImplementedError(NULL); + int _errno; + + mp_uint_t ret = 0; + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: { + memcpy(ip, &socket->peer, sizeof(socket->peer)); + *port = (mp_uint_t)socket->peer_port; + ret = lwip_tcp_receive(socket, (byte *)buf, len, &_errno); + break; + } + case SOCKETPOOL_SOCK_DGRAM: + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: + #endif + ret = lwip_raw_udp_receive(socket, (byte *)buf, len, ip, port, &_errno); + break; + } + if (ret == -1) { + mp_raise_OSError(_errno); + } + + return ret; } -int socketpool_socket_recv_into(socketpool_socket_obj_t *self, +int socketpool_socket_recv_into(socketpool_socket_obj_t *socket, const uint8_t *buf, uint32_t len) { - mp_raise_NotImplementedError(NULL); + mp_uint_t ret = 0; + int _errno = 0; + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: { + ret = lwip_tcp_receive(socket, (byte *)buf, len, &_errno); + break; + } + case SOCKETPOOL_SOCK_DGRAM: + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: + #endif + ret = lwip_raw_udp_receive(socket, (byte *)buf, len, NULL, NULL, &_errno); + break; + } + return ret; } mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { - mp_raise_NotImplementedError(NULL); + int received = socketpool_socket_recv_into(self, buf, len); + if (received < 0) { + mp_raise_OSError(received); + } + return received; } -int socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { - mp_raise_NotImplementedError(NULL); +int socketpool_socket_send(socketpool_socket_obj_t *socket, const uint8_t *buf, uint32_t len) { + mp_uint_t ret = 0; + int _errno = 0; + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: { + ret = lwip_tcp_send(socket, buf, len, &_errno); + break; + } + case SOCKETPOOL_SOCK_DGRAM: + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: + #endif + ret = lwip_raw_udp_send(socket, buf, len, NULL, 0, &_errno); + break; + } + if (ret == -1) { + return -_errno; + } + return ret; } mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { - mp_raise_NotImplementedError(NULL); + int sent = socketpool_socket_send(self, buf, len); + + if (sent < 0) { + mp_raise_OSError(-sent); + } + return sent; } -mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *self, +mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *socket, const char *host, size_t hostlen, uint32_t port, const uint8_t *buf, uint32_t len) { - mp_raise_NotImplementedError(NULL); + int _errno; + ip_addr_t ip; + int error = socketpool_resolve_host(socket->pool, host, &ip); + if (error != 0) { + mp_raise_OSError(EHOSTUNREACH); + } + + mp_uint_t ret = 0; + switch (socket->type) { + case SOCKETPOOL_SOCK_STREAM: { + ret = lwip_tcp_send(socket, buf, len, &_errno); + break; + } + case SOCKETPOOL_SOCK_DGRAM: + #if MICROPY_PY_LWIP_SOCK_RAW + case SOCKETPOOL_SOCK_RAW: + #endif + ret = lwip_raw_udp_send(socket, buf, len, &ip, port, &_errno); + break; + } + if (ret == -1) { + mp_raise_OSError(_errno); + } + + return ret; } void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t *self, uint32_t timeout_ms) { - mp_raise_NotImplementedError(NULL); + self->timeout = timeout_ms; } diff --git a/ports/raspberrypi/common-hal/socketpool/SocketPool.c b/ports/raspberrypi/common-hal/socketpool/SocketPool.c index 723b753064..d2f403c852 100644 --- a/ports/raspberrypi/common-hal/socketpool/SocketPool.c +++ b/ports/raspberrypi/common-hal/socketpool/SocketPool.c @@ -57,8 +57,7 @@ STATIC void lwip_getaddrinfo_cb(const char *name, const ip_addr_t *ipaddr, void } } -mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self, - const char *host) { +int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr) { getaddrinfo_state_t state; state.status = 0; @@ -85,13 +84,27 @@ mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_ob } if (state.status < 0) { + return state.status; // TODO: CPython raises gaierror, we raise with native lwIP negative error // values, to differentiate from normal errno's at least in such way. mp_raise_OSError(state.status); } + *addr = state.ipaddr; + return 0; +} + +mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self, + const char *host) { + + ip_addr_t addr; + int result = socketpool_resolve_host(self, host, &addr); + if (result < 0) { + mp_raise_OSError(-result); + } + char ip_str[IP4ADDR_STRLEN_MAX]; - inet_ntoa_r(state.ipaddr, ip_str, IP4ADDR_STRLEN_MAX); + inet_ntoa_r(addr, ip_str, IP4ADDR_STRLEN_MAX); mp_obj_t ip_obj = mp_obj_new_str(ip_str, strlen(ip_str)); return ip_obj; } diff --git a/ports/raspberrypi/common-hal/socketpool/SocketPool.h b/ports/raspberrypi/common-hal/socketpool/SocketPool.h index ea0679b5eb..3d498d9a49 100644 --- a/ports/raspberrypi/common-hal/socketpool/SocketPool.h +++ b/ports/raspberrypi/common-hal/socketpool/SocketPool.h @@ -31,3 +31,5 @@ typedef struct { mp_obj_base_t base; } socketpool_socketpool_obj_t; + +int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr); diff --git a/ports/raspberrypi/lib/cyw43-driver b/ports/raspberrypi/lib/cyw43-driver new file mode 160000 index 0000000000..195dfcc10b --- /dev/null +++ b/ports/raspberrypi/lib/cyw43-driver @@ -0,0 +1 @@ +Subproject commit 195dfcc10bb6f379e3dea45147590db2203d3c7b diff --git a/ports/raspberrypi/lib/lwip b/ports/raspberrypi/lib/lwip new file mode 160000 index 0000000000..239918ccc1 --- /dev/null +++ b/ports/raspberrypi/lib/lwip @@ -0,0 +1 @@ +Subproject commit 239918ccc173cb2c2a62f41a40fd893f57faf1d6 diff --git a/ports/raspberrypi/pioasm/CMakeLists.txt b/ports/raspberrypi/pioasm/CMakeLists.txt new file mode 100644 index 0000000000..1923068bae --- /dev/null +++ b/ports/raspberrypi/pioasm/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.12) +project(pioasm) +set(PICO_TINYUSB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/tinyusb) +include(../sdk/pico_sdk_init.cmake) +pico_sdk_init() + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools) +find_package(Pioasm REQUIRED) diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c index c4a1f853b5..8ede649cd7 100644 --- a/shared-bindings/digitalio/DigitalInOut.c +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -158,10 +158,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_ drive_mode = DRIVE_MODE_OPEN_DRAIN; } // do the transfer - digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); - if (result == DIGITALINOUT_INPUT_ONLY) { - mp_raise_NotImplementedError(translate("Pin is input only")); - } + check_result(common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode)); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output); @@ -229,10 +226,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_ob if (value == MP_ROM_PTR(&digitalio_direction_input_obj)) { check_result(common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE)); } else if (value == MP_ROM_PTR(&digitalio_direction_output_obj)) { - digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL); - if (result == DIGITALINOUT_INPUT_ONLY) { - mp_raise_NotImplementedError(translate("Pin is input only")); - } + check_result(common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL)); } else { mp_arg_error_invalid(MP_QSTR_direction); } From 4380292848d52c16597fce54bac4c290b03580dd Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:10:57 -0500 Subject: [PATCH 08/18] comment why not actually reset wifi --- ports/raspberrypi/common-hal/wifi/__init__.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/wifi/__init__.c b/ports/raspberrypi/common-hal/wifi/__init__.c index 9a42320cf5..73486c516f 100644 --- a/ports/raspberrypi/common-hal/wifi/__init__.c +++ b/ports/raspberrypi/common-hal/wifi/__init__.c @@ -80,6 +80,7 @@ void wifi_reset(void) { if (!wifi_inited) { return; } + // the cyw43 wifi chip is not reset due to https://github.com/raspberrypi/pico-sdk/issues/980 common_hal_wifi_monitor_deinit(MP_STATE_VM(wifi_monitor_singleton)); wifi_radio_obj_t *radio = &common_hal_wifi_radio_obj; common_hal_wifi_radio_obj.current_scan = NULL; From bc63f9370a726be5d583a0a66a05f95d1eba239e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:15:30 -0500 Subject: [PATCH 09/18] comment why it's this way --- shared-bindings/digitalio/DigitalInOut.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c index 8ede649cd7..839bee2c0d 100644 --- a/shared-bindings/digitalio/DigitalInOut.c +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -89,6 +89,9 @@ STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type, self->base.type = &digitalio_digitalinout_type; #if CIRCUITPY_CYW43 + // The GPIO pin attached to the CYW43 co-processor can only be used for + // DigitalInOut, not for other purposes like PWM. That's why this check + // is here, and it's not rolled into validate_obj_is_free_pin. const mcu_pin_obj_t *pin = validate_obj_is_free_pin_including_cyw43(args[0]); #else const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]); From e100981d9039d2f4681d86ae1303524b3bb0ecfa Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:16:58 -0500 Subject: [PATCH 10/18] revert CFLAGS change --- ports/raspberrypi/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index cf3967fee4..18a2a69fd6 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -215,7 +215,7 @@ endif # Remove -Wno-stringop-overflow after we can test with CI's GCC 10. Mac's looks weird. DISABLE_WARNINGS = -Wno-stringop-overflow -Wno-unused-function -Wno-unused-variable -Wno-strict-overflow -Wno-cast-align -Wno-strict-prototypes -Wno-nested-externs -Wno-double-promotion -Wno-sign-compare -CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes -Wno-error=unused-result +CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes CFLAGS += \ -march=armv6-m \ From edf1efd728708133a34cabb26e0e0fff931ad3c1 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:17:27 -0500 Subject: [PATCH 11/18] Add CYW43 guards to more things --- .../raspberrypi/common-hal/microcontroller/Pin.c | 16 ++++++++-------- .../raspberrypi/common-hal/microcontroller/Pin.h | 8 +++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ports/raspberrypi/common-hal/microcontroller/Pin.c b/ports/raspberrypi/common-hal/microcontroller/Pin.c index 59adb5395f..d1cfcfe2b1 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Pin.c +++ b/ports/raspberrypi/common-hal/microcontroller/Pin.c @@ -28,19 +28,23 @@ #include "common-hal/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" -#include "bindings/cyw43/__init__.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" #if CIRCUITPY_CYW43 +#include "bindings/cyw43/__init__.h" #include "pico/cyw43_arch.h" -#endif - -STATIC uint32_t never_reset_pins; bool cyw_ever_init; static uint32_t cyw_pin_claimed; +void reset_pin_number_cyw(uint8_t pin_no) { + cyw_pin_claimed &= ~(1 << pin_no); +} +#endif + +STATIC uint32_t never_reset_pins; + void reset_all_pins(void) { for (size_t i = 0; i < TOTAL_GPIO_COUNT; i++) { if ((never_reset_pins & (1 << i)) != 0) { @@ -82,10 +86,6 @@ void reset_pin_number(uint8_t pin_number) { hw_set_bits(&padsbank0_hw->io[pin_number], PADS_BANK0_GPIO0_OD_BITS); } -void reset_pin_number_cyw(uint8_t pin_no) { - cyw_pin_claimed &= ~(1 << pin_no); -} - void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) { never_reset_pin_number(pin->number); } diff --git a/ports/raspberrypi/common-hal/microcontroller/Pin.h b/ports/raspberrypi/common-hal/microcontroller/Pin.h index 5042dd7d66..1c400fe48e 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Pin.h +++ b/ports/raspberrypi/common-hal/microcontroller/Pin.h @@ -34,15 +34,17 @@ #include "peripherals/pins.h" -extern bool cyw_ever_init; - void reset_all_pins(void); // reset_pin_number takes the pin number instead of the pointer so that objects don't // need to store a full pointer. void reset_pin_number(uint8_t pin_number); -void reset_pin_number_cyw(uint8_t pin_number); void never_reset_pin_number(uint8_t pin_number); void claim_pin(const mcu_pin_obj_t *pin); bool pin_number_is_free(uint8_t pin_number); +#if CIRCUITPY_CYW43 +extern bool cyw_ever_init; +void reset_pin_number_cyw(uint8_t pin_number); +#endif + #endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_MICROCONTROLLER_PIN_H From 6189156a0b42cf127b064616c2d3fec29f7be258 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:17:35 -0500 Subject: [PATCH 12/18] Add missing NotImplementedErrors --- ports/raspberrypi/common-hal/wifi/Radio.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index 7d9dcbee23..7981b0952e 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -141,6 +141,7 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_ } void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) { + mp_raise_NotImplementedError(NULL); } wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len) { @@ -229,6 +230,7 @@ void common_hal_wifi_radio_set_ipv4_dns(wifi_radio_obj_t *self, mp_obj_t ipv4_dn } void common_hal_wifi_radio_start_dhcp_client(wifi_radio_obj_t *self) { + mp_raise_NotImplementedError(NULL); } void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self) { From 09d4fbc5573a797bba6f87d8bbe7c316d0b57969 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:17:46 -0500 Subject: [PATCH 13/18] Remove FIXME this was verbatim-copied from micropython --- ports/raspberrypi/common-hal/socketpool/Socket.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/socketpool/Socket.c b/ports/raspberrypi/common-hal/socketpool/Socket.c index 05c8212d69..95f79c5880 100644 --- a/ports/raspberrypi/common-hal/socketpool/Socket.c +++ b/ports/raspberrypi/common-hal/socketpool/Socket.c @@ -346,7 +346,6 @@ STATIC mp_uint_t lwip_raw_udp_send(socketpool_socket_obj_t *socket, const byte * MICROPY_PY_LWIP_ENTER - // FIXME: maybe PBUF_ROM? struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); if (p == NULL) { MICROPY_PY_LWIP_EXIT From 3d76aa00f53ebea79191e6eb29272be5c58dc680 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:18:36 -0500 Subject: [PATCH 14/18] implementations not needed --- .../boards/raspberry_pi_pico_w/board.c | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c index de6e424ed9..e69de29bb2 100644 --- a/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c @@ -1,40 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "supervisor/board.h" - -void board_init(void) { -} - -bool board_requests_safe_mode(void) { - return false; -} - -void reset_board(void) { -} - -void board_deinit(void) { -} From dbd7ce9fdcd9203b76ecd4ef693787b6a25fc291 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:35:46 -0500 Subject: [PATCH 15/18] restore checking hostname format on esp32 where it was checked before --- shared-bindings/wifi/Radio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index aecb1b561f..3238025622 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -89,7 +89,7 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) mp_arg_validate_length_range(hostname.len, 1, 253, MP_QSTR_hostname); - #if 0 // ndef CONFIG_IDF_TARGET_ESP32C3 + #if !(defined(CONFIG_IDF_TARGET_ESP32C3) || defined(RASPBERRYPI)) regex_t regex; // validate hostname according to RFC 1123 regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); if (regexec(®ex, hostname.buf, 0, NULL, 0)) { From 72bce51ce31fe8d1fadd72094009696a3458fc37 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 14:48:54 -0500 Subject: [PATCH 16/18] restore submodule commits --- frozen/circuitpython-stage | 2 +- ports/espressif/esp32-camera | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frozen/circuitpython-stage b/frozen/circuitpython-stage index 9a8338b3bd..4124dfbdaa 160000 --- a/frozen/circuitpython-stage +++ b/frozen/circuitpython-stage @@ -1 +1 @@ -Subproject commit 9a8338b3bdaeac9eeb5b74d147107c67db33fdac +Subproject commit 4124dfbdaadce1966f457d7d6c6984e9832999bf diff --git a/ports/espressif/esp32-camera b/ports/espressif/esp32-camera index 28804391c0..54c3f61c86 160000 --- a/ports/espressif/esp32-camera +++ b/ports/espressif/esp32-camera @@ -1 +1 @@ -Subproject commit 28804391c002f6a3ea5ce6a55aee3b191be3ecde +Subproject commit 54c3f61c864c3d3ffd779042454554045b9dd9d2 From d59774d7508e40f5ef73e6515b1ea9df0c7e8354 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 15:11:55 -0500 Subject: [PATCH 17/18] don't use regcomp to check hostname validity --- shared-bindings/wifi/Radio.c | 45 ++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index 3238025622..7463cdc77e 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -27,7 +27,6 @@ #include "shared-bindings/wifi/__init__.h" #include "shared-bindings/wifi/AuthMode.h" -#include #include #include "py/runtime.h" @@ -35,6 +34,43 @@ #define MAC_ADDRESS_LENGTH 6 +STATIC bool hostname_valid(const char *ptr, size_t len) { + #if 0 // validated by mp_arg_validate_length_range + if (len == 0 || len > 253) { + // at most 253 characters long + return false; + } + #endif + int partlen = 0; + while (len) { + char c = *ptr++; + len--; + if (c == '.') { + if (partlen == 0 || partlen > 63) { + return false; + } + partlen = 0; + continue; + } + partlen++; + if (c == '-') { + if (partlen == 1) { + return false; // part cannot begin with a dash + } + continue; + } else if ( + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9')) { + continue; + } + return false; + } + // check length of last part + return !(partlen > 63); +} + + //| class Radio: //| """Native wifi radio. //| @@ -89,14 +125,9 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) mp_arg_validate_length_range(hostname.len, 1, 253, MP_QSTR_hostname); - #if !(defined(CONFIG_IDF_TARGET_ESP32C3) || defined(RASPBERRYPI)) - regex_t regex; // validate hostname according to RFC 1123 - regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); - if (regexec(®ex, hostname.buf, 0, NULL, 0)) { + if (!hostname_valid(hostname.buf, hostname.len)) { mp_raise_ValueError(translate("invalid hostname")); } - regfree(®ex); - #endif wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_wifi_radio_set_hostname(self, hostname.buf); From 2dd6df9d93cff1b496fa95ee3dbd3ef8b35980ee Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 15:19:06 -0500 Subject: [PATCH 18/18] better to explain why this file has no useful content --- .../boards/raspberry_pi_pico_w/board.c | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c index e69de29bb2..331653173e 100644 --- a/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c +++ b/ports/raspberrypi/boards/raspberry_pi_pico_w/board.c @@ -0,0 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "supervisor/board.h" + +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.