25 #include "periph_cpu.h"
35 # define GPIO_APB_BASE PORT_SEC
37 # define GPIO_APB_BASE PORT
41 #if defined(PORT_IOBUS_SEC)
42 # define GPIO_IOBUS_BASE PORT_IOBUS_SEC
43 #elif defined(PORT_IOBUS)
44 # define GPIO_IOBUS_BASE PORT_IOBUS
46 # define GPIO_IOBUS_BASE GPIO_APB_BASE
49 #define GPIO_PORT_NUMBERING_ALPHABETIC 1
52 # define GPIO_PORT_0 ((uintptr_t)&GPIO_IOBUS_BASE->Group[0])
55 # define GPIO_PORT_1 ((uintptr_t)&GPIO_IOBUS_BASE->Group[1])
58 # define GPIO_PORT_2 ((uintptr_t)&GPIO_IOBUS_BASE->Group[2])
61 # define GPIO_PORT_3 ((uintptr_t)&GPIO_IOBUS_BASE->Group[3])
64 # define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
67 # define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
70 # define GPIO_PORT_5 ((uintptr_t)&GPIO_IOBUS_BASE->Group[5])
73 # define GPIO_PORT_6 ((uintptr_t)&GPIO_IOBUS_BASE->Group[6])
76 # define GPIO_PORT_7 ((uintptr_t)&GPIO_IOBUS_BASE->Group[7])
82 #define GPIO_PORT(num) ((uintptr_t)&GPIO_IOBUS_BASE->Group[(num)])
87 #define GPIO_PORT_NUM(port) \
88 (((port) - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]))
92 return (uintptr_t)&GPIO_IOBUS_BASE->Group[num];
97 return (port - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) /
sizeof(GPIO_IOBUS_BASE->Group[0]);
100 static inline PortGroup *sam0_gpio_iobus2ap(PortGroup *iobus)
102 const uintptr_t iobus_base = (uintptr_t)GPIO_IOBUS_BASE;
103 const uintptr_t apb_base = (uintptr_t)GPIO_APB_BASE;
105 return (PortGroup *)((uintptr_t)iobus - (iobus_base - apb_base));
110 PortGroup *p = (PortGroup *)port;
111 if (!
IS_USED(MODULE_PERIPH_GPIO_FAST_READ)) {
112 p = sam0_gpio_iobus2ap(p);
119 PortGroup *p = (PortGroup *)port;
125 PortGroup *p = (PortGroup *)port;
126 p->OUTSET.reg = mask;
131 PortGroup *p = (PortGroup *)port;
132 p->OUTCLR.reg = mask;
137 PortGroup *p = (PortGroup *)port;
138 p->OUTTGL.reg = mask;
143 PortGroup *p = (PortGroup *)port;
149 PortGroup *p = (PortGroup *)port;
150 p->DIRSET.reg = outputs;
155 PortGroup *p = (PortGroup *)port;
156 p->DIRCLR.reg = inputs;
166 const uintptr_t gpio_legacy_base =
GPIO_PIN(0, 0) & ~(0x1f);
167 uintptr_t addr = (pin & ~(0x1f));
169 return addr + (gpio_ll_base - gpio_legacy_base);
184 if (port < GPIO_PORT(0)) {
187 if (port > GPIO_PORT(
ARRAY_SIZE(GPIO_IOBUS_BASE->Group))) {
196 return (num <
ARRAY_SIZE(GPIO_IOBUS_BASE->Group));
Platform-independent access to architecture details.
#define GPIO_PIN(x, y)
Define a CPU specific GPIO pin generator macro.
#define ARRAY_SIZE(a)
Calculate the number of elements in a static array.
#define GPIO_PORT_0
Get the gpio_port_t value of the port labeled 0.
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
static void gpio_ll_set(gpio_port_t port, uword_t mask)
Perform an reg |= mask operation on the I/O register of the port.
gpio_port_t gpio_port(uword_t num)
Get the gpio_port_t value of the port number num.
static gpio_port_t gpio_port_pack_addr(void *addr)
Pack a pointer into a gpio_port_t.
static void gpio_ll_switch_dir_output(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to outputs.
static void gpio_ll_switch_dir_input(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to inputs.
static uword_t gpio_ll_read(gpio_port_t port)
Get the current input value of all GPIO pins of the given port as bitmask.
static gpio_port_t gpio_get_port(gpio_t pin)
Extract the gpio_port_t from a gpio_t
uword_t gpio_port_num(gpio_port_t port)
Get the number of the GPIO port port refers to.
static bool is_gpio_port_num_valid(uint_fast8_t num)
Check if the given number is a valid argument for gpio_port.
static uword_t gpio_ll_read_output(gpio_port_t port)
Get the current output value of all GPIO pins of the given port as bitmask.
static void gpio_ll_clear(gpio_port_t port, uword_t mask)
Perform an reg &= ~mask operation on the I/O register of the port.
static void gpio_ll_toggle(gpio_port_t port, uword_t mask)
Perform an reg ^= mask operation on the I/O register of the port.
static void gpio_ll_write(gpio_port_t port, uword_t state)
Perform a masked write operation on the I/O register of the port.
uintptr_t gpio_port_t
GPIO port type.
uint< NUM > _t uword_t
Word sized unsigned integer.
#define IS_USED(module)
Checks whether a module is being used or not.