Without this, the register address is not copied into the single
buffer, and so the requested register address is garbage when
the driver reads it.
Signed-off-by: Julia Vassiliki <julia.vassiliki@unsw.edu.au>
This commit adds `.deinit()` to the `machine.I2C` class, bringing it in
line with both the target I2C class variant and the rest of the
peripheral classes.
Ports that want to allocate I²C bus entries dynamically can implement
the `self.deinit()` method to add deallocation/cleanup code, otherwise
this method is entirely optional to have. If no method is found in the
port-provided object structure then calling `deinit()` on the object
will do nothing, following what the `machine.SPI` object does.
This addresses #19096.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This prevents Python code from accidentally performing an
operation that resizes the buffer.
However, in the case that the build excludes memoryview, the crash
is still possible.
Closes: #17848
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
To disable `machine.SoftSPI` set zero in line
#define MICROPY_PY_MACHINE_SOFTSPI (0)
in file `ports/esp32/mpconfigport.h`.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
To disable `machine.SoftI2C` set zero
#define MICROPY_PY_MACHINE_SOFTI2C (0)
in `ports/esp32/mpconfigport.h`.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Removes the need for -I$(TOP)/shared/tinyusb/ in the stm32 Makefile
by using an explicit path in the two files that include mp_usbd.h
outside of the shared/tinyusb/ directory itself.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
After machine.ADC has been moved to extmod/machine_adc.c.
Adding adc.read_timed() and adc.busy() to extmod/machine_adc.c with
a corresponding flag to enable them.
ADC/DAC timed are by default enabled only at all SAMD51 devices and
at SAMD21 devices with an external flash for the file system.
Add class constants for the reference voltage source.
As far as possible the STM32 names are used, except where they should
match common board silkscreen labels.
Signed-off-by: robert-hh <robert@hammelrath.com>
This callback argument was previously mis-labelled as a boolean,
but it's actually the tusb_xfer_result_t values from TinyUSB.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
User callbacks allow code to respond to incoming messages without blocking
or polling. User callbacks are optional, and if no callback is registered
the code has no effect.
The mechanism is the same as for TCP: when a connection is accepted or a
TCP packet is received, a user callback is executed.
Fixes issue #3594.
Signed-off-by: Jack Whitham <jack.d.whitham@gmail.com>
Because socket objects have a finaliser they must be created carefully, in
case an exception is raised during the population of their members, eg
invalid input argument or out-of-memory when allocating additional arrays.
Prior to the fix in this commit, the finaliser would crash due to
`incoming.udp_raw.array` being an invalid pointer in the following cases:
- if a SOCK_RAW was created with a proto argument that was not an integer
- if a SOCK_DGRAM or SOCK_RAW was created where the allocation of
`lwip_incoming_packet_t` failed
- if an integer was passed in for the socket type but it was not one of
SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
Furthermore, if the allocation of `lwip_incoming_packet_t` failed then it
may have led to corruption within lwIP when freeing `socket->pcb.raw`
because that PCB was not fully set up with its callbacks.
This commit fixes all of these issues by ensuring:
- `pcb.tcp` and `incoming.udp_raw.array` are initialised to NULL early on
- the proto argument is parsed before allocating the PCB
- the allocation of `lwip_incoming_packet_t` occurs befor allocating the
PCB
- `incoming.udp_raw.array` is checked for NULL in the finaliser code
The corresponding test (which already checked most of these causes of
failure) has been updated to include a previously-uncovered scenario.
Signed-off-by: Damien George <damien@micropython.org>
This commit fixes an issue related to the NimBLE initialisation
procedure in low memory environments on ESP32 boards.
MicroPython uses at least two different NimBLE stacks across the
supported ports, mynewt (imported as an external library), and the one
provided by Espressif in their own SDKs. The problem is that these two
ports differ in the signature for `nimble_port_init(void)`, with mynewt
returning `void`, and Espressif's returning a status code on failure.
On ESP32 boards, allocating almost all the available heap and then
turning on the Bluetooth stack would trigger a failure in the NimBLE
initialisation function that is not handled by the NimBLE integration
code, as there's no expectation of a recoverable condition. Since the
stack initialisation would progress, then uninitialised memory accesses
crash the board.
Since we cannot really modify neither mynewt nor Espressif SDKs, the
next best thing is to provide two conditional initialisation paths
depending on a configuration setting. This would make Espressif ports
recover from a failed initialisation whilst retaining the existing
behaviour on other ports.
This fixes#14293.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
API is different to the original machine.CAN proposal, as numerous
shortcomings were found during initial implementation.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
These classes are based on the Quadrature Encoder blocks of the i.MXRT
MCUs. The i.MXRT 102x has two encoders, the other ones four. The i.MXRT
101x does not support this function. It is implemented as two classes,
Encoder and Counter.
The number of pins that can be uses as inputs is limited by the MCU
architecture and the board schematics. The Encoder class supports:
- Defining the module.
- Defining the input pins.
- Defining a pin for an index signal.
- Defining a pin for a reset signal.
- Defining an output pin showing the compare match signal.
- Setting the number of cycles per revolution (min/max).
- Setting the initial value for the position.
- Setting the counting direction.
- Setting a glitch filter.
- Defining callbacks for getting to a specific position, overrun and
underrun (starting the next revolution). These callbacks can be hard
interrupts to ensure short latency.
The encoder counts all phases of a cycle. The span for the position is
2**32, for the revolution is 2**16. The highest input frequency is
CPU-Clock/24. Note that the "phases" argument is emulated at the API
level (the hardware will always count all phases).
The Counter mode counts single pulses on input A of the Encoder. The
configuration supports:
- Defining the module.
- Defining the input pin.
- Defining the counting direction, either fixed or controlled by the level
of an input pin.
- Defining a pin for an index signal.
- Defining an output pin showing the compare match signal.
- Setting the counter value.
- Setting the glitch filter.
- Defining a callback which is called at a certain value.
- Settings for MIMXRT1015. The MIMXRT1015 MCU has only one encoder/counter
unit.
The counting range is 0 - 2**32-1 and a 16 bit overrun counter. The
highest input frequency is CPU-Clock/12.
The implementation of the `.irq()` method uses the common code from
`shared/runtime/mpirq.c`, including the `irq().flags()` and
`irq().trigger()` methods.
Signed-off-by: robert-hh <robert@hammelrath.com>
Fixes rp2 issue where socket.getaddrinfo() could block indefinitely if an
interface goes down and still has a DNS server configured, as the LWIP
timer stops running and can't time out the DNS query.
Adds a regression test under multi_wlan that times out on rp2 without this
fix.
Fixes issue #18797.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Factor out mp_os_urandom() of each port into extmod/modos.c, which then
calls the port-specific function mp_hal_get_random().
Move mp_hal_get_random() to mphalport where suitable. At the
MIMXRT and SAMD it is left in modos.c, since there are different
implementation depending on the MCU family.
At the ALIF, ESP32, CC3200 and RP2 port the file modos.c was removed,
since it was empty after moving mp_hal_get_random().
Tested for the cc3200, esp32, esp8266, mimxrt, nrf, rp2, samd, stm32
and unix ports. Compiled for the alif and the renesas port.
Signed-off-by: robert-hh <robert@hammelrath.com>
Call `mp_event_handle_nowait()` in the VFS reader buffer refill path so
that pending scheduled events (USB task, network poll, etc.) get processed
during long-running import/parse/compile operations.
Without this, importing a large Python module from the filesystem blocks
for too long causing TinyUSB event queue to overflow. For example, on
renesas-ra, running a script that imports iperf3 via mpremote run, asserts,
most likely due to SOF interrupts not getting processing:
queue_event at lib/tinyusb/src/device/usbd.c:382
dcd_event_handler at lib/tinyusb/src/device/usbd.c:1318
dcd_event_sof at lib/tinyusb/src/device/dcd.h:237
dcd_int_handler at tinyusb/src/portable/renesas/rusb2/dcd_rusb2.c:964
<signal handler called>
disk_ioctl at extmod/vfs_fat_diskio.c:125
validate at lib/oofatfs/ff.c:3359
f_read at lib/oofatfs/ff.c:3625
file_obj_read at extmod/vfs_fat_file.c:75
mp_stream_rw at py/stream.c:60
mp_reader_vfs_readbyte at extmod/vfs_reader.c:59
next_char at py/lexer.c:174
mp_lexer_to_next at py/lexer.c:713
mp_parse at py/parse.c:1167
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
If the return value is set only when certain condition are true,
the compiler sometimes raises an error.
Signed-off-by: robert-hh <robert@hammelrath.com>
Both mbedTLS and axTLS have support for producing more detailed error
strings. However, these are not used if the error is raised in stream
protocol functions (read/write/ioctl).
This commit adds support for more detailed error messages from streams.
Under the hood it's using a new MP_STREAM_RAISE_ERROR ioctl request to pass
the error code back to the stream implementation which can raise a more
detailed error. If the ioctl is not implemented, we fall back to the old
behaviour and raise an OSError with the error code.
Currently the detailed messages are only implemented for TLS sockets since
those already had helper functions for raising detailed exceptions, but can
be easily implemented in any other stream.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
This commit adds support to the `marshal` module to be able to dump
functions that have child functions. For example:
import marshal
def f():
def child():
return 1
return child
marshal.dumps(f.__code__)
It also covers the case of marshalling functions that use list
comprehensions, because a list comprehension uses a child function.
This is made possible by the newly enhanced
`mp_raw_code_save_fun_to_bytes()` that can now handle nested functions.
Unmarshalling via `marshal.loads()` already supports nested functions
because it uses the standard `mp_raw_code_load_mem()` function which is
used to import mpy files (and hence can handle all possibilities).
Signed-off-by: Damien George <damien@micropython.org>
This commit fixes a long standing bug/deficiency in the lwIP socket code,
whereby it would abandon all incoming TCP data if the remote sent a TCP
RST.
This behaviour it tested by the existing
`tests/multi_net/tcp_client_rst.py` and
`tests/multi_net/asyncio_tcp_client_rst.py` tests, and they both fail on
boards like PYBD_SFx and RPI_PICO_W due to the deficiency.
With the fix here, both of those tests now pass on lwIP targets, along with
all existing socket tests.
Signed-off-by: Damien George <damien@micropython.org>
This allows tests like `asyncio_event_queue.py` to succeed under the native
emitter when poll is enabled.
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit reworks the setup procedure for the OpenAMP trace buffer,
used by the libmetal framework to provide cross-core logging data if
needed.
Before these changes, the buffer was provided by MicroPython, as a fixed
size 128 bytes chunk that was accidentally put into the .rodata section,
making it not usable for its intended purpose.
Now, a buffer placed in .bss with a default size of 128 bytes is
provided by MicroPython unless chosen otherwise. A user-chosen buffer
pointer can be provided to MicroPython using the
MICROPY_PY_OPENAMP_TRACE_BUF preprocessor definition. If what
MicroPython provides by default is fine, the buffer size can be
overridden with a new value for the MICROPY_PY_OPENAMP_TRACE_BUF_LEN
preprocessor definition instead.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
TinyUSB defines TUD_OPT_RHPORT which is the same thing, make
shorter definition RHPORT in the two files which use it.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This fixes several assertion errors that were found in fuzz testing, for
unimplemented portions of the websocket spec. The assertions were either
turned into Python exceptions, or the missing functionality was
implemented.
Split frames are now enabled and work, enabling reception of frames up to
64kB (assuming they are encoded with a 16-bit size field).
Frames with a 64-bit size fields remain unsupported but no longer result in
an assertion error. Instead, Initial reception of such a frame will result
in OSError(EIO) and subsequent operations on the same websocket will fail
because framing has been lost.
Transmitting frames larger than 64kB is unsupported. Attempting to
transmit such a frame will result in OSError(ENOBUFS). Subsequent
operations on the websocket are possible.
Signed-off-by: Jeff Epler <jepler@gmail.com>
With the aim of getting consistency, and removing the need to learn an
additional term, replace uses of uPy/uPython with MPy/MicroPython.
Rule of thumb was to use "MPy" abbreviation where "CPy" is used nearby, but
the full word MicroPython otherwise.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Ensures that the underlying socket is opened with the correct protocol as
parsed by `getaddrinfo()`.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This catches cases where the return type is not a small int, eg it could be
a string, or even a big integer.
Signed-off-by: Damien George <damien@micropython.org>
That is, an object whose type defines the protocol slot.
Note that due to protocol confusion, a variant of the original crasher that
returned e.g., a machine.Pin instance could still lead to a crash (#17852).
Fixes issue #17841.
Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
machine.Timer() has inconsistent behaviour between ports: some run
callbacks in hard IRQ context whereas others schedule them like soft IRQs.
As on the rp2 port, add support to the generic software timer for a hard=
argument to explicitly choose between these, setting the default to False
to match the existing behaviour. This enables hard timer callbacks for
the alif, mimxrt and samd ports.
Signed-off-by: Chris Webb <chris@arachsys.com>
Avoiding the double timeout when used with the UART class.
`stream.readinto1()` returns after the first timeout.
Fixes issue #17611.
Signed-off-by: robert-hh <robert@hammelrath.com>
This adds the ability to expose CPU-specific features/extensions to
scripts when the `platform` module is compiled in, by implementing
`platform.processor()`. Right now this is only available on
bare-metal RV32 and RV64.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>