pioasm: .clock_div emits wrong fractional divider #3189
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk#3189
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The
.clock_divdirective in pioasm computes the fractional part of the divider incorrectly for most inputs:github.com/raspberrypi/pico-sdk@079c6f3902/tools/pioasm/pio_assembler.cpp (L72)At this point, the value of
clock_div_fracis zero. Instead, it should subtractclock_div_int. The result is the whole divider multiplied by 256. This can be demonstrated by buildingtools/pioasm/test/amethyst.pio, which includes this.clock_divstatement:github.com/raspberrypi/pico-sdk@079c6f3902/tools/pioasm/test/amethyst.pio (L11).clock_div 3.6has fractional part 0.6, which is 153/256. Therefore, we expectsm_config_set_clkdiv_int_frac(&c, 3, 153);, but instead we getsm_config_set_clkdiv_int_frac(&c, 3, 921);and a warning from GCC:Because function
sm_config_set_clkdiv_int_frac()takes as its third argument an 8-bit unsigned integer, the overflow logic happens to work out fine in the end. However, the result is needless tedium:-Werrortreats this as a hard build error..clock_div 1yields (1, 256), which the compiler folds to (1, 0). This is correct, but it needlessly emits aclkdivcall for the default divider that a correct pioasm would omit.-o json, pioasm emits"clockDiv": {"int": 3, "frac": 921}. There is no 8-bit conversion downstream, so any non-C consumer of that format gets a wrong divider. This is the one case where the bug is not merely cosmetic.