Skip to content

Prevent wrong compiler optimization; unify pin-names - #11884

Merged
dhalbert merged 3 commits into
adafruit:mainfrom
bablokb:inky_tweaks
Nov 17, 2026
Merged

dhalbert merged 3 commits into
adafruit:mainfrom
bablokb:inky_tweaks

Conversation

@bablokb

@bablokb bablokb commented Nov 16, 2026

Copy link
Copy Markdown

This PR changes board.c for the Pimoroni Inky-Frame 5.7 and 7.3:

  • add volatile to the loop in pre_init(): this does not seem necessary for CP11, but when I backported the code to CP10/CP9 it turned out to be necessary. Probably CP10/CP9 use different compiler optimizations. Since nobody knows what future compilers do, I am adding this for CP11 too.
  • Unify pin-names for 7.3: this switches to 5.7 names (the names in the official schematic). Although this seems like a breaking change, it probably does not hurt anybody. The pin-names for the shift-register (and therefore also the SW_xxx constants) were defined but not useable before my last PR anyhow.

gpio_put_masked(SR_PIN_MASK, SR_PIN_VALUE);
uint8_t state = 0u;
for (uint8_t i = 0u; i < 8; i++) {
for (volatile uint8_t i = 0u; i < 8; i++) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess about this is not that it needs to be volatile, but that the pin toggling is happening too fast. A short delay would accomplish the same thing and would make the intent clearer.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also thinking about adding delays, but this code is from Pimoroni and they don't have any delays here either. Interestingly they do have an additional noop in their shift-register MicroPython module.

Anyway, I will give it a try although it seems a bit hacky given the fact that the exact timings of the loop are not clear.

@JonNelson JonNelson Nov 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts: volatile is not the right expression of intent here. It is true that a common (but not guaranteed) side effect of volatile is that the performance of accessing the value of i will be negatively impacted because volatile instructs the compiler that it is necessary to fetch the value of i each time it is accessed because the value of i may outside of the compiler's control (such as an interrupt). That's not what's happening here.

If the problem is because the pin states changing "too quickly", an explicit delay is probably the best awful way to address the problem. An explicit delay is also a clear expression of intent. NOTE: A comment as to why there is a delay would be beneficial. Furthermore, a delay is generally a fixed period of time. Using some other mechanism with performance-related impact (e.g., volatile) may work today but won't necessarily work tomorrow. Finally, a delay here is more or less meaningless in the overall context of things; this is a routine that is executed once upon startup and a few additional microseconds is unlikely to be meaningful.

In summary:

  1. Use a delay if that's what is intended. Don't rely on side-effects of other approaches.
  2. Ensure there is a comment stating why there is a delay
  3. A delay is not harmful (except to startup performance, and minimally so in this case)

Request: Can you link the code referenced here:

Interestingly they do have an additional noop in their shift-register MicroPython module.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to add: volatile usually prevents unrolling of the loop which just might slow down the pin-state changes enough to make it work with the older compilers. I don't think they principally create faster code but the compiler settings have changed since then (e.g. -O2 instead of -O3) and that could have enough impact.

All of this is speculation until it is tested.

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, thanks!

@dhalbert
dhalbert merged commit ad9f856 into adafruit:main Nov 17, 2026
17 checks passed
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