Skip to content

{bp-19415} drivers/spi/ice40: fix operator precedence in final clock cycle count - #19613

Merged
xiaoxiang781216 merged 1 commit into
apache:releases/13.0from
jerpelea:bp-19415
Aug 3, 2026
Merged

{bp-19415} drivers/spi/ice40: fix operator precedence in final clock cycle count#19613
xiaoxiang781216 merged 1 commit into
apache:releases/13.0from
jerpelea:bp-19415

Conversation

@jerpelea

@jerpelea jerpelea commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

ice40_endwrite() computes how many dummy SPI bytes to clock out after the bitstream to finish FPGA configuration with:

for (size_t i = 0; i < ICE40_SPI_FINAL_CLK_CYCLES + 7 / 8; i++)

/ binds tighter than + in C, so this parses as ICE40_SPI_FINAL_CLK_CYCLES + (7 / 8) = 160 + 0 = 160, i.e. the "+ 7 / 8" is a silent no-op. The macro name and the classic (n + 7) / 8 ceiling-division idiom (used elsewhere in embedded code to convert a bit/cycle count into a byte count) make clear the intent was to send ceil(ICE40_SPI_FINAL_CLK_CYCLES / 8) = 20 bytes (160 SPI clock cycles, matching the macro name). Instead the unmodified code sends 160 bytes, i.e. 1280 clock cycles - 8x more than intended.

Fix by parenthesizing the ceiling-division: (ICE40_SPI_FINAL_CLK_CYCLES

    1. / 8, which evaluates to 20, restoring the intended 160-clock-cycle finalization sequence.

Fixes #19367

Impact

RELEASE

Testing

CI

ice40_endwrite() computes how many dummy SPI bytes to clock out after
the bitstream to finish FPGA configuration with:

    for (size_t i = 0; i < ICE40_SPI_FINAL_CLK_CYCLES + 7 / 8; i++)

`/` binds tighter than `+` in C, so this parses as
ICE40_SPI_FINAL_CLK_CYCLES + (7 / 8) = 160 + 0 = 160, i.e. the "+ 7 / 8"
is a silent no-op. The macro name and the classic `(n + 7) / 8`
ceiling-division idiom (used elsewhere in embedded code to convert a
bit/cycle count into a byte count) make clear the intent was to send
ceil(ICE40_SPI_FINAL_CLK_CYCLES / 8) = 20 bytes (160 SPI clock cycles,
matching the macro name). Instead the unmodified code sends 160 bytes,
i.e. 1280 clock cycles - 8x more than intended.

Fix by parenthesizing the ceiling-division: (ICE40_SPI_FINAL_CLK_CYCLES
+ 7) / 8, which evaluates to 20, restoring the intended 160-clock-cycle
finalization sequence.

Fixes apache#19367

Assisted-by: Claude Code:claude-sonnet-5
Signed-off-by: yi chen <94xhn1@gmail.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: XS The size of the change in this PR is very small labels Aug 3, 2026
@xiaoxiang781216
xiaoxiang781216 merged commit b7c3b39 into apache:releases/13.0 Aug 3, 2026
16 of 41 checks passed
@jerpelea
jerpelea deleted the bp-19415 branch August 3, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants