标签:devices default gcc urb 复制 mpi node ice har
led.c
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/cdev.h> 4 #include <linux/fs.h> 5 #include <linux/io.h> 6 7 #define GPMCON 0x7F008820 8 #define GPMDAT 0x7F008824 9 10 static int * __gp_gpmdat; 11 12 static int led_open (struct inode *p_inode, struct file *p_file) 13 { 14 int *p_gpmcon = NULL; 15 16 p_gpmcon = ioremap(GPMCON, 4); 17 writel(0x1111, p_gpmcon); 18 19 __gp_gpmdat = ioremap(GPMDAT, 4); 20 21 return 0; 22 } 23 24 static long led_ioctl (struct file *p_file, unsigned int cmd, unsigned long arg) 25 { 26 switch (cmd) { 27 case 0: 28 writel(0x0, __gp_gpmdat); 29 break; 30 31 case 1: 32 writel(0xF, __gp_gpmdat); 33 break; 34 35 default: 36 return -1; 37 } 38 return 0; 39 } 40 41 static struct cdev led_dev; 42 static struct file_operations fops = { 43 .open = led_open, 44 .unlocked_ioctl = led_ioctl, 45 }; 46 dev_t devno; 47 48 static __init int led_init (void) 49 { 50 cdev_init(&led_dev, &fops); 51 alloc_chrdev_region(&devno, 0, 1, "myled"); 52 cdev_add(&led_dev, devno, 1); 53 54 return 0; 55 } 56 57 static __exit void led_exit (void) 58 { 59 cdev_del(&led_dev); 60 unregister_chrdev_region(devno, 1); 61 } 62 63 module_init(led_init); 64 module_exit(led_exit); 65 66 MODULE_LICENSE("GPL");
led_app.c
1 #include <unistd.h> 2 #include <sys/ioctl.h> 3 #include <stdio.h> 4 #include <sys/types.h> 5 #include <sys/stat.h> 6 #include <fcntl.h> 7 8 int main(int argc, const char *argv[]) 9 { 10 int fd, cmd; 11 12 if (argc != 2) { 13 printf(".example: ./led_app 0\n"); 14 return; 15 } 16 17 cmd = atoi(argv[1]); 18 19 fd = open("/dev/myled", O_RDWR); 20 21 ioctl(fd, cmd); 22 23 return 0; 24 }
Makefile
1 obj-m := led.o 2 KDIR = /home/linux/zkf/6410/urbetter-linux2.6.28-v1.0/ 3 4 all: 5 make -C $(KDIR) $(objs) M=$(PWD) modules CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm 6 arm-none-linux-gnueabi-gcc led_app.c -o led_app 7 sudo cp led.ko ~/zkf/6410/rootfs/home/test 8 sudo cp led_app ~/zkf/6410/rootfs/home/test 9 clean: 10 rm *.o *.symvers *.mod.c *.order
make之后会生成led.ko和led_app两个要用到的文件,在Makefile中已经将他们复制到了开发板挂载目录中。在开发板终端中输入命令:insmod led.ko 之后输入命令:cat /proc/devices 可以看到myled主设备号为251
输入命令:mknod /dev/myled c 251 0
之后便能执行应用程序led_app了,执行 ./led_app 0 则点亮所有LED,执行 ./led_app 1 则熄灭所有LED。
标签:devices default gcc urb 复制 mpi node ice har
原文地址:https://www.cnblogs.com/Suzkfly/p/10317562.html