RK 平台 gpio 使用说明
Gpio 的使用方法:分复用管脚和非复用管脚,复用管脚就是有多种功能的管脚,需要通过
iomux 设置成 gpio 功能。
// 如果是复用管脚,则首先要设置一下 iomux ,不是复用管脚无需设置 iomux
rk30_mux_api_set(char *name, unsigned int mode) ;//
\arch\arm\mach-rk30\include\mach\iomux.h 中可以查 name 和 mode
ret = gpio_request(RK30_PINx_Pxx, NULL);
if(ret != 0)
{
gpio_free(RK30_PINx_Pxx);
printk(">>>>>>RK30_PINx_Pxx gpio_request err \n ");
} ( a )如果输入:
gpio_direction_input(RK30_PINx_Pxx);
/******************* 以下是设置成中断的代码 ****************************
irq = gpio_to_irq(pdata->gpio);
if (irq < 0) {
gpio_free(RK29_PINx_Pxx);
//todo other
}
error = request_irq(irq, isr_call_name, IRQF_TRIGGER_FALLING , "name", data); //
IRQF_TRIGGER_RISING
if (error) {
gpio_free(RK30_PINx_Pxx);
//todo other
}
/*********************************** 设置中断结束 ***********************
( b )或者设置输出
gpio_direction_output(RK30_PINx_Pxx, 0); // 设置 gpio 为输出并且初始为低, 0 就是低, 1 就
是高
gpio_set_value(RK30_PINx_Pxx , 1); // 设置 gpio 的电平 0 就是低, 1 就是高
示例 1 :复用 io 口设置成输出
rk30_mux_api_set(GPIO3A7_SDMMC0PWREN_NAME, GPIO3A_GPIO3A7);
error = gpio_request(RK30_PIN3_PA7, "sdmmc-power");
if (error < 0) {
printk("failed to request RK30_PIN6_PA1, error= %d\n", error);
//goto fail;
}
error = gpio_direction_output(RK30_PIN3_PA7, GPIO_LOW);
if (error < 0) {
printk("failed to configure input direction for GPIO, error %d\n",error);
gpio_free(RK30_PIN6_PA1);
//goto fail; }
gpio_set_value(RK30_PIN3_PA7,GPIO_LOW); //GPIO_HIGH
示例 2 :非复用 io 口设置成输入
//sample gpio input
error = gpio_request(RK30_PIN6_PA1, "irq_gpio_test");
if (error < 0) {
printk("failed to request RK30_PIN6_PA1, error= %d\n", error);
//goto fail;
}
error = gpio_direction_input(RK30_PIN6_PA1);
if (error < 0) {
printk("failed to configure input direction for GPIO, error %d\n",error);
gpio_free(RK30_PIN6_PA1);
//goto fail;
}
value=gpio_get_value(RK30_PIN6_PA1);
示例 3 :非复用 io 口设置成中断输入
irqreturn_t irq_sample_call(int irq, void *dev_id)
{
//todo
}
//sample gpio irq
error = gpio_request(RK30_PIN6_PA1, "irq_gpio_test");
if (error < 0) {
printk("failed to request RK30_PIN6_PA1, error= %d\n", error);
//goto fail;
}
error = gpio_direction_input(RK30_PIN6_PA1);
if (error < 0) {
printk("failed to configure input direction for GPIO, error %d\n",error);
gpio_free(RK30_PIN6_PA1);
//goto fail;
}
irq = gpio_to_irq(RK30_PIN6_PA1);
if (irq < 0) {
error = irq;
printk("Unable to get irq number for GPIO, error %d\n", error);
gpio_free(RK30_PIN6_PA1);
//goto fail;
}
error = request_irq(irq, irq_sample_call, IRQF_TRIGGER_FALLING , "irq_sample",
dev_id); //or IRQF_TRIGGER_RISING
if (error) {
printk("Unable to claim irq %d; error %d\n", irq, error);
gpio_free(pdata->gpio);
//goto fail1;
}