cpu/rp2350_common: add periph_timer driver for TIMER0#22270
Conversation
|
@Teufelchen1 Done, sorry about the friction. Both your comment and @AnnsAnns's landed while all three PRs were still in draft; I'd marked them ready for review at almost the same time without realising the comments had come in. The descriptions on all three PRs are now rewritten using Explicit AI declaration: this was done with Claude Code (Anthropic, claude-opus-4-7) in agent mode with my review on every commit. The driver was hardware-verified on a Pi Pico 2 H by running Happy to take any further changes. |
| } | ||
|
|
||
| /* RP2350 TIMER0 has four independent IRQ lines (one per alarm channel) */ | ||
| void TIMER_0_ISRA(void) { _isr(0, 0); } |
There was a problem hiding this comment.
| void TIMER_0_ISRA(void) { _isr(0, 0); } | |
| void TIMER_0_ISRA(void) | |
| { | |
| _isr(0, 0); | |
| } |
We usually try to avoid single line ifs, functions, etc, see our Coding Convention: https://github.com/RIOT-OS/RIOT/blob/master/CODING_CONVENTIONS.md#indentation-and-braces
Same for the remaining three functions of course.
There was a problem hiding this comment.
Also, the _isr(0, 0) looks magic-numbery, as in: it is not clear what the 0 and 0,1,2,3 stand for.
Are there macros that could be used or what do the numbers stand for?
There was a problem hiding this comment.
The four ISRs are now multi-line per the coding convention. On the magic numbers: I looked for an existing RIOT pattern and the closest precedent is the rpx0xx (RP2040) timer driver, which uses the same _isr(0, 0) literal indices with a /* timer 0 IRQ0 */ comment per ISR. I'm matching that pattern here for consistency, with an added block comment above the ISRs explaining what the two integers stand for: first arg is the tim_t device index (always 0 here since only TIMER0 is exposed, TIMER_NUMOF == 1); second arg is the alarm channel index 0..3, mapping 1:1 onto the hardware's ALARM0..ALARM3 registers. These are runtime function-call indices, not register bit positions; the picosdk TIMER_INTE_ALARM_0_BITS (= 1 << 0) macros are bit masks for register writes and a different concept. I had introduced TIMER_DEV_0 / ALARM_CH_0..3 defines in an earlier revision but reverted them since they had no precedent in any existing RIOT timer driver. If you'd prefer the defines anyway, happy to add them back.
774bc76 to
3dd586f
Compare
Implement the periph_timer driver for the RP2350 BSP using TIMER0 (64-bit microsecond counter, ALARM0..3 compare channels). Each alarm has its own NVIC IRQ line, simplifying the ISR routing relative to the RP2040 (where alarms share one IRQ). Alarms compare against the low 32 bits of the counter, giving a ~71-minute wraparound at 1 MHz - the same trade-off as the rpx0xx implementation. The driver supports periodic alarms with TIM_FLAG_RESET_ON_MATCH / RESET_ON_SET / SET_STOPPED. The 1 us tick is provided by the RP2350 TICKS block, set up at boot by the ROM loader; no extra clock configuration needed. Enables tests/periph/uart, tests/periph/timer, and the ztimer_msec backend chain to build for BOARD=rpi-pico-2-arm. Build-verified with tests/periph/timer for rpi-pico-2-arm.
3dd586f to
087d55b
Compare
Revert the TIMER_DEV_0 / ALARM_CH_0..3 defines and use literal indices in the per-channel ISR thunks instead, matching the existing rpx0xx (RP2040) timer driver pattern. The explanatory comment block above the ISRs is kept so the meaning of the two integer args is documented.
|
Hello, once again thank you for being wilful to contribute to RIOT, we do appreciate it. Due to various reasons, such as the currently unresolved legal issues around AI code combined with the very high degree of AI assistance, among other things, we have decided that we can not accept this PR as of now. This is not meant to stop you from contributing to RIOT. For example, if you want to PR code written by yourself, we are open to review and help you. |
Contribution description
Adds the
periph_timerdriver for the RP2350 BSP using TIMER0,plus the matching
timer_conf_ttypedef andtimer_config[]arrayin the shared rpi-pico-2 board header. With this PR,
BOARD=rpi-pico-2-armnow provides theperiph_timerfeature,which unblocks
ztimer_msecand any RIOT module / test thatdepends on it (including
tests/periph/timer,tests/periph/uart, the shell read-timeout path, and so on).Hardware notes:
(TIMER0, TIMER1), each with four ALARM compare channels
(
ALARM0..3). Unlike the RP2040 (rpx0xx BSP), each ALARM hasits own NVIC IRQ line (
TIMERx_IRQ_0..3), so the ISR routingis one-to-one, with no shared-IRQ demux needed.
tim_t = 0) with all fourchannels. A second device for TIMER1 can be added later.
giving a ~71-minute wraparound at 1 MHz.
timer_set()computesthe target as
TIMERAWL + timeoutto keep the alarm in thecorrect 32-bit window, the same idiom as
cpu/rpx0xx/periph/timer.c.the boot ROM. No additional clock plumbing is required in
timer_init().What is intentionally not included:
timer_init()returns-1for anyfreq != 1000000). Supporting other frequencieswould require reconfiguring the TICKS block, which is out of
scope here.
periph_timer_query_freqs(only one supported frequency).Files touched:
cpu/rp2350_common/periph/timer.c: the driver (~210 lines)boards/common/rpi-pico-2/include/periph_conf.h:timer_conf_t,timer_channel_conf_t,timer_config[],TIMER_0_ISRA..Dcpu/rp2350_common/Makefile.features: declaresperiph_timerTesting procedure
Build and run RIOT's stock periph_timer test on a Pi Pico 2 H:
On master this fails with
There are unsatisfied feature requirements: periph_timer. Withthis PR applied, the test builds, flashes, and prints (the
[A]prefix below is added by an external multi-board UART aggregator,
not the firmware):
All four ALARM channels fire at the expected offsets, the
out-of-range channel is correctly rejected, and no spurious IRQs
are triggered.
Before this PR is applied: on
rpi-pico-2-arm, theperiph_timerfeature is missing, so any RIOT application or testthat selects
ztimer_msec(directly or viaztimer_sec, theshell read-timeout path, network stack timers, etc.) fails at
feature-resolution. With the PR applied:
tests/periph/timerbuilds and passes (output above);tests/periph/uartbuilds (itsztimer_msecdependency issatisfied; that is the original motivation for this PR);
ztimerchain (ztimer_msec,ztimer_sec,ztimer_periodic) becomes selectable for any RP2350 application;cpu/rp2350_common/Makefile.featuresdeclaresperiph_timer, so feature-gatedUSEMODULErules across thetree resolve correctly on RP2350.
Issues/PRs references
Companion PRs from the same investigation, independent merge order:
pkg/pqm4: ML-DSA-44 (FIPS 204) packagecpu/rp2350_common/periph/uart: PL011 FIFO/ISR fixes.Once this PR lands, RIOT's stock
tests/periph/uartbuildsfor
BOARD=rpi-pico-2-arm, providing an additional validationroute for those UART fixes.
Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are:
review for code drafting (the driver structure mirrors
cpu/rpx0xx/periph/timer.c, adapted for RP2350's per-alarm IRQrouting and the local
atomic_set/clearhelpers in the BSP),PR description authoring, and hardware test orchestration. All
commits are authored by me; every code change was reviewed
before commit; the driver was hardware-verified on a Pi Pico 2 H
using
tests/periph/timer(full output above) before openingthe PR.