Skip to content

Fix three core/library bugs affecting all AVR - #650

Open
ws-asahi wants to merge 3 commits into
SpenceKonde:masterfrom
ws-asahi:fix/core-lib-bugs
Open

Fix three core/library bugs affecting all AVR#650
ws-asahi wants to merge 3 commits into
SpenceKonde:masterfrom
ws-asahi:fix/core-lib-bugs

Conversation

@ws-asahi

@ws-asahi ws-asahi commented Aug 8, 2026

Copy link
Copy Markdown

Three small, unrelated bug fixes.
None are part-specific - they affectcurrent master as-is.

1. wiring_analog.c
if (!ADC0.CTRLA & 0x01) evaluates as (!ADC0.CTRLA) & 0x01, so the "ADC disabled" early return never fired.
Parenthesized (both occurrences).

2. wiring_extra.cpp
(chan + 1) < 3 promotes to int,
so the documented auto-select value chan = 255 became 256 < 3 and could never take this path.
Compare explicitly.

3. Flash.cpp / writeBytes
on unaligned/odd-length requests the leading byte was written without advancing the data pointer (shifting the rest by one),
state wasn't advanced after writeWords, and the trailing byte landed on the wrong address.
Rewritten to advance state explicitly at each stage; also returns FLASHWRITE_0LENGTH on zero length.

Verified to compile for AVR128DA48.
(core changes via a standard sketch; Flash via FlashDemo and a minimal writeBytes sketch, with "Write to flash from app" enabled)

These were found while working on AVR-DU support (#637).
with these fixes applied, everything runs fine on an AVR64DU32 on CuriosityNano.

`!ADC0.CTRLA & 0x01` evaluates as `(!ADC0.CTRLA) & 0x01`, so the
early return for a disabled ADC never fired as intended. Parenthesize
the mask. The same expression appears twice (analogRead paths for the
two ADC generations); the other checks in this file already had the
parentheses right.
`(chan + 1) < 3` promotes chan to int, so the documented auto-select
value chan = 255 evaluated as 256 < 3 and could never take this path.
Compare explicitly instead.
…ost data pointer

Three bugs in the unaligned/odd-length paths:
- the unaligned leading byte was written without advancing the data
  pointer, so the rest of the buffer was written shifted by one
- writeWords was called without afterwards advancing tAddress/data/
  length, so the odd-byte epilogue used stale values
- the trailing byte was written to tAddress + length - 2 with a
  recomputed data offset, landing on the wrong address and corrupting
  the neighbouring byte while dropping the intended one

Rewrite the function to advance state explicitly at each stage. Also
return early on a zero-length request (FLASHWRITE_0LENGTH).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant