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");