Commit Graph

15902 Commits

Author SHA1 Message Date
Damien George 5d07d0c7b0 tests/run-natmodtests.py: Fix search for supported native tests.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-27 11:44:54 +10:00
Damien George df41913782 examples/natmod/btree: Make btree.open use mp_arg_parse_all for kwargs.
Python code is no longer needed to implement keyword arguments in
`btree.open()`, it can now be done in C.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-24 13:50:57 +10:00
Brian Pugh c624a5c0c4 py/dynruntime: Export mp_load_method_maybe and mp_arg_parse_all* funcs.
Also define `mp_type_bytearray`.  These all help to write native modules.

Signed-off-by: Brian Pugh <bnp117@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2024-05-24 13:50:57 +10:00
Jared Hancock a196468c47 esp32: Add support for TCP_NODELAY.
This adds support for the TCP_NODELAY socket option for lwIP sockets.
Generally, TCP sockets use the Nagle algorithm and will send data when
an ACK is received or after all previously-sent data has already been
ACKed.

If the TCP_NODELAY option is set for a socket, every write to the socket
will trigger a packet to be sent.

Signed-off-by: Jared Hancock <jared@greezybacon.me>
2024-05-23 20:47:17 -05:00
Jared Hancock b1e9602702 extmod/modlwip: Use Nagle algorithm and add support for TCP_NODELAY.
This adds support to use the Nagle algorithm implemented already in lwIP to
determine when TCP data should be sent.

As currently written, MicroPython will only create packets if there is <25%
remaining in the send buffer.  Using it, sending a small message of ~50
bytes will not trigger output of the message on the network.  So it will
remained queued until the TCP interval timer expires, which can be up to
500ms.

Using Nagle's algorithm, the first write, no matter how small, will
generate a packet on the network.  And sending lots of data still makes
efficient use of the link.

In addition to this, an application designer may choose to always create
packets for every write by setting the TCP_NODELAY socket option.  That's
also implemented in this commit.
2024-05-23 22:25:06 +10:00
Damien George d532f960a4 examples/natmod/features4: Create custom FactorialError as exc example.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-23 14:20:20 +10:00
Damien George 482292cc66 py/dynruntime: Add mp_obj_exception_init function to create C exception.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-23 14:20:20 +10:00
Damien George a919ce26d3 webassembly/modjsffi: Add mem_info function to get detailed stats.
This allows querying the GC heap size/used/free values, as well as the
number of alive JsProxy and PyProxy objects, referenced by proxy_c_ref and
proxy_js_ref.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-22 17:37:37 +10:00
Damien George 57a9ffa632 webassembly: Register PyProxy objects for JS-side finalisation.
And clear the corresponding `proxy_c_ref[c_ref]` entry when the finaliser
runs.  This then allows the C side to (eventually) garbage collect the
corresponding Python object.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-22 17:37:37 +10:00
Damien George 5c7a414574 webassembly: Add C-level finaliser to JsProxy object.
And clear the corresponding `proxy_js_ref[js_ref]` entry when the finaliser
runs.  This then allows the JavaScript side to (eventually) free the
corresponding JavaScript object.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-22 17:06:18 +10:00
Damien George c0ca4bb85f webassembly: Set GC threshold and do top-level GC collect when possible.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-22 16:32:47 +10:00
Damien George cdaf2de80c webassembly: Track the current depth of calls to external C functions.
So it's possible to know when an external C function is being called at the
top-level, eg by JavaScript without any intermediate C->JS->C calls.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-22 16:29:31 +10:00
Damien George ed2885facb webassembly/proxy_c: Don't return value of a void function.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-21 15:14:14 +10:00
Damien George d7f031397d webassembly/objjsproxy: Make jsproxy_it keep ref to jsproxy.
So that there is a one-to-one correspondence between js_ref and
JsProxy objects.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-21 15:13:55 +10:00
Damien George cfd5a8ea3a webassembly/proxy_c: Return undefined if dict lookup failed on JS side.
Instead of raising KeyError.  These semantics match JavaScript behaviour
and make it much more seamless to pass Python dicts through to JavaScript
as though they were JavaScript {} objects.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:49:42 +10:00
Damien George aa2e3880c1 webassembly/proxy_js: Create a special "undefined" type for Python.
This adds a new undefined singleton to Python, that corresponds directly to
JavaScript `undefined`.  It's accessible via `js.undefined`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:49:10 +10:00
Damien George 0148bbb495 webassembly/proxy_js: Revert back to converting Py None to JS null.
This reverts part of commit fa23e4b093, to
make it so that Python `None` converts to JavaScript `null` (and JavaScript
`null` already converts to Python `None`).  That's consistent with how the
`json` module converts these values back and forth.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:44:43 +10:00
Daniël van de Giessen c10a74b162 esp32/panichandler: Print support information on panic.
When a fatal error occurs it's important to know which precise version it
occurred on in order to be able to decode the crash dump information such
as the backtrace.

By wrapping around the built-in IDF panic handler we can print some extra
information whenever a fatal error occurs.  The message links to a new wiki
page which contains additional information on how to debug ESP32 issues,
and links to the bug reporting issue template.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2024-05-16 12:25:45 +10:00
Angus Gratton a0d4fdcce0 stm32/pyb_can: Fix STM32G4 FDCAN source clock frequency.
Fixes automatic baudrate calculation results.

Default clock source on this SoC is HSE not PCLK1.  We could fix this by
switching to PCLK1 instead, but two extra complications:

- PCLK1 on this board is a 42.5MHz and the Pyboard CAN sample_point
  calculation requires an exact match, which is harder to hit with this
  source frequency.

- Would be a breaking change for any existing Python code on this board,
  i.e. specifying brp, bs1, bs2 to initialise CAN.

In the future it might be worth looking switching to the PLL source on this
SoC instead, as this is a much higher frequency that would give higher
quality BRS bitrate matches (probably too high without using the second
divider going into the CAN peripheral though, so more code changes needed
also).

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-05-15 15:32:41 +10:00
Angus Gratton 47ae739409 examples/usb: Add README that points out the alternative usb modules.
If someone starts from this directory then they won't know they exist,
otherwise.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-05-15 15:20:40 +10:00
Damien George abd1f28bc2 docs/library/asyncio: Document that ThreadSafeFlag now works on unix.
ThreadSafeFlag works on the unix port since commit
df08c38c28.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-15 14:13:30 +10:00
Olivier Lenoir e816b49c44 docs/reference: Document how to mip install packages from GitLab.
Signed-off-by: Olivier Lenoir <olivier.len02@gmail.com>
2024-05-15 13:35:27 +10:00
Olivier Lenoir 85c85e8f0d tools/mpremote: Add support to mip install from GitLab.
Changes are:
- main.py: Add gitlab:org/repo, gitlab:org/repo@branch.
- mip.py: Implement install from GitLab.
- README.md: Add mip install gitlab:org/repo@branch example.

Signed-off-by: Olivier Lenoir <olivier.len02@gmail.com>
2024-05-15 13:32:02 +10:00
Damien George 025d10a702 tests/micropython/import_mpy_invalid.py: Skip if target cant import mpy.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-14 16:02:30 +10:00
Damien George 154d602b6e webassembly/mpconfigport: Enable importing of .mpy files.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-14 15:19:27 +10:00
Damien George fa23e4b093 webassembly/proxy_js: Convert JS undefined and JS null to Py None.
And change Py None conversion so it converts to JS undefined.

The semantics for conversion of these objects are then:
- Python None           -> JavaScript undefined
- JavaScript undefined  -> Python None
- JavaScript null       -> Python None

This follows Pyodide:
https://pyodide.org/en/stable/usage/type-conversions.html

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:53:10 +10:00
Damien George a67e326cb9 webassembly/proxy_c: Ensure objs thrown into generators are exceptions.
This commit defines a new `JsException` exception type which is used on the
Python side to wrap JavaScript errors.  That's then used when a JavaScript
Promise is rejected, and the reason is then converted to a `JsException`
for the Python side to handle.

This new exception is exposed as `jsffi.JsException`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:52:17 +10:00
Damien George 3f34be69c7 webassembly/asyncio: Fix case where a Promise is resolved with no arg.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:48:41 +10:00
Damien George c37eb93f2d webassembly/proxy_c: Support more than 4 args when JS calls Py func.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:48:41 +10:00
Damien George cc3550eeef examples/network: Add example of HTTPS client using non-blocking socket.
Non-blocking SSL streams can be difficult to get right, so provide a
working example, of a HTTPS client.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:37:00 +10:00
Damien George bd610ff016 examples/network: Rename SSL examples to start with https.
It's better for discoverability to have these examples named `https_xxx.py`
rather than `http_xxx_ssl.py`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:35:41 +10:00
Damien George eb517a0a12 examples/usb: Add a USBDevice example implementing the DFU protocol.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:26:29 +10:00
Damien George b2df89c417 examples/usb: Add a very simple USBDevice example with host.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:26:29 +10:00
Angus Gratton c3301da176 docs/library/machine.USBDevice: Update note about packages in mp-lib.
Also fix a small typo.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-05-09 15:48:26 +10:00
Angus Gratton 9a43989a86 shared/tinyusb: Stall the CDC IN endpoint while disconnecting.
Only when dynamic USB devices are enabled.

The issue here is that when the USB reset triggers, the dynamic USB device
reset callback is called from inside the TinyUSB task.

If that callback tries to print something then it'll call through to
tud_cdc_write_flush(), but TinyUSB hasn't finished updating state yet to
know it's no longer configured. Subsequently it may try to queue a transfer
and then the low-level DCD layer panics.

By explicitly stalling the endpoint first, usbd_edpt_claim() will fail and
tud_cdc_write_flush() returns immediately.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-05-09 15:22:31 +10:00
Jared Hancock 8762fe8b4c extmod/network_wiznet5k: Properly enable socket buffers for W5100(S).
The W5100 and W5100S only have 4 available sockets and 16kB of socket
buffer.  Allocating 16kB to either the receive or transmit buffer of a
single socket is not allowed, so the current setup does not change the
allocation for socket 0 from the reset default.  ctlwizchip is returning -1
to indicate the error, but the response isn't being inspected and probably
doesn't need to be.

Signed-off-by: Jared Hancock <jared@greezybacon.me>
2024-05-07 17:42:34 +10:00
Rick Sorensen 63c30a2dfc esp32/modesp32: Add mcu_temperature() function for C3/S2/S3 devices.
For ESP32C3/S2/S3 IDFv5 exposes new internal temperature API which is
different to the base ESP32, IDFv4.

Thanks to @robert-hh for cleaner code and testing sensor capability in
these devices.

See discussion #10443.

Signed-off-by: Rick Sorensen <rick.sorensen@gmail.com>
2024-05-07 17:29:22 +10:00
Rick Sorensen 595f86155a docs/esp32/quickref: Add note about different ESP32 varieties.
Signed-off-by: Rick Sorensen <rick.sorensen@gmail.com>
2024-05-07 17:28:25 +10:00
Damien George be1ecb54e6 webassembly/api: Resolve thenables returned from runPythonAsync.
JavaScript semantics are such that the caller of an async function does not
need to await that function for it to run to completion.  This commit makes
that behaviour also apply to top-level async Python code run via
`runPythonAsync()`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-07 11:33:05 +10:00
Damien George c056840ee8 webassembly/objpyproxy: Implement JS iterator protocol for Py iterables.
This allows using JavaScript for..of on Python iterables.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-07 00:20:56 +10:00
Damien George e860e32e24 webassembly/objjsproxy: Fix proxying in arguments to JS new function.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:47:05 +10:00
Damien George 50b43fec1a webassembly/proxy_c: Only proxy across resolve/reject funs when needed.
To improve efficiency.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:04:22 +10:00
Damien George 9da63a343e webassembly/proxy_c: Reject promises with a PythonError instance.
The `reason` in a rejected promise should be an instance of `Error`.  That
leads to better error messages on the JavaScript side.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:04:13 +10:00
Damien George 9681a66c6b webassembly/api: Fix importing micropython.mjs module from node REPL.
Fixes issue #14363.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 13:53:58 +10:00
Damien George a521df27dc stm32/i2c: Fix clock enable for I2C4 on STM32F7 MCUs.
This was broken by 5aec051f9f when adding
support for I2C4 on H7 MCUs.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-03 17:22:22 +10:00
Damien George a7d34b6f7c stm32/mboot: Buffer the correct amount of bytes for a flash write.
Different MCUs have different requirements for the minimum number of bytes
that can be written to internal flash.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-01 22:49:38 +10:00
Damien George d3fe0a06e8 stm32/flash: Fix writing final words to flash on H5 and H7 MCUs.
The calculations `num_word32 / 4` and `num_word32 / 8` were rounding down
the number of words to program to flash, and therefore possibly truncating
the data (eg mboot could miss writing the final few words of the firmware).

That's fixed in this commit by adding extra logic to program any remaining
words.  And the logic for H5 and H7 is combined.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-01 22:49:38 +10:00
Damien George 64f28dc1eb stm32/boards/LEGO_HUB_NO7: Add robust update logic to mboot.
Following change in 899592ac34

Signed-off-by: Damien George <damien@micropython.org>
2024-05-01 16:16:33 +10:00
Damien George b896fa9b1f stm32/boards/LEGO_HUB_NO6: Write key after writing elements.
In case there is a power failure after during this operation, the key must
be the last thing that is written, to indicate valid data.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-01 15:44:32 +10:00
Matt Trentini b1ac266bb5 docs/develop/optimizations: Fix typo identified in issue 14391.
Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
2024-04-29 09:20:28 +10:00