GPIO
概述
GPIO(General Purpose Input Ouput,通用输入输出)由引脚/功能寄存器构成,用于控制与其相连接的外设。
功能特性
每个 GPIO 由独立寄存器控制
输出模式,支持开漏输出 0/1
输入模式,可以产生电平和上下边沿中断
支持三种驱动能力
可以同时作为输入和输出
包含普通 GPIO 和 AON-GPIO
支持电平值唤醒芯片
功能说明
OD 模式
当用 OD 模式输出 0 时,需要设置上拉,当用 OD 模式输出 1 时,需要设置下拉。
GPIO 编号及功能
GPIOID |
复用功能 |
电压 |
类型 |
冷启动状态 |
睡眠默认状态 |
热启动状态 |
|---|---|---|---|---|---|---|
00 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
01 |
UART_TXD |
1.8v |
I/O |
PU |
KEEP |
KEEP |
02 |
UART_RXD |
1.8v |
I/O |
PU |
KEEP |
KEEP |
03 |
/ |
1.8v |
I/O |
PU |
KEEP |
KEEP |
04 |
/ |
1.8v |
I/O |
PU |
KEEP |
KEEP |
05 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
06 |
/ |
1.8v/3.3v |
I/O |
PU |
KEEP |
KEEP |
07 |
/ |
1.8v/3.3v |
I/O |
PU |
KEEP |
KEEP |
08 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
09 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
10 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
11 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
12 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
13 |
/ |
1.8v |
I/O |
PD |
KEEP |
KEEP |
21 |
/ |
1.8v |
I/O |
PU |
KEEP |
KEEP |
83 |
GPIO_A00 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
84 |
GPIO_A01 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
85 |
GPIO_A02 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
86 |
GPIO_A03 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
87 |
GPIO_A04 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
88 |
GPIO_A09 |
1.8v |
I |
PU |
KEEP |
KEEP |
89 |
GPIO_A10 |
1.8v |
I |
PU |
KEEP |
KEEP |
90 |
GPIO_A11 |
1.8v |
I |
PU |
KEEP |
KEEP |
91 |
GPIO_A12 |
1.8v |
I |
PU |
KEEP |
KEEP |
92 |
GPIO_A13 |
1.8v |
I/O |
PD |
KEEP |
KEEP |
93 |
GPIO_A14 |
1.8v |
I/O |
PU |
KEEP |
KEEP |
94 |
GPIO_A15/CHARGER |
3.3v |
I/O |
/ |
CHARGER |
CHARGER |
95 |
ADC0 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
96 |
ADC1 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
97 |
ADC2 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
98 |
ADC3 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
99 |
ADC4 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
100 |
ADC5 |
1.8v |
I/O/AI |
PN |
KEEP |
KEEP |
状态说明
状态
说明
复位
出现致命软件错误或入仓充电时将产生复位
上电
芯片由掉电状态首次上电
冷启动状态
芯片上电到软件运行前io的状态
热启动状态
关机后再次开机芯片到软件运行前io的状态
睡眠
运行中的低功耗状态会反复在睡眠默认状态和软件配置状态直接切换
关机
仅保留唤醒模块运行的状态,此时所有IO进入睡眠默认状态
KEEP
指在进入睡眠状态后可保持睡眠前的状态
PU
PULLUP上拉
PD
PULLDOWN下拉
PN
PULLNONE无上下拉,且非高阻态,此状态会漏电
配置说明
通用数字 IO:
可用于通用外设的 IO,如UART/I2C/SPI
用作普通 GPIO 时可使用硬件去抖(debounce)模块
可复用为 UART 的 IO:默认烧录接口,上电后默认为烧录用 uart
可复用为 BOOT_MODE 的 IO: 若为高电平,则允许进入烧录模式(等待1S),否则直接启动
可复用为 AON 的 IO:
作为普通 GPIO 使用时可用于通用外设的IO,如UART,I2C,SPI
作为 AON_GPIO 使用时,具有低功耗及关机状态的唤醒能力
可复用为 TK 的 IO:
数字模式仅支持GPI(输入)
可配置为 cap touch 输入,可以用作触摸及入耳功能
电压可切换的 IO:
普通数字 IO,将 VDD33/18_IO_TOP 引脚与 VDD33_AON/VDD18_AON 引脚相连,即可切换对应的电压
可复用为 AON 的 IO,将 VDD33/18_IO_AON 引脚与 VDD33_AON/VDD18_AON 引脚相连,即可切换对应的电压
用法流程
初始化
wq_gpio_init()开关中断
wq_gpio_int_enable()、wq_gpio_int_disable()设置上下拉
wq_gpio_set_pull_mode()配置并打开
wq_gpio_open()、wq_gpio_open_as_interrupt()读写
wq_gpio_read()、wq_gpio_write()关闭
wq_gpio_close()注销
wq_gpio_deinit()
API 介绍
This section introduces the GPIO module's enum, structure, functions and how to use this driver.
Typedefs
-
typedef struct gpio_out_config gpio_out_config_t
GPIO output configuration
-
typedef struct gpio_in_config gpio_in_config_t
GPIO input configuration
-
typedef void (*wq_gpio_int_callback)(WQ_GPIO_ID gpio, WQ_GPIO_INT_MODE mode)
GPIO interrupt callback.
Enums
-
enum WQ_GPIO_DIRECTION
GPIO direction.
Values:
-
enumerator WQ_GPIO_DIRECTION_INPUT
-
enumerator WQ_GPIO_DIRECTION_OUTPUT
-
enumerator WQ_GPIO_DIRECTION_INPUT
-
enum WQ_GPIO_INT_MODE
GPIO Modes of interrupt. Only when gpio_mode set as GPIO_INTERRUPT,int_mode is available.
Values:
-
enumerator WQ_GPIO_INT_DISABLE
Disable the interrupt. Interrupt triggered when switchs from LOW to HIGH.
-
enumerator WQ_GPIO_INT_EDGE_RAISING
Interrupt triggered when switchs from HIGH to LOW.
-
enumerator WQ_GPIO_INT_EDGE_FALLING
Interrupt triggered when switchs to HIGH or LOW .
-
enumerator WQ_GPIO_INT_EDGE_BOTH
Interrupt triggered when stays in LOW.
-
enumerator WQ_GPIO_INT_LEVEL_LOW
Interrupt triggered when stays in HIGH.
-
enumerator WQ_GPIO_INT_LEVEL_HIGH
Invalid value
-
enumerator WQ_GPIO_INT_MODE_MAX
-
enumerator WQ_GPIO_INT_DISABLE
Functions
-
void wq_gpio_init(void)
This function is to initialise the gpio driver module.
-
void wq_gpio_deinit(void)
This function is to de-initialise the GPIO module.
-
WQ_RET wq_gpio_open(WQ_GPIO_ID gpio, WQ_GPIO_DIRECTION dir)
This function is to open a GPIO for either output or input.
- 参数:
gpio -- is the gpio to use.
dir -- is input or output direction.
- 返回:
WQ_RET WQ_RET_OK for success else error.
-
WQ_RET wq_gpio_open_as_interrupt(WQ_GPIO_ID gpio, WQ_GPIO_INT_MODE mode, wq_gpio_int_callback cb)
This function is to open gpio and let it as interrupt.
- 参数:
gpio -- is the gpio to use.
mode -- is the gpio interrupt mode.
cb -- is gpio callback interrupt function.
- 返回:
WQ_RET WQ_RET_OK for success else error.
-
WQ_RET wq_gpio_int_enable(WQ_GPIO_ID gpio)
This function is to enable the gpio interrupt.
- 参数:
gpio -- is the gpio to use.
-
WQ_RET wq_gpio_int_disable(WQ_GPIO_ID gpio)
This function is to disable the gpio interrupt.
- 参数:
gpio -- is the gpio to use.
-
WQ_RET wq_gpio_close(WQ_GPIO_ID gpio)
This function is to close a GPIO previously opened.
- 参数:
gpio -- is the gpio to use.
-
WQ_RET wq_gpio_write(WQ_GPIO_ID gpio, uint8_t value)
This function is to write data through gpio.
- 参数:
gpio -- is the gpio to use.
value -- is the data to write.
-
uint8_t wq_gpio_read(WQ_GPIO_ID gpio)
This function is to read data through gpio.
- 参数:
gpio -- is the gpio to use.
- 返回:
uint8_t is the readed data.
-
WQ_RET wq_gpio_toggle(WQ_GPIO_ID gpio)
This function is to toggle the gpio.
- 参数:
gpio -- is the gpio to use.
-
WQ_RET wq_gpio_set_pull_mode(WQ_GPIO_ID gpio, WQ_GPIO_PULL_MODE mode)
This function is to set gpio pull mode.
- 参数:
gpio -- is the gpio to use.
mode -- is the gpio pull mode.
-
WQ_RET wq_gpio_wakeup_enable(WQ_GPIO_ID gpio)
Enable gpio wakeup chip at deepsleep or shutdown mode.
参见
备注
Only support AON gpio, need config gpio as interrupt first, and enable GPIO wakeup source
备注
Only support LEVEL interrupt wakeup
- 参数:
gpio -- [in] AON gpio num
- 返回:
WQ_RET
-
WQ_RET wq_gpio_wakeup_disable(WQ_GPIO_ID gpio)
Disable gpio wakeup chip at deepsleep or shutdown mode.
参见
备注
Only support AON gpio
- 参数:
gpio -- [in] AON gpio num
- 返回:
WQ_RET
-
void wq_gpio_get_wakeup_source(WQ_GPIO_ID *gpio, uint8_t *level)
This function is to get gpio wakeup source's information.
- 参数:
gpio -- [out] is wakeup gpio number;
level -- [out] is wakeup gpio's low/high state.
-
WQ_RET wq_gpio_set_drive(WQ_GPIO_ID gpio, WQ_GPIO_DRIVE_MODE drv)
Set GPIO drive capability.
- 参数:
gpio -- The gpio number
drv -- Drive mode
- 返回:
WQ_RET WQ_RET_OK for success otherwise fail
-
struct gpio_out_config
- #include <wq_gpio.h>
GPIO output configuration
Public Members
-
WQ_GPIO_PULL_MODE pull
-
WQ_GPIO_PULL_MODE pull
-
struct gpio_in_config
- #include <wq_gpio.h>
GPIO input configuration
Public Members
-
WQ_GPIO_PULL_MODE pull
-
WQ_GPIO_PULL_MODE pull