Go to the documentation of this file.
21 #ifndef PERIPH_CPU_COMMON_H
22 #define PERIPH_CPU_COMMON_H
25 #include "exti_config.h"
26 #include "timer_config.h"
35 #define CPUID_LEN (16U)
41 #define PERIPH_SPI_NEEDS_INIT_CS
42 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
43 #ifndef MODULE_PERIPH_DMA
44 #define PERIPH_SPI_NEEDS_TRANSFER_REG
45 #define PERIPH_SPI_NEEDS_TRANSFER_REGS
53 #define PERIPH_I2C_NEED_READ_REG
54 #define PERIPH_I2C_NEED_READ_REGS
55 #define PERIPH_I2C_NEED_WRITE_REG
56 #define PERIPH_I2C_NEED_WRITE_REGS
70 #define GPIO_UNDEF (0xffffffff)
77 #define GPIO_PIN(x, y) (((gpio_t)(&PORT_SEC->Group[x])) | y)
78 #elif defined(PORT_IOBUS)
79 #define GPIO_PIN(x, y) (((gpio_t)(&PORT_IOBUS->Group[x])) | y)
81 #define GPIO_PIN(x, y) (((gpio_t)(&PORT->Group[x])) | y)
102 #define GPIO_MODE(pr, ie, pe) (pr | (ie << 1) | (pe << 2))
108 #define HAVE_GPIO_MODE_T
122 #define HAVE_GPIO_FLANK_T
186 #define HAVE_UART_DATA_BITS_T
199 #define uart_pin_rx(dev) uart_config[dev].rx_pin
200 #define uart_pin_tx(dev) uart_config[dev].tx_pin
209 #ifndef UART_TXBUF_SIZE
210 #define UART_TXBUF_SIZE (64)
230 #ifdef MODULE_PERIPH_UART_HW_FC
259 volatile uint32_t *mclk;
272 #define TC_CONFIG(tim) { \
273 .dev = {.tc = tim}, \
274 .mclk = MCLK_ ## tim, \
275 .mclk_mask = MCLK_ ## tim ## _MASK, \
276 .gclk_id = tim ## _GCLK_ID, \
277 .type = TIMER_TYPE_TC, }
279 #define TC_CONFIG(tim) { \
280 .dev = {.tc = tim}, \
281 .pm_mask = PM_APBCMASK_ ## tim, \
282 .gclk_id = tim ## _GCLK_ID, \
283 .type = TIMER_TYPE_TC, }
290 #define TCC_CONFIG(tim) { \
291 .dev = {.tcc = tim}, \
292 .mclk = MCLK_ ## tim, \
293 .mclk_mask = MCLK_ ## tim ## _MASK, \
294 .gclk_id = tim ## _GCLK_ID, \
295 .type = TIMER_TYPE_TCC, }
297 #define TCC_CONFIG(tim) { \
298 .dev = {.tcc = tim}, \
299 .pm_mask = PM_APBCMASK_ ## tim, \
300 .gclk_id = tim ## _GCLK_ID, \
301 .type = TIMER_TYPE_TCC, }
348 #define HAVE_SPI_MODE_T
361 #define HAVE_SPI_CLK_T
375 #define spi_pin_mosi(dev) spi_config[dev].mosi_pin
376 #define spi_pin_miso(dev) spi_config[dev].miso_pin
377 #define spi_pin_clk(dev) spi_config[dev].clk_pin
396 #ifdef MODULE_PERIPH_DMA
416 #define HAVE_I2C_SPEED_T
430 #define i2c_pin_sda(dev) i2c_config[dev].sda_pin
431 #define i2c_pin_scl(dev) i2c_config[dev].scl_pin
465 volatile uint32_t *mclk;
479 #define TIMER_CHANNEL_NUMOF (2)
524 #ifdef MODULE_PERIPH_GPIO
534 #ifdef MODULE_PERIPH_GPIO
571 SUPC->VREG.bit.SEL = src;
572 while (!SUPC->STATUS.bit.VREGRDY) {}
605 if (sercom == SERCOM0) {
610 if (sercom == SERCOM1) {
615 if (sercom == SERCOM2) {
620 if (sercom == SERCOM3) {
625 if (sercom == SERCOM4) {
630 if (sercom == SERCOM5) {
635 if (sercom == SERCOM6) {
640 if (sercom == SERCOM7) {
648 return SERCOM_INST_NUM;
659 #if defined(CPU_COMMON_SAMD21)
660 PM->APBCMASK.reg |= (PM_APBCMASK_SERCOM0 << id);
661 #elif defined (CPU_COMMON_SAMD5X)
663 MCLK->APBAMASK.reg |= (1 << (
id + 12));
665 MCLK->APBBMASK.reg |= (1 << (
id + 7));
667 MCLK->APBDMASK.reg |= (1 << (
id - 4));
671 MCLK->APBCMASK.reg |= (MCLK_APBCMASK_SERCOM0 << id);
673 #if defined(CPU_COMMON_SAML21)
675 MCLK->APBDMASK.reg |= (MCLK_APBDMASK_SERCOM5);
689 #if defined(CPU_COMMON_SAMD21)
690 PM->APBCMASK.reg &= ~(PM_APBCMASK_SERCOM0 << id);
691 #elif defined (CPU_COMMON_SAMD5X)
693 MCLK->APBAMASK.reg &= ~(1 << (
id + 12));
695 MCLK->APBBMASK.reg &= ~(1 << (
id + 7));
697 MCLK->APBDMASK.reg &= ~(1 << (
id - 4));
701 MCLK->APBCMASK.reg &= ~(MCLK_APBCMASK_SERCOM0 << id);
703 #if defined (CPU_COMMON_SAML21)
705 MCLK->APBDMASK.reg &= ~(MCLK_APBDMASK_SERCOM5);
711 #ifdef CPU_COMMON_SAMD5X
712 static inline uint8_t _sercom_gclk_id_core(uint8_t
sercom_id) {
732 #if defined(CPU_COMMON_SAMD21)
733 GCLK->CLKCTRL.reg = (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(gclk) |
734 (SERCOM0_GCLK_ID_CORE + id));
735 while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
736 #elif defined(CPU_COMMON_SAMD5X)
737 GCLK->PCHCTRL[_sercom_gclk_id_core(
id)].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
740 GCLK->PCHCTRL[SERCOM0_GCLK_ID_CORE + id].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
742 #if defined(CPU_COMMON_SAML21)
744 GCLK->PCHCTRL[SERCOM5_GCLK_ID_CORE].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
755 #ifdef RSTC_RCAUSE_BACKUP
756 return RSTC->RCAUSE.bit.BACKUP;
773 #if defined(USB_INST_NUM) || defined(DOXYGEN)
789 #define NWDT_TIME_LOWER_LIMIT (8U)
790 #define NWDT_TIME_UPPER_LIMIT (16384U)
797 #define WDT_HAS_STOP (1)
801 #define WDT_HAS_INIT (1)
852 #define DMA_TRIGGER_DISABLED 0
857 #if defined(CPU_COMMON_SAML21) || defined(DOXYGEN)
858 #define DMA_DESCRIPTOR_IN_LPSRAM
864 #ifdef DMA_DESCRIPTOR_IN_LPSRAM
865 #define DMA_DESCRIPTOR_ATTRS __attribute__((section(".backup.bss")))
867 #define DMA_DESCRIPTOR_ATTRS
917 void dma_setup(
dma_t dma,
unsigned trigger, uint8_t prio,
bool irq);
999 const void *src,
void *dst,
size_t num,
dma_incr_t incr);
1020 size_t num,
bool incr);
1117 #ifdef FLASH_USER_PAGE_SIZE
1118 #define FLASH_USER_PAGE_AUX_SIZE (FLASH_USER_PAGE_SIZE - sizeof(nvm_user_page_t))
1120 #define FLASH_USER_PAGE_AUX_SIZE (AUX_PAGE_SIZE * AUX_NB_OF_PAGES - sizeof(nvm_user_page_t))
1157 #define sam0_flashpage_aux_get(offset) \
1158 (const void*)((uint8_t*)NVMCTRL_USER + sizeof(nvm_user_page_t) + (offset))
1165 #define sam0_flashpage_aux_cfg() \
1166 ((const nvm_user_page_t*)NVMCTRL_USER)
spi_misopad_t
Available values for SERCOM SPI MISO pad selection.
void dma_append(dma_t dma, DmacDescriptor *descriptor, uint8_t width, const void *src, void *dst, size_t num, dma_incr_t incr)
Append a second transfer descriptor after the default channel descriptor.
uint16_t gclk_id
TCn_GCLK_ID.
static void sam0_cortexm_sleep(int deep)
Wrapper for cortexm_sleep calling power management callbacks.
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
uint16_t gclk_ctrl
GCLK_CLKCTRL_ID for the Timer.
void sam0_flashpage_aux_write(uint32_t offset, const void *data, size_t len)
Write data to the user configuration area.
@ SPI_MODE_3
CPOL=1, CPHA=1.
uint32_t muxpos
ADC channel pin multiplexer value.
@ UART_DATA_BITS_5
5 data bits
@ GPIO_MUX_F
select peripheral function F
int rtc_tamper_register(gpio_t pin, gpio_flank_t flank)
Enable Tamper Detection IRQs.
SercomSpi * dev
pointer to the used SPI device
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
uint32_t sam0_gclk_freq(uint8_t id)
Returns the frequency of a GCLK provider.
sam0_supc_t
Available voltage regulators on the supply controller.
uint8_t gclk_src
GCLK source which supplys SERCOM.
@ UART_PAD_RX_3
select pad 3
gpio_mux_t clk_mux
alternate function for CLK pin (mux)
dma_incr_t
Available DMA address increment modes.
uart_flag_t flags
set optional SERCOM flags
@ UART_PAD_TX_0_RTS_2_CTS_3
TX is pad 0, on top RTS on pad 2 and CTS on pad 3.
void dma_prepare_dst(dma_t dma, void *dst, size_t num, bool incr)
Prepare a transfer without modifying the source address settings.
gpio_mux_t d_mux
alternate function (mux) for data pins
void dma_cancel(dma_t dma)
Cancel an active DMA transfer.
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
unsigned dma_t
DMA channel type.
uint8_t gclk_src
GCLK source which clocks TIMER.
SercomUsart * dev
pointer to the used UART device
void dma_append_dst(dma_t dma, DmacDescriptor *next, void *dst, size_t num, bool incr)
Append a second transfer descriptor after the default channel descriptor, copying source and block si...
@ GPIO_IN_PD
configure as input with pull-down resistor
@ GPIO_MUX_E
select peripheral function E
NVM User Row Mapping - Dedicated Entries Config values will be applied at power-on.
#define assert(cond)
abort the program if assertion is false
Common configuration for timer devices.
void rtc_tamper_init(void)
Power on the RTC (if the RTC/RTT is not otherwise used)
@ SPI_MODE_0
CPOL=0, CPHA=0.
@ GPIO_OD
configure as output in open-drain mode without pull resistor
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
@ UART_FLAG_TXINV
invert TX signal
enum IRQn IRQn_Type
Interrupt Number Definition.
@ UART_FLAG_WAKEUP
wake from sleep on receive
@ UART_DATA_BITS_8
8 data bits
@ UART_PAD_TX_0
select pad 0
void gpio_pm_cb_leave(int deep)
Called after the power management left a power mode.
@ I2C_FLAG_RUN_STANDBY
run SERCOM in standby mode
static void sam0_set_voltage_regulator(sam0_supc_t src)
Switch the internal voltage regulator used for generating the internal MCU voltages.
uart_flag_t
Available SERCOM UART flag selections.
uart_txpad_t
Available values for SERCOM UART TX pad selection.
@ I2C_FLAG_NONE
No flags set.
@ UART_FLAG_NONE
No flags set.
@ GPIO_OUT
configure as output in push-pull mode
@ UART_PAD_RX_0
use pad 0 for RX line
void dma_wait(dma_t dma)
Wait for a DMA channel to finish the transfer.
@ SPI_PAD_MOSI_0_SCK_3
use pad 0 for MOSI, pad 3 for SCK
@ TIMER_TYPE_TCC
Timer is a TCC timer.
uint8_t gclk_src
GCLK source which supplys SERCOM.
@ UART_DATA_BITS_6
6 data bits
void rtc_tamper_enable(void)
Enable Tamper Detection IRQs.
i2c_speed_t
Default mapping of I2C bus speed values.
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
uint8_t chan_numof
number of channels
uart_rxpad_t rx_pad
pad selection for RX line
@ DMA_INCR_NONE
Don't increment any addresses after a beat.
@ UART_FLAG_RXINV
invert RX signal
USB peripheral parameters.
void dma_append_src(dma_t dma, DmacDescriptor *next, const void *src, size_t num, bool incr)
Append a second transfer descriptor after the default channel descriptor, copying destination and blo...
uart_txpad_t tx_pad
pad selection for TX line
uint8_t type
Timer type (TC/TCC)
@ DMA_INCR_SRC
Increment the source address after a beat.
@ GPIO_FALLING
emit interrupt on falling flank
uint8_t gclk_src
GCLK source which supplys 48 MHz
uint8_t gclk_src
GCLK source which supplys SERCOM.
@ SPI_PAD_MISO_1
use pad 0 for MISO line
static void sercom_set_gen(void *sercom, uint8_t gclk)
Configure generator clock for given SERCOM device.
void gpio_init_mux(gpio_t pin, gpio_mux_t mux)
Set up alternate function (PMUX setting) for a PORT pin.
void dma_release_channel(dma_t dma)
Release a previously acquired DMA channel.
SercomI2cm * dev
pointer to the used I2C device
@ GPIO_IN_PU
configure as input with pull-up resistor
@ GPIO_RISING
emit interrupt on rising flank
void dma_init(void)
Initialize DMA.
void gpio_pm_cb_enter(int deep)
Called before the power management enters a power mode.
PWM device configuration.
@ UART_PAD_RX_1
select pad 1
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
@ SPI_PAD_MISO_0
use pad 0 for MISO line
@ GPIO_IN
configure as input without pull resistor
uint8_t flags
allow SERCOM to run in standby mode
spi_clk_t
Available SPI clock speeds.
uint32_t pm_mask
PM_APBCMASK bits to enable Timer.
spi_misopad_t miso_pad
pad to use for MISO line
PWM channel configuration data structure.
uint8_t gclk_src
GCLK source which supplys Timer.
@ DMA_INCR_BOTH
Increment both addresses after a beat.
@ UART_PAD_TX_2
select pad 2
void dma_start(dma_t dma)
Start a DMA transfer.
UART device configuration.
void cpu_pm_cb_enter(int deep)
Called before the power management enters a power mode.
void gpio_disable_mux(gpio_t pin)
Disable alternate function (PMUX setting) for a PORT pin.
gpio_mux_t
Available MUX values for configuring a pin's alternate function.
spi_mosipad_t mosi_pad
pad to use for MOSI and CLK line
@ GPIO_MUX_A
select peripheral function A
gpio_mux_t mux
alternative function for pins
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
void sam0_flashpage_aux_reset(const nvm_user_page_t *cfg)
Reset the configuration area, apply a new configuration.
@ GPIO_MUX_B
select peripheral function B
gpio_mux_t mosi_mux
alternate function for MOSI pin (mux)
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
static bool cpu_woke_from_backup(void)
Returns true if the CPU woke deep sleep (backup/standby)
void cpu_pm_cb_leave(int deep)
Called after the power management left a power mode.
gpio_mux_t miso_mux
alternate function for MISO pin (mux)
uart_rxpad_t
Available values for SERCOM UART RX pad selection.
@ UART_FLAG_RUN_STANDBY
run SERCOM in standby mode
Tc * dev
pointer to the used Timer device
@ GPIO_MUX_C
select peripheral function C
@ DMA_INCR_DEST
Increment destination address after a beat.
void dma_setup(dma_t dma, unsigned trigger, uint8_t prio, bool irq)
Initialize a previously allocated DMA channel with one-time settings.
@ SPI_PAD_MOSI_0_SCK_1
use pad 0 for MOSI, pad 1 for SCK
@ SPI_PAD_MOSI_2_SCK_3
use pad 2 for MOSI, pad 3 for SCK
@ GPIO_BOTH
emit interrupt on both flanks
spi_mosipad_t
Available values for SERCOM SPI MOSI and SCK pad selection.
void dma_prepare(dma_t dma, uint8_t width, const void *src, void *dst, size_t num, dma_incr_t incr)
Prepare the DMA channel for an individual transfer.
gpio_t clk_pin
used CLK pin
uint8_t chan
TCC channel to use.
void sam0_gclk_enable(uint8_t id)
Enables an on-demand GCLK that has been configured in cpu.c.
ADC Channel Configuration.
Timer device configuration.
@ SPI_PAD_MISO_3
use pad 0 for MISO line
@ TIMER_TYPE_TC
Timer is a TC timer
static void cortexm_sleep(int deep)
Put the CPU into (deep) sleep mode, using the WFI instruction.
gpio_mode_t
Available pin modes.
static uint8_t sercom_id(const void *sercom)
Return the numeric id of a SERCOM device derived from its address.
@ GPIO_MUX_D
select peripheral function D
@ UART_DATA_BITS_7
7 data bits
gpio_t pin
ADC channel pin.
@ GPIO_MUX_H
select peripheral function H
gpio_mux_t mux
pin function multiplex value
uart_data_bits_t
Definition of possible data bits lengths in a UART frame.
IRQn_Type irq
IRQ# of Timer Interrupt.
#define GPIO_MODE(pr, ie, pe)
Generate GPIO mode bitfields.
unsigned int gpio_t
GPIO type identifier.
@ SPI_PAD_MOSI_3_SCK_1
use pad 3 for MOSI, pad 1 for SCK
@ SPI_PAD_MISO_2
use pad 0 for MISO line
I2C configuration options.
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
dma_t dma_acquire_channel(void)
Acquire a DMA channel.
static void sercom_clk_en(void *sercom)
Enable peripheral clock for given SERCOM device.
uint16_t flags
flags for CTRA, e.g.
@ GPIO_MUX_G
select peripheral function G
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
static void sercom_clk_dis(void *sercom)
Disable peripheral clock for given SERCOM device.
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
@ UART_PAD_RX_2
select pad 2
void dma_prepare_src(dma_t dma, const void *src, size_t num, bool incr)
Prepare a transfer without modifying the destination address settings.
const pwm_conf_chan_t * chan
channel configuration
uint32_t pm_mask
PM_APBCMASK bits to enable Timer.
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
SPI configuration structure type.
@ SPI_MODE_1
CPOL=0, CPHA=1.
UsbDevice * device
ptr to the device registers
i2c_flag_t
Available SERCOM I2C flag selections.
gpio_mux_t mux
alternate function (mux)
@ SPI_MODE_2
CPOL=1, CPHA=0.
tc_tcc_cfg_t tim
timer configuration