24#include "vendor/RP2040.h"
33#if !defined(PLL_SYS_REF_DIV) || defined(DOXYGEN)
39#define PLL_SYS_REF_DIV 1
42#if !defined(PLL_USB_REF_DIV) || defined(DOXYGEN)
46#define PLL_USB_REF_DIV 1
49#if !defined(PLL_SYS_VCO_FEEDBACK_SCALE) || defined(DOXYGEN)
56#define PLL_SYS_VCO_FEEDBACK_SCALE 125
59#if !defined(PLL_SYS_POSTDIV1) || defined(DOXYGEN)
65#define PLL_SYS_POSTDIV1 6
68#if !defined(PLL_SYS_POSTDIV2) || defined(DOXYGEN)
74#define PLL_SYS_POSTDIV2 2
77#if !defined(PLL_USB_VCO_FEEDBACK_SCALE) || defined(DOXYGEN)
84#define PLL_USB_VCO_FEEDBACK_SCALE 40
87#if !defined(PLL_USB_POSTDIV1) || defined(DOXYGEN)
93#define PLL_USB_POSTDIV1 5
96#if !defined(PLL_USB_POSTDIV2) || defined(DOXYGEN)
102#define PLL_USB_POSTDIV2 2
105#if !defined(CLOCK_XOSC) || defined(DOXYGEN)
110#define CLOCK_XOSC MHZ(12)
116#define PLL_CLOCK(vco_feedback, postdiv1, postdiv2) \
117 (CLOCK_XOSC * (vco_feedback) / (postdiv1) / (postdiv2))
122#define CLOCK_CORECLOCK \
123 PLL_CLOCK(PLL_SYS_VCO_FEEDBACK_SCALE, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2)
129 PLL_CLOCK(PLL_USB_VCO_FEEDBACK_SCALE, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2)
135#define CLOCK_XOSC_MAX MHZ(15)
136#define CLOCK_XOSC_MIN MHZ(5)
137#define PLL_POSTDIV_MIN (1U)
138#define PLL_POSTDIV_MAX (7U)
139#define PLL_VCO_FEEDBACK_SCALE_MIN (16U)
140#define PLL_VCO_FEEDBACK_SCALE_MAX (320U)
141#define PLL_REF_DIV_MIN (1U)
143#define PLL_REF_DIV_MAX (1U)
147#if CLOCK_USB != MHZ(48)
148#error "USB clock != 48 MHz, check PLL_USB_VCO_FEEDBACK_SCALE, PLL_USB_POSTDIV1, PLL_SYS_POSTDIV2"
151#if (CLOCK_XOSC > CLOCK_XOSC_MAX) || (CLOCK_XOSC < CLOCK_XOSC_MIN)
152#error "Value for CLOCK_XOSC out of range, check config"
155#if (PLL_SYS_REF_DIV < PLL_REF_DIV_MIN) || (PLL_SYS_REF_DIV > PLL_REF_DIV_MAX)
156#error "Value for PLL_SYS_REF_DIV out of range, check config"
159#if (PLL_USB_REF_DIV < PLL_REF_DIV_MIN) || (PLL_USB_REF_DIV > PLL_REF_DIV_MAX)
160#error "Value for PLL_USB_REF_DIV out of range, check config"
163#if (PLL_SYS_VCO_FEEDBACK_SCALE < PLL_VCO_FEEDBACK_SCALE_MIN) \
164 || (PLL_SYS_VCO_FEEDBACK_SCALE > PLL_VCO_FEEDBACK_SCALE_MAX)
165#error "Value for PLL_SYS_VCO_FEEDBACK_SCALE out of range, check config"
168#if (PLL_USB_VCO_FEEDBACK_SCALE < PLL_VCO_FEEDBACK_SCALE_MIN) \
169 || (PLL_USB_VCO_FEEDBACK_SCALE > PLL_VCO_FEEDBACK_SCALE_MAX)
170#error "Value for PLL_USB_VCO_FEEDBACK_SCALE out of range, check config"
173#if (PLL_SYS_POSTDIV1 < PLL_POSTDIV_MIN) || (PLL_SYS_POSTDIV1 > PLL_POSTDIV_MAX)
174#error "Value for PLL_SYS_POSTDIV1 out of range, check config"
177#if (PLL_SYS_POSTDIV2 < PLL_POSTDIV_MIN) || (PLL_SYS_POSTDIV2 > PLL_POSTDIV_MAX)
178#error "Value for PLL_SYS_POSTDIV2 out of range, check config"
181#if (PLL_USB_POSTDIV1 < PLL_POSTDIV_MIN) || (PLL_USB_POSTDIV1 > PLL_POSTDIV_MAX)
182#error "Value for PLL_USB_POSTDIV1 out of range, check config"
185#if (PLL_USB_POSTDIV2 < PLL_POSTDIV_MIN) || (PLL_USB_POSTDIV2 > PLL_POSTDIV_MAX)
186#error "Value for PLL_USB_POSTDIV2 out of range, check config"
189#if !defined(CLOCK_PERIPH_SOURCE) || defined(DOXYGEN)
193#define CLOCK_PERIPH_SOURCE CLOCKS_CLK_PERI_CTRL_AUXSRC_clk_sys
196#if !defined(CLOCK_PERIPH) || defined(DOXYGEN)
200#define CLOCK_PERIPH CLOCK_CORECLOCK
206#define RESETS_RESET_MASK \
207 (RESETS_RESET_usbctrl_Msk | \
208 RESETS_RESET_uart1_Msk | \
209 RESETS_RESET_uart0_Msk | \
210 RESETS_RESET_timer_Msk | \
211 RESETS_RESET_tbman_Msk | \
212 RESETS_RESET_sysinfo_Msk | \
213 RESETS_RESET_syscfg_Msk | \
214 RESETS_RESET_spi1_Msk | \
215 RESETS_RESET_spi0_Msk | \
216 RESETS_RESET_rtc_Msk | \
217 RESETS_RESET_pwm_Msk | \
218 RESETS_RESET_pll_usb_Msk | \
219 RESETS_RESET_pll_sys_Msk | \
220 RESETS_RESET_pio1_Msk | \
221 RESETS_RESET_pio0_Msk | \
222 RESETS_RESET_pads_qspi_Msk | \
223 RESETS_RESET_pads_bank0_Msk | \
224 RESETS_RESET_jtag_Msk | \
225 RESETS_RESET_io_qspi_Msk | \
226 RESETS_RESET_io_bank0_Msk | \
227 RESETS_RESET_i2c1_Msk | \
228 RESETS_RESET_i2c0_Msk | \
229 RESETS_RESET_dma_Msk | \
230 RESETS_RESET_busctrl_Msk | \
231 RESETS_RESET_adc_Msk)
239#define GPIO_PIN(port, pin) ((((port) & 0)) | (pin))
246typedef uint32_t gpio_t;
252#define GPIO_UNDEF UINT32_MAX
258#define HAVE_GPIO_FLANK_T
275#define GPIO_PAD_REGISTER_RESET_VALUE (0x00000056)
310#define GPIO_IO_REGISTER_RESET_VALUE (0x0000001f)
405#define PWM_SLICE_NUMOF (8)
410#define PWM_CHANNEL_NUMOF (2)
443#define PERIPH_TIMER_PROVIDES_SET
489 return (uint32_t *)(PADS_BANK0_BASE + 4 + (pin << 2));
498 uint32_t *c = (uint32_t *)&config;
507 return (uint32_t *)(IO_BANK0_BASE + 4 + (pin << 3));
516 uint32_t *c = (uint32_t *)&config;
526 IO_BANK0_GPIO0_CTRL_FUNCSEL_Msk);
558 while ((~RESETS->RESET_DONE) & components) { }
575 CLOCKS_CLK_SYS_CTRL_SRC_Enum source);
588 CLOCKS_CLK_SYS_CTRL_AUXSRC_Enum aux);
601 CLOCKS_CLK_REF_CTRL_SRC_Enum source);
614 CLOCKS_CLK_REF_CTRL_AUXSRC_Enum aux);
698 uint16_t vco_feedback_scale,
699 uint8_t post_div_1, uint8_t post_div_2);
713 uint16_t vco_feedback_scale,
714 uint8_t post_div_1, uint8_t post_div_2);
786#define HAVE_SPI_CLK_T
811#define PERIPH_SPI_NEEDS_TRANSFER_REG
812#define PERIPH_SPI_NEEDS_TRANSFER_REGS
813#define PERIPH_SPI_NEEDS_TRANSFER_BYTE
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
gpio_t adc_conf_t
ADC configuration wrapper.
High-level PIO peripheral driver interface.
enum IRQn IRQn_Type
Interrupt Number Definition.
unsigned pio_t
PIO index type.
RP2040 atomic register access macros.
static void io_reg_atomic_clear(volatile uint32_t *reg, uint32_t mask)
Clear the bits in the register at address reg as given by the set bits in operand op.
static void io_reg_write_dont_corrupt(volatile uint32_t *reg, uint32_t value, uint32_t mask)
Updates part of an I/O register without corrupting its contents.
static void io_reg_atomic_set(volatile uint32_t *reg, uint32_t mask)
Set the bits in the register at address reg as given by the set bits in operand op.
void rosc_start(void)
Start the ring oscillator in default mode.
void clock_gpout2_configure(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_Enum aux)
Configure gpio24 as clock output pin.
@ GPIO_LEVEL_LOW
emit interrupt level-triggered on low input
@ GPIO_LEVEL_HIGH
emit interrupt level-triggered on low input
void clock_gpout3_configure(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_Enum aux)
Configure gpio25 as clock output pin.
static void gpio_set_pad_config(uint8_t pin, gpio_pad_ctrl_t config)
Convenience function to set the pad configuration of the given pin using the bit-field convenience ty...
gpio_function_select_t
Possible function values for gpio_io_ctrl_t::function_select.
@ FUNCTION_SELECT_PIO0
connect pin to the first PIO peripheral
@ FUNCTION_SELECT_CLOCK
connect pin to the timer (depending on pin: external clock, clock output, or not supported)
@ FUNCTION_SELECT_PIO1
connect pin to the second PIO peripheral
@ FUNCTION_SELECT_I2C
connect pin to the I2C peripheral (SCL/SDA depends on pin)
@ FUNCTION_SELECT_PWM
connect pin to the timer for PWM (channel depends on pin)
@ FUNCTION_SELECT_UART
connect pin to the UART peripheral (TXD/RXD depends on pin)
@ FUNCTION_SELECT_USB
connect pin to the USB peripheral (function depends on pin)
@ FUNCTION_SELECT_SIO
use pin as vanilla GPIO
@ FUNCTION_SELECT_SPI
connect pin to the SPI peripheral (MISO/MOSI/SCK depends on pin)
@ FUNCTION_SELECT_NONE
Reset value, pin unconnected.
@ INPUT_OVERRIDE_LOW
signal low to connected peripheral
@ INPUT_OVERRIDE_INVERT
invert signal to connected peripheral
@ INPUT_OVERRIDE_NUMOF
number of possible input override settings
@ INPUT_OVERRIDE_NOMARL
don't mess with peripheral input signal
@ INPUT_OVERRIDE_HIGH
signal high to connected peripheral
@ OUTPUT_OVERRIDE_HIGH
drive pin high, overriding peripheral signal
@ OUTPUT_OVERRIDE_NORMAL
drive pin from connected peripheral
@ OUTPUT_OVERRIDE_LOW
drive pin low, overriding peripheral signal
@ OUTPUT_OVERRIDE_NUMOF
number of possible output override settings
@ OUTPUT_OVERRIDE_INVERT
drive pin from connected peripheral, but invert output
void xosc_start(uint32_t f_ref)
Configures the Crystal to run.
static void gpio_reset_all_config(uint8_t pin)
Restore the default I/O and PAD configuration of the given GPIO pin.
void pll_start_usb(uint8_t ref_div, uint16_t vco_feedback_scale, uint8_t post_div_1, uint8_t post_div_2)
Start the PLL for the USB clock output[MHz] = f_ref / ref_div * vco_feedback_scale / post_div_1 / pos...
void pll_stop_sys(void)
Stop the PLL of the system clock.
void clock_adc_configure(CLOCKS_CLK_ADC_CTRL_AUXSRC_Enum aux)
Configure the ADC clock to run from a dedicated auxiliary clock source.
static void gpio_set_function_select(uint8_t pin, gpio_function_select_t funcsel)
Set the function select subregister for the given pin to the given value.
void xosc_stop(void)
Stop the crystal.
#define GPIO_PAD_REGISTER_RESET_VALUE
Reset value of the GPIO pad configuration register.
void clock_ref_configure_aux_source(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_REF_CTRL_AUXSRC_Enum aux)
Configure the reference clock to run from an auxiliary clock source, like PLL.
#define PWM_CHANNEL_NUMOF
Number of channels available per slice.
static void gpio_set_io_config(uint8_t pin, gpio_io_ctrl_t config)
Convenience function to set the I/O configuration of the given pin using the bit-field convenience ty...
@ OUTPUT_ENABLE_OVERRIDE_NUMOF
number of possible output enable override settings
@ OUTPUT_ENABLE_OVERRIDE_DISABLE
disable output, overriding peripheral signal
@ OUTPUT_ENABLE_OVERRIDE_NOMARL
enable output as specified by connected peripheral
@ OUTPUT_ENABLE_OVERRIDE_ENABLE
enable output, overriding peripheral signal
@ OUTPUT_ENABLE_OVERRIDE_INVERT
invert output enable setting of peripheral
void clock_ref_configure_source(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_REF_CTRL_SRC_Enum source)
Configure the reference clock to run from a clock source, which is either the ROSC or the XOSC.
void pll_stop_usb(void)
Stop the PLL of the USB clock.
@ DRIVE_STRENGTH_NUMOF
number of different drive strength options
@ DRIVE_STRENGTH_4MA
set driver strength to 4 mA
@ DRIVE_STRENGTH_8MA
set driver strength to 8 mA
@ DRIVE_STRENGTH_12MA
set driver strength to 12 mA
@ DRIVE_STRENGTH_2MA
set driver strength to 2 mA
@ IRQ_OVERRIDE_INVERT
invert IRQ signal
@ IRQ_OVERRIDE_LOW
set IRQ signal to low
@ IRQ_OVERRIDE_NORMAL
don't mess with IRQ signal
@ IRQ_OVERRIDE_HIGH
set IRQ signal to high
@ IRQ_OVERRIDE_NUMOF
number of possible IRQ override settings
void clock_sys_configure_aux_source(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_SYS_CTRL_AUXSRC_Enum aux)
Configure the system clock to run from an auxiliary clock source, like PLL.
static volatile uint32_t * gpio_io_register(uint8_t pin)
Get the IO control register for the given GPIO pin as word.
static volatile uint32_t * gpio_pad_register(uint8_t pin)
Get the PAD control register for the given GPIO pin as word.
void pll_reset_sys(void)
Reset the PLL of the system clock.
void pll_reset_usb(void)
Reset the PLL of the USB clock.
void clock_sys_configure_source(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_SYS_CTRL_SRC_Enum source)
Configure the system clock to run from the reference clock, which is the default on boot.
void clock_gpout1_configure(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_Enum aux)
Configure gpio23 as clock output pin.
void clock_gpout0_configure(uint32_t f_in, uint32_t f_out, CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_Enum aux)
Configure gpio21 as clock output pin.
void pll_start_sys(uint8_t ref_div, uint16_t vco_feedback_scale, uint8_t post_div_1, uint8_t post_div_2)
Start the PLL for the system clock output[MHz] = f_ref / ref_div * vco_feedback_scale / post_div_1 / ...
static void periph_reset_done(uint32_t components)
Waits until hardware components have been reset.
void clock_periph_configure(CLOCKS_CLK_PERI_CTRL_AUXSRC_Enum aux)
Configure the peripheral clock to run from a dedicated auxiliary clock source.
#define GPIO_IO_REGISTER_RESET_VALUE
Reset value of the GPIO I/O configuration register.
void rosc_stop(void)
Turn off the ROSC to save power.
static void periph_reset(uint32_t components)
Reset hardware components.
Memory layout of GPIO control register in IO bank 0.
uint32_t output_enable_override
output enable override
uint32_t input_override
input value override
uint32_t irq_override
interrupt inversion override
uint32_t output_override
output value override
uint32_t function_select
select GPIO function
Memory layout of GPIO control register in pads bank 0.
uint32_t pull_up_enable
enable pull up resistor
uint32_t schmitt_trig_enable
enable Schmitt trigger
uint32_t input_enable
enable as input
uint32_t slew_rate_fast
set slew rate control to fast
uint32_t output_disable
disable output, overwrite output enable from peripherals
uint32_t drive_strength
GPIO driver strength.
uint32_t pull_down_enable
enable pull down resistor
IRQn_Type irqn0
PIO IRQ0 interrupt number.
IRQn_Type irqn1
PIO IRQ1 interrupt number.
PIO0_Type * dev
PIO device.
PIO I2C configuration type.
gpio_t sda
Pin to use as SDA pin.
pio_t pio
PIO number of the PIO to run this configuration.
unsigned irq
PIO IRQ line to use.
gpio_t scl
Pin to use as SCL pin.
PWM device configuration.
uint8_t pwm_slice
PWM slice instance, must be < to PWM_SLICE_NUMOF.
SPI device configuration.
SPI0_Type * dev
Base address of the I/O registers of the device.
gpio_t clk_pin
GPIO pin to use for CLK.
Configuration type of a timer channel.
IRQn_Type irqn
timer channel interrupt number
Timer device configuration.
const timer_channel_conf_t * ch
pointer to timer channel configuration
uint8_t ch_numof
number of timer channels
UART device configuration.
UART0_Type * dev
Base address of the I/O registers of the device.
#define MHZ(x)
A macro to return the Hz in x MHz.
#define KHZ(x)
A macro to return the Hz in x kHz.