1 driver
#include <linux/delay.h>
#include <asm/irq.h>#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#define DEVICE_NAME "led0"
static unsigned long led_table [] = {
S3C2410_GPB(5),
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
/*将I/O口设置为输出功能*/
static unsigned int led_cfg_table [] = {
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
/*对LED文件操作*/
static int sbc2440_leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
/*miscdevice*/
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.ioctl = sbc2440_leds_ioctl,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
/*设备的初始化函数*/
static int __init dev_init(void)
{
int ret;
int i;
for (i = 0; i < 4; i++) {
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);//off
}
ret = misc_register(&misc);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
/*设备注销程序*/
static void __exit dev_exit(void)
{
misc_deregister(&misc);
printk(DEVICE_NAME"\tclose!!\n");
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
2 test
#include <string.h>
#include <stdlib.h>#include <fcntl.h> // open() close()
#include <unistd.h> // read() write()
#define LED_ON 1
#define LED_OFF 0
int main(void)
{
int fd,i;
fd = open("/dev/led0", 0);
if (fd < 0)
{
printf("open device /lib/modulew/2.6.32.2/myled-ioctl error!\n");
}
else
{
while(1)
{
for(i=0;i<4;i++)
{
printf("led%d is on!\n",i);
ioctl(fd,LED_ON,i);
sleep(1);//等待1秒再做下一步操作
printf("led%d is off!\n",i);
ioctl(fd,LED_OFF,i);
sleep(1);
}
if(i==4)
i=0;
}
close(fd);
}
return 0;
}