Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

picow: turn off CIRCUITPY_USB_HOST and CIRCUITPY_PICODVI #9303

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Jun 4, 2024

Free up RAM on the Pico W build so that more networking can be done, by disabling CIRCUITPY_USB_HOST and CIRCUITPY_PICODVI. Note that CIRCUIPY_PICODVI off by default on many RP2040 boards.

It still seems to be the case that once a networking error happens, the co-processor or its supporting code may get into a bad state that requires a hard reset.

@dhalbert dhalbert requested a review from tannewt June 4, 2024 16:28
@dhalbert dhalbert changed the title picow: turn off CIRCUITPY_USB_HOST picow: turn off CIRCUITPY_USB_HOST and CIRCUITPY_PICODVI Jun 4, 2024
@dhalbert
Copy link
Collaborator Author

dhalbert commented Jun 4, 2024

@bablokb @anecdata pinging for interest

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config:
Adafruit CircuitPython 9.1.0-beta.3-16-g9601f4e58b on 2024-06-04; Raspberry Pi Pico W with rp2040
adafruit-circuitpython-bundle-9.x-mpy-20240604 (except for HTTP Server from #88):

Results:

Not quite enough for the HTTP Server library with TLS, but should give a good bit more headroom for more complex HTTP[S] client, HTTP Server, or socket-level projects.

HTTPS Requests Code...
# HTTP[S] Requests Test
# Pico W + W5100S hat

import time
time.sleep(3)  # wait for serial
import gc
print(f'{gc.mem_free()=}')

import os
import ssl
import adafruit_connection_manager
import adafruit_requests
print(f'{gc.mem_free()=}')

NATIVE = False

if NATIVE:
    import wifi

    radio = wifi.radio
    radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
else:
    import board
    import busio
    import digitalio
    from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K

    spi = busio.SPI(board.GP18, board.GP19, board.GP16)
    cs = digitalio.DigitalInOut(board.GP17)
    rst = digitalio.DigitalInOut(board.GP20)
    radio = WIZNET5K(spi, cs, reset=rst, mac='de:ad:be:ef:fe:cd')

pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
requests = adafruit_requests.Session(pool, ssl_context)

URL = "https://httpbin.org/get"
for _ in range(0, 10):
    print("-" * 40)
    print("Fetching JSON data from %s" % URL)
    with requests.get(URL) as response:
        print("JSON Response: ", response.json())
    gc.collect()
    print(f'{gc.mem_free()=}')
    time.sleep(1)

PicoW HTTPS Requests:
84976 bytes free after each request

WIZnet HTTPS Requests:
45168 bytes free after each request

HTTP[S] Server Test Code...
# HTTP[S] Server Test
# Pico W + W5100S hat

import time
time.sleep(3)  # wait for serial
import gc
print(f'{gc.mem_free()=}')

import os
import ssl
import adafruit_connection_manager
from  adafruit_httpserver import Server, Request, Response
print(f'{gc.mem_free()=}')

PORT = 443
NATIVE = False
HTTPS = True

if NATIVE:
    import wifi

    radio = wifi.radio
    radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
    ipv4_address = str(radio.ipv4_address)
else:
    import board
    import busio
    import digitalio
    from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K

    spi = busio.SPI(board.GP18, board.GP19, board.GP16)
    cs = digitalio.DigitalInOut(board.GP17)
    rst = digitalio.DigitalInOut(board.GP20)
    radio = WIZNET5K(spi, cs, reset=rst, mac='de:ad:be:ef:fe:cd')
    ipv4_address = radio.pretty_ip(radio.ip_address)

pool = adafruit_connection_manager.get_radio_socketpool(radio)
server = Server(
    pool,
    root_path="/static",
    https=HTTPS,
    certfile="cert.pem",
    keyfile="key.pem",
    debug=True,
)

@server.route("/")
def base(request: Request):
    gc.collect()
    return Response(request, f"Hello from {ipv4_address}:{PORT} {gc.mem_free()=}")

server.start(ipv4_address)
while True:
    try:
        server.poll()
    except OSError as error:
        print(error)

PicoW HTTP Server:
71408 bytes free during each Response

WIZnet HTTP Server:
33328 bytes free during each Response

PicoW HTTPS Server:
MemoryError: (in poll)

WIZnet HTTPS Server:
MemoryError: memory allocation failed, allocating 2120 bytes (in poll)

@tannewt
Copy link
Member

tannewt commented Jun 5, 2024

How much do you gain by leaving PICODVI on? I could see it being useful for data display from wifi.

Does usb host work at all as-is?

@dhalbert
Copy link
Collaborator Author

dhalbert commented Jun 5, 2024

CIRCUITPY_USB_HOST and CIRCUITPY_PICODVI both on (current settings main):

Memory region         Used Size  Region Size  %age Used
  FLASH_FIRMWARE:     1408412 B      1532 KB     89.78%
             RAM:      106772 B       256 KB     40.73%

Only CIRCUITPY_USB_HOST on:

  FLASH_FIRMWARE:     1397816 B      1532 KB     89.10%
             RAM:      103076 B       256 KB     39.32%

Only CIRCUITPY_PICODVI on:

  FLASH_FIRMWARE:     1379108 B      1532 KB     87.91%
             RAM:       93732 B       256 KB     35.76%

Both off (this PR):

  FLASH_FIRMWARE:     1368176 B      1532 KB     87.21%
             RAM:       90036 B       256 KB     34.35%

So CIRCUITPY_USB_HOST uses 3.7kB RAM even before instantiation and CIRCUITPY_PICO_DVI uses 13kB before instantiation.

So I'm inclined to turn them both off, especially CIRCUITPY_PICO_DVI.

Does usb host work at all as-is?

I don't know at all, but I think using the Pico W for any reasonable-sized web work outweighs both of the above.

@tannewt
Copy link
Member

tannewt commented Jun 6, 2024

How are the other boards with picow soldered down setup? I guess this is ok for picow because folks can use the pico build if they don't want wifi.

@dhalbert
Copy link
Collaborator Author

dhalbert commented Jun 6, 2024

@tannewt

Board PICODVI (off by default) USB_HOST (on by default) notes
cytron_edu_pico_w + + lots of frozen modules, no display
pajenicko_picopad + - gaming board with display
pimoroni_badger2040w - - e-ink badge
pimoroni_inky_frame_5_7 - - e-ink picture frame
pimoroni_inky_frame_7_3 - - e-ink picture frame
pimoroni_pico_dv_base_w + + point of board is DVI , non pico-W build available
pimoroni_plasma2040w - - RGB LED driver
raspberry_pi_pico_w + + current

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Thanks for figuring this out.

@tannewt tannewt merged commit 6b3665a into adafruit:main Jun 6, 2024
15 checks passed
@dhalbert dhalbert deleted the picow-usb-host-off branch June 6, 2024 20:07
@bablokb
Copy link

bablokb commented Jun 7, 2024

I guess this is ok for picow because folks can use the pico build if they don't want wifi.

@tannewt: I don't think so. The board led is wired differently. And VBUS and VSYS monitoring also are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SSL not working when importing Adafruit-azureiot on a rpPico W
4 participants