Skip to content

Commit 9682896

Browse files
authored
Gpio to gpout clock handle (#2561)
* add gpio_to_gpout_clock_handle to encapsulate GPIO->gpout mapping * oops; wrong parenthesis placement
1 parent af518de commit 9682896

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/rp2_common/hardware_clocks/clocks.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,8 @@ void clocks_enable_resus(resus_callback_t resus_callback) {
243243
}
244244

245245
void clock_gpio_init_int_frac16(uint gpio, uint src, uint32_t div_int, uint16_t div_frac16) {
246-
// Bit messy but it's as much code to loop through a lookup
247-
// table. The sources for each gpout generators are the same
248-
// so just call with the sources from GP0
249-
uint gpclk = 0;
250-
if (gpio == 21) gpclk = clk_gpout0;
251-
else if (gpio == 23) gpclk = clk_gpout1;
252-
else if (gpio == 24) gpclk = clk_gpout2;
253-
else if (gpio == 25) gpclk = clk_gpout3;
254-
#if !PICO_RP2040
255-
else if (gpio == 13) gpclk = clk_gpout0;
256-
else if (gpio == 15) gpclk = clk_gpout1;
257-
#endif
258-
else {
259-
invalid_params_if(HARDWARE_CLOCKS, true);
260-
}
246+
// note this includes an invalid_params_if before defaulting to clk_gpout0
247+
uint gpclk = gpio_to_gpout_clock_handle(gpio, clk_gpout0);
261248

262249
invalid_params_if(HARDWARE_CLOCKS, div_int >> REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_INT));
263250
// Set up the gpclk generator

src/rp2_common/hardware_clocks/include/hardware/clocks.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,42 @@ static inline bool set_sys_clock_khz(uint32_t freq_khz, bool required) {
577577
return false;
578578
}
579579

580+
#define GPIO_TO_GPOUT_CLOCK_HANDLE_RP2040(gpio, default_clk_handle) \
581+
((gpio) == 21 ? clk_gpout0 : \
582+
((gpio) == 23 ? clk_gpout1 : \
583+
((gpio) == 24 ? clk_gpout2 : \
584+
((gpio) == 25 ? clk_gpout3 : \
585+
(default_clk_handle)))))
586+
587+
#define GPIO_TO_GPOUT_CLOCK_HANDLE_RP2350(gpio, default_clk_handle) \
588+
((gpio) == 13 ? clk_gpout0 : \
589+
((gpio) == 15 ? clk_gpout1 : \
590+
(GPIO_TO_GPOUT_CLOCK_HANDLE_RP2040(gpio, default_clk_handle))))
591+
592+
/**
593+
* \def GPIO_TO_GPOUT_CLOCK_HANDLE(gpio, default_clk_handle)
594+
* \ingroup hardware_clocks
595+
* \hideinitializer
596+
* \brief Returns the GPOUT clock number associated with a particular GPIO if there is one, or default_clk_handle otherwise
597+
*
598+
* Note this macro is intended to resolve at compile time, and does no parameter checking
599+
*/
600+
#ifndef GPIO_TO_GPOUT_CLOCK_HANDLE
601+
#if PICO_RP2040
602+
#define GPIO_TO_GPOUT_CLOCK_HANDLE GPIO_TO_GPOUT_CLOCK_HANDLE_RP2040
603+
#else
604+
#define GPIO_TO_GPOUT_CLOCK_HANDLE GPIO_TO_GPOUT_CLOCK_HANDLE_RP2350
605+
#endif
606+
#endif
607+
608+
/**
609+
* \brief return the associated GPOUT clock for a given GPIO if any
610+
* \ingroup hardware_clocks
611+
* \return the GPOUT clock number associated with a particular GPIO or default_clk_handle otherwise
612+
*/
613+
static inline clock_handle_t gpio_to_gpout_clock_handle(uint gpio, clock_handle_t default_clk_handle) {
614+
return GPIO_TO_GPOUT_CLOCK_HANDLE(gpio, ({invalid_params_if(HARDWARE_CLOCKS, true); default_clk_handle;}));
615+
}
580616
#ifdef __cplusplus
581617
}
582618
#endif

0 commit comments

Comments
 (0)