Skip to content

Remove unnecessary delays from SSD1306 SPI transfer code #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025

Conversation

mateusznowakdev
Copy link
Contributor

Hello,

for my recent project I'm using a small SSD1306 display, 128x32, over SPI.

I've noticed that the SPI communication code - the tx function - has 1ms delay for each SPI transfer, which is quite a lot even if considered a "safety margin". There's 7ms of total delay introduced each time new screen contents are sent (set column address, set page address, then send buffer data). Actually, on lower performance microcontrollers this can be as much as 10ms, as tested with custom SAMD21 board. This makes it impossible to create an application that tries to target 60fps - games, smooth UI, etc.

This behaviour was introduced with the very first version of SSD1306 driver. I guess this went unnoticed because most of the time 128x64 displays are used, and registers are not updated in that case. The delay of 1.5ms was simply less of a problem than 10ms.

As an example, MicroPython SSD1306 library does not have any delay before each transaction. TinyGo SH1106 driver has 1µs (not 1ms), but based on comments even such tiny delay may be unnecessary.

I also fail to see why CS pin needs to be pulled high before DC pin is changed, but since other implementations have this logic let's keep it as is.

@deadprogram
Copy link
Member

Hello @mateusznowakdev finally got a chance to look at/test this PR. A most noticeable improvement! I think we (I?) probably copied those delays without considering if they were actually needed.

Thank you very much for this PR, now merging.

@deadprogram deadprogram merged commit d41bc0b into tinygo-org:dev May 20, 2025
1 check passed
@aykevl
Copy link
Member

aykevl commented May 21, 2025

The CS pin is necessary when using multiple SPI devices on the same bus. If you only use one, it is (for most SPI devices) not necessary.

We can make that pin optional, if needed. Toggling GPIO pins is extremely fast, however.

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.

3 participants