The implementation samples rosc.randombits at a frequency lower than the
oscillator frequency. This gives better random values. In addition, for
an 8-bit value 8 samples are taken and fed through a 8-bit CRC,
distributing the sampling over the byte. The resulting sampling rate is
about 120k/sec.
The RNG does not include testing of error conditions, like the ROSC being
in sync with the sampling or completely failing. Making the interim value
static causes it to perform a little bit better in short sync or drop-out
situations.
The output of uos.urandom() performs well with the NIST800-22 test suite.
In my trial it passed all tests of the sts 2.1.2 test suite. I also ran a
test of the random data with the Common Criteria test suite AIS 31, and it
passed all tests too.
STM32L476RG MCU of NUCLEO_L476RG board has 6 UART/USART units in total
(USART1, USART2, USART3, UART4, UART5 and LPUART1), but only UART2,
connected to REPL, was defined and available in Python code.
Defined are all 5 remaining UART/USART units including LPUART1.
Signed-off-by: Alexander Ziubin aziubin@googlemail.com
This commit simplifies the customisation of the main MicroPython execution
loop (4 macros are reduced to 2), and allows a board to have full control
over the execution (or not) of boot.py and main.py.
For boards that use the default start-up code, there is no functional
change in this commit.
Signed-off-by: Damien George <damien@micropython.org>
Per CPython everything which comes after the command, module or file
argument is not an option for the interpreter itself. Hence the processing
of options should stop when encountering those, and the remainder be passed
as sys.argv. Note the latter was already the case for a module or file but
not for a command.
This fixes issues like 'micropython myfile.py -h' showing the help and
exiting instead of passing '-h' as sys.argv[1], likewise for
'-X <something>' being treated as a special option no matter where it
occurs on the command line.
Some forum users noticed that `sm.exec()` took longer the more was present
on the flash filesystem connected to the RP2040. They traced this back to
the `array` import inside `asm_pio()`, which is causing MicroPython to scan
the filesystem.
uarray is a built-in module, so importing it shouldn't require scanning the
filesystem.
We avoid moving the import to the top-level in order to keep the namespace
clean; we don't want to accidentally expose `rp2.array`.
Support for C++ was added in 97960dc7de but
that commit didn't include the C++ exception handling table in the binary
firmware image. This commit fixes that.
Signed-off-by: Damien George <damien@micropython.org>
This is a workaround for errata RP2040-E5, and is needed to make USB more
reliable on certain USB ports.
Signed-off-by: Damien George <damien@micropython.org>
It's a bit of a pitfall with user C modules that including them in the
build does not automatically enable them. This commit changes the docs and
examples for user C modules to encourage writers of user C modules to
enable them unconditionally. This makes things simpler and covers most use
cases.
See discussion in issue #6960, and also #7086.
Signed-off-by: Damien George <damien@micropython.org>
It was noticed that the esp32 port didn't build ulab correctly. The
problem was a multiple defintion of the 'mp_hal_stdout_tx_str' and
'mp_hal_stdout_tx_strn_cooked' functions.
They were defined in stdout_helpers.c but also in the
ports/esp32/mphalport.c.
Fixed by removing stdout_helpers.c from the build.
Signed-off-by: Michael O'Cleirigh <michael.ocleirigh@rivulet.ca>
Support for User C and C++ modules was lost due to upgrading the esp32 to
the latest CMake based IDF from the GNUMakefile build process.
Restore the support for the esp32 port by integrating with the approach
recently added for the rp2 port.
Signed-off-by: Michael O'Cleirigh <michael.ocleirigh@rivulet.ca>
This USB feature is currently not supported. With this flag enabled (and
the feature not implemented) the USB serial will stop working if there is a
delay of more than about 2 seconds between messages, which can occur with
USB autosuspend enabled.
Fixes issue #6866.
The parts that are generic are added to py/ so they can be used by other
ports that use CMake.
py/usermod.cmake:
* Creates a usermod target to hang user C/CXX modules from.
* Gathers sources from user C/CXX modules and libs for QSTR scan.
ports/rp2/CMakeLists.txt:
* Includes py/usermod.cmake.
* Links the resulting usermod library to the MicroPython target.
py/mkrules.cmake:
Add cxxflags to qstr.i.last custom command for CXX modules:
* MICROPY_CPP_FLAGS so CXX modules will find includes.
* -DNO_QSTR to fix fatal error missing "genhdr/qstrdefs.generated.h".
Usage:
The rp2 port can be linked against user C modules by running:
make USER_C_MODULES=/path/to/module/micropython.cmake
CMake will print a list of included modules.
Co-authored-by: Graham Sanderson <graham.sanderson@raspberrypi.org>
Co-authored-by: Michael O'Cleirigh <michael.ocleirigh@rivulet.ca>
Signed-off-by: Phil Howard <phil@pimoroni.com>
This commit simplifies and cleans up the bare-arm port, and adds just
enough system and library code to make it execute on an STM32F405 MCU.
The mpconfigport.h configuration is simplified to just specify those
configuration values that are different from the defaults. And the
addition of -fdata-sections and -ffunction-sections means the final
firmware is smaller than it previously was, by about 4200 bytes.
A README is also added.
Signed-off-by: Damien George <damien@micropython.org>
The GNU Make dir command uses spaces as item separator so it does not
work for e.g building the STM32 port on Cygwin with a default Arm
installation in "c:/program files (x86)/GNU Arm Embedded Toolchain".
Fix by using POSIX dirname on a quoted path instead.
When UART is used for REPL and the MCU frequency is changed, the UART
has to be re-initialised. Besides that the UART may have to be recreated
after a frequency change, but with USB REPL this is not a problem.
Thanks to @HermannSW for spotting and providing the change.
Using the standard machine.freq().
The safe ranges tested were 10 and 12-270MHz, at which USB REPL still
worked. Requested settings can be checked with the script:
pico-sdk/src/rp2_common/hardware_clocks/scripts/vcocalc.py. At frequencies
like 300MHz the script still signaled OK, but USB did not work any more.
sm.get(buf) was waiting for one item more than the length of the supplied
buffer. Even if this item was not stored, sm_get would block trying to get
an item from the RX fifo.
As part of the fix, the edge case for a zero length buffer was moved up to
the section where the function arguments are handled. In case of a zero
length buffer, sm.get() now returns immediately that buffer.
The bitmasks supplied for initialization of out/set/sideset were only 8 bit
instead of 32. This resulted in an error, that not more than 8 consecutive
pins would get initialized.
Fixes issue #6933.
This allows the user to enable wake-up sources using the EWUP bits, on F7
MCUs.
Disabling the wake-up sources while clearing the wake-up flags follows the
reference manual and ST examples.
state.reset_mode is updated by `MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP` but
not passed to `init_flash_fs`, and so factory reset is not executed on
boards that do not have a bootloader. This bug was introduced by
4c3976bbcaFixes#6903.
A corrupt filesystem may lead to a request for a block which is out of
range of the block device limits. Return an error instead of passing the
request down to the lower layer.
Two of the defaults have also changed in this commit:
- MICROPY_HW_RFCORE_BLE_LSE_SOURCE changed from 1 to 0, which configures
the LsSource to be LSE (needed due to errata 2.2.1).
- MICROPY_HW_RFCORE_BLE_VITERBI_MODE changed from 0 to 1, which enables
Viterbi mode, following all the ST examples.
Signed-off-by: Damien George <damien@micropython.org>
These ports already have uzlib enabled so this additional ubinascii.crc32
function only costs about 90 bytes of flash.
Signed-off-by: Damien George <damien@micropython.org>
Add LPUART1 as a standard UART. No low power features are supported, yet.
LPUART1 is enabled as the next available UART after the standard U(S)ARTs:
STM32WB: LPUART1 = UART(2)
STM32L0: LPUART1 = UART(6)
STM32L4: LPUART1 = UART(6)
STM32H7: LPUART1 = UART(9)
On all ports: LPUART1 = machine.UART('LP1')
LPUART1 is enabled by defining MICROPY_HW_LPUART1_TX and
MICROPY_HW_LPUART1_RX in mpconfigboard.h.
Signed-off-by: Chris Mason <c.mason@inchipdesign.com.au>
This commit fixes two issues on the esp32:
- it enables machine.soft_reset() to be called in main.py;
- it enables machine.reset_cause() to correctly identify a soft reset.
The former is useful in that it enables soft resets in applications that
are started at boot time. The support is patterned after the stm32 port.
This commit implements basic NVS support for the esp32. It follows the
pattern of the esp32.Partition class and exposes an NVS object per NVS
namespace. The initial support provided is only for signed 32-bit integers
and binary blobs. It's easy (albeit a bit tedious) to add support for
more types.
See discussions in: #4436, #4707, #6780
This enables -Os for compilation, but still keeps full assertion messages.
With IDF v4.2, -Os changes the GENERIC firmware size from 1512176 down to
1384640, and the GENERIC_SPIRAM firmware is now 1452320 which fits in the
allocated partition.
Signed-off-by: Damien George <damien@micropython.org>
So that mboot can be used to program encrypted/signed firmware to regions
of flash that are not the main application, eg that are the filesystem.
Signed-off-by: Damien George <damien@micropython.org>
The default for these is to enable them, but they can now be disabled
individually by a board configuration.
Signed-off-by: Damien George <damien@micropython.org>
If a board defines USBD_VID then that will be used instead of the default.
And then the board must also define all USBD_PID_xxx values that it needs.
Signed-off-by: Damien George <damien@micropython.org>
The following simple usocket example throws an error EINVAL on connect
import usocket
s = usocket.socket()
s.connect(usocket.getaddrinfo('www.micropython.org', 80)[0][-1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] EINVAL
Fixing the context parameter in calls of net_context_get_family() and
net_context_get_type(), the connect works fine.
Tested on a nucleo_h743zi board.
Refactors the zephyr build infrastructure to build MicroPython as a
cmake target, using the recently introduced core cmake rules.
This change makes it possible to build the zephyr port like most other
zephyr applications using west or cmake directly. It simplifies building
with extra cmake arguments, such as specifying an alternate conf file or
adding an Arduino shield. It also enables building the zephyr port
anywhere in the host file system, which will allow regressing across
multiple boards with the zephyr twister script.
Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
Disables frozen source modules in the zephyr port. They are deprecated
in the makefile rules and not implemented in the new cmake rules.
Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
The underlying OS (the ESP-IDF) uses it's own internal errno codes and so
it's simpler and cleaner to use those rather than trying to convert
everything to the values defined in py/mperrno.h.
It's now replaced by cmake/idf.py. But a convenience Makefile is still
provided with traditional targets like "all" and "deploy".
Signed-off-by: Damien George <damien@micropython.org>
This commit adds support for building the esp32 port with cmake, and in
particular it builds MicroPython as a component within the ESP-IDF. Using
cmake and the ESP-IDF build infrastructure makes it much easier to maintain
the port, especially with the various new ESP32 MCUs and their required
toolchains.
Signed-off-by: Damien George <damien@micropython.org>
This allows changing the baudrate of the UART without reinitialising it
(reinitialising can lead to spurious characters sent on the TX line).
Signed-off-by: Damien George <damien@micropython.org>
On i.MX the SysTick IRQ cannot wake the CPU from a WFI so the CPU was
blocked on WFI waiting for USB data in mp_hal_stdin_rx_chr() even though it
had already arrived (because it may arrive just after calling the check
tud_cdc_available()). This commit fixes this problem by using SEV/WFE to
indicate that there has been a USB event.
The mp_hal_stdout_tx_strn() function is also fixed so that it doesn't
overflow the USB buffers.
Signed-off-by: Damien George <damien@micropython.org>
So that all MicroPython ports that use tinyusb use the same version. Also
requires fewer submodule checkouts when building rp2 along with other ports
that use tinyusb.
Signed-off-by: Damien George <damien@micropython.org>
START_SEC was changed in e0905e85a7.
Also, update the error message to mention how to format the partition at
the REPL, and make the total message shorter to save a bit of flash.
Signed-off-by: Damien George <damien@micropython.org>
In particular the firmware can now be built in a build directory that lives
outside the source tree, and the py/modarray.c file will still be found.
See issue #6837.
Signed-off-by: Damien George <damien@micropython.org>
The "word" referred to by BYTES_PER_WORD is actually the size of mp_obj_t
which is not always the same as the size of a pointer on the target
architecture. So rename this config value to better reflect what it
measures, and also prefix it with MP_.
For uses of BYTES_PER_WORD in setting the stack limit this has been
changed to sizeof(void *), because the stack usually grows with
machine-word sized values (eg an nlr_buf_t has many machine words in it).
Signed-off-by: Damien George <damien@micropython.org>
To simplify config, there's no need to specify MP_PLAT_PRINT_STRN if it's
the same as the default definition in py/mpconfig.h.
Signed-off-by: Damien George <damien@micropython.org>
Otherwise it resets the ADC peripheral each time a new ADC object is
constructed, which can reset other state that has already been set up.
See issue #6833.
Signed-off-by: Damien George <damien@micropython.org>
Default to just calling python since that is most commonly available: the
official installer or zipfiles from python.org, anaconda, nupkg all result
in python being available but not python3. In other words: the default
used so far is wrong. Note that os.name is 'posix' when running the python
version which comes with Cygwin or MSys2 so they are not affected by this.
However of all possible ways to get Python on Windows, only Cygwin provides
no python command so update the default way for running tests in the
README.
With mboot encrpytion and fsload enabled, the DEBUG build -O0 compiler
settings result in mboot no longer fitting in the 32k sector. This commit
changes this to -Og which also brings it into line with the regular stm32
build.
MCUs with device-only USB peripherals (eg L0, WB) do not implement (at
least not in the ST HAL) the HAL_PCD_DisconnectCallback event. So if a USB
cable is disconnected the USB driver does not deinitialise itself
(usbd_cdc_deinit is not called) and the CDC driver can stay in the
USBD_CDC_CONNECT_STATE_CONNECTED state. Then if the USB was attached to
the REPL, output can become very slow waiting in usbd_cdc_tx_always for
500ms for each character.
The disconnect event is not implemented on these MCUs but the suspend event
is. And in the situation where the USB cable is disconnected the suspend
event is raised because SOF packets are no longer received.
The issue of very slow output on these MCUs is fixed in this commit (really
worked around) by adding a check in usbd_cdc_tx_always to see if the USB
device state is suspended, and, if so, breaking out of the 500ms wait loop.
This should also help all MCUs for a real USB suspend.
A proper fix for MCUs with device-only USB would be to implement or somehow
synthesise the HAL_PCD_DisconnectCallback event.
See issue #6672.
Signed-off-by: Damien George <damien@micropython.org>
PIO state machines can make a conditional jump on the state of a pin: the
`JMP PIN` command. This requires the pin to be configured with
`sm_config_set_jmp_pin`, but until now we didn't have a way of doing that
in MicroPython.
This commit adds a new `jmp_pin=None` argument to `StateMachine`. If it is
not `None` then we try to interpret it as a Pin, and pass its value to
`sm_config_set_jmp_pin`.
Signed-off-by: Tim Radvan <tim@tjvr.org>
Add "make submodules" to commands when building for the first time.
Otherwise, on a first time build, the submodules have not been checked out
and a lot of `fatal error: nrfx.h: No such file or directory` errors are
printed.
It practically does the same as qstr_from_str and was only used in one
place, which should actually use the compile-time MP_QSTR_XXX form for
consistency; qstr_from_str is for runtime strings only.
Don't clear the IPCC channel flag until we've actually handled the incoming
data, or else the wireless firmware may clobber the IPCC buffer if more
data arrives. This requires masking the IRQ until the data is handled.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit adds a new port "rp2" which targets the new Raspberry Pi RP2040
microcontroller.
The build system uses pure cmake (with a small Makefile wrapper for
convenience). The USB driver is TinyUSB, and there is a machine module
with most of the standard classes implemented. Some examples are provided
in the examples/rp2/ directory.
Work done in collaboration with Graham Sanderson.
Signed-off-by: Damien George <damien@micropython.org>
It's enabled by default to retain the existing behaviour. A board can
disable this option if it manages mounting the filesystem itself, for
example in frozen code.
Signed-off-by: Damien George <damien@micropython.org>
Changes are:
- refactor to use new _create_element function
- support extended version of MOUNT element with block size
- support STATUS element
Signed-off-by: Damien George <damien@micropython.org>
This new element takes the form: (ELEM_TYPE_STATUS, 4, <address>). If this
element is present in the mboot command then mboot will store to the given
address the result of the filesystem firmware update process. The address
can for example be an RTC backup register.
Signed-off-by: Damien George <damien@micropython.org>
Instead it is now passed in as an optional parameter to the ELEM_MOUNT
element, with a compile-time configurable default.
Signed-off-by: Damien George <damien@micropython.org>
The superblock for littlefs is in block 0 and 1, but block 0 may be erased
or partially written, so block 1 must be checked if block 0 does not have a
valid littlefs superblock in it.
Prior to this commit, if block 0 did not contain a valid littlefs
superblock (but block 1 did) then the auto-detection would fail, mounting a
FAT filesystem would also fail, and the system would reformat the flash,
even though it may have contained a valid littlefs filesystem. This is now
fixed.
Signed-off-by: Damien George <damien@micropython.org>
* Fix a typo in the Makefile that prevented the debug build to be actually
enabled when BTYPE=debug is used.
* Add a missing header in modmachine.c that is used when a debug build is
created.
This commit improves some FTP implementation details for better
compatibility with FTP clients:
* The PWD command now puts quotes around the directory name before
returning it. This fixes BBEdit’s FTP client, which performs a PWD after
each CWD and gets confused if the returned directory path is not
surrounded by quotes.
* The FEAT command is now allowed before logging in. This fixes the lftp
client, which send FEAT first and gets confused (tries to use TLS) if the
server responds with 332.
With MICROPY_FLOAT_IMPL_FLOAT the results of utime.time(), gmtime() and
localtime() change only every 129 seconds. As one consequence
tests/extmod/vfs_lfs_mtime.py will fail on a unix port with LFS support.
With this patch these functions only return floats if
MICROPY_FLOAT_IMPL_DOUBLE is used. Otherwise they return integers.
To match the definition of GENERATE_PACK_DFU, so a board can customise the
location/name of this file if needed.
Signed-off-by: Damien George <damien@micropython.org>
To have at least one board configured with MBOOT_ENABLE_PACKING, for CI
testing purposes and demonstration of the feature.
Signed-off-by: Damien George <damien@micropython.org>
This commit adds support to stm32's mboot for signe, encrypted and
compressed DFU updates. It is based on inital work done by Andrew Leech.
The feature is enabled by setting MBOOT_ENABLE_PACKING to 1 in the board's
mpconfigboard.mk file, and by providing a header file in the board folder
(usually called mboot_keys.h) with a set of signing and encryption keys
(which can be generated by mboot_pack_dfu.py). The signing and encryption
is provided by libhydrogen. Compression is provided by uzlib. Enabling
packing costs about 3k of flash.
The included mboot_pack_dfu.py script converts a .dfu file to a .pack.dfu
file which can be subsequently deployed to a board with mboot in packing
mode. This .pack.dfu file is created as follows:
- the firmware from the original .dfu is split into chunks (so the
decryption can fit in RAM)
- each chunk is compressed, encrypted, a header added, then signed
- a special final chunk is added with a signature of the entire firmware
- all chunks are concatenated to make the final .pack.dfu file
The .pack.dfu file can be deployed over USB or from the internal filesystem
on the device (if MBOOT_FSLOAD is enabled).
See #5267 and #5309 for additional discussion.
Signed-off-by: Damien George <damien@micropython.org>
Prior to this fix, the final piece of data in a compressed file may have
been lost when decompressing.
Signed-off-by: Damien George <damien@micropython.org>
Changes are:
- Remove include of stm32's adc.h because it was recently changed and is
no longer compatible with teensy (and not used anyway).
- Remove define of __disable_irq in mpconfigport.h because it was clashing
with an equivalent definition in core/mk20dx128.h.
- Add -Werror to CFLAGS, and change -std=gnu99 to -std=c99.
Signed-off-by: Damien George <damien@micropython.org>
Mboot builds do not use the external SPI flash in caching mode, and
explicitly disabling it saves RAM and a small bit of flash.
Signed-off-by: Damien George <damien@micropython.org>
This only needs to be enabled if a board uses FAT FS on external SPI flash.
When disabled (and using external SPI flash) 4k of RAM can be saved.
Signed-off-by: Damien George <damien@micropython.org>
When littlefs is enabled extended reading must be supported, and using this
function to read the first block for auto-detection is more efficient (a
smaller read) and does not require a cached SPI-flash read.
Signed-off-by: Damien George <damien@micropython.org>
These functions enable SDRAM data retention in stop mode. Example usage,
in mpconfigboard.h:
#define MICROPY_BOARD_ENTER_STOP sdram_enter_low_power();
#define MICROPY_BOARD_LEAVE_STOP sdram_leave_low_power();
Calculate the bit timing from baudrate if provided, allowing sample point
override. This makes it a lot easier to make CAN work between different
MCUs with different clocks, prescalers etc.
Tested on F4, F7 and H7 Y/V variants.
This much buffer space is required for CDC data out endpoints to avoid any
buffer overflows when the USB CDC is saturated with data.
Signed-off-by: Damien George <damien@micropython.org>
The nrf52840-mdk-usb-dongle and pca10050 comes with a pre-flashed
bootloader (OpenBootloader).
This commit updates the boards "mpconfigboard.mk" to use DFU as
default flashing method and set the corresponding BOOTLOADER
settings such that nrf52840_open_bootloader_1.2.x.ld linker
script is used.
The default DFU flashing method can be disabled by issuing "DFU=0"
when invoking make. This will lead to "segger" being used as default
flashing tool. When using "DFU=0", the linker scripts will not
compensate for any MBR and Bootloader region being present, and might
overwrite them if they were present.
The commit also removes the custom linker script specific to
nrf52840-mdk-usb-dongle as it now points to a generic.
Updated nrf52840-mdk-usb-dongle's README.md to be more clear on
how to deploy the built firmware.
The port README.md has also been updated. In the list of target
boards a new column has been added to indicate which bootloader
is present on the target board. And for consistency, changed all
examples in the README.md to use "deploy" instead of "flash".
An additional Makefile parameter NRFUTIL_PORT can be set in order
to define the serial port to used for the DFU (Default: /dev/ttyACM0).
The "nrfutil" that is used as flasher towards OpenBootloader is
available for installation through Python "pip".
In case of SD=s140, SoftDevice ID 0xB6 is passed to nrfutil's package
generation which corresponds to SoftDevice s140 v6.1.1.
Add the option for "mpconfigboard.mk" to define whether the
board hosts a bootloader or not. The BOOTLOADER make variable
must be set to the name of the bootloader.
When the BOOTLOADER name is set it is also required to supply
the BOOTLOADER_VERSION_MAJOR and the BOOTLOADER_VERSION_MINOR
from the "mpconfigboards.mk". These will be used to resolve which
bootloader linker script that should be passed to the linker.
The BOOTLOADER section also supplies the C-compiler with
BOOTLOADER_<bootloader name>=<version major><version minor>
as a compiler define. This is for future use in case a bootloader
needs to do modification to the startup files or similar (like
setting the VTOR specific to a version of a bootloader).
Adding variables that can be set from other linker scripts:
- _bootloader_head_size:
Bootloader flash offset in front of the application.
- _bootloader_tail_size:
Bootloader offset from the tail of the flash.
In case the bootloader is located at the end.
- _bootloader_head_ram_size:
Bootloader RAM usage in front of the application.
Updated calculations of application flash and RAM.
Two issues are tackled:
1. The calculation of the correct length to print is fixed to treat the
precision as a maximum length instead as the exact length.
This is done for both qstr (%q) and for regular str (%s).
2. Fix the incorrect use of mp_printf("%.*s") to mp_print_strn().
Because of the fix of above issue, some testcases that would print
an embedded null-byte (^@ in test-output) would now fail.
The bug here is that "%s" was used to print null-bytes. Instead,
mp_print_strn is used to make sure all bytes are outputted and the
exact length is respected.
Test-cases are added for both %s and %q with a combination of precision
and padding specifiers.