Repository navigation
Prevent wrong compiler optimization; unify pin-names - #11884
Conversation
| 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++) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Use a delay if that's what is intended. Don't rely on side-effects of other approaches.
- Ensure there is a comment stating why there is a delay
- 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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
This looks good, thanks!
This PR changes
board.cfor the Pimoroni Inky-Frame 5.7 and 7.3:volatileto the loop inpre_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.SW_xxxconstants) were defined but not useable before my last PR anyhow.