驱动源码:
#include
.
.
#include
int major;
int minor = 0;
static struct class *leddrv_class;
static struct class_device *leddrv_class_devs[4];
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
switch(minor)
{
case 0:
{
// 配置3引脚为输出
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
*gpfcon &= ~(0x3<(4*2));
*gpfcon |= (1<(4*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
*gpfcon &= ~(0x3<(5*2));
*gpfcon |= (1<(5*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
*gpfcon &= ~(0x3<(6*2));
*gpfcon |= (1<(6*2));
// 都输出0
//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
*gpfdat &= ~(1<4);
//s3c2410_gpio_setpin(S3C2410_GPF5, 0);
*gpfdat &= ~(1<5);
//s3c2410_gpio_setpin(S3C2410_GPF6, 0);
*gpfdat &= ~(1<6);
break;
}
case 1:
{
s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF4, 0);
break;
}
case 2:
{
s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 0);
break;
}
case 3:
{
s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF6, 0);
break;
}
}
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val;
int minor = MINOR(file->f_dentry->d_inode->i_rdev);
copy_from_user(&val, buf, 1);
switch (minor)
{
case 0:
{
s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
break;
}
case 1:
{
s3c2410_gpio_setpin(S3C2410_GPF4, val);
break;
}
case 2:
{
s3c2410_gpio_setpin(S3C2410_GPF5, val);
break;
}
case 3:
{
s3c2410_gpio_setpin(S3C2410_GPF6, val);
break;
}
}
return 0;
}
static struct file_operations led_drv_fops = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
major = register_chrdev(0, "led_drv", &led_drv_fops); // 注册字符驱动, 告诉内核
leddrv_class = class_create(THIS_MODULE, "leddrv"); //创建设备节点
leddrv_class_devs[0] = class_device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, "leddrv");
for (minor = 1; minor < 4; minor++)
{
leddrv_class_devs[minor] = class_device_create(leddrv_class, NULL, MKDEV(major, minor), NULL, "leddrv%d", minor);
if (unlikely(IS_ERR(leddrv_class_devs[minor])))
return PTR_ERR(leddrv_class_devs[minor]);
}
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, "led_drv"); // 卸载
for (minor = 0; minor < 4; minor++)
{
class_device_unregister(leddrv_class_devs[minor]);
}
class_destroy(leddrv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
==================================================================================================
测试程序:
#include
#include
#include
#include
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s \n",file);
printf("eg. \n");
printf("%s /dev/leds on\n", file);
printf("%s /dev/leds off\n", file);
printf("%s /dev/led1 on\n", file);
printf("%s /dev/led1 off\n", file);
}
int main(int argc, char **argv)
{
int fd;
char* filename;
char val;
if (argc != 3)
{
print_usage(argv[0]);
return 0;
}
filename = argv[1];