Repository navigation
Bus_SPI (ESP32): read the SPI data registers with 32-bit loads only - #415
Merged
Merged
Conversation
The W0..W15 data registers only take 32-bit accesses, but readBytes(), readPixels() and lgfx::spi::readBytes() still read them with memcpy(), and lgfx::spi::writeBytes() still wrote them with memcpy(). The access width and alignment memcpy() uses depend on the libc build: - On ESP32-S3 an 8- or 16-bit load from the registers reads 0. readBytes() into a misaligned buffer returned zeros for some bytes, and lgfx::spi::readBytes() returned zeros at the end of a read whose length is not a multiple of 4. - On ESP32-P4 and ESP32-C5 (Arduino builds), memcpy() to a misaligned destination uses a misaligned load from the registers, which faults (Load address misaligned). Chips with GDMA always take the CPU path in readBytes(), so reading more than 32 bytes into a misaligned buffer crashed. The registers are now read through helpers that use 32-bit loads only; a misaligned destination goes through an aligned buffer. The write helpers move from Bus_SPI.inl to common.inl so that lgfx::spi:: uses them as well. lgfx::spi::writeBytes() now reads only the source words that hold the bytes to send, instead of up to 3 bytes past the end; the data sent is unchanged. Checked on ESP32, ESP32-S3, C3, C6, H2, P4, C5 and C61: reads match for destination offsets 0-3 and lengths 1-64, and readBytes() into a misaligned buffer no longer faults on ESP32-C5.
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.
The SPI data registers (W0..W15) only take 32-bit accesses. #409 moved the writes in Bus_SPI to 32-bit stores, but the reads in
readBytes()/readPixels()and bothlgfx::spi::writeBytes()/lgfx::spi::readBytes()still usedmemcpy().What goes wrong
readBytes()into a misaligned buffer returns zeros for some bytes, andlgfx::spi::readBytes()returns zeros at the end of a read whose length is not a multiple of 4.memcpy()to a misaligned destination uses a misaligned load from the registers and faults (Load address misaligned). Chips with GDMA always take the CPU path inreadBytes(), so reading more than 32 bytes into a misaligned buffer crashes.Change
wreg_read_aligned/wreg_read_bytes); a misaligned destination goes through an aligned 64-byte buffer.Bus_SPI.inltocommon.inlso thatlgfx::spi::uses them too.lgfx::spi::writeBytes()now reads only the source words that hold the bytes to send (before: up to 3 bytes past the end of the source). The data sent is unchanged.Tested
readBytes()(no DMA) into buffers at offsets 0-3, lengths 1-60, on ESP32 / S3 / C5 / C6: no fault, no write outside the buffer (before: ESP32-C5 faulted at offsets 1-3).memcpy().