RP2040 specific definitions for handling peripherals. More...
RP2040 specific definitions for handling peripherals.
Definition in file periph_cpu.h.
#include "cpu.h"
#include "vendor/RP2040.h"
#include "io_reg.h"
#include "macros/units.h"
#include "periph/pio.h"
Go to the source code of this file.
Data Structures | |
struct | gpio_pad_ctrl_t |
Memory layout of GPIO control register in pads bank 0. More... | |
struct | gpio_io_ctrl_t |
Memory layout of GPIO control register in IO bank 0. More... | |
struct | adc_conf_t |
ADC device configuration. More... | |
struct | pwm_chan_t |
PWM channel. More... | |
struct | pwm_conf_t |
PWM device configuration. More... | |
struct | uart_conf_t |
UART device configuration. More... | |
struct | timer_channel_conf_t |
Configuration type of a timer channel. More... | |
struct | timer_conf_t |
Timer device configuration. More... | |
struct | pio_conf_t |
PIO configuration type. More... | |
struct | pio_i2c_conf_t |
PIO I2C configuration type. More... | |
struct | spi_conf_t |
SPI device configuration. More... | |
Macros | |
#define | CLOCK_PERIPH_SOURCE CLOCKS_CLK_PERI_CTRL_AUXSRC_clk_sys |
How to source the peripheral clock (default: 125 MHz system clock) | |
#define | CLOCK_PERIPH CLOCK_CORECLOCK |
Peripheral clock (by default source from system clock) | |
#define | RESETS_RESET_MASK |
Periphery blocks that can be reset. | |
#define | GPIO_PIN(port, pin) ((((port) & 0)) | (pin)) |
Convert (port, pin) tuple to a gpio_t value. | |
#define | GPIO_UNDEF UINT32_MAX |
Definition of a fitting UNDEF value. | |
#define | PWM_SLICE_NUMOF (8) |
Number of slices available per PWM device. | |
#define | PWM_CHANNEL_NUMOF (2) |
Number of channels available per slice. | |
#define | PERIPH_TIMER_PROVIDES_SET |
Prevent shared timer functions from being used. | |
#define | PERIPH_SPI_NEEDS_TRANSFER_REG |
#define | PERIPH_SPI_NEEDS_TRANSFER_REGS |
#define | PERIPH_SPI_NEEDS_TRANSFER_BYTE |
Functions | |
static volatile uint32_t * | gpio_pad_register (uint8_t pin) |
Get the PAD control register for the given GPIO pin as word. | |
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 type. | |
static volatile uint32_t * | gpio_io_register (uint8_t pin) |
Get the IO control register for the given GPIO pin as word. | |
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 type. | |
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. | |
static void | gpio_reset_all_config (uint8_t pin) |
Restore the default I/O and PAD configuration of the given GPIO pin. | |
static void | periph_reset (uint32_t components) |
Reset hardware components. | |
static void | periph_reset_done (uint32_t components) |
Waits until hardware components have been reset. | |
#define | PLL_SYS_REF_DIV 1 |
For generating the system clock via PLL, the XOSC reference clock can be divided. | |
#define | PLL_USB_REF_DIV 1 |
Same as PLL_SYS_REF_DIV but for the PLL generating the USB clock. | |
#define | PLL_SYS_VCO_FEEDBACK_SCALE 125 |
VCO feedback scale used for system clock. | |
#define | PLL_SYS_POSTDIV1 6 |
First post-PLL clock divider for system clock. | |
#define | PLL_SYS_POSTDIV2 2 |
Second post-PLL clock divider for system clock. | |
#define | PLL_USB_VCO_FEEDBACK_SCALE 40 |
VCO feedback scale used for USB clock. | |
#define | PLL_USB_POSTDIV1 5 |
First post-PLL clock divider for USB clock. | |
#define | PLL_USB_POSTDIV2 2 |
Second post-PLL clock divider for USB clock. | |
#define | CLOCK_XOSC MHZ(12) |
The RP2040 reference design attaches a 12 MHz crystal to the MCU, so we take this value as default. | |
#define | PLL_CLOCK(vco_feedback, postdiv1, postdiv2) (CLOCK_XOSC * (vco_feedback) / (postdiv1) / (postdiv2)) |
Calculate the resulting PLL clock frequency for the given parameters. | |
#define | CLOCK_CORECLOCK PLL_CLOCK(PLL_SYS_VCO_FEEDBACK_SCALE, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2) |
System core clock speed, 125 MHz unless board changes parameters. | |
#define | CLOCK_USB PLL_CLOCK(PLL_USB_VCO_FEEDBACK_SCALE, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2) |
USB clock speed. | |
Ranges for clock frequencies and clock settings | |
#define | CLOCK_XOSC_MAX MHZ(15) |
Maximum crystal frequency. | |
#define | CLOCK_XOSC_MIN MHZ(5) |
Minimum crystal frequency. | |
#define | PLL_POSTDIV_MIN (1U) |
Minimum value of the post PLL clock divers. | |
#define | PLL_POSTDIV_MAX (7U) |
Maximum value of the post PLL clock divers. | |
#define | PLL_VCO_FEEDBACK_SCALE_MIN (16U) |
Minimum value of the PLL VCO feedback scaler. | |
#define | PLL_VCO_FEEDBACK_SCALE_MAX (320U) |
Maximum value of the PLL VCO feedback scaler. | |
#define | PLL_REF_DIV_MIN (1U) |
Minimum value of the clock divider applied before feeding in the reference clock into the PLL. | |
#define | PLL_REF_DIV_MAX (1U) |
Minimum value of the clock divider applied before feeding in the reference clock into the PLL. | |
#define | HAVE_GPIO_T |
Overwrite the default gpio_t type definition. | |
typedef uint32_t | gpio_t |
#define | HAVE_GPIO_FLANK_T |
Override GPIO active flank values. | |
enum | gpio_flank_t { GPIO_LEVEL_LOW = 0x1 , GPIO_LEVEL_HIGH = 0x2 , GPIO_FALLING = 0x4 , GPIO_RISING = 0x8 , GPIO_BOTH = 0xc } |
GPIO pad configuration register defines and types | |
#define | GPIO_PAD_REGISTER_RESET_VALUE (0x00000056) |
Reset value of the GPIO pad configuration register. | |
enum | { DRIVE_STRENGTH_2MA , DRIVE_STRENGTH_4MA , DRIVE_STRENGTH_8MA , DRIVE_STRENGTH_12MA , DRIVE_STRENGTH_NUMOF } |
Possible drive strength values for gpio_pad_ctrl_t::drive_strength. More... | |
RP2040 clock configuration | |
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_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. | |
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 | 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. | |
void | clock_periph_configure (CLOCKS_CLK_PERI_CTRL_AUXSRC_Enum aux) |
Configure the peripheral clock to run from a dedicated auxiliary clock source. | |
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 | 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_gpout2_configure (uint32_t f_in, uint32_t f_out, CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_Enum aux) |
Configure gpio24 as clock output pin. | |
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. | |
void | clock_adc_configure (CLOCKS_CLK_ADC_CTRL_AUXSRC_Enum aux) |
Configure the ADC clock to run from a dedicated auxiliary clock source. | |
RP2040 PLL configuration | |
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 / post_div_2 . | |
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 / post_div_2 . | |
void | pll_stop_sys (void) |
Stop the PLL of the system clock. | |
void | pll_stop_usb (void) |
Stop the PLL of the USB clock. | |
void | pll_reset_sys (void) |
Reset the PLL of the system clock. | |
void | pll_reset_usb (void) |
Reset the PLL of the USB clock. | |
RP2040 XOSC configuration | |
void | xosc_start (uint32_t f_ref) |
Configures the Crystal to run. | |
void | xosc_stop (void) |
Stop the crystal. | |
RP2040 ROSC configuration | |
void | rosc_start (void) |
Start the ring oscillator in default mode. | |
void | rosc_stop (void) |
Turn off the ROSC to save power. | |
#define CLOCK_CORECLOCK PLL_CLOCK(PLL_SYS_VCO_FEEDBACK_SCALE, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2) |
System core clock speed, 125 MHz unless board changes parameters.
Definition at line 122 of file periph_cpu.h.
#define CLOCK_PERIPH CLOCK_CORECLOCK |
Peripheral clock (by default source from system clock)
Definition at line 200 of file periph_cpu.h.
#define CLOCK_PERIPH_SOURCE CLOCKS_CLK_PERI_CTRL_AUXSRC_clk_sys |
How to source the peripheral clock (default: 125 MHz system clock)
Definition at line 193 of file periph_cpu.h.
#define CLOCK_USB PLL_CLOCK(PLL_USB_VCO_FEEDBACK_SCALE, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2) |
USB clock speed.
Definition at line 128 of file periph_cpu.h.
#define CLOCK_XOSC MHZ(12) |
The RP2040 reference design attaches a 12 MHz crystal to the MCU, so we take this value as default.
Definition at line 110 of file periph_cpu.h.
#define CLOCK_XOSC_MAX MHZ(15) |
Maximum crystal frequency.
Definition at line 135 of file periph_cpu.h.
#define CLOCK_XOSC_MIN MHZ(5) |
Minimum crystal frequency.
Definition at line 136 of file periph_cpu.h.
#define GPIO_IO_REGISTER_RESET_VALUE (0x0000001f) |
Reset value of the GPIO I/O configuration register.
Definition at line 310 of file periph_cpu.h.
#define GPIO_PAD_REGISTER_RESET_VALUE (0x00000056) |
Reset value of the GPIO pad configuration register.
Definition at line 275 of file periph_cpu.h.
#define GPIO_PIN | ( | port, | |
pin | |||
) | ((((port) & 0)) | (pin)) |
Convert (port, pin) tuple to a gpio_t value.
Definition at line 239 of file periph_cpu.h.
#define GPIO_UNDEF UINT32_MAX |
Definition of a fitting UNDEF value.
Definition at line 252 of file periph_cpu.h.
#define HAVE_GPIO_FLANK_T |
Override GPIO active flank values.
Definition at line 258 of file periph_cpu.h.
#define HAVE_GPIO_T |
Overwrite the default gpio_t type definition.
Definition at line 245 of file periph_cpu.h.
#define HAVE_SPI_CLK_T |
Override SPI clock speed values.
Definition at line 786 of file periph_cpu.h.
#define PERIPH_SPI_NEEDS_TRANSFER_BYTE |
Definition at line 813 of file periph_cpu.h.
#define PERIPH_SPI_NEEDS_TRANSFER_REG |
Definition at line 811 of file periph_cpu.h.
#define PERIPH_SPI_NEEDS_TRANSFER_REGS |
Definition at line 812 of file periph_cpu.h.
#define PERIPH_TIMER_PROVIDES_SET |
Prevent shared timer functions from being used.
Definition at line 443 of file periph_cpu.h.
#define PLL_CLOCK | ( | vco_feedback, | |
postdiv1, | |||
postdiv2 | |||
) | (CLOCK_XOSC * (vco_feedback) / (postdiv1) / (postdiv2)) |
Calculate the resulting PLL clock frequency for the given parameters.
Definition at line 116 of file periph_cpu.h.
#define PLL_POSTDIV_MAX (7U) |
Maximum value of the post PLL clock divers.
Definition at line 138 of file periph_cpu.h.
#define PLL_POSTDIV_MIN (1U) |
Minimum value of the post PLL clock divers.
Definition at line 137 of file periph_cpu.h.
#define PLL_REF_DIV_MAX (1U) |
Minimum value of the clock divider applied before feeding in the reference clock into the PLL.
Definition at line 144 of file periph_cpu.h.
#define PLL_REF_DIV_MIN (1U) |
Minimum value of the clock divider applied before feeding in the reference clock into the PLL.
Definition at line 142 of file periph_cpu.h.
#define PLL_SYS_POSTDIV1 6 |
First post-PLL clock divider for system clock.
Definition at line 65 of file periph_cpu.h.
#define PLL_SYS_POSTDIV2 2 |
Second post-PLL clock divider for system clock.
Definition at line 74 of file periph_cpu.h.
#define PLL_SYS_REF_DIV 1 |
For generating the system clock via PLL, the XOSC reference clock can be divided.
The datasheet says for the supported frequency range of the crystal, only a divider of 1 is reasonable.
Definition at line 39 of file periph_cpu.h.
#define PLL_SYS_VCO_FEEDBACK_SCALE 125 |
VCO feedback scale used for system clock.
PLL_SYS_VCO_FEEDBACK_SCALE * CLOCK_XOSC
must be in the range of 400 MHz to 1600 MHz Definition at line 56 of file periph_cpu.h.
#define PLL_USB_POSTDIV1 5 |
First post-PLL clock divider for USB clock.
Definition at line 93 of file periph_cpu.h.
#define PLL_USB_POSTDIV2 2 |
Second post-PLL clock divider for USB clock.
Definition at line 102 of file periph_cpu.h.
#define PLL_USB_REF_DIV 1 |
Same as PLL_SYS_REF_DIV but for the PLL generating the USB clock.
Definition at line 46 of file periph_cpu.h.
#define PLL_USB_VCO_FEEDBACK_SCALE 40 |
VCO feedback scale used for USB clock.
PLL_USB_VCO_FEEDBACK_SCALE * CLOCK_XOSC
must be in the range of 400 MHz to 1600 MHz Definition at line 84 of file periph_cpu.h.
#define PLL_VCO_FEEDBACK_SCALE_MAX (320U) |
Maximum value of the PLL VCO feedback scaler.
Definition at line 140 of file periph_cpu.h.
#define PLL_VCO_FEEDBACK_SCALE_MIN (16U) |
Minimum value of the PLL VCO feedback scaler.
Definition at line 139 of file periph_cpu.h.
#define PWM_CHANNEL_NUMOF (2) |
Number of channels available per slice.
Definition at line 410 of file periph_cpu.h.
#define PWM_SLICE_NUMOF (8) |
Number of slices available per PWM device.
Definition at line 405 of file periph_cpu.h.
#define RESETS_RESET_MASK |
Periphery blocks that can be reset.
Definition at line 206 of file periph_cpu.h.
typedef uint32_t gpio_t |
Definition at line 246 of file periph_cpu.h.
typedef uint32_t spi_clk_t |
SPI clock type.
Definition at line 798 of file periph_cpu.h.
anonymous enum |
Possible function values for gpio_io_ctrl_t::output_enable_override.
Definition at line 347 of file periph_cpu.h.
anonymous enum |
Possible function values for gpio_io_ctrl_t::input_override.
Definition at line 358 of file periph_cpu.h.
anonymous enum |
Possible function values for gpio_io_ctrl_t::irq_override.
Definition at line 369 of file periph_cpu.h.
anonymous enum |
Definition at line 787 of file periph_cpu.h.
anonymous enum |
Possible drive strength values for gpio_pad_ctrl_t::drive_strength.
Definition at line 279 of file periph_cpu.h.
anonymous enum |
Possible function values for gpio_io_ctrl_t::output_override.
Definition at line 336 of file periph_cpu.h.
enum gpio_flank_t |
Definition at line 259 of file periph_cpu.h.
Possible function values for gpio_io_ctrl_t::function_select.
Definition at line 314 of file periph_cpu.h.
void clock_adc_configure | ( | CLOCKS_CLK_ADC_CTRL_AUXSRC_Enum | aux | ) |
Configure the ADC clock to run from a dedicated auxiliary clock source.
aux | Auxiliary clock source |
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.
Can be used as an external clock source for another circuit or to check the expected signal with a logic analyzer
f_in | Input frequency |
f_out | Output frequency |
aux | Auxiliary clock source |
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.
Can be used as an external clock source for another circuit or to check the expected signal with a logic analyzer
f_in | Input frequency |
f_out | Output frequency |
aux | Auxiliary clock source |
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.
Can be used as an external clock source for another circuit or to check the expected signal with a logic analyzer
f_in | Input frequency |
f_out | Output frequency |
aux | Auxiliary clock source |
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.
Can be used as an external clock source for another circuit or to check the expected signal with a logic analyzer
f_in | Input frequency |
f_out | Output frequency |
aux | Auxiliary clock source |
void clock_periph_configure | ( | CLOCKS_CLK_PERI_CTRL_AUXSRC_Enum | aux | ) |
Configure the peripheral clock to run from a dedicated auxiliary clock source.
aux | Auxiliary clock source |
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.
f_in | Input frequency of the auxiliary clock source |
f_out | Output frequency of the reference clock |
aux | Which auxiliary clock source to use |
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.
f_in | Input frequency of the reference clock |
f_out | Output frequency of the system clock |
source | Clock source |
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.
f_in | Input frequency of the auxiliary clock source |
f_out | Output frequency of the system clock |
aux | Which auxiliary clock source to use |
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.
f_in | Input frequency of the reference clock |
f_out | Output frequency of the system clock |
source | Clock source |
|
inlinestatic |
Get the IO control register for the given GPIO pin as word.
Definition at line 505 of file periph_cpu.h.
|
inlinestatic |
Get the PAD control register for the given GPIO pin as word.
Note: Only perform 32-bit writes to I/O registers.
Definition at line 487 of file periph_cpu.h.
|
inlinestatic |
Restore the default I/O and PAD configuration of the given GPIO pin.
Definition at line 532 of file periph_cpu.h.
|
inlinestatic |
Set the function select subregister for the given pin to the given value.
Definition at line 523 of file periph_cpu.h.
|
inlinestatic |
Convenience function to set the I/O configuration of the given pin using the bit-field convenience type.
Definition at line 514 of file periph_cpu.h.
|
inlinestatic |
Convenience function to set the pad configuration of the given pin using the bit-field convenience type.
Definition at line 496 of file periph_cpu.h.
|
inlinestatic |
Reset hardware components.
components | bitmask of components to be reset, |
Definition at line 544 of file periph_cpu.h.
|
inlinestatic |
Waits until hardware components have been reset.
components | bitmask of components that must have reset, |
Definition at line 555 of file periph_cpu.h.
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
/ post_div_2
.
ref_div | Input clock divisor |
vco_feedback_scale | VCO feedback scales |
post_div_1 | Output post divider factor 1 |
post_div_2 | Output post divider factor 2 |
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
/ post_div_2
.
ref_div | Input clock divisor |
vco_feedback_scale | VCO feedback scales |
post_div_1 | Output post divider factor 1 |
post_div_2 | Output post divider factor 2 |
void rosc_start | ( | void | ) |
Start the ring oscillator in default mode.
The ROSC is running at boot time but may be turned off to save power when switching to the accurate XOSC. The default ROSC provides an instable frequency of 1.8 MHz to 12 MHz.
void rosc_stop | ( | void | ) |
Turn off the ROSC to save power.
The system clock must be switched to to another lock source before the ROSC is stopped, other wise the chip will be lock up.
void xosc_start | ( | uint32_t | f_ref | ) |
Configures the Crystal to run.
f_ref | Desired frequency in Hz |
f_ref
<= 15 MHz.The reference hardware manual suggests to use a 12 MHz crystal.