free-dap/platform/samd21/hal_gpio.h

102 lines
3.7 KiB
C
Raw Normal View History

2024-01-13 08:26:41 +08:00
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2014-2016, Alex Taradov <alex@taradov.com>. All rights reserved.
2021-05-14 10:36:00 +08:00
#ifndef _HAL_GPIO_H_
#define _HAL_GPIO_H_
/*- Definitions -------------------------------------------------------------*/
#define HAL_GPIO_PORTA 0
#define HAL_GPIO_PORTB 1
#define HAL_GPIO_PORTC 2
#define HAL_GPIO_PMUX_A 0
#define HAL_GPIO_PMUX_B 1
#define HAL_GPIO_PMUX_C 2
#define HAL_GPIO_PMUX_D 3
#define HAL_GPIO_PMUX_E 4
#define HAL_GPIO_PMUX_F 5
#define HAL_GPIO_PMUX_G 6
#define HAL_GPIO_PMUX_H 7
#define HAL_GPIO_PMUX_I 8
#define HAL_GPIO_PIN(name, port, pin) \
static inline void HAL_GPIO_##name##_set(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_set; \
} \
\
static inline void HAL_GPIO_##name##_clr(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_clr; \
} \
\
static inline void HAL_GPIO_##name##_toggle(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_toggle; \
} \
\
static inline void HAL_GPIO_##name##_write(int value) \
{ \
if (value) \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
2021-05-14 10:36:00 +08:00
else \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_write; \
} \
\
static inline void HAL_GPIO_##name##_in(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN;\
PORT_IOBUS->Group[HAL_GPIO_PORT##port].CTRL.reg |= (1 << pin); \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_in; \
} \
\
static inline void HAL_GPIO_##name##_out(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_out; \
} \
\
static inline void HAL_GPIO_##name##_pullup(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_pullup; \
} \
\
static inline int HAL_GPIO_##name##_read(void) \
{ \
2024-01-13 08:26:41 +08:00
return (PORT_IOBUS->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_read; \
} \
\
static inline int HAL_GPIO_##name##_state(void) \
{ \
2024-01-13 08:26:41 +08:00
return (PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_state; \
} \
\
static inline void HAL_GPIO_##name##_pmuxen(int mux) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \
2021-05-14 10:36:00 +08:00
if (pin & 1) \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \
2021-05-14 10:36:00 +08:00
else \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_pmuxen; \
} \
\
static inline void HAL_GPIO_##name##_pmuxdis(void) \
{ \
2024-01-13 08:26:41 +08:00
PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \
2021-05-14 10:36:00 +08:00
(void)HAL_GPIO_##name##_pmuxdis; \
} \
#endif // _HAL_GPIO_H_