Commit Graph

1126 Commits

Author SHA1 Message Date
David Lechner c118b5d0e4 extmod/extmod.mk: Separate out extmod file list from py.mk to extmod.mk.
This separates extmod source files from `py.mk`.  Previously, `py.mk`
assumed that every consumer of the py/ directory also wanted to include
extmod/.  However, this is not the case.  For example, building mpy-cross
uses py/ but doesn't need extmod/.

This commit moves all extmod-specific items from `py.mk` to `extmod.mk` and
explicitly includes `extmod.mk` in ports that use it.

Signed-off-by: David Lechner <david@pybricks.com>
2022-06-21 00:14:34 +10:00
Damien George 4802b6d3af extmod/extmod.cmake: Only include modbtree in build if it's enabled.
Following how it's done in extmod.mk.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-20 23:37:38 +10:00
Damien George f5769698e5 extmod/modlwip: Clean up inclusion of modlwip in build process.
The following changes are made:

- Guard entire file with MICROPY_PY_LWIP, so it can be included in the
  build while still being disabled (for consistency with other extmod
  modules).

- Add modlwip.c to list of all extmod source in py/py.mk and
  extmod/extmod.cmake so all ports can easily use it.

- Move generic modlwip GIT_SUBMODULES build configuration code from
  ports/rp2/CMakeLists.txt to extmod/extmod.cmake, so it can be reused by
  other ports.

- Remove now unnecessary inclusion of modlwip.c in EXTMOD_SRC_C in esp8266
  port, and in SRC_QSTR in mimxrt port.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-20 23:37:38 +10:00
iabdalkader efa73ca833 extmod/network_ninaw10: Rename WLAN connect argument from essid to ssid.
Addresses issue #8083.
2022-06-17 21:43:44 +10:00
iabdalkader c502cf73e0 extmod/network_cyw43: Rename WLAN keyword args to ssid/security/key.
Rename WLAN keyword args to scan(), connect() and config() to be more
consistent across ports and WLAN drivers.  This change is backwards
compatible and will support obsolete keyword args, except for positional
"essid" which is now deprecated in favor of "ssid".

The changed argument names are
- "essid" changed to "ssid"
- "auth" or "authmode" changed to "security"
- "password" changed to "key"

Addresses issue #8083.
2022-06-17 21:43:25 +10:00
Damien George 5233fb3a3d extmod/machine_i2c: Only use WRITE1 option if transfer supports it.
When MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 is enabled the port's hardware
I2C transfer functions should support the MP_MACHINE_I2C_FLAG_WRITE1
option, but software I2C will not.  So add a flag to the I2C protocol
struct so each individual protocol can indicate whether it supports this
option or not.

Fixes issue #8765.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-17 11:57:57 +10:00
iabdalkader 58b35c9abd extmod/extmod.cmake: Fix hard-coded mbedtls config file path.
* The mbedtls config file path is hard-coded to the config file in
the stm32 port. Any port using this cmake fragment is not actually
using its own config file.
2022-06-11 21:00:13 +10:00
iabdalkader a4eef90b22 extmod/modusocket: Fix polling of closed sockets.
Unbound sockets in NEW state should return HUP|WR when polled, and return
NVAL when in CLOSED state.
2022-06-08 14:15:01 +10:00
iabdalkader 70bf6ab6fb extmod/modusocket: Add socket state to track new/listening/conn/closed. 2022-06-08 14:13:59 +10:00
Damien George bd375df02c extmod/extmod.cmake: Require components to be explicitly enabled.
Otherwise include directories are added unconditionally to the build
variables if the component (submodule) is checked out.  This can lead to,
eg, the esp32 build using lib/lwip header files, instead of lwip header
files from the IDF.

Fixes issue #8727.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-08 13:03:34 +10:00
Damien George e8e8c7c354 extmod/modurandom: Fix missing void in empty argument list.
Signed-off-by: Damien George <damien@micropython.org>
2022-06-07 23:41:49 +10:00
Damien George 9670a156da all: Rename MICROPY_PY_WIZNET5K to MICROPY_PY_NETWORK_WIZNET5K.
To match MICROPY_PY_NETWORK_CYW43 and MICROPY_PY_NETWORK_NINAW10.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-07 16:55:18 +10:00
Andrew Leech 4188bbd3d3 extmod/modussl_mbedtls: Poll EVENT_POLL_HOOK in ssl handshake loop.
Otherwise this is essentially an infinite loop on ports that do not use
interrupts to service network interfaces.

Signed-off-by: Andrew Leech <andrew@alelec.net>
2022-06-03 14:34:29 +10:00
Andrew Leech 15fea3a1ff rp2: Integrate lwIP network stack.
Signed-off-by: Andrew Leech <andrew@alelec.net>
2022-06-03 14:34:18 +10:00
Andrew Leech 21b3a396de extmod/network_wiznet5k: Add Wiznet Ethernet network interface.
Originally based on both stm32/network_wiznet5k and stm32/modnwwiznet5k.

If MICROPY_PY_LWIP is enabled it uses the lwIP TCP stack in MicroPython,
communicating with the Wiznet controller in MACRAW mode.  In this mode it
supports using the INTN pin from Wiznet controller to receive data from an
interrupt trigger.

If lwIP is not enabled, it runs in modnetwork/socket mode providing an
interface to the TCP stack running on the Wiznet controller chip.  In this
mode it includes some updates by @irinakim12 from #8021, most notably
bringing in DHCP support.

Supports defining hardware pins in board config or dynamically set at
runtime.  Sets a default MAC address in the random namespace from board
unique-id.

Signed-off-by: Andrew Leech <andrew@alelec.net>
2022-06-03 14:29:11 +10:00
Andrew Leech 91fb9e7888 extmod/nimble: Add support for reading whole HCI UART packets.
This can improve efficiency for Bluetooth systems that already process
whole packets at the lower layers.
2022-06-03 11:53:28 +10:00
Damien George a1afb337d2 extmod/uasyncio: Fix edge case for cancellation of wait_for.
This fixes the cases where the task being waited on finishes just before or
just after the wait_for itself is cancelled.

Fixes issue #8717.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-02 17:14:20 +10:00
Damien George efe23aca71 all: Remove third argument to MP_REGISTER_MODULE.
It's no longer needed because this macro is now processed after
preprocessing the source code via cpp (in the qstr extraction stage), which
means unused MP_REGISTER_MODULE's are filtered out by the preprocessor.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-02 16:31:37 +10:00
Damien George 4a1ae99ac3 extmod/machine_i2c: Add optional support for write-then-read transfers.
This option is useful for ports where it's more efficient to do a full I2C
transfer in one go.

Signed-off-by: Damien George <damien@micropython.org>
2022-06-01 13:20:27 +10:00
iabdalkader 0bdfceacbe extmod/network_ninaw10: Add support for socket events callback. 2022-05-26 11:15:07 +10:00
iabdalkader be83bdf9ec drivers/ninaw10: Update driver to support firmware 1.5.0.
* Firmware 1.5.0 introduces a new BSD-like sockets ABI,
which improves the integration with MicroPython.
2022-05-25 16:05:50 +02:00
Damien George 5956466c0e py/builtin: Clean up and simplify import_stat and builtin_open config.
The following changes are made:

- If MICROPY_VFS is enabled then mp_vfs_import_stat and mp_vfs_open are
  automatically used for mp_import_stat and mp_builtin_open respectively.

- If MICROPY_PY_IO is enabled then "open" is automatically included in the
  set of builtins, and points to mp_builtin_open_obj.

This helps to clean up and simplify the most common port configuration.

Signed-off-by: Damien George <damien@micropython.org>
2022-05-25 13:04:45 +10:00
iabdalkader edf41d2bf8 extmod/modusocket: Add sendall function. 2022-05-25 00:19:41 +10:00
iabdalkader b9d2f1e844 extmod/modusocket: Add timeout and callback to socket object. 2022-05-25 00:19:41 +10:00
iabdalkader 6841fecbb2 extmod/modusocket: Fix socket_make_new argument parsing. 2022-05-25 00:19:40 +10:00
iabdalkader 3438e80060 extmod/modusocket: Add support for socket events callback.
Add support for the special sockopt 20.
2022-05-25 00:18:33 +10:00
iabdalkader fc1f876175 extmod/modusocket: Add socket type print function. 2022-05-25 00:18:28 +10:00
iabdalkader eb957b0c95 extmod/modusocket: Fix errcode returned from socket read/write.
Drivers should ensure a positive errcode is returned from read/write.
2022-05-25 00:18:18 +10:00
iabdalkader 6136c7644a extmod/modusocket: Bind unconnected socket to default NIC in setsockopt.
Bind socket to default NIC if setsockopt is called before the socket is
bound, to allow setting SO_REUSEADDR before calling socket_bind().

Fixes issue #8653.
2022-05-25 00:16:32 +10:00
iabdalkader 7b4147dd0b extmod/modusocket: Fix polling of a new socket.
New sockets should return HUP and WR when polled, following modlwip.
2022-05-25 00:12:42 +10:00
iabdalkader 6b6ceafe1a extmod/webrepl: Fix setting password in foreground mode and factor code.
The password was not being set when running in foreground mode.  Duplicate
code has been removed.
2022-05-24 13:12:40 +10:00
Jim Mussared 75efb3267c extmod: Revert accidental usocket->socket rename.
The registration of the usocket module was accidentally changed to socket
in moving to MP_REGISTER_MODULE in bb794f05b7
2022-05-24 00:43:44 +10:00
Jim Mussared bb794f05b7 extmod: Make port-included extmod modules use MP_REGISTER_MODULES.
_onewire, socket, and network were previously added by the port rather
than objmodule.c.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-18 20:49:12 +10:00
Jim Mussared 4eab44a1ec extmod: Make extmod modules use MP_REGISTER_MODULE.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-18 20:49:12 +10:00
Damien George b0a1b60a9b extmod: Move font_petme128_8x8.h from ports/stm32 to extmod.
And add spaces after commas so it is consistently formatted.

Signed-off-by: Damien George <damien@micropython.org>
2022-05-05 13:30:40 +10:00
Jim Mussared 0e7bfc88c6 all: Use mp_obj_malloc everywhere it's applicable.
This replaces occurences of

    foo_t *foo = m_new_obj(foo_t);
    foo->base.type = &foo_type;

with

    foo_t *foo = mp_obj_malloc(foo_t, &foo_type);

Excludes any places where base is a sub-field or when new0/memset is used.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-03 22:28:14 +10:00
Damien George caaff940a2 extmod/uasyncio: Rename and merge TaskQueue push/pop methods.
These are internal names and can be safely renamed without affecting user
code.  push_sorted() and push_head() are merged into a single push()
method, which is already how the C version is implemented.  pop_head() is
simply renamed to pop().

The changes are:
- q.push_sorted(task, t) -> q.push(task, t)
- q.push_head(task) -> q.push(task)
- q.pop_head() -> q.pop()

The shorter names and removal of push_head() leads to a code size reduction
of between 40 and 64 bytes on bare-metal targets.

Signed-off-by: Damien George <damien@micropython.org>
2022-04-22 16:37:02 +10:00
Damien George 28e7e15c0a extmod/uasyncio: Fix bug with task ending just after gather is cancel'd.
This fixes a bug where the gather is cancelled externally and then one of
its sub-tasks (that the gather was waiting on) finishes right between the
cancellation being queued and being executed.

Signed-off-by: Damien George <damien@micropython.org>
2022-04-21 14:25:17 +10:00
Damien George f7454f850f extmod/uasyncio: Make Python Task match C version with use of asserts.
This helps to catch bugs when a Task is put on more than one pairing heap.

Signed-off-by: Damien George <damien@micropython.org>
2022-04-21 14:25:17 +10:00
Damien George aab005c75b extmod/modusocket: Provide config macro for socket.listen backlog deflt.
To make it possible to change this value for any given port or board.

Signed-off-by: Damien George <damien@micropython.org>
2022-04-11 15:28:56 +10:00
Jon Bjarni Bjarnason 919f696ad2 extmod/modusocket: Implement optional socket.listen backlog argument.
This follows the CPython change: https://bugs.python.org/issue21455

Socket listen backlog defaults to 2 if not given, based on most bare metal
targets not having many resources for a large backlog.  On UNIX it defaults
to SOMAXCONN or 128, whichever is less.
2022-04-11 15:26:47 +10:00
Damien George 11ab90391d extmod/extmod.cmake: Add micropy_lib_mbedtls component.
Signed-off-by: Damien George <damien@micropython.org>
2022-04-04 23:15:01 +10:00
Damien George 90aaf2dbef extmod/uasyncio: Fix gather cancelling and handling of exceptions.
The following fixes are made:
- cancelling a gather now cancels all sub-tasks of the gather (previously
  it would only cancel the first)
- if any sub-task of a gather raises an exception then the gather finishes
  (previously it would only finish if the first sub-task raised)

Fixes issues #5798, #7807, #7901.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-30 16:07:44 +11:00
Damien George 335002a4c0 extmod/uasyncio: Allow task state to be a callable.
This implements a form of CPython's "add_done_callback()", but at this
stage it is a hidden feature and only intended to be used internally.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-30 16:07:44 +11:00
Damien George 63f0e700f4 extmod/modure: Set subject begin_line so ^ doesn't match interior.
Fixes issue #8402.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-16 12:21:00 +11:00
Damien George 0149cd6b8b windows: Switch to VFS subsystem and use VfsPosix.
Following the unix port.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-10 00:26:36 +11:00
Damien George 2b409ef8a4 unix/moduos: Convert module to use extmod version.
All variants now use extmod/moduos.c as their uos module implementation.
In particular this means they all have MICROPY_VFS enabled and use VfsPosix
for their filesystem.

As part of this, the available functions in uos become more consistent with
other ports:
- coverage variant gets uos.urandom
- minimal and standard variant get: unlink, chdir, getcwd, listdir

Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 21:13:57 +11:00
Damien George ade2720e55 esp8266/moduos: Convert module to use extmod version.
Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 10:03:23 +11:00
Damien George 11b77263ef stm32/moduos: Convert module to use extmod version.
Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 10:03:23 +11:00
Damien George 818be10bb5 zephyr/moduos: Convert module to use extmod version.
This also adds uos.unlink(), for all ports that use extmod/moduos.c.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 10:03:23 +11:00
Damien George 1c53d85162 esp32/moduos: Convert module to use extmod version.
Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 10:03:23 +11:00
Damien George 926b554daf extmod/moduos: Create general uos module to be used by all ports.
Based on the rp2 port version, with the rp2 port converted to use this
module.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-09 10:03:23 +11:00
Daniël van de Giessen e2513bfe8d extmod/moduzlib: Fix parsing zlib header dict size.
From RFC 1950 section 2.2: "CINFO is the base-2 logarithm of the LZ77
window size, minus eight (CINFO=7 indicates a 32K window size)"

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2022-03-08 23:16:14 +11:00
stijn 49934fcf8b extmod/moduplatform: Move platform PP definitions into a header.
These are more generally useful than just for the module so make them
globally available, prefixed consistently with MICROPY_PLATFORM_.
2022-02-22 00:59:14 +11:00
iabdalkader 465b74e78d drivers/ninaw10: Add NIC-level ioctl function.
This commit adds support in the driver for irregular commands.  It
currently supports setting GPIO pin mode, and GPIO pin read/write value.
2022-02-18 14:35:26 +11:00
Christian Decker 2e3a2785cd extmod/modubinascii: Add newline keyword to b2a_base64 function.
This allows encoding things (eg a Basic-Auth header for a request) without
slicing the \n from the string, which allocates additional memory.

Co-authored-by: David Lechner <david@lechnology.com>
2022-01-23 10:18:01 +11:00
Damien George 7d71ae25ed extmod/machine_i2c: Increase default SoftI2C timeout to 50ms.
Some devices, eg BNO055, can stretch SCL for a long time, so make the
default large to accommodate them.  50ms matches the current default for
stm32 hardware I2C .

Signed-off-by: Damien George <damien@micropython.org>
2022-01-21 15:10:30 +11:00
Damien George 9438fb7321 extmod/modusocket: Support additional args to getaddrinfo.
Signed-off-by: Damien George <damien@micropython.org>
2022-01-21 13:40:06 +11:00
iabdalkader 1aac151d68 drivers/ninaw10: Return standard error numbers. 2022-01-21 13:35:05 +11:00
iabdalkader a63875d5ad extmod/modusocket: Create new sockets in blocking mode.
To conform with CPython and other MicroPython ports.
2022-01-21 13:34:56 +11:00
iabdalkader 155eb1361e extmod/modusocket: Add makefile() method and common socket options. 2022-01-21 13:34:06 +11:00
iabdalkader b23178a9c0 extmod/modusocket: Make setsockopt return if NIC is not connected. 2022-01-21 13:32:09 +11:00
iabdalkader e401ff8935 drivers/ninaw10: Fix timeout handling to match modusocket. 2022-01-21 13:31:41 +11:00
iabdalkader 9a61bc3aa7 extmod/network_ninaw10: Implement MP_STREAM_POLL in ioctl.
There is currently no function to query if the socket is writable.
2022-01-21 13:30:48 +11:00
Damien George e7fff736b5 extmod/modbluetooth: Put declaration of connect_cancel in correct place.
This fixes a bug introduced in 851ecb2da1

Signed-off-by: Damien George <damien@micropython.org>
2022-01-13 13:45:43 +11:00
Damien George 889dee8076 extmod/modbluetooth: Fix conditional compilation of ringbuf_put_uuid.
This fixes a bug introduced in a76604afba

Signed-off-by: Damien George <damien@micropython.org>
2022-01-13 13:45:07 +11:00
iabdalkader f2ccf87e0b extmod/network_ninaw10: Use socket timeout preset in modusocket. 2022-01-12 14:37:40 +11:00
iabdalkader 842da93011 extmod/modusocket: Initialise accepted socket state. 2022-01-12 14:37:32 +11:00
iabdalkader 67420de4f4 extmod/modusocket: Allow setting timeout on unbound sockets.
For an extended state socket, if settimeout() is called before a NIC is
bound, save the timeout until the NIC is bound.
2022-01-12 14:36:55 +11:00
Damien George 3243abfda2 extmod/moduplatform: Detect xtensa arch.
Signed-off-by: Damien George <damien@micropython.org>
2022-01-06 18:24:52 +11:00
iabdalkader 5a86031223 extmod/network_ninaw10: Make recv/recvfrom interchangeable. 2022-01-06 14:36:57 +11:00
iabdalkader 73a6b53dbe extmod/network_ninaw10: Return -1 on timeout from recv/send. 2022-01-06 14:36:55 +11:00
iabdalkader 544c232eb7 extmod/network_ninaw10: Make NIC state persistent. 2022-01-06 14:36:51 +11:00
iabdalkader 0f25e0387c extmod/network_ninaw10: Disable active connections before connecting. 2022-01-06 14:36:44 +11:00
iabdalkader 1b7eee24eb extmod/network_ninaw10: Fix config of AP mode.
* Fix missing call to connect to configure module in AP mode.
* Use enum for config/connect args indices.
2021-12-14 15:14:58 +11:00
Damien George efde4b2c75 extmod/modure: Redirect regex debug printing to mp_printf.
Signed-off-by: Damien George <damien@micropython.org>
2021-12-09 12:53:04 +11:00
Jim Mussared 3770fab334 all: Update Python formatting to latest Black version 21.12b0.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-12-09 12:09:40 +11:00
Jonathan Hogg 851ecb2da1 extmod/modbluetooth: Support gap_connect(None) to cancel a connection.
Allow cancellation of in-progress peripheral connections.
2021-12-01 11:56:37 +11:00
iabdalkader f7a0c98e00 extmod/network_ninaw10: Fix scan list order to match other NICs. 2021-11-19 15:41:26 +11:00
stijn 5900257dd6 extmod/uplatform: Use generic custom platform string.
Don't force the 'HAL' string to be part of the platform string because
it doesn't have a sensible meaning for all possible platforms, and
swap it with the PLATFORM_ARCH string so the strings which most platforms
have come first.
2021-11-18 10:46:14 +11:00
stijn 1e5875557a extmod/uplatform: Remove unused definitions. 2021-11-18 10:46:14 +11:00
Damien George 43d08688c3 extmod/uasyncio: Fix gather returning exceptions from a cancelled task.
Fixes issue #5882.
2021-11-17 14:11:31 +11:00
iabdalkader 43079aaf86 drivers/ninaw10: Add ublox Nina-W10 WiFi/BT module driver.
- Add WiFi/BT drivers for ublox Nina-W10 (esp32 based) module.
- Add ublox Nina-W10 Python module in extmod.
2021-11-13 23:01:03 +11:00
Jim Mussared 9519484c56 extmod/nimble: Remove workaround for OS_ENOMEM.
This was fixed in NimBLE 1.4.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-10-26 16:43:18 +11:00
Jim Mussared 948e3289bf extmod/nimble: Update to NimBLE v1.4.
We're using the MicroPython fork of NimBLE, which on the
`micropython_1_4_0` branch re-adds support for 64-bit targets and fixes
initialisation of g_msys_pool_list.

Also updates modbluetooth_nimble.c to suit v1.4.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-10-26 16:43:18 +11:00
Jim Mussared 43467b9c71 extmod/modbluetooth: Add connection interval to gap_connect.
This forwards through directly to the NimBLE and BTStack connect functions.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-10-26 00:16:53 +11:00
Andrew Leech 2ceeabf180 extmod/vfs_posix_file: Support MP_STREAM_POLL in vfs_posix_file_ioctl.
Allows asyncio reading of sys.stdin when MICROPY_PY_USELECT is used in the
build configuration.
2021-10-19 22:47:18 +11:00
stijn d42cba0d22 extmod/moduplatform: Improve implementation for PC ports.
Fix identification of 32/64 bit and of the Windows platform and add a
platform string mimicking CPython for the latter.
2021-09-24 13:51:39 +10:00
iabdalkader 2c5e9bbdfa extmod: Add platform module.
It contains the compiler version, and underlying system HAL/SDK version.
2021-09-19 23:35:10 +10:00
iabdalkader 38f8e852e0 rp2: Add framework for networking.
MICROPY_PY_NETWORK and MICROPY_PY_USOCKET need to be enabled by a board to
get networking.  No NICs have yet been defined.
2021-09-19 23:20:13 +10:00
iabdalkader 8064c3bf9c extmod/nimble: Add nimble CMake fragment file. 2021-09-19 23:02:16 +10:00
iabdalkader 80f2c794e6 extmod/mpbthci.h: Add mp_bluetooth_hci_uart_any prototype.
This allows drivers to use mpbthciport functions to read/write/poll UART.
2021-09-19 23:00:39 +10:00
iabdalkader d9749f90ad extmod/modnetwork: Remove modnetwork socket u_state member.
To simplify the socket state.

The CC3K driver (see drivers/cc3000/inc/socket.h and src/socket.c) has
socket() returning an INT16 so there is now enough room to store it
directly in the fileno member.
2021-09-15 11:29:02 +10:00
iabdalkader f9d573a4ac extmod/modnetwork: Remove STM32 references. 2021-09-15 11:27:38 +10:00
Damien George a34d43b2b7 extmod/network_cyw43: Make consistent use of STA and AP constants.
The network.STA_IF and network.AP_IF constants are now independent to the
CYW43_ITF_STA and CYW43_ITF_AP constants.

Signed-off-by: Damien George <damien@micropython.org>
2021-09-15 01:37:27 +10:00
iabdalkader 4dba04a50f extmod/modnetwork: Define network interfaces in port config files.
So this network implementation becomes more generic.
2021-09-15 01:29:26 +10:00
iabdalkader c13e25c329 extmod/modusocket: Add read/write stream methods to socket object.
Following other socket implementations.
2021-09-15 01:28:37 +10:00
iabdalkader e7429389a6 extmod/modnetwork: Add extended socket state. 2021-09-15 01:26:23 +10:00
iabdalkader d889f672da extmod/modnetwork: Add STA_IF and AP_IF constants. 2021-09-15 01:25:42 +10:00
iabdalkader 7aab0dc5d8 extmod: Move modnetwork and modusocket from stm32 to extmod.
So they can be used by other ports.
2021-09-15 01:25:12 +10:00
Damien George af64c2ddbd extmod/machine_pwm: Factor out machine.PWM bindings to common code.
This commit refactors machine.PWM and creates extmod/machine_pwm.c.  The
esp8266, esp32 and rp2 ports all use this and provide implementations of
the required PWM functionality.  This helps to reduce code duplication and
keep the same Python API across ports.

This commit does not make any functional changes.

Signed-off-by: Damien George <damien@micropython.org>
2021-09-04 16:31:17 +10:00
Damien George d41f6dde56 extmod/modonewire: Make _onewire module configurable via macro option.
Signed-off-by: Damien George <damien@micropython.org>
2021-09-02 13:11:23 +10:00
Damien George afe0634c98 extmod/machine_spi: Make SoftSPI configurable via macro option.
Signed-off-by: Damien George <damien@micropython.org>
2021-09-02 13:11:23 +10:00
Damien George 122d901ef1 extmod/machine_i2c: Make SoftI2C configurable via macro option.
The zephyr port doesn't support SoftI2C so it's not enabled, and the legacy
I2C constructor check can be removed.

Signed-off-by: Damien George <damien@micropython.org>
2021-09-02 13:11:23 +10:00
Damien George 30691ed2a1 drivers/cyw43: Make wifi join fail if interface is not active.
Otherwise the Python network object continues to report that it is
attempting to connect.

Also make the return error code consistent with wifi scan.

Signed-off-by: Damien George <damien@micropython.org>
2021-08-31 13:00:11 +10:00
Peter Hinch 2296df0a32 extmod/modframebuf: Enable blit between different formats via a palette.
This achieves a substantial performance improvement when rendering glyphs
to color displays, the benefit increasing proportional to the number of
pixels in the glyph.
2021-08-25 15:31:23 +10:00
Jim Mussared 870000f35b extmod: Add machine.bitstream.
This is a generic API for synchronously bit-banging data on a pin.

Initially this adds a single supported encoding, which supports controlling
WS2812 LEDs.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19 22:50:11 +10:00
Jim Mussared 1d9e489af3 extmod/modbluetooth: Add send_update arg to gatts_write.
This allows the write to trigger a notification or indication, but only to
subscribed clients. This is different to gatts_notify/gatts_indicate,
which will unconditionally notify/indicate.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-14 22:21:55 +10:00
Damien George 8fcdb5490c extmod/modlwip: Fix close and clean up of UDP and raw sockets.
The correct callback-deregister functions must be called dependent on the
socket type, otherwise resources may not be freed correctly.

Signed-off-by: Damien George <damien@micropython.org>
2021-08-13 23:46:11 +10:00
Peter Züger ffc854f17f extmod/modujson: Add support for dump/dumps separators keyword-argument.
Optionally enabled via MICROPY_PY_UJSON_SEPARATORS.  Enabled by default.

For dump, make sure mp_get_stream_raise is called after
mod_ujson_separators since CPython does it in this order (if both
separators and stream are invalid, separators will raise an exception
first).

Add separators argument in the docs as well.

Signed-off-by: Peter Züger <zueger.peter@icloud.com>
Signed-off-by: Damien George <damien@micropython.org>
2021-08-07 13:52:16 +10:00
oclyke e29259d171 extmod/uasyncio: In open_connection use address info in socket creation.
Rudimentary support for various address families.

Signed-off-by: oclyke <oclyke@gmail.com>
2021-07-31 15:33:48 +10:00
Jim Mussared 341158c251 extmod/nimble: Add "memory stalling" mechanism for l2cap_send.
When l2cap_send detects that the sys mempool is running low (used to store
the outgoing HCI payloads), it will report stalled back to the application,
and then only unstall once these HCI payloads have been sent.

This prevents a situation where a remote receiver with very large MTU can
cause NimBLE to queue up more than MYNEWT_VAL_MSYS_1_BLOCK_COUNT (i.e. 12)
payloads, causing further attempts to send to fail with ENOMEM (even though
the channel is not stalled and we have room in the channel mbufs). The
regular credit/stall flow control is not effective here because the
receiver's MTU is large enough that it will not activate (i.e. there are
lots of credits available).

Thresholds of 1/2 (stall) and 1/4 (unstall) chosen to allow headroom for
other payloads (e.g. notifications) and that when a regular stall occurs it
might keep sending (and creating more payloads) in the background.
2021-07-23 21:58:10 +10:00
Jim Mussared edfb5d56c8 extmod/nimble: Allow modbluetooth binding to hook "sent HCI packet". 2021-07-23 21:58:04 +10:00
Jim Mussared 53dfb279da extmod/modbluetooth: Clamp MTU values to 32->UINT16_MAX. 2021-07-23 21:57:58 +10:00
Josh Lloyd db6d60b079 extmod/utime: Always invoke mp_hal_delay_ms when >= to 0ms.
This makes sleep_ms(0) useful as a "yield" so event processing and thread
switching can take place.

Fixes issue #5345.
2021-07-22 00:05:17 +10:00
Damien George 74db526cf0 extmod/btstack/btstack.mk: Use -Wno-implicit-fallthrough, not =0.
In 2ae3c890bd, -Wimplicit-fallthrough=0 was
added to get the build to pass.  This option is equivalent to
-Wno-implicit-fallthrough, and the latter is compatible with clang (while
the former is not).

Fixes issue #7546.

Signed-off-by: Damien George <damien@micropython.org>
2021-07-17 23:55:25 +10:00
Jim Mussared 12e3fcc785 extmod/nimble: Fix leak in l2cap_send if send-while-stalled.
A correctly-behaved application shouldn't do this, but in the
case where the channel is stalled, there's still enough room
in the mbuf pool, then we'll fail the send (BLE_HS_EBUSY) so
the mbuf needs to be freed.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-07-17 23:38:39 +10:00
David Lechner 8758504f0f extmod/moduselect: Conditionally compile select().
This adds #if MICROPY_PY_USELECT_SELECT around the uselect.select()
function. According to the docs, this function is only for CPython
compatibility and should not normally be used. So we can disable it
and save a few bytes of flash space where possible.

Signed-off-by: David Lechner <david@pybricks.com>
2021-07-17 23:32:39 +10:00
Damien George 38a204ed96 py: Introduce and use mp_raise_type_arg helper.
To reduce code size.

Signed-off-by: Damien George <damien@micropython.org>
2021-07-15 00:12:41 +10:00
Damien George 136369d72f all: Update to point to files in new shared/ directory.
Signed-off-by: Damien George <damien@micropython.org>
2021-07-12 17:08:10 +10:00
Damien George 94dfaff18b extmod: Update for move of crypto-algorithms, re1.5, uzlib to lib.
Signed-off-by: Damien George <damien@micropython.org>
2021-07-12 16:37:07 +10:00
Damien George 6dd92d7742 lib/crypto-algorithms: Move crypto-algorithms code from extmod to lib.
It's third-party code, and not necessarily tied to extmod.

Signed-off-by: Damien George <damien@micropython.org>
2021-07-12 16:36:37 +10:00
Damien George d1bfb271d7 lib/uzlib: Move uzlib code from extmod to lib.
It's third-party code, and not necessarily tied to extmod.

Signed-off-by: Damien George <damien@micropython.org>
2021-07-12 16:36:34 +10:00
Damien George d1d172f536 lib/re1.5: Move re1.5 code from extmod to lib.
It's third-party code, and not necessarily tied to extmod.

Signed-off-by: Damien George <damien@micropython.org>
2021-07-12 16:36:26 +10:00
Damien George e2f0b181f9 extmod/axtls-include: Add axtls_os_port.h to customise axTLS.
Signed-off-by: Damien George <damien@micropython.org>
2021-07-08 23:51:49 +10:00
Damien George 7ec95c2768 extmod/uasyncio: Get addr and bind server socket before creating task.
Currently when using uasyncio.start_server() the socket configuration is
done inside a uasyncio.create_task() background function.  If the address
and port are already in use however this throws an OSError which cannot be
cleanly caught behind the create_task().

This commit moves the getaddrinfo and socket binding to the start_server()
function, and only creates the task if that succeeds.  This means that any
OSError from the initial socket configuration is propagated directly up the
call stack, compatible with CPython behaviour.

See #7444.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-26 22:30:22 +10:00
Jeff Epler 413f34cd8f all: Fix signed shifts and NULL access errors from -fsanitize=undefined.
Fixes the following (the line numbers match commit 0e87459e2b):

../../extmod/crypto-algorithms/sha256.c:49:19: runtime error: left shif...
../../extmod/moduasyncio.c:106:35: runtime error: member access within ...
../../py/binary.c:210:13: runtime error: left shift of negative value -...
../../py/mpz.c:744:16: runtime error: negation of -9223372036854775808 ...
../../py/objint.c:109:22: runtime error: left shift of 1 by 31 places c...
../../py/objint_mpz.c:374:9: runtime error: left shift of 4611686018427...
../../py/objint_mpz.c:374:9: runtime error: left shift of negative valu...
../../py/parsenum.c:106:14: runtime error: left shift of 46116860184273...
../../py/runtime.c:395:33: runtime error: left shift of negative value ...
../../py/showbc.c:177:28: runtime error: left shift of negative value -...
../../py/vm.c:321:36: runtime error: left shift of negative value -1```

Testing was done on an amd64 Debian Buster system using gcc-8.3 and these
settings:

    CFLAGS += -g3 -Og -fsanitize=undefined
    LDFLAGS += -fsanitize=undefined

The introduced TASK_PAIRHEAP macro's conditional (x ? &x->i : NULL)
assembles (under amd64 gcc 8.3 -Os) to the same as &x->i, since i is the
initial field of the struct.  However, for the purposes of undefined
behavior analysis the conditional is needed.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-06-24 23:01:04 +10:00
Damien George 6ed5b843cf extmod/btstack: Check that BLE is active before performing operations.
Otherwise it can easily lead to a hard crash.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-23 14:58:39 +10:00
Damien George 0fc0f7536b extmod/btstack: Add missing call to mp_bluetooth_hci_uart_deinit.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-23 13:14:26 +10:00
Damien George 38bc5a9f67 stm32: Provide a custom BTstack runloop that integrates with soft timer.
It reschedules the BT HCI poll soft timer so that it is called exactly when
the next timer expires.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-23 13:14:26 +10:00
Damien George 8107c9b75b extmod/nimble: Remove TODO comment about notify_custom freeing om.
The comments in NimBLE for ble_gattc_notify_custom() state that "This
function consumes the supplied mbuf regardless of the outcome.".  And
inspection of NimBLE code shows that this is the case.  So the comment can
be removed.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-17 14:54:04 +10:00
Damien George 514bf1a191 extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B.  If task B completes just before
task A is cancelled then the cancellation of A does not work.  Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError).  The correct behaviour is for B to bounce that
CancelledError back out.

This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
  at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
  "<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
  uasyncio run queue) and all tasks waiting on this are blocked forever
  from running
- uasyncio otherwise continues to function and other tasks continue to be
  scheduled as normal

The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on.  This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.

A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.

Fixes issue #7386.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-16 13:02:37 +10:00
Mike Teachman b0b8ebc4f6 extmod/uasyncio: Add readinto() method to Stream class.
With docs and a multi-test using TCP server/client.

This method is a MicroPython extension, although there is discussion of
adding it to CPython: https://bugs.python.org/issue41305

Signed-off-by: Mike Teachman <mike.teachman@gmail.com>
2021-06-15 13:13:35 +10:00
Miguel Grinberg de2e081260 extmod/uasyncio: Fix start_server and wait_closed race condition.
This fix prevents server.wait_closed() from raising an AttributeError when
trying to access server.task.  This can happen if it is called immediately
after start_server().
2021-06-08 15:10:50 +10:00
Damien George c3199f5649 extmod/modurandom: Support an argument of bits=0 to getrandbits.
This was changed in CPython 3.9; see https://bugs.python.org/issue40282.

Signed-off-by: Damien George <damien@micropython.org>
2021-05-30 17:05:56 +10:00
Macarthur Inbody 34d4dab683 extmod/modurandom: Add error message when getrandbits has bad value.
The random module's getrandbits() method didn't give a proper error message
when calling it with a value that was outside of the range of 1-32, which
can lead to confusion using this function (which under CPython can accept
numbers larger than 32).  Now instead of simply giving a ValueError it
gives an error message that states that the number of bits is constrained.

Also, since the random module's functions getrandbits() and randint()
differ from CPython, tests have been added to describe these differences.
For getrandbits the relevant documentation is shown and added to the docs.
The same is given for randint method so that the information is more easily
found.

Finally, since the int object lacks the bit_length() method there is a test
for that method also to include within the docs, showing the difference to
CPython.
2021-05-30 16:41:30 +10:00
Damien George 6a127810c0 extmod/moduhashlib: Put hash obj in final state after digest is called.
If digest is called then the hash object is put in a "final" state and
calling update() or digest() again will raise a ValueError (instead of
silently producing the wrong result).

See issue #4119.

Signed-off-by: Damien George <damien@micropython.org>
2021-05-26 21:44:46 +10:00
Damien George 47583d8cbd extmod/moductypes: Fix size and offset calculation for ARRAY of FLOAT32.
uctypes.FLOAT32 has a special value representation and
uctypes_struct_scalar_size() should be used instead of GET_SCALAR_SIZE().

Signed-off-by: Damien George <damien@micropython.org>
2021-05-06 13:11:33 +10:00
Damien George 350a66a863 extmod/moductypes: Replace numbers with macro constants.
Signed-off-by: Damien George <damien@micropython.org>
2021-05-06 12:40:53 +10:00
Damien George 02dc1644b6 extmod/moductypes: Remove double blank lines and debugging printf's.
Signed-off-by: Damien George <damien@micropython.org>
2021-05-06 12:32:09 +10:00
Damien George 4791d290c6 extmod: Remove old comments used for auto-doc generation.
They are no longer used, and the text in the docs is more up to date.

Signed-off-by: Damien George <damien@micropython.org>
2021-05-06 12:27:31 +10:00
Damien George e9e9c76ddf all: Rename mp_keyboard_interrupt to mp_sched_keyboard_interrupt.
To match mp_sched_exception() and mp_sched_schedule().

Signed-off-by: Damien George <damien@micropython.org>
2021-04-30 15:13:43 +10:00
Damien George 342d55529d extmod/uasyncio: Use .errno instead of .args[0] for OSError exceptions.
Signed-off-by: Damien George <damien@micropython.org>
2021-04-23 22:03:46 +10:00
Damien George 212fe7f33e extmod/extmod.cmake: Add support to build btree module with CMake.
Signed-off-by: Damien George <damien@micropython.org>
2021-04-09 13:33:26 +10:00
Jeff Epler 172fb5230a extmod/re1.5: Check and report byte overflow errors in _compilecode.
The generated regex code is limited in the range of jumps and counts, and
this commit checks all cases which can overflow given the right kind of
input regex, and returns an error in such a case.

This change assumes that the results that overflow an int8_t do not
overflow a platform int.

Closes: #7078

Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-04-06 13:36:42 +10:00
Damien George 2b888aa2f3 extmod/modbluetooth: Free temp arrays in gatts register services.
This helps to reduce memory fragmentation, by freeing the heap data as soon
as it is not needed.  It also helps the compiler keeps a reference to the
beginning of both arrays, which need to be traceable by the GC (otherwise
some compilers may optimise this reference to something else).

Signed-off-by: Damien George <damien@micropython.org>
2021-03-16 13:55:45 +11:00
Damien George eccd73a403 extmod/extmod.cmake: Add modonewire.c to MICROPY_SOURCE_EXTMOD list.
Signed-off-by: Damien George <damien@micropython.org>
2021-03-14 15:51:28 +11:00
Jim Mussared a76604afba extmod/modbluetooth: Separate enabling of "client" from "central".
Previously, the MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE macro
controlled enabling both the central mode and the GATT client
functionality (because usually the two go together).

This commits adds a new MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
macro that separately enables the GATT client functionality.
This defaults to MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE.

This also fixes a bug in the NimBLE bindings where a notification
or indication would not be received by a peripheral (acting as client)
as gap_event_cb wasn't handling it. Now both central_gap_event_cb
and peripheral_gap_event_cb share the same common handler for these
events.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-02-19 17:53:43 +11:00
Damien George cf6a015880 extmod/btstack: Use MICROPY_HW_BLE_UART_BAUDRATE for first UART init.
Otherwise the UART may be left in a state at baudrate=0.

Signed-off-by: Damien George <damien@micropython.org>
2021-02-17 16:06:56 +11:00
Thorsten von Eicken 2c1299b007 extmod/modussl: Fix ussl read/recv/send/write errors when non-blocking.
Also fix related problems with socket on esp32, improve docs for
wrap_socket, and add more tests.
2021-02-17 11:50:54 +11:00