字符设备驱动-模板

 

iorw.c

 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include <linux/cdev.h>
 4 #include <linux/fs.h>
 5 #include <linux/io.h>
 6 #include <mach/gpio-bank-k.h>
 7 #include "led.h"
 8 
 9 #define LEDCON 0x7f008800
10 #define LEDDAT 0x7f008808
11 unsigned int *led_config; 
12 unsigned int *led_data; 
13 
14 struct cdev cdev;
15 dev_t devno;
16 
17 int led_open(struct inode *node, struct file *filp)
18 {
19     led_config = ioremap(LEDCON,4);
20     writel(0x11110000,led_config);
21     
22     led_data = ioremap(LEDDAT,4);
23     
24     return 0;
25 }
26 
27 long led_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
28 {
29     switch (cmd)
30     {
31         case LED_ON:
32             writel(0x00,led_data);
33             return 0;
34         
35         case LED_OFF:
36             writel(0xff,led_data);
37             return 0;
38         
39         default:
40             return -EINVAL;
41     }
42 }
43 
44 static struct file_operations led_fops =
45 {
46     .open = led_open,
47     .unlocked_ioctl = led_ioctl,
48 };
49 
50 static int led_init()
51 {
52     cdev_init(&cdev,&led_fops);
53     
54     alloc_chrdev_region(&devno, 0 , 1 , "myled");
55     cdev_add(&cdev, devno, 1);
56     
57     return 0;    
58 }
59 
60 static void led_exit()
61 {
62     cdev_del(&cdev);
63     unregister_chrdev_region(devno,1);
64 }
65 
66 
67 module_init(led_init);
68 module_exit(led_exit);

 

 

Makefile

obj-m := iorw.o

KDIR := /home/arm/Desktop/share/资料/OK6818-C/源码/kernel-3.4.39

all:
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-cortex_a9-linux-gnueabi- ARCH=arm
    
clean:
    rm -f *.o *.ko *.order *.symvers
    

 

 

编译

  make 

 

posted @ 2021-04-15 11:12  迷人的危险~  阅读(58)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css