The STATIC macro was introduced a very long time ago in commit
d5df6cd44a. The original reason for this was
to have the option to define it to nothing so that all static functions
become global functions and therefore visible to certain debug tools, so
one could do function size comparison and other things.
This STATIC feature is rarely (if ever) used. And with the use of LTO and
heavy inline optimisation, analysing the size of individual functions when
they are not static is not a good representation of the size of code when
fully optimised.
So the macro does not have much use and it's simpler to just remove it.
Then you know exactly what it's doing. For example, newcomers don't have
to learn what the STATIC macro is and why it exists. Reading the code is
also less "loud" with a lowercase static.
One other minor point in favour of removing it, is that it stops bugs with
`STATIC inline`, which should always be `static inline`.
Methodology for this commit was:
1) git ls-files | egrep '\.[ch]$' | \
xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/"
2) Do some manual cleanup in the diff by searching for the word STATIC in
comments and changing those back.
3) "git-grep STATIC docs/", manually fixed those cases.
4) "rg -t python STATIC", manually fixed codegen lines that used STATIC.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
This commit changes all uses in the rp2 port, and drivers that are
optionally supported by that port.
The old MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST macros are
no longer used for rp2 builds and are removed (C user code will need to be
changed to suit).
Also take the opportunity to change some timeouts that used 64-bit
arithmetic to 32-bit, to hopefully claw back a little code size.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
best_effort_wfe_or_timeout() already calls time_reached() and returns the
result of it, so no need to call it again.
Signed-off-by: Damien George <damien@micropython.org>
The recent change in bcbdee2357 means that
TinyUSB can no longer be run from within a soft (or hard) IRQ handler, ie
when the scheduler is locked. That means that Python code that calls
`print(...)` from within a scheduled function may block indefinitely if the
USB CDC buffers are full.
This commit fixes that problem by explicitly running the TinyUSB stack when
waiting within stdio tx/rx functions.
Signed-off-by: Damien George <damien@micropython.org>
Currently on rp2 the time.time_ns() function has only seconds resolution.
This commit makes it have microsecond resolution, by using the output of
time_us_64() instead of the RTC.
Tested that it does not drift from the RTC over long periods of time.
Signed-off-by: Damien George <damien.p.george@gmail.com>
Building the Pico-W needs the MICROPY_PY_NETWORK_CYW43 flag to be set in
order to include building the CYW43 Wifi driver. But then mp_hal_get_mac()
handles the MAC assignment for all nics the "CYW43 way", copying the real
MAC provided by the WiFi hardware. This will fail for all other NIC types,
resulting in an invalid MAC address.
The solution in this commit is to add a check for the NIC type parameter
idx and handle the MAC address respectively.
This is a best-effort implementation of write polling. It's difficult to
do correctly because if there are multiple output streams (eg UART and USB
CDC) then some may not be writeable while others are. A full solution
should also have a return value from mp_hal_stdout_tx_strn(), returning the
number of bytes written to the stream(s). That's also hard to define.
The renesas-ra and stm32 ports already implement a similar best-effort
mechanism for write polling.
Fixes issue #11026.
Signed-off-by: Damien George <damien@micropython.org>
If USB CDC is connected and the board sends data, but the host does not
receive the data, the device locks up. This is fixed in this commit by
having a timeout of 500ms, after which time the transmission is skipped.
Fixes issue #9634.
This includes:
- Configuration file for the cyw43-driver.
- Integration of cyw43-driver into the build, using lwIP.
- Enhancements to machine.Pin to support extension IO pins provided by the
CYW43xx.
- More mp-hal pin helper functions.
- mp_hal_get_mac_ascii MAC address helper function.
- Addition of rp2.country() function to set the country code.
A board can enable this driver by setting MICROPY_PY_NETWORK_CYW43 in their
cmake snippet.
Work done in collaboration with Graham Sanderson and Peter Harper.
Signed-off-by: Damien George <damien@micropython.org>
Prior to this commit, the USB CDC OUT endpoint got NACK'd if a character
was received but not consumed by the application, e.g. via
sys.stdin.read(). This meant that USB CDC was blocked and no additional
characters could be sent from the host. In particular a ctrl-C could not
interrupt the application if another character was pending.
To fix the issue, the approach in this commit uses a callback tud_cdc_rx_cb
which is called by the TinyUSB stack on reception of new CDC data. By
consuming the data immediately, the endpoint does not stall anymore. The
previous handler tud_cdc_rx_wanted_cb was made obsolete and removed.
In addition some cleanup was done along the way: by adding interrupt_char.c
and removing the existing code mp_hal_set_interrupt_char(). Also, there is
now only one (stdin) ringbuffer.
Fixes issue #7996.
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>
This is a more logical place to clear the KeyboardInterrupt traceback,
right before it is set as a pending exception. The clearing is also
optimised from a function call to a simple store of NULL.
This function is tightly coupled to the state and behaviour of the
scheduler, and is a core part of the runtime: to schedule a pending
exception. So move it there.
Otherwise mp_interrupt_char will have a value of zero on start up (because
it's in the BSS) and a KeyboardInterrupt may be raised during start up.
For example this can occur if there is a UART attached to the REPL which
sends spurious null bytes when the device turns on.
Several ports use identical code for the 1-argument form of the builtin
help function. Move this code to a library function to allow easier
re-use by ports.
Move definition of mp_builtin_open_obj to a separate module, then file.c
becomes more or less compartmentalized FatFs file class, which can be used
together with file class implementations for other (V)FSes.
Blanket wide to all .c and .h files. Some files originating from ST are
difficult to deal with (license wise) so it was left out of those.
Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.