Linux bootloader阶段GPIO的控制
1. Bootloader阶段
(1) gpio_tlmm_config()GPIO_CFGn寄存器的配置。
#define TLMM_VOL_UP_BTN_GPIO 85
gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0,GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE)
gpio_tlmm_config()定义如下:
void gpio_tlmm_config(uint32_t gpio,uint8_t func,
uint8_tdir, uint8_t pull,
uint8_tdrvstr, uint32_t enable)
{
uint32_tval = 0;
val|= pull;
val|= func << 2;
val|= drvstr << 6;
val|= enable << 9;//如果func=0,enable=1为输出,enable=0为输出
writel(val,(uint32_t *)GPIO_CONFIG_ADDR(gpio));
return;
}
Gpio:要控制的GPIO编号,比如85,表示GPIO85
Func:表示IO口的功能,可参考xxx_GPIO_Config_Spreadsheet.xlsm文件,如下图:
Primary function对应0,Alternate Function 1为1,以此类推。
Dir:配置方向,输入或是输出
/* GPIO TLMM: Direction */
#define GPIO_INPUT 0
#define GPIO_OUTPUT 1
Pull:GPIO上下拉配置
/* GPIO TLMM: Pullup/Pulldown */
#define GPIO_NO_PULL 0
#define GPIO_PULL_DOWN 1
#define GPIO_KEEPER 2
#define GPIO_PULL_UP 3
Drvstr:驱动强度,
/* GPIO TLMM: Drive Strength */
#define GPIO_2MA 0
#define GPIO_4MA 1
#define GPIO_6MA 2
#define GPIO_8MA 3
#define GPIO_10MA 4
#define GPIO_12MA 5
#define GPIO_14MA 6
#define GPIO_16MA 7
对应的GPIO_CFGn寄存器描述如下
When the FUNC_SEL of a GPIO is set to 0,the GPIO is used as a general purpose pin, and it can be used as either aninput or output. This is controlled by the GPIO_OE bit of the GPIO_CFGnregister. When this bit is set to 1, it is an output pin. If the bit is set to0, it is an input pin.
如果配置为普通的GPIO,GPIO_OE=1设置为输出,GPIO_OE=0设置为输入。所以实际是enable参数控制输出输入,dir参数实际没有用到。
(2) gpio_set_dir()输出高低电平
void gpio_set_dir(uint32_t gpio, uint32_tdir)
{
writel(dir,(uint32_t *)GPIO_IN_OUT_ADDR(gpio));
return;
}
实例:
gpio_set_dir(38, 2);//输出高电平
gpio_set_dir(38, 0);//输出低电平
为什么第2个参数2是对应高电平,而不是1是低电平呢?看GPIO_IN_OUT寄存器的描述:
由GPIO_OUT控制输出,2相当于二进制10,也就是GPIO_OUT=1。
(3) gpio_status()获取输入的值
uint32_t gpio_status(uint32_t gpio)
{
returnreadl(GPIO_IN_OUT_ADDR(gpio)) & GPIO_IN;
}
示例:
/* Get status of GPIO */
status= gpio_status(TLMM_VOL_DOWN_BTN_GPIO);