Fix invalid hardcoded page-end address in display() - #305
Open
94xhn wants to merge 2 commits into
Open
Conversation
display() sent 0xFF as the SSD1306 "Set Page Address" (0x22) end-page byte. That field is only 3 bits wide per the datasheet (valid range 0-7), so 0xFF is silently truncated to 7 in hardware -- correct for a 64-row (8-page) display, but wrong for any shorter display (32, 48, or 16 rows), where the real last GDDRAM page is HEIGHT/8 - 1. In Horizontal Addressing Mode this went unnoticed because display() always writes exactly WIDTH * pages bytes, so the page-end wraparound byte is never actually reached. In Vertical Addressing Mode the page-end value is consulted after every byte, so the wrong value causes large parts of the framebuffer to be written to the wrong columns, or never written at all, as reported in adafruit#282. Compute the real last page from HEIGHT instead of hardcoding 0xFF. Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>
Match the repo's clang-format style for the shortened dlist1 initializer introduced in the previous commit (fixes the failing "build" CI check's clang-format step).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #282.
The bug
Adafruit_SSD1306::display()hardcodes the "Page end address" byte of theSSD1306's
Set Page Addresscommand (0x22) to0xFF:Per the SSD1306 datasheet, that field (
B[2:0]) is only 3 bits wide,valid range 0-7 — the controller silently truncates whatever is written
there to its low 3 bits.
0xFF & 0x07 == 7, which happens to be thecorrect last-page value for a 64-row display (8 pages), but is wrong
for any shorter display: 32-row (4 pages), 48-row (6 pages), or 16-row (2
pages) modules, where the real last page is
HEIGHT/8 - 1.invisible:
display()always writes exactlyWIDTH * pagesbytes, andthe controller's column address wraps before the page-end wraparound
logic is ever exercised, so the wrong register value never actually gets
consulted during a normal full-buffer refresh.
every single byte (page increments first; only after wrapping through
the full page range does the column advance). An incorrect page-end value
there corrupts the entire transfer, not just the tail end — exactly what
was reported in A source code bug creates issues when operating in Vertical memory access mode. #282.
The fix
Compute the real last page from
HEIGHTinstead of hardcoding0xFF:This mirrors the existing pattern already used elsewhere in
begin()(
ssd1306_command1(HEIGHT - 1)forSSD1306_SETMULTIPLEX). For a 64-rowdisplay this evaluates to
7, byte-for-byte identical to the previous0xFF(truncated), so there is no behavior change for the most common128x64 module.
Independent verification (no OLED hardware required)
I wrote a standalone, host-only C++ program that implements the SSD1306's
documented GDDRAM address-pointer state machine for Vertical Addressing
Mode (page increments first; on reaching page-end it wraps to page-start
and the column increments — straight from the datasheet's description of
Memory Addressing Mode
01b), and feeds it the exact byte count thatdisplay()really transmits (WIDTH * pagesbytes), once with the oldbuggy page-end value and once with the fixed one, across the module sizes
this library ships examples for (128x64, 128x32, 96x16, 64x48). It requires
no Arduino headers, no I2C/SPI, and no physical display — only the
address-generation rule the SSD1306 datasheet itself specifies, which is a
hardware fact independent of this driver's implementation.
Output:
With the old
0xFFvalue, a full-bufferdisplay()refresh in VerticalAddressing Mode on a 128x32 module leaves half the columns completely
untouched (never written at all, so they retain whatever stale content
was already in GDDRAM); on a 96x16 module it's 75% of columns. With the
fix, every configuration gets full, exact, one-write-per-cell coverage
matching the number of bytes actually transferred, and the common 128x64
case is completely unaffected.
Repro source (self-contained, single file, standard library only):
Compatibility
No functional change for the 128x64 module (by far the most common), which
is why this has likely gone unnoticed in the wild for a while — the bug
only manifests on shorter modules (128x32, 96x16, 64x48, etc.) when the
application explicitly switches to Vertical Addressing Mode via
ssd1306_command(SSD1306_MEMORYMODE); ssd1306_command(0x01);, which isn'tthe library's default.
Disclosure
I used Claude (Anthropic) to help investigate this issue, work out the
correct fix, and write the independent host-side verification program
described above. I reviewed the diagnosis, the fix, and the repro program
myself before opening this PR, and I stand behind the technical claims
made here.