Add support for 32 and 48 pin variants of RP2350.
Add new `PIO.gpio_base()` method, mirroring the Pico SDK.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Damien George <damien@micropython.org>
This commit separates various build settings and include files that are
specific to RP2040 and RP2350, and uses the aon_timer interface instead of
rtc, to work across both MCU variants.
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Two new bits were added to the ctrl register, and existing bits were
shifted, so use DMA_CH0_CTRL_TRIG_xxx constants to generalise the code.
Signed-off-by: Damien George <damien@micropython.org>
This isn't fully working, the CPU often wakes up early. That will be fixed
when a newer version of pico-sdk is released.
Signed-off-by: Damien George <damien@micropython.org>
The cyw43-driver uses `printf` by default for `CYW43_PRINTF`, but on the
rp2 port `printf` only goes to a UART output and not to USB CDC.
By defining `CYW43_PRINTF` to `mp_printf`, all the messages from the
cyw43-driver are seen on USB CDC.
For example this allows `network.WLAN().config(trace=1)` to show async
WALN events.
Signed-off-by: Damien George <damien@micropython.org>
Necessary to fix "mpremote run" over hardware UART.
Bisect shows bug was introduced by d420b4e4, but looks like made more
complex by 01c046d2. Specifically: resetting and re-initialising the REPL
UART during soft reset clears the FIFO before it's done printing the "MPY:
soft reboot" line.
Fixed by adding a UART TX flush in the deinit path.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
lwIP was recently updated in a89ac9e24a
to STABLE-2_2_0_RELEASE, and this introduced a change in the configuration
variable `DHCP_DOES_ARP_CHECK`, renaming it to `LWIP_DHCP_DOES_ACD_CHECK`.
This commit fixes the ports lwIP settings to use the new configuration
option.
Without this option, connecting to a WiFi access-point takes about 12.5
seconds. With this option (ie disabling DHCP ARP checks) connecting takes
about 4 seconds.
Tested on an RPI_PICO_W and PYBD_SF2.
Signed-off-by: Damien George <damien@micropython.org>
This change helps detect if the filesystem is invalid, by also including
the first mount attempt within the try-except. Then the FAT is reformatted
if needed.
Fixes issue #15779.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
The `#if` check only checks that `MICROPY_PY_NETWORK_CYW43` and
`MICROPY_HW_PIN_EXT_COUNT` are defined. This is a reasonable assumption
for the Pico W, but causes conflicts if someone wants to attach an external
IO expander to their Pico W and have its pins appear as Pin objects.
This commit addresses this by adding the additional checks, letting board
builds include wireless but separately choose whether the external IO pins
come from the cyw43 or not.
Signed-off-by: Christopher Parrott <chris@pimoroni.com>
Supported trigger names: IRQ_RXIDLE, IRQ_TXIDLE, IRQ_BREAK
- IRQ_RXIDLE: The handler for IRQ_RXIDLE is called reliably 31 UART bit
times after the last incoming data.
- IRQ_TXIDLE: This IRQ is triggered after at least >5 characters are sent
at once. It is triggered when the TX FIFO falls below 4 elements. At
that time, up to 5 bytes may still be in the FIFO and output shift
register.
- IRQ_BREAK: The IRQ triggers if a BREAK state is detected at RX.
Properties & side effects:
- After a BREAK, a valid character must be received before another break
can be detected.
- Each break puts a 0xff character into the input buffer.
The irq.flags() value is cleared only with a new wanted event. Do not
change the flags otherwise.
Signed-off-by: robert-hh <robert@hammelrath.com>
By default, the peripheral clock for UART and SPI is set to 48 MHz and will
not be affected by the MCU clock change. This can be changed by a second
argument to `machine.freq(freq, peripheral_freq)`. The second argument
must be either 48 MHz or identical with the first argument.
Note that UART and SPI baud rates may have to be re-configured after
changing the MCU clock.
Signed-off-by: robert-hh <robert@hammelrath.com>
As a side effect, the peripheral clock will be set to 48Mhz and both UART
and I2C will not be affected by CPu speed changed using `machine.freq()`.
With the change the UART baud rate range is 50 to 3_000_000.
Signed-off-by: robert-hh <robert@hammelrath.com>
If `MICROPY_PY_THREAD` is set to 0 (ie: a user C module wishes to use core1
exclusively) then the test of `core1_entry` would fail to compile with an
"undeclared identifier" error. Fix it by wrapping in `MICROPY_PY_THREAD`.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Add `machine_i2s_deinit_all` to teardown any active I2S instances on soft
reset. Prior to this fix, code using I2S required a try/finally in order
to avoid a hard fault on soft reset.
Fixes issue #14339.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
This change has no impact on vanilla MicroPython builds, but is intended to
avoid RP2's PIO implementation from trampling PIO usage in USER_C_MODULES.
This is consistent with PIOs tracking of used state machines and managed
programs, and makes working with PIO in USER_C_MODULES much less of an
uphill battle.
Since PIO deinit runs before gc_sweep_all it's impossible to work around
this wrinkle otherwise. A module finalizer does not get the opportunity to
put the PIOs back into a state which wont crash rp2_pio_deinit.
Changes are:
- init: Avoid exclusive handlers being added to all PIOs and add them only
when needed.
- deinit: Only remove handlers we have set.
- rp2_pio_irq: Add the exlusive handler if needed.
- rp2_state_machine_irq: Add the exclusive handler if needed.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Reduce mimimum heap requirement. This value allows more room for large,
static buffers in user C modules (such as graphics buffers or otherwise)
which might be allocated outside of MicroPython's heap to guarantee
alignment or avoid fragmentation.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Add MICROPY_DEF_BOARD as per esp32 port, allows board variants to override
the board name with:
list(APPEND MICROPY_DEF_BOARD
MICROPY_HW_BOARD_NAME="New Board Name"
)
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Having IPv6 support is important, especially for IoT-Devices which might be
many, requiring individual IP-addresses. In particular direct access via
link-local addresses and having deterministic SLAAC-addresses can be quite
convenient. Also in IPv6-only networks or for connecting to IPv6-only
services, this is very useful.
For the Pico W, there is enough flash and RAM that enabling IPv6 by default
is the right choice.
Should IPv6 support in a network exist (i.e. there are Router
Advertisements), but not provide connectivity, connecting by domain name
should not be a problem as DNS will default to return the IPv4-address (if
that exists), unless reconfigured at runtime to prefer IPv6.
In any case a user can disable obtaining SLAAC-addresses with:
<nic>.ipconfig(autoconf6=False)
Signed-off-by: Felix Dörre <felix@dogcraft.de>
Updates rp2 port to always resume from idle within 1ms max.
When rp2 port went tickless the behaviour of machine.idle() changed as
there is no longer a tick interrupt to wake it up every millisecond. On a
quiet system it would now block indefinitely. No other port does this.
See parent commit for justification of why this change is useful.
Also adds a test case that fails without this change.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
If core1 executes `mp_wfe_or_timeout()` then it needs to receive an
interrupt or a SEV to resume execution, but the soft timer interrupt only
fires on core 0. This fix adds a SEV to the soft timer interrupt handler.
This issue was masked by the issue fixed in the previous commit, as WFE
previously wasn't suspending properly.
Verified via the existing thread_sleep2 test.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Fixes a regression introduced in 3af006efb3
where WFE never blocked in `mp_wfe_or_timeout()` function and would
busy-wait instead. This increases power consumption measurably.
Root cause is that `mp_wfe_or_timeout()` calls soft timer functions that
(after the regression) call `recursive_mutex_enter()` and
`recursive_mutex_exit()`. The exit calls
`lock_internal_spin_unlock_with_notify()` and the default pico-sdk
implementation of this macro issues a SEV which negates the WFE that
follows it, meaning the CPU never suspends.
See https://forums.raspberrypi.com/viewtopic.php?p=2233908 for more
details.
The fix in this comment adds a custom "nowait" variant mutex that doesn't
do WFE/SEV, and uses this one for PendSV. This will use more power when
there's contention for the PendSV mutex as the other core will spin, but
this shouldn't happen very often.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
The change closes the gap in the API when an integer is used as Pin
reference. With the change, e.g. ADC(26), ADC(Pin(26)) and ADC("GP26")
behave identically and the GPIO is initialised in ACD/high-Z mode.
Only when using ADC channel numbers 0-3 are the corresponding GPIO left
uninitialised, and then the user is responsible for configuring the GPIO.
Signed-off-by: robert-hh <robert@hammelrath.com>
There are three changes here:
- Fix `rp2_pio_print` to use `pio_get_index()` too, since it had its own
copy of the ternary expression.
- Remove a ternary from `rp2_pio_state_machine` and calculate it from
`pio_get_index`.
- Remove a ternary on `GPIO_FUNC_PIO0` vs `GPIO_FUNC_PIO1`. These
constants are sequentially ordered so we can calculate them too.
Signed-off-by: Phil Howard <github@gadgetoid.com>
The `PIO_NUM` macro was defined when `rp2_pio.c` was first conceived.
There's now a Pico SDK function for this, `pio_get_index()`, which is
already used in some parts of the code.
This commit removes `PIO_NUM` in favour of using `pio_get_index()`
everywhere.
Signed-off-by: Phil Howard <github@gadgetoid.com>
Pico SDK defines `__dsb()` and `__sev()` so use those instead of the CMSIS
equivalents. This matches the use of `__wfi()` in lieu of `__WFI()` and
lowers the dependency on CMSIS headers.
And then, move the include of "RP2040.h" from the widely-included
"mphalport.h" to specific source files that need this header, to keep its
inclusion contained.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
It looks like the variants for this board were never being built properly,
because the auto-build system used the variant name from `board.json` which
did not match the variant names in the original `mpconfigboard.mk`. Eg
`FLASH_2MB` in `board.json` but `FLASH_2M` in `mpconfigboard.mk`.
This mistake is apparent since 5dff78f38e,
which made it a build error to specify an invalid variant.
Fix this by using the correct variant names in `board.json`.
Signed-off-by: Damien George <damien@micropython.org>
Follow-up to a84c7a0ed9, this commit works most of the time but has an
intermittent bug where USB doesn't resume as expected after waking from
light sleep.
Turns out waking calls clocks_init() which will re-initialise the USB PLL.
Most of the time this is OK but occasionally it seems like the clock
glitches the USB peripheral and it stops working until the next hard reset.
Adds a machine.lightsleep() test that consistently hangs in the first
two dozen iterations on rp2 without this fix. Passed over 100 times in a
row with this fix.
The test is currently rp2-only as it seems similar lightsleep USB issues
exist on other ports (both pyboard and ESP32-S3 native USB don't send any
data to the host after waking, until they receive something from the host
first.)
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Adapts pico-sdk clocks_init() into clocks_init_optional_usb() which takes
an argument to initialise USB clocks or not.
To avoid a code size increase the SDK clocks_init() function is linker
wrapped to become clocks_init_optional_usb(true).
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
mp_thread_begin_atomic_section() is expected to be recursive (i.e. for
nested machine.disable_irq() calls, or if Python code calls disable_irq()
and then the Python runtime calls mp_handle_pending() which also enters an
atomic section to check the scheduler state).
On rp2 when not using core1 the atomic sections are recursive.
However when core1 was active (i.e. _thread) then there was a bug that
caused the core to live-lock if an atomic section recursed.
Adds a test case specifically for mutual exclusion and recursive atomic
sections when using two threads. Without this fix the test immediately
hangs on rp2.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Currently, `cyw43_delay_ms()` calls `mp_hal_delay_ms()` which uses PendSV
to set up a timer and wait for an interrupt, using wfe. But in the cyw43
initialisation stage PendSV is disabled and so this delay suspends on the
wfe instruction for an indefinite amount of time.
Work around this by changing the implementation of `cyw43_delay_ms()` to a
busy loop.
Fixes issue #15220.
Signed-off-by: Damien George <damien@micropython.org>
Assuming that ${MICROPY_PORT_DIR}/boards/${MICROPY_BOARD} is equal to
${MICROPY_BOARD_DIR} is not valid, because the latter could point to a path
outside the main MicroPython repository.
Replace this path with the canonical ${MICROPY_BOARD_DIR} so that pins.csv
is correctly located when building against out-of-tree board definitions.
Additionally remove MICROPY_BOARDS_DIR to discourage similar mistakes.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
Without this change going to lightsleep stops the USB peripheral clock, and
can lead to either the device going into a weird state or the host deciding
to issue a bus reset.
This change only keeps the USB peripheral clocks enabled if the USB device
is currently active and a host has configured the device. This means the
USB device continues to respond to host transfers and (presumably) will
even complete pending endpoint transfers. All other requests are NAKed
while still asleep, but the interaction with the host seems to resume
correctly on wake
Otherwise, if USB is not active or configured by a host, USB clocks are
disabled, the same as before.
With the change, one can issue a `machine.lightsleep(...)` with USB CDC
connected and the USB CDC remains connected during the sleep and resumes
when the lightsleep finishes.
Tested on a RPi Pico, the power consumption is:
- During normal idle at the REPL, about 15.3mA.
- During lightsleep, prior to this change, about 1.35mA.
- During lightsleep, with this change and USB CDC connected, about 3.7mA.
If power consumption should be as low as possible when USB is connected,
one can use `machine.USBDevice` to disable the USB before entering
lightsleep.
As discussed at https://github.com/orgs/micropython/discussions/14401
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
To avoid undefined references to `mp_thread_begin_atomic_section()` /
`mp_thread_end_atomic_section()`, replace them with the
`MICROPY_BEGIN_ATOMIC_SECTION` / `MICROPY_END_ATOMIC_SECTION`
macros. That way, it's possible to build again with `MICROPY_PY_THREAD`
disabled (made possible by efa54c27b9).
Fixes commit 19844b4983.
Signed-off-by: Matthias Blankertz <matthias@blankertz.org>