1 13 #include<stdio.h> 14 #include<stdlib.h> 15 #include<unistd.h> 16 #include<fcntl.h> 17 #include<errno.h> 18 #include<string.h> 19 #include<asm/ioctl.h> 20 #ifndef __KERNEL__ 21 #include <sys/ioctl.h> 22 #endif 23 24 #define PLATDRV_MAGIC 0X60 25 #define LED_ON _IO (PLATDRV_MAGIC, 0x19) 26 #define LED_OFF _IO (PLATDRV_MAGIC, 0x18) 27 #define LEN_LED 4 28 #define LED_BUF 128 29 30 int main(int argc,char **argv) 31 { 32 int fd[LEN_LED]; 33 char dev_name[LED_BUF]; 34 int i; 35 36 for(i=0;i<LEN_LED;i++) 37 { 38 snprintf(dev_name,sizeof(dev_name),"/dev/led%d",i); 39 fd[i]=open(dev_name,O_RDWR,0755); 40 if(fd[i]<0) 41 { 42 printf("open failure:%s",strerror(errno)); 43 } 44 } 45 46 while(1) 47 { 48 for(i=0;i<LEN_LED;i++) 49 { 50 51 52 ioctl(fd[i],LED_ON); 53 sleep(1); 54 ioctl(fd[i],LED_OFF); 55 sleep(0.5); 56 } 57 58 59 } 60 61 for(i=0;i<LEN_LED;i++) 62 { 63 close(fd[i]); 64 return 0; 65 } 66 }
注意:因为我们要把测试程序放到开发板上去测试,所以我们应该使用交叉编译器arm-linux-gcc来编译上述test.c文件,生成可执行文件。
生成a.out可执行文件
接下来在开发板上测试:
一:将s3c_led.ko,a.out传到开发板上
tftp -gr s3c_led.ko 192.168.1.2
tftp -gr a.out 192.168.1.2
二:开发板中用insmod安装我们的s3c_led模块
insmod s3c_led.ko
此时lsmod看看我们安装的驱动模块:
驱动安装好以后我们就该创建设备节点了,因为要找次设备号嘛,主设备号都是统一的,我们的板子有4个led,亮哪个灭哪个由次设备号决定,我们先查一下我们的主设备号:
用mknod建立设备节点
给a.out执行权限
chmod 777 a.out
./a.out
做到这里就可以观察开发板上led的测试情况了!(我们如果要卸载驱动,既可以rmmod)
注:
转载请注明原文地址: https://www.6miu.com/read-3988.html