Linux中的GPIO操作的一些函数:
要使用gpio,首先要使用这个函数来分配一个gpio
int gpio_request(unsigned gpio, const char *label)
指定输入还是输入模式
int gpio_direction_input(unsigned gpio);
这个函数能设置gpio的输出模式为输入
int gpio_direction_output(unsigned gpio, int value)
这个函数能设置gpio的控制寄存器为输出模式,并且输出value(0-低电平 1-高电平)
gpio_set_value则是一个宏,它只负责让指定的gpio输出高低电平,但是不担保gpio的控制器是输出模式
#define gpio_set_value __gpio_set_value /** * __gpio_set_value() - assign a gpio's value * @gpio: gpio whose value will be assigned * @value: value to assign * Context: any * * This is used directly or indirectly to implement gpio_set_value(). * It invokes the associated gpio_chip.set() method. */ void __gpio_set_value(unsigned gpio, int value) { struct gpio_chip *chip; chip = gpio_to_chip(gpio); WARN_ON(chip->can_sleep); trace_gpio_value(gpio, 0, value); chip->set(chip, gpio - chip->base, value); } EXPORT_SYMBOL_GPL(__gpio_set_value);
linux/arch/arm/plat-s3c/include/plat/gpio-cfg.h这个里面给我们提供了很多S3C平台的GPIO操作接口,我们来分析分析
第一部分:关于控制寄存器的设置函数
1.s3c_gpio_cfgpin()设置pin的功能
/** * s3c_gpio_cfgpin() - Change the GPIO function of a pin. * @pin pin The pin number to configure. * @to to The configuration for the pin's function. * * Configure which function is actually connected to the external * pin, such as an gpio input, output or some form of special function * connected to an internal peripheral block. * * The @to parameter can be one of the generic S3C_GPIO_INPUT, S3C_GPIO_OUTPUT * or S3C_GPIO_SFN() to indicate one of the possible values that the helper * will then generate the correct bit mask and shift for the configuration. * * If a bank of GPIOs all needs to be set to special-function 2, then * the following code will work: * * for (gpio = start; gpio < end; gpio++) * s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN(2)); * * The @to parameter can also be a specific value already shifted to the * correct position in the control register, although these are discouraged * in newer kernels and are only being kept for compatibility. */ extern int s3c_gpio_cfgpin(unsigned int pin, unsigned int to);
功能:设置GPIO的一个引脚的功能。
参数:@pin:要设置的引脚号
@to: 要设置成什么功能,有三个选择,如下
/* Defines for generic pin configurations */ #define S3C_GPIO_INPUT (S3C_GPIO_SPECIAL(0)) //设置成输出模式 #define S3C_GPIO_OUTPUT (S3C_GPIO_SPECIAL(1)) //设置成输入模式 #define S3C_GPIO_SFN(x) (S3C_GPIO_SPECIAL(x)) //设置成其他模式,根据参数x决定
其实前两种都可以使用第三种宏来实现,
设置成输出模式:S3C_GPIO_SFN(0)
设置成输入模式:S3C_GPIO_SFN(1)
它还举个例子,如果一组GPIO都需要设置成相同的功能,比如功能2,可以使用下面的代码
for (gpio = start; gpio < end; gpio++) s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN(2));
to这个参数也可以自己用掩码方式实现
#define S3C_GPIO_SPECIAL_MARK (0xfffffff0) #define S3C_GPIO_SPECIAL(x) (S3C_GPIO_SPECIAL_MARK | (x))
可以看到,实际那三个宏也是通过掩码方式实现的,当然我们也可以自己用位移,屏蔽等方法实现,但是因为兼容性的问题,所以不鼓励那样做。
2.s3c_gpio_getcfg()获得pin的设置值
/** * s3c_gpio_getcfg - Read the current function for a GPIO pin * @pin: The pin to read the configuration value for. * * Read the configuration state of the given @pin, returning a value that * could be passed back to s3c_gpio_cfgpin(). * * @sa s3c_gpio_cfgpin */ extern unsigned s3c_gpio_getcfg(unsigned int pin);
功能:读取一个pin的当前功能值
参数:@pin:要读取设置的pin
很好理解,有设置的,就要有读取的,有写就有读。
3.s3c_gpio_cfgin_range()设置一个范围內的引脚设置
/** * s3c_gpio_cfgpin_range() - Change the GPIO function for configuring pin range * @start: The pin number to start at * @nr: The number of pins to configure from @start. * @cfg: The configuration for the pin's function * * Call s3c_gpio_cfgpin() for the @nr pins starting at @start. * * @sa s3c_gpio_cfgpin. */ extern int s3c_gpio_cfgpin_range(unsigned int start, unsigned int nr, unsigned int cfg);
功能:设置一个范围內的所有引脚设置
参数:@start:起始引脚
@nr: 设置的引脚数量,从其实引脚开始,一共要设置几个引脚的数量
@cfg: 要设置的引脚功能
还记得我们刚才那个例子吗,设置一组GPIO的功能,看看实现就知道了,就是那个s3c_gpio_cfgpin的封装
int s3c_gpio_cfgpin_range(unsigned int start, unsigned int nr, unsigned int cfg) { int ret; for (; nr > 0; nr--, start++) { ret = s3c_gpio_cfgpin(start, cfg); if (ret != 0) return ret; } return 0; } EXPORT_SYMBOL_GPL(s3c_gpio_cfgpin_range);
第二部分:关于上下拉设置的寄存器设置
下面几个宏上下拉设置寄存器的几个状态值
/* Define values for the pull-{up,down} available for each gpio pin. * * These values control the state of the weak pull-{up,down} resistors * available on most pins on the S3C series. Not all chips support both * up or down settings, and it may be dependent on the chip that is being * used to whether the particular mode is available. */ #define S3C_GPIO_PULL_NONE ((__force s3c_gpio_pull_t)0x00) //关闭,上拉下拉都关闭 #define S3C_GPIO_PULL_DOWN ((__force s3c_gpio_pull_t)0x01) //下拉 #define S3C_GPIO_PULL_UP ((__force s3c_gpio_pull_t)0x02) //上拉
1.s3c_gpio_setpull()设置GPIO引脚的上下拉使能
/** * s3c_gpio_setpull() - set the state of a gpio pin pull resistor * @pin: The pin number to configure the pull resistor. * @pull: The configuration for the pull resistor. * * This function sets the state of the pull-{up,down} resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested pull setting. * * @pull is one of S3C_GPIO_PULL_NONE, S3C_GPIO_PULL_DOWN or S3C_GPIO_PULL_UP. */ extern int s3c_gpio_setpull(unsigned int pin, s3c_gpio_pull_t pull);
功能:设置GPIO引脚的上下拉使能状态
参数:@pin:要设置的引脚
@pull:设置的状态,值就是上面三个宏之一
2.s3c_gpio_getpull()读取指定GPIO的上下拉状态
/** * s3c_gpio_getpull() - get the pull resistor state of a gpio pin * @pin: The pin number to get the settings for * * Read the pull resistor value for the specified pin. */ extern s3c_gpio_pull_t s3c_gpio_getpull(unsigned int pin);
功能:读取指定GPIO的上下拉状态
参数:@pin:要读取的引脚
3.s3c_gpio_cfgall()设置一组GPIO引脚的功能和上下拉状态
/** * s3c_gpio_cfgall_range() - configure range of gpio functtion and pull. * @start: The gpio number to start at. * @nr: The number of gpio to configure from @start. * @cfg: The configuration to use * @pull: The pull setting to use. * * Run s3c_gpio_cfgpin() and s3c_gpio_setpull() over the gpio range starting * @gpio and running for @size. * * @sa s3c_gpio_cfgpin * @sa s3c_gpio_setpull * @sa s3c_gpio_cfgpin_range */ extern int s3c_gpio_cfgall_range(unsigned int start, unsigned int nr, unsigned int cfg, s3c_gpio_pull_t pull);
功能:设置一组GPIO引脚的功能和上下拉状态
参数:@start:要设置的GPIO起始引脚
@nr:要设置的引脚范围,从其实引脚算起
@cfg:要设置成什么功能
@pull:要设置成什么状态(上下拉)
这个应该是s3c_set_cfgpin_range和s3c_gpio_setpull()的混合体,我们看看它的实现
int s3c_gpio_cfgall_range(unsigned int start, unsigned int nr, unsigned int cfg, s3c_gpio_pull_t pull) { int ret; for (; nr > 0; nr--, start++) { s3c_gpio_setpull(start, pull); ret = s3c_gpio_cfgpin(start, cfg); if (ret != 0) return ret; } return 0; } EXPORT_SYMBOL_GPL(s3c_gpio_cfgall_range);
哦,是s3c_gpio_setpull()和s3c_gpio_setpull()的混合体
第三部分:关于驱动能力寄存器
/* Define values for the drvstr available for each gpio pin. * * These values control the value of the output signal driver strength, * configurable on most pins on the S5P series. */ #define S5P_GPIO_DRVSTR_LV1 ((__force s5p_gpio_drvstr_t)0x0) #define S5P_GPIO_DRVSTR_LV2 ((__force s5p_gpio_drvstr_t)0x2) #define S5P_GPIO_DRVSTR_LV3 ((__force s5p_gpio_drvstr_t)0x1) #define S5P_GPIO_DRVSTR_LV4 ((__force s5p_gpio_drvstr_t)0x3)
1.s5p_gpio_get_drvstr设置GPIO引脚的驱动能力寄存器
/** * s3c_gpio_set_drvstr() - set the driver streght value of a gpio pin * @pin: The pin number to configure the driver streght value * @drvstr: The new value of the driver strength * * This function sets the driver strength value for the specified pin. * It will return 0 if successful, or a negative error code if the pin * cannot support the requested setting. */ extern int s5p_gpio_set_drvstr(unsigned int pin, s5p_gpio_drvstr_t drvstr);
功能:设置GPIO引脚的驱动能力寄存器
参数:@pin:要设置的引脚
@drvstr:设置驱动能力的强度,可想而知,它的值就是那四个宏的其中之一
2.s5p_gpio_get_drvstr()读取GPIO引脚的驱动能力寄存器的值
/** * s5c_gpio_get_drvstr() - get the driver streght value of a gpio pin * @pin: The pin number to get the settings for * * Read the driver streght value for the specified pin. */ extern s5p_gpio_drvstr_t s5p_gpio_get_drvstr(unsigned int pin);
功能:读取GPIO引脚的驱动能力寄存器的值
参数:@pin:要读取的GPIO引脚
第四部分:关于掉电模式寄存器
/* Define values for the power down configuration available for each gpio pin. * * These values control the state of the power down configuration resistors * available on most pins on the S5P series. */ #define S5P_GPIO_PD_OUTPUT0 ((__force s5p_gpio_pd_cfg_t)0x00) #define S5P_GPIO_PD_OUTPUT1 ((__force s5p_gpio_pd_cfg_t)0x01) #define S5P_GPIO_PD_INPUT ((__force s5p_gpio_pd_cfg_t)0x02) #define S5P_GPIO_PD_PREV_STATE ((__force s5p_gpio_pd_cfg_t)0x03)
1.s5p_gpio_set_pd_cfg()设置引脚的掉电模式寄存器
/** * s5p_gpio_set_pd_cfg() - set the configuration of a gpio power down mode * @pin: The pin number to configure the pull resistor. * @pd_cfg: The configuration for the pwer down mode configuration register. * * This function sets the configuration of the power down mode resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested power down mode. * */ extern int s5p_gpio_set_pd_cfg(unsigned int pin, s5p_gpio_pd_cfg_t pd_cfg);
功能:设置引脚的掉电模式寄存器
参数:@pin:要设置的引脚
@pd_cfg:要配置成的模式,当然是那四个宏之一
2.s5p_gpio_get_pd_cfg()取得引脚的掉电模式寄存器的值
/** * s5p_gpio_get_pd_cfg() - get the power down mode configuration of a gpio pin * @pin: The pin number to get the settings for * * Read the power down mode resistor value for the specified pin. */ extern s5p_gpio_pd_cfg_t s5p_gpio_get_pd_cfg(unsigned int pin);
功能:取得引脚的掉电模式寄存器的值
参数:@pin:要取得设置的引脚
/* Define values for the power down pull-{up,down} available for each gpio pin. * * These values control the state of the power down mode pull-{up,down} * resistors available on most pins on the S5P series. */ #define S5P_GPIO_PD_UPDOWN_DISABLE ((__force s5p_gpio_pd_pull_t)0x00) #define S5P_GPIO_PD_DOWN_ENABLE ((__force s5p_gpio_pd_pull_t)0x01) #define S5P_GPIO_PD_UP_ENABLE ((__force s5p_gpio_pd_pull_t)0x03)
3.s5p_gpio_set_pd_pull()设置掉点模式的引脚的上下拉模式
/** * s5p_gpio_set_pd_pull() - set the pull-{up,down} of a gpio pin power down mode * @pin: The pin number to configure the pull resistor. * @pd_pull: The configuration for the power down mode pull resistor. * * This function sets the configuration of the pull-{up,down} resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested pull setting. * */ extern int s5p_gpio_set_pd_pull(unsigned int pin, s5p_gpio_pd_pull_t pd_pull);
功能:设置掉点模式的引脚的上下拉模式
参数:@pin:要设置的引脚
@pd_pull:要设置成什么模式,上下拉模式,当然是那三个宏拉
4.s5p_gpio_get_pd_pull()取得引脚的掉电模式上下拉电阻的设置
/** * s5p_gpio_get_pd_pull() - get the power down pull resistor config of gpio pin * @pin: The pin number to get the settings for * * Read the power mode pull resistor value for the specified pin. */ extern s5p_gpio_pd_pull_t s5p_gpio_get_pd_pull(unsigned int pin);
功能:取得引脚的掉电模式上下拉电阻的设置
参数:@pin:要取得设置的引脚
第五部分:关于中断引脚
这里我还不是很清楚,先不写了
1.s5p_register_gpio_interrupt()
/** * s5p_register_gpio_interrupt() - register interrupt support for a gpio group * @pin: The pin number from the group to be registered * * This function registers gpio interrupt support for the group that the * specified pin belongs to. * * The total number of gpio pins is quite large ob s5p series. Registering * irq support for all of them would be a resource waste. Because of that the * interrupt support for standard gpio pins is registered dynamically. * * It will return the irq number of the interrupt that has been registered * or -ENOMEM if no more gpio interrupts can be registered. It is allowed * to call this function more than once for the same gpio group (the group * will be registered only once). */ extern int s5p_register_gpio_interrupt(int pin);
功能:
参数:
2.s5p_register_gpioinit_bank()
/** s5p_register_gpioint_bank() - add gpio bank for further gpio interrupt * registration (see s5p_register_gpio_interrupt function) * @chain_irq: chained irq number for the gpio int handler for this bank * @start: start gpio group number of this bank * @nr_groups: number of gpio groups handled by this bank * * This functions registers initial information about gpio banks that * can be later used by the s5p_register_gpio_interrupt() function to * enable support for gpio interrupt for particular gpio group. */ #ifdef CONFIG_S5P_GPIO_INT extern int s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups); #else #define s5p_register_gpioint_bank(chain_irq, start, nr_groups) do { } while (0) #endif