First_drv.c内容如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_devs;
volatile unsigned long *GPFCON = NULL;
volatile unsigned long *GPFDAT = NULL;
static int first_drv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/*配置引脚GPF 4 5 6为输出*/
*GPFCON &= ~((3<< (4*2)) |(3<< (5*2)) |(3<< (6*2)));
*GPFCON |= ((1<< (4*2)) |(1<< (5*2)) |(1<< (6*2)));
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//printk("first_drv_write\n");
/*用户空间和内核空间之间的数据传递cope_to_user*/
copy_from_user(&val, buf, count);
if(val == 1)
{
//点灯
*GPFDAT &= ~((1<<5) |(1<<6)|(1<<4));
}MODULE_LICENSE("GPL");
测试文件firstdrvtest.c如下:
#include
#include
#include
#include
/*
firstdrvtest on
firstdrvtest off
*/
int main(int argc , char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz",O_RDWR);
if (fd < 0)
printf("cannt open!\n");
if(argc != 2)
{
printf("Usage :\n");
printf("%s
return 0;
}
if(strcmp(argv[1], "on") == 0)
{
val=1;
}
else
{
val=0;}
注:
led驱动文件编译的时候要制定头文件的目录,韦东山视频里在makefile中指定了编译好的内核目录。