On top of #873, tested on a XIAO nRF52840 with SCL tied to GND:
- the STOP/SUSPEND waits broke out without setting timed_out, so an empty
probe on a stuck bus returned 0, "device ACKed" (every RTC address of an
autodiscovery scan looked present); requestFrom() alike;
- after a timeout the TWIM stayed wedged once the bus was released: every
call timed out until a reboot. A disable/enable cycle recovers it.
Result: stuck bus -> error 4 after ~100 ms per call, no hang; bus released
-> NACK 2 / ACK 0 again in under 1 ms, without a reboot.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
endTransmission() and requestFrom() both spin on unbounded while(!EVENT)
loops waiting on the TWIM peripheral. Several of these loops - notably
the EVENTS_STOPPED wait - have no EVENTS_ERROR escape at all, so if the
hardware never raises the expected event (e.g. probing an I2C address
on a bus with nothing attached and no pull-ups), the call never
returns and the whole single-threaded firmware hangs forever.
Reported in #771 with the same root cause already identified in the
issue thread. Also affects boards that probe for an optional/absent
I2C device at boot before anything else initializes - e.g. MeshCore's
Xiao_nrf52 target, which runs an RTC autodiscovery scan in radio_init()
and hangs on every cold boot when no RTC is wired up (no serial, no
BLE - see meshcore-dev/MeshCore#2068).
Adds a bounded I2C_TWIM_TIMEOUT_MS (default 100ms, overridable) to
every wait loop in both functions. A timeout that occurs before the
hardware transaction actually completed is surfaced as a proper error
return (endTransmission: 4 "other error"; requestFrom: 0 bytes read)
rather than silently falling through to a success path - a timeout on
the address/data phase must not be reported as if a device ACKed.
Tested on a Seeed XIAO nRF52840 + Wio-SX1262 board that reliably hung
at boot (via MeshCore's RTC autodiscovery probe) after every power
cycle. With this patch the same probe now completes (with a timeout)
in well under a second per address instead of hanging indefinitely,
and the board boots and runs normally.
xPortSysTickHandler catches up with diff = (RTC - tick count) & 0xFFFFFF.
With the tick count even one tick ahead, diff wrapped to ~2^24 and the loop
added 16,777,215 ticks: millis() jumped 16384 s (4.55 h). Seen on MeshCore
repeaters (nRF52840 + BLE) as uptime, airtime and the clock jumping ahead,
several times within minutes. A difference above half the counter range is
now taken as 'ahead' and skipped; the RTC catches up in a tick or two.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fix semaphore leak bug that causes device lockup when BLE central disconnects unexpectedly (e.g., going out of range, supervision timeout).
Co-authored-by: liquidraver <504870+liquidraver@users.noreply.github.com>
Co-authored-by: liamcottle <liam@liamcottle.com>
Arduino IDE v2 invokes tools in an environment where `LANG=""` and `LC_ALL=`
are not set to a specific encoding, as described in:
https://github.com/arduino/arduino-ide/issues/1513#issuecomment-1293383639
The bundled adafruit-nrfutil binary is a PyInstaller-frozen app using Click,
which expects the encoding to be set to UTF-8, and throws the following error
on macOS:
Traceback (most recent call last):
File "__main__.py", line 317, in <module>
File "click/core.py", line 1134, in __call__
File "click/core.py", line 1040, in main
File "click/_unicodefun.py", line 100, in _verify_python_env
RuntimeError: Click will abort further execution because Python was
configured to use ASCII as encoding for the environment.
Consult https://click.palletsprojects.com/unicode-support/ for
mitigation steps.
This system supports the C.UTF-8 locale which is recommended.
You might be able to resolve your issue by exporting the following
environment variables:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
So, prepends `env LANG=C.UTF-8 LC_ALL=C.UTF-8` to each invokation in macOS,
via a `.macosx` override, to force a valid locale.
To avoid duplication, the tool arguments have been moved into a `.args`
variable that is included in the normal call and the `.macosx` override.
File names are returned unterminated in the edge case where their length
is max size This is due to the fact that strncpy does terminate the
destination string when the source string is greater than or equal to
the specifid maximum length.
Clearing `_file` and `_dir` pointers after freeing on failure to open
just in case close() is later called.
To elaborate, the `_file` and `_dir` pointers are normally freed and
cleared by _close(), but when a failure occurs upon opening, these
pointers were being left orphaned since calling close() after failure to
open doesn't make much sense, and indeed caused serious issues within
lfs.
Allow reading the VDDHDIV5 ADC source on devices that support it, such as nRF52840. For designs where the VDDH pin is connected to a battery, this is a great way to measure battery voltage without any additional external components.