The RV32 emitter used an additional temporary register, as certain code
sequences required extra storage. This commit removes its usage in all
but one case, using REG_TEMP2 instead.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
The RV32 emitter sometimes generated short load opcodes even when it
was not supposed to. This commit fixes an off-by-one error in its
offset eligibility range calculation and corrects one case of offset
calculation, operating on the raw label index number rather than its
effective offset in the stack (C.LW assumes all loads are
word-aligned).
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
The RV32 emitter always scheduled short jumps even outside the emit
compiler pass. Running the full test suite through the native emitter
instead of just the tests that depend on the emitter at runtime (as in,
`micropython/native_*` and `micropython/viper_* tests`) uncovered more
places where the invalid behaviour was still present.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Skip whitespace characters between pairs of hex numbers.
This makes `bytes.fromhex()` compatible with cpython.
Includes simple test in `tests/basic/builtin_str_hex.py`.
Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
The extra limit for C3 dates from 6823514 which added C3 support.
Measuring the minimum stack margins that can pass the stress tests I
measured 768 bytes for ESP32-S3 and 512 bytes for ESP32-C3 on ESP-IDF
V5.2.2 and similar on V5.0.4. i.e. The ESP32-C3 actually needs less stack
margin not more!
I think the extra margin for ESP32-C3 probably arose from:
1. Some toolchain inefficiency in the IDF V4.x RISC-V compiler codegen,
that has since been improved.
OR
2. The race condition that was fixed in e3955f42 where sometimes the limit
wasn't set correctly at all. This seems to trigger more on C3, presumably
some timing artifact, and I'd believe that some binaries might be more
susceptible than others due to random factors.
OR
3. Commit 6007f3e206 which enabled custom
NLR handling for ESP32-C3.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This value should have been adjusted when the new cstack API was adopted in
e3955f421d, as otherwise the stack limit is too small especially on
ESP32-C3 where the stack limit was 6144 - 2048 - 2048.
Some extra margin is needed for bluetooth irq because invoke_irq_handler()
isn't a top-level task function, NimBLE calls through multiple layers
first. Measuring this overhead on IDF V5.2.2 (by putting an abort() in
invoke_irq_handler() and then measuring the stack size) yielded 672 bytes
on ESP32-S3 and 612 bytes on ESP32-C3, similar to the size reported in
cd66aa05cf.
Sticking with 1024 bytes for added safety margin. This means on Xtensa the
total margin for the BLE task stays the same (2048 bytes) as before
switching to cstack.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
If verbose debugging is enabled there is some stdout output happening
before the GIL is ready (for example, GC initialisation), and the code
assumed that no string printing occurred before the interpreter was fully
initialised. Printing long strings would operate without holding the GIL,
which would crash if string output would happen too early in the startup
process.
This commit addresses that issue, making sure verbose debugging output will
work even before the interpreter is fully initialised (as if it is not yet
ready there's no GIL to take care of).
Also, the threshold that would indicate whether a string is "long" (and
thus requiring a GIL release/lock operation) or not was hardcoded to 20
bytes. This commit makes that configurable, maintaining 20 bytes as a
default.
Fixes issue #15408.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Commit a66bd7a489 added the
ESP32_GENERIC_C3_USB board (now merged with ESP32_GENERIC_C3) and changed
the brownout detector from the default level 7 (~2.51V) to level 4
(~2.92V).
Raising the level again seems to fix random BOD resets on some of the
cheaper ESP32-C3 dev boards (that likely skimp on power supply
capacitance).
Specifically, this change prevents random resets running multi_bluetooth
tests on ESP32-C3 "SuperMini" board.
Also removed from the LOLIN_C3_MINI board as it seems this config is a copy
of the generic one.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
The ESP-IDF default on C3 is primary UART0, secondary USB serial/jtag.
Previously MicroPython configured the primary as USB Serial/JTAG and
manually worked with the UART0 console. However UART0 console stopped
working this way in v5.2.2.
The big change is that CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG is no longer set,
as primary console is UART0. However
CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG is set and IDF provides a
macro CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED which is set if either
primary or secondary esp_console is USB serial/jtag. So need to use that
macro instead.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Saves code size, MicroPython doesn't appear to rely on any of the missing
formatters (64-bit integers, c99-style named arguments).
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Replaces the deprecated ESP32 calibration API with the "line" method
instead.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Update to the test added in 1e98c4cb75,
changes the SPI pins for ESP32-C3 (IO 18 and 19 are the native USB pins).
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Necessary to pass CI when testing the V2 preview APIs.
Also adds an extra coverage test for the legacy stackctrl API, to maintain
coverage and check for any regression.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This change moves that complexity out into the stack checker and fixes the
bug where stack margin wasn't set correctly by ESP32-C3 threads.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Currently the stack limit margin is hard-coded in each port's call to
`mp_stack_set_limit()`, but on threaded ports it's fiddlier and can lead to
bugs (such as incorrect thread stack margin on esp32).
This commit provides a new API to initialise the C Stack in one function
call, with a config macro to set the margin. Where possible the new call
is inlined to reduce code size in thread-free ports.
Intended replacement for `MP_TASK_STACK_LIMIT_MARGIN` on esp32.
The previous `stackctrl.h` API is still present and unmodified apart from a
deprecation comment. However it's not available when the
`MICROPY_PREVIEW_VERSION_2` macro is set.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
These were changed in v1.11 (2019). Prepare to remove the compatibility
macros as part of V2 changes.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
`mbedtls_pk_parse_key()` expects `key_len` to include the NULL terminator
for PEM data but not for DER encoded data. This also applies to
`mbedtls_x509_crt_parse()` and `cert_len`.
Since all PEM data contains "-----BEGIN" this is used to check if the data
is PEM (as per mbedtls code).
This can be done for both v2 and v3 of mbedtls since the fundamental
behaviour/expectation did not change. What changed is that in v3 the
PKCS#8 DER parser now checks that the passed key buffer is fully utilized
and no bytes are remaining (all other DER formats still do not check this).
Signed-off-by: Peter Züger <zueger.peter@icloud.com>
`get_lan()`: If the ethernet MAC address is uninitialised, set it to the
address reserved by the ESP32 for the ETH interface.
SPI LAN devices may be initialised with a MAC address of 00:00:00:00:00:00.
So check that a valid unicast MAC address has been set (using
`LAN.config(mac=...)`) when initialising the LAN interface.
Fixes#15425.
Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
This PR ensures that `network.LAN.active(True/False)` will succeed if the
LAN is already in the desired state.
Currently, `lan.active(True)` will raise an `OSError` exception if the LAN
is already in the desired state. This is inconsistent with
`network.WLAN.active(True/False)` and causes `lan.active(True)` to raise an
exception after a soft reset (causing common network startup scripts to
fail for LAN interfaces).
Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
The reason for this change is that it makes allows custom code, that needs
to use an MPU region, to find a free one by using this macro or starting
from the max number and downwards, without concern that it might change in
the future.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Add native gchelper support for 64 bits RISC-V RV64I targets.
Now that RV64 is under CI, this also enables platform-specific ghelper
in the Unix port.
Also changes the data type holding the register contents to something more
appropriate, so in the remote eventuality somebody wants to use this with
RV128 all they have to do is update the `__riscv_xlen` check.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Updating a set must use `.update()` rather than `.add()`.
Also apply the same pattern to qemu-riscv to prevent the same issue when
directories/files are added to that port's `tests_profile.txt` file.
Signed-off-by: Damien George <damien@micropython.org>
Based on machine_i2s_rate, allows testing basic SPI functionality and
timings.
Implemented and confirmed working for rp2, esp32, and pyboard.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Finalisers that run during `gc_sweep_all()` may run native code, for
example if an open file is closed and the underlying block device is
implemented in native code, then the filesystem driver (eg FAT) may call
into the native code.
Therefore, native code must be freed after the call to `gc_sweep_all()`.
That can only be achieved if the GC heap is not used to store the list of
allocated native code blocks. Instead, this commit makes the native code
blocks a linked list.
Signed-off-by: Damien George <damien@micropython.org>
Without this commit, math.gamma(-float("inf")) returns inf instead of
raising a math domain ValueError. Needed for float/math_domain_special.py
test to pass on esp32.
Root cause is an upstream libm bug, has been reported to ESP-IDF.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This is needed for a workaround on esp32 port (in child commit),
which produces incorrect results otherwise.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
The `emit_load_reg_with_object()` helper function will clobber `REG_TEMP0`.
This is currently OK on architectures where `REG_RET` and `REG_TEMP0` are
the same (all architectures except RV32), because all callers of
`emit_load_reg_with_object()` use either `REG_RET` or `REG_TEMP0` as the
destination register. But on RV32 these registers are different and so
when `REG_RET` is the destination, `REG_TEMP0` is clobbered, leading to
incorrectly generated machine code.
This commit fixes the issue simply by using `REG_TEMP0` as the destination
register for all uses of `emit_load_reg_with_object()`, and adds a comment
to make sure the caller of this function is careful.
Signed-off-by: Damien George <damien@micropython.org>
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>