Fix error in watchdog.h #2497
No reviewers
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 assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk!2497
Loading…
Reference in a new issue
No description provided.
Delete branch "patch-1"
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?
Fixes #2496
As said in the issue, the documentation for
watchdog_get_time_remaining_mssays it returns the time in microseconds, while this should be milliseconds. Function its self is fine, just a small docs error.I wonder whether it's actually returning microseconds. I don't see any division in watchdog_get_time_remaining_ms
Hmm, but then the function name should be
watchdog_get_time_remaining_usinstead ofwatchdog_get_time_remaining_ms.I see the description on the RP2040 is also different:
I have to say I'm not very familiar with the internals of the Pico SDK.
I had a look at this last night - I thought it might be a duplicate of an old issue, but I found #715 and #1567 which are similar but not actually the same as the issue here. I guess it shows just how easy it is to mix up milliseconds and microseconds!
It does indeed look like it's the comment-description that is correct (i.e. the function is returning microseconds). Looking at the current implementation of
watchdog_get_time_remaining_msshows that it's just returning the WATCHDOG'sCTRL.TIMEfield, and as Peter shows above (and you can double-check in section 4.7.6 of RP2040 datasheet and section 12.9.7 of RP2350 datasheet) this is just the raw "ticks remaining" value (with each "tick" being 1 microsecond (us) ).If you compare this to the implementation of
_watchdog_enablewhich does correctly accept a value in milliseconds (ms) you can see that it multiplies the value in milliseconds by 1000, to get the value in microseconds, before updating the watchdog'sLOADregister (which is also in the unit of "ticks" i.e. microseconds).On top of all that, there's also errata RP2040-E1 which means that RP2040 decrements the wachdog twice per tick.
_watchdog_enablealready takes this into account, butwatchdog_get_time_remaining_msdoesn't take RP2040-E1 into account.So, putting all of that together, I think that as well as changing the "microseconds" in the comment to "milliseconds" in the
.hfile, you also ought to change the implementation ofwatchdog_get_time_remaining_msin the.cfile to do(which means you'll need to move the function below the definition of
WATCHDOG_XFACTOR).@kilograham Perhaps it also makes sense to update
_watchdog_enablefor consistency, and replacewith
?
For backwards-compatibility reasons, I guess we have to hope that nobody is relying on the current implementation of
watchdog_get_time_remaining_msincorrectly returning microseconds instead of milliseconds? 🤔 But IMHO given that thewatchdog_enablevalue is set in milliseconds, it makes more sense for the "get_time_remaining" value to also be in milliseconds.Ooof, I've just noticed that
watchdog.halso doesso that probably explains why this function is returning milliseconds instead of microseconds. If we change
watchdog_get_time_remaining_msto actually return milliseconds instead of microseconds, we'll need to add a separate implementation ofwatchdog_get_count, just like we had pre-SDK2.0.0 (here's the implementation that was in SDK1.5.1). There's also another small bug here as in SDK1.5.1watchdog_get_countwas returningCTRL.TIME / 2but in SDK2.0.0 (which is when RP2350 support was added and thewatchdog_get_time_remaining_msfunction was added) it's returningCTRL.TIMEwithout division, even on RP2040.TL;DR - I'm suggesting that perhaps this PR ought to do:
But it's obviously up to @kilograham to decide if this is too much of a backwards-incompatible change.
Alternatively we could just decide that the function is misnamed, and do something like this instead:
@lurch can you make a PR for the former - I think we should add the
_usvariant alsoSee #2533
superseded by #2533 - thanks for your PR though
Pull request closed