drv_gpio.h 2.2 KB
Newer Older
Bruceoxl's avatar
Bruceoxl 已提交
1
/*
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
Bruceoxl's avatar
Bruceoxl 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2021-08-20     BruceOu           the first version
 */

#ifndef __DRV_GPIO_H__
#define __DRV_GPIO_H__

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#ifdef __cplusplus
extern "C" {
#endif

22 23 24 25 26 27 28 29 30
#if defined SOC_SERIES_GD32F10x
#include "gd32f10x_gpio.h"
#elif defined SOC_SERIES_GD32F20x
#include "gd32f20x_gpio.h"
#elif defined SOC_SERIES_GD32F30x
#include "gd32f30x_gpio.h"
#elif defined SOC_SERIES_GD32F4xx
#include "gd32f4xx_gpio.h"
#endif
Bruceoxl's avatar
Bruceoxl 已提交
31

32
#define __GD32_PORT(port)  GPIO##port
Bruceoxl's avatar
Bruceoxl 已提交
33

34
#if defined SOC_SERIES_GD32F4xx
Bruceoxl's avatar
Bruceoxl 已提交
35 36 37 38
#define GD32_PIN(index, port, pin) {index, RCU_GPIO##port,      \
                                    GPIO##port, GPIO_PIN_##pin, \
                                    EXTI_SOURCE_GPIO##port,     \
                                    EXTI_SOURCE_PIN##pin}
39 40
#else
#define GD32_PIN(index, port, pin) {index, RCU_GPIO##port,        \
41
                                    GPIO##port, GPIO_PIN_##pin,   \
42 43 44 45 46
                                    GPIO_PORT_SOURCE_GPIO##port,  \
                                    GPIO_PIN_SOURCE_##pin}

#endif

Bruceoxl's avatar
Bruceoxl 已提交
47 48
#define GD32_PIN_DEFAULT            {-1, (rcu_periph_enum)0, 0, 0, 0, 0}

49
#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__GD32_PORT(PORTx) - (rt_base_t)GPIO_BASE)/(0x0400UL) )) + PIN)
Bruceoxl's avatar
Bruceoxl 已提交
50

W
wang0huan 已提交
51 52 53
#define PIN_NUM(port, no)       (((((port)&0xFu) << 4) | ((no)&0xFu)))
#define PIN_PORT(pin)           ((uint8_t)(((pin) >> 4) & 0xFu))
#define PIN_NO(pin)             ((uint8_t)((pin)&0xFu))
54

W
wang0huan 已提交
55 56 57
#define PIN_GDPORT(pin) (GPIO_BASE + (0x400u * PIN_PORT(pin))) /* gpio_periph GPIOA~GPIOG */
#define PIN_GDPIN(pin)  ((uint16_t)(1u << PIN_NO(pin)))        /* GPIO_PIN_0~GPIO_PIN_15 */
#define PIN_GDRCU(pin)  RCU_REGIDX_BIT(APB2EN_REG_OFFSET, PIN_PORT(pin) + 2)    /* pin gpio外设时钟 */
58

Bruceoxl's avatar
Bruceoxl 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
struct pin_index
{
    rt_int16_t index;
    rcu_periph_enum clk;
    rt_uint32_t gpio_periph;
    rt_uint32_t pin;
    rt_uint8_t port_src;
    rt_uint8_t pin_src;
};

struct pin_irq_map
{
    rt_uint16_t pinbit;
    IRQn_Type irqno;
};

#ifdef __cplusplus
}
#endif

#endif /* __DRV_GPIO_H__ */