sunbeyond 发表于 2015-5-28 09:40
可以编译单个驱动模块。 可以参照附件一个简单的Makefile。
注意对应的内核是要编译过的。并且要把内核 ...
我写了个驱动程序,又写了个测试程序,可是没有出现错误,可是测试程序执行时无法执行驱动里面的内容,是哪里的问题- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/errno.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- #include <linux/device.h>
- #include <asm/uaccess.h>
- #include <asm/irq.h>
- #include <asm/io.h>
- #if 1
- #define GPIO_BASE 0X06000800
- #define GPH_CFG0 (GPIO_BASE+0XFC) //PH CFG0~~the port of led is PH06
- #define GPH06_CFG (1<<24) //閰嶇疆璇ョ鍙d负杈撳嚭妯″紡锝炲嵆led绔彛鏄緭鍑篜H06
- #define GPH_DATA (GPIO_BASE+0X10C) //offset about the gpio_data register
- #endif
- static struct class *leddrv_class;
- /******cfg0閲岄潰绔彛鍖呮嫭PH06**********/
- static volatile unsigned int *gpioh_cfg0 ;
- static volatile unsigned int *gpioh_data ;
- static int led_drv_open(struct inode *inode,struct file *file)
- {
- /*灏哖H06_CFG浣嶆竻闆?/
- *gpioh_cfg0 &= ~(GPH06_CFG);
- /*灏哖H06_CFG浣嶇疆1锛屼娇鍏朵负杈撳嚭妯″紡*/
- *gpioh_cfg0 |= GPH06_CFG;
- /****鍦ㄥ唴鏍搁噷鎵撳嵃瑕佺敤鍑芥暟printk****/
- printk("led openi\n");
- return 0;
- }
- static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
- {
- int val;
- /**鍐呮牳绌洪棿鍜岀敤鎴风┖闂寸浉浜掔嫭绔嬶紝涓よ€呬箣闂存暟鎹氦鎹㈠嚱鏁?*/
- copy_from_user(&val,buf,count);
- if(val == 1)
- {
- /**缃?锛岀浉褰撲簬pin_write鍐?**/
- *gpioh_data &= ~(0x01<<6); //鍐橮H06涓?
- }
- else
- {
- /**缃?锛岀浉褰撲簬pin_write鍐?**/
- *gpioh_data |= (0x01<<6);
- }
- return 0;
- }
- static struct file_operations led_drv_fops = {
- .owner = THIS_MODULE, //杩欐槸涓€涓畯锛屾帹鍚戠紪璇戞ā鍧楁椂鑷姩鍒涘缓褰揰_this_module鍙橀噺
- .open = led_drv_open, //鍑芥暟鎸囬拡璧嬪€? .write = led_drv_write, //鍑芥暟鎸囬拡璧嬪€?};
- /****涓昏澶囧彉閲?**/
- int major;
- /******褰撴墽琛宨nsmod鍛戒护褰撴椂鍊欒繖涓嚱鏁颁細琚墽琛岋紝鍗冲垵濮嬪寲鍑芥暟*****/
- static int led_drv_init(void)
- { pr_err("leds_init by wanzokyoung\n");
- major = register_chrdev(0,"wanzokyoung",&led_drv_fops);
- leddrv_class = class_create(THIS_MODULE,"wanzokyoung");
- device_create(leddrv_class,NULL,MKDEV(major,0),NULL,"wanzokyoung");
- gpioh_cfg0 = (volatile unsigned int *)ioremap(GPH_CFG0,20); //20琛ㄧず20byte锛?涓?2锛燂紵锛燂紵铏氭嫙鍦板潃鏄犲皠
- gpioh_data = (volatile unsigned int *)ioremap(GPH_DATA,20);
- return 0;
- }
- /**鎵цrmmod鐨勬椂鍊欎細琚墽琛岋綖鍗虫帹鍑篸rv**/
- static void led_drv_exit(void)
- {
- /*娉ㄩ攢led璁惧*/
- unregister_chrdev(major,"led"); //鍗歌浇
- /*閲婃斁鍦板潃*/
- iounmap(gpioh_cfg0);
- /*閿€姣佸垱寤哄綋璁惧鑺傜偣*/
- device_destroy(leddrv_class,MKDEV(major,0));
- class_destroy(leddrv_class);
- printk(KERN_ALERT"leds unregister\n");
- }
- /*linux璁惧椹卞姩褰撲袱涓慨楗扮*/
- module_init(led_drv_init);
- module_exit(led_drv_exit);
- /*閬靛惊GPL鍗忚*/
- MODULE_AUTHOR("by wanzokyoung");
- MODULE_LICENSE("GPL");
复制代码 上面是驱动程序,下面是测试程序- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- int main(int argc,char **argv)
- {
- int fd;
- int val = 1;
- fd = open("/dev/xxx",O_RDWR);
- if(fd<0)
- printf("can't open!\n");
- else
- {
- write(fd,&val,4);
- printf("done\n");
- }
- return 0;
- }
复制代码
|