AON tick generation: selection of the LPOSC as timer source when XOSC is not the reference clock
Board: Pico 2 (RP2350)
SDK version: 2.3.0 (current master branch)
In `rp2_common/pico_aon_timer/aon_timer.c`, the `aon_timer_start` function always uses the XOSC as reference clock to generate ticks. In some experiments I did, I needed to turn off the XOSC and change the reference clock. The datasheet (12.10.5.1.) mentions the possibility to use the LPOSC instead of the XOSC to generate ticks when the latter isn't in use. The sdk provides a method to this end: `powman_timer_set_1khz_tick_source_lposc()`. The datasheet (12.10.5.3.) also mentions the `CLK_REF_SELECTED` register which specifies which clock source is used as reference clock.
In my experiments, I ended up modying the `aon_timer_start` function like the following:
```c
bool aon_timer_start(const struct timespec *ts) {
#if HAS_RP2040_RTC
rtc_init();
return aon_timer_set_time(ts);
#elif HAS_POWMAN_TIMER
// Check if the XOSC is set as reference clock. If not the case, use the LPOSC to generate ticks
uint16_t *clk_ref_selected = (uint16_t *)(CLOCKS_BASE+CLOCKS_CLK_REF_SELECTED_OFFSET);
if(*clk_ref_selected == 0x4) {
powman_timer_set_1khz_tick_source_xosc();
} else {
powman_timer_set_1khz_tick_source_lposc();
}
bool ok = aon_timer_set_time(ts);
if (ok) {
powman_timer_set_ms(timespec_to_ms(ts));
powman_timer_start();
}
return ok;
#else
panic_unsupported();
#endif
}
```
The code above was working for me when using the ROSC or LPOSC as reference clocks. The only issue was that when using the LPOSC to generate ticks, the default frequency specified by the sdk (32768) was slightly too fast compared to the one I measured on my board (between 29 and 30kHz). So maybe it would be relevant to let the user specify its own frequency when using the LPOSC as 1khz tick instead of relying on the default.
Note that I haven't found the `CLK_REF_SELECTED` register in `clock_hw` so I couldn't use this struct to access the value. This is why I was reading the value directly from the memory without using the `clock_hw` struct.
0 条评论