3.4.2点灯驱动(完成)

开发板:jz2440
内核:3.4.2
芯片:S3C2440
/*
**********应用测试程序**************/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> /* 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("can't open!\n"); } if (argc != 2) //判断执行时是否2个字符串 { printf("Usage :\n"); printf("%s <on|off>\n", argv[0]); return 0; } if (strcmp(argv[1], "on") == 0) //比较第二个字符串,相同为0 { val = 1; } else { val = 0; } write(fd, &val, 4); return 0; } /********************操作********************/ //编译---arm-linux-gcc -o firstdrvtest firstdrvtest.c //开灯---./firstdrvtest on //关灯---./firstdrvtest off /********************************************/ /*****************驱动程序如下******************/ #include <linux/module.h> #include <linux/kernel.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> static struct class *firstdrv_class; //分配一个类 static struct device *firstdrv_class_dev; 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"); /* 配置GPF4,5,6为输出 */ *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2))); *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(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"); copy_from_user(&val, buf, count); // copy_to_user(); if (val == 1) { // 点灯 *gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); } else { // 灭灯 *gpfdat |= (1<<4) | (1<<5) | (1<<6); } return 0; } static struct file_operations first_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = first_drv_open, .write = first_drv_write, }; int major; static int first_drv_init(void) { major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核 //参数为0是系统随机分配主设备号 firstdrv_class = class_create(THIS_MODULE, "firstdrv"); firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ //功能是让系统自动生成设备节点 gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1; return 0; } static void first_drv_exit(void) { unregister_chrdev(major, "first_drv"); // 卸载 device_destroy(firstdrv_class_dev, MKDEV(major, 0)); class_destroy(firstdrv_class); iounmap(gpfcon); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");//这是一个协议 /**********************操作***********************/ 安装---insmod first_drv.ko 卸载---rmmod first_drv /*************************************************/ /*************************Makefile如下****************************/ KERN_DIR = /work/system/linux-3.4.2 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m += first_drv.o /*******************操作*******************/ //make---生成.ko文件 //make clean---删除.ko文件 /******************************************/

猜你喜欢

转载自www.cnblogs.com/threegold/p/9343824.html