This version is believed to work from Clang 3.0 to 22.1.0
(all versions on godbolt at the time of writing).
Clang rejects the `(void)x;` notation for a used variable
in a naked asm function, so do this only conditionally.
Introduces use of `__builtin_unreachable()` with gcc.
This saves 1 byte by causing gcc not to emit an `ud2` opcode
at the end. However, the unreachable sanitizer (enabled by
default(!) on Ubuntu 24.04 with gcc version 13.3.0) corrupts
the ebx register, so it must be disabled.
Clang does not accept `__builtin_unreachable`
or `return 0;` here, UNREACHABLE must expand to nothing.
Closes: #17415
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
Some object representations have floats as literal objects (ie not heap
allocated) and in such a case using `mp_obj_is_type(t, &mp_type_float)`
will always return false. So add a compile-time assertion to force the
correct usage.
Signed-off-by: Damien George <damien@micropython.org>
This enables C++ modules to correctly postion -l linker flags at the end of
the flags instead of at the start. Updated the example C++ micropython.mk
accordingly.
Signed-off-by: Dryw Wade <dryw.wade@sparkfun.com>
This adds support for `bytes.find(x)` where x is an integer value. It also
extends to `bytearray` as well as the methods `.rfind()`, `.index()` and
`.rindex()`.
This allows existing Python code that uses integers like this to "just
work" (i.e. CPython compatibility is always good). The Python alternative
is a bit awkward, i.e. given a byte value, to use find/index you have to
make it into a single-element bytes, e.g. `b.find(chr(n).encode())`.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
When averaging is selected, the resolution is fixed to 12 bit.
The configuration has to be changed to cater for the result shifts.
Side change: Remove a duplicated code line in init().
Signed-off-by: robert-hh <robert@hammelrath.com>
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>
Fixes:
- Leave no half-initialized device if init fails.
- Fix dac_deinit_channel(). Perform deinit only for channels that
had been initilized.
Signed-off-by: robert-hh <robert@hammelrath.com>
Both together require ~1.9k of flash space, including the DMA-manager
and the TC-manager. adc.read_timed() uses ~700 bytes, dac.write_timed()
~600 bytes.
Signed-off-by: robert-hh <robert@hammelrath.com>
Since the two channels of a SAMD51 are not completely independent,
dac.deinit() now clears both channels, and both channels have to
be re-instantiated after a deinit().
Side change:
- rearrange some code lines.
Signed-off-by: robert-hh <robert@hammelrath.com>
These return True, while a timed action is ongoing.
Side change:
Reorder some code in machine_dac.c and do not reset DAC twice.
Signed-off-by: robert-hh <robert@hammelrath.com>
Enabling a callback that will be called when a adc.read_timed_into() run
is finished. That's especially useful with slow sampling rates and/or
many samples, avoiding to guess the sampling time.
Raise an error is adc.read_u16() is called while a read_timed_into()
is active.
Other ADC changes:
- SAMD51: use ADC1 if both ADC1 and ADC0 are available at a Pin.
Signed-off-by: robert-hh <robert@hammelrath.com>
The callback is called when a dac_timed() sequence finishes. It will be
reset with callback=None or omitting the callback option in the
constructor.
Side change: Set the clock freq. to 48Mhz.
Signed-off-by: robert-hh <robert@hammelrath.com>
Used as:
adc.read_timed(buffer, freq)
Buffer must be preallocated. The size determines the number of 16 bit
words to be read. The numeric range of the results is that of the raw
ADC. The call returns immediately, and the data transfer is done by DMA.
The caller must wait sufficiently long until the data is sampled
and can be noticed by a callback. No internal checks are made for
a too-high freq value.
Read speeds depends on Average and bit length setting:
SAMD21: Max. 350kS/s (8 bit, Average 1)
SAMD51: Max. 1 MS/s (8 bit, Average 1)
Signed-off-by: robert-hh <robert@hammelrath.com>
Used as:
dac.write_timed(data, freq [, count])
dac.deinit()
Working range for dac_timed():
SAMD21: 1 Hz - 100 kHz (1 MHz clock, 10 bit)
SAMD51: 1 Hz - ~500 kHz (8 MHz clock, 12 bit)
The buffer has to be a byte array or a halfword array,
and the data is sent once.
The default for count is 1. If set to a value > 0, the data will be
transmitted count times. If set to 0 or < 0, the date will be
transmitted until deliberately stopped. The playback
can be stopped with dac.deinit().
dac.deinit() just releases the timer and DMA channel needed by
dac_timed(). The DAC object itself does not have to be released.
Signed-off-by: robert-hh <robert@hammelrath.com>
These functions are use to allocate, free and configure a set
of TC counter instances. The SAMxx MCU have between 3 to 5 (SAMD21) and
4 to 8 (SAMD51) TC instances. Two of them are used for the µs counter,
the remaining 1 - 6 instances are administered here for use by
various functions, like timed DMA transfers.
Signed-off-by: robert-hh <robert@hammelrath.com>
Used for allocation of DMA channels. It will be needed for planned
modules and methods like adc_timed(), dac_timed(), I2S.
It includes management code for DMA IRQ handlers, similar to what
was made for Sercom.
Signed-off-by: robert-hh <robert@hammelrath.com>
Document the three methods that IOBase subclasses implement (readinto,
write, ioctl) and the common ioctl operations. Includes a simple write-only
stream example and a pollable ring buffer example.
IOBase docs are inline in io.rst before StringIO/BytesIO, following
CPython's structure.
Also fix a statement in io.rst that incorrectly claimed streams cannot be
subclassed in pure Python.
Signed-off-by: Andrew Leech <andrew.leech@planet-innovation.com>
Per the docs for `time.sleep()` and `time.sleep_ms()`. This gets the
`tests/micropython/schedule_sleep.py` test working when using the native
emitter.
Signed-off-by: Damien George <damien@micropython.org>
`systick.h` and `pendsv.h` both use MICROPY-level configuration macros, so
must include `py/mpconfig.h`.
Signed-off-by: Damien George <damien@micropython.org>
These legacy functions were copied verbatim from the stm32 port and never
used. And the use of WFI in `systick_wait_at_least()` is probably wrong.
And correct the comment in SysTick_Handler, which was also copied from
stm32.
Signed-off-by: Damien George <damien@micropython.org>
This function gives direct access to the 32-bit SysTick millisecond
counter. And then use it to implement `mp_hal_ticks_ms()`.
Signed-off-by: Damien George <damien@micropython.org>
Add board configuration files for the Arduino UNO Q, enabling MicroPython
on its STM32U585 (Cortex-M33) microcontroller.
arduino_uno_q.overlay:
- Redirects zephyr,console from &usart1
- Adds a 256 KB storage_partition at 0xF0000 for littlefs /flash
arduino_uno_q.conf:
- Enables serial console, GPIO, flash, and littlefs filesystem
Signed-off-by: rmouro_QCOM <rmouro@qti.qualcomm.com>
Fix three bugs in the Zephyr UART init:
1. Stop bits variable typo: `data_bits = UART_CFG_STOP_BITS_2` assigned to
the wrong variable, corrupting data_bits and leaving stop_bits
uninitialized when stop=2 is requested.
2. Config struct used raw `args[ARG_stop].u_int` (e.g. integer 2) instead
of the computed `stop_bits` enum (e.g. `UART_CFG_STOP_BITS_2 = 3`),
resulting in `UART_CFG_STOP_BITS_1_5` being configured instead of
`UART_CFG_STOP_BITS_2`.
3. Swapped default values for txbuf/rxbuf arguments: `MP_QSTR_txbuf`
defaulted to `UART_RX_RING_BUF_DEF_SIZE` and vice versa (latent since
both are 128 today).
Signed-off-by: Calin Faja <calinfaja@gmail.com>
This commit converts the zephyr port to use the new event waiting
mechanism, with the `MICROPY_INTERNAL_WFE` macro.
Signed-off-by: Damien George <damien@micropython.org>
A new argument is added to this function to select whether it exits early
when signalled via `mp_hal_signal_event()`, so it can be used to wait for
general events (rather than just a specific semaphore).
And rename this function to `mp_hal_wait_event()` to better match its new
semantics.
Signed-off-by: Damien George <damien@micropython.org>
Stack checking is enabled with the minimal configuration, so this margin
configuration must be set or else the stack can easily overflow when
running the recursive stress tests.
Signed-off-by: Damien George <damien@micropython.org>
Stop using `mp_machine_i2c_transfer_adaptor` and instead implement support
for `MP_MACHINE_I2C_FLAG_WRITE1`. That allows combined write-read
transactions like `I2C.readfrom_mem()` to work properly with a RESTART,
instead of being split into a separate write-with-STOP followed by a
read-with-STOP (many I2C controllers in zephyr don't support incomplete
transactions).
Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
This commit brings the QEMU port in line with other ports when it comes
to making sure the port builds with a user module to be part of the main
interpreter.
To not impact too much on the build time, only the `MPS2_AN385` board
does this as there'd be just too many targets to test, for not much
gain.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit adds a naive implementation of the "abort" standard C
library, that is needed by certain C++ runtimes let code link.
Although the Arm toolchain used in the CI image does not need this,
newer or different toolchains may actually need this (eg. the Arm EABI
toolchain provided by Arch Linux). Given the limited scope of the QEMU
port, the function simply spins forever.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit expands the linkerscript for STM32-series boards to let C++
code link with the interpreter core.
The linkerscript now contains all necessary sections for C++ code that
uses exceptions to be part of a user module inside the MicroPython
image.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit expands the linkerscript for nRF51-series boards to let C++
code link with the interpreter core.
The linkerscript now contains all necessary sections for C++ code that
uses exceptions to be part of a user module inside the MicroPython
image.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit expands the linkerscript for MPS3-series boards to let C++
code link with the interpreter core.
The linkerscript now contains all necessary sections for C++ code that
uses exceptions to be part of a user module inside the MicroPython
image.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit expands the linkerscript for MPS2-series boards to let C++
code link with the interpreter core.
The linkerscript now contains all necessary sections for C++ code that
uses exceptions to be part of a user module inside the MicroPython
image.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>