General Purpose Input Output (通用输入/输出)简称为GPIO,或总线扩展器,利用工业标准I2C、SMBus™或SPI™接口简化了I/O口的扩展。当微控制器或芯片组没有足够的 I/O端口,或当系统需要采用远端串行通信或控制时,GPIO产品能够提供额外的控制和监视功能。
One Module driver example,
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#define SNOWBALL_LED_GPIO_NR 142
#define LED_OFF 0
#define LED_ON 1
static struct delayed_work blinking_work;
static int i = 0;
static void blink_function(struct work_struct *work)
{
gpio_set_value(SNOWBALL_LED_GPIO_NR, i++ % 2);
schedule_delayed_work(&blinking_work, 1000);
}
static int start_blinking(void)
{
int err = 0;
INIT_DELAYED_WORK(&blinking_work, blink_function);
printk(KERN_ERR "We're starting our module!\n");
err = gpio_request(SNOWBALL_LED_GPIO_NR, "led_blinking");
if (err) {
printk(KERN_ERR "GPIO request failed %d\n", err);
goto request_failed;
}
err = gpio_direction_output(SNOWBALL_LED_GPIO_NR, LED_OFF);
if (err) {
printk(KERN_ERR "gpio_direction_output failed %d\n", err);
gpio_free(SNOWBALL_LED_GPIO_NR);
goto request_failed;
}
schedule_delayed_work(&blinking_work, 1);
request_failed:
return err;
}
static void stop_blinking(void) {
printk(KERN_ERR "Bye, cruel world\n");
cancel_delayed_work_sync(&blinking_work);
gpio_free(SNOWBALL_LED_GPIO_NR);
}
module_init(start_blinking);
module_exit(stop_blinking);
MODULE_LICENSE("GPL");