ARM-Linux自動(dòng)創(chuàng)建設(shè)備結(jié)點(diǎn)
內(nèi)核版本:2.6.28
本文引用地址:http://m.butianyuan.cn/article/201611/318863.htm主機(jī)平臺(tái):Ubuntu 11.04
內(nèi)核版本:2.6.39
1、首先配置busybox
busybox
Linux System Utilities --->
[*] mdev
[*] Support /etc/mdev.conf
[*] Support command execution at device addition/removal
2、配置內(nèi)核
3、修改文件系統(tǒng)里的/etc/init.d/rcS
#!/bin/sh
/bin/mount -a
/sbin/ifconfig eth0 192.168.0.3 up
#exec /usr/etc/rc.mouse
4、修改文件系統(tǒng)中/linuxrc文件
#!/bin/sh
#echo "mount /etc as ramfs"
#/bin/mount -n -t ramfs ramfs /etc
#/bin/cp -a /mnt/etc/* /etc
#/bin/mount -n -t ramfs ramfs /var/state/dhcp
#/bin/mount -n -t ramfs ramfs /var/log/boa
#/bin/mount -n -t ramfs ramfs /usr/Setting
#/bin/cp -a /mnt/Setting/* /usr/Setting
#/bin/mount -n -t ramfs ramfs /tmp
#/bin/cp -a /mnt/etc/* /etc
/bin/mount -t proc proc /proc
/bin/mount -t sysfs sysfs /sys
/bin/mount -t tmpfs tmpfs /dev
mkdir /dev/pts
mkdir /dev/shm
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
exec /sbin/init
4、修改/etcfstab
vi ./etc/fstab
#device mount-point type options dump fsck order
none /dev/pts devpts mode=0622 0 0
tmpfs /dev/shm tmpfs defaults 0 0
這樣編寫驅(qū)動(dòng)時(shí)不用手動(dòng)創(chuàng)建設(shè)備結(jié)點(diǎn)文件了
下面是改寫的使用混雜設(shè)備的ADC驅(qū)動(dòng)程序,這樣可以自動(dòng)創(chuàng)建和刪除設(shè)備結(jié)點(diǎn)了
- #include
- #include
- #include
- #include
/*創(chuàng)建設(shè)備節(jié)點(diǎn)*/ - #include
- #include
/*定義DECLARE_WAIT_QUEUE_HEAD*/ - #include
/*定義了irqreturn_t等*/ - #include
interrupt.h>/*request_irqdisable_irqenable_irq*/ - #include
- #include
- #include/*其中包含了#include"mach/irqs.h"*/
- #include
adc.h> - #include
- #defineADC_MAJOR102
- #defineADC_NAME"my_adc"
- #defineSUCCESS0
- staticintadc_open(structinode*,structfile*);
- staticintadc_release(structinode*,structfile*);
- staticint__initadc_init(void);
- staticint__exitadc_exit(void);
- staticssize_tadc_read(structfile*,char*,size_t,loff_t*);
- volatileunsignedlongadc_con;
- unsignedlongadc_dat0;
- intflag;//等待任務(wù)完成標(biāo)志
- unsignedlongbuf;//存放轉(zhuǎn)換完成的數(shù)據(jù)
- //聲明等待隊(duì)列
- DECLARE_WAIT_QUEUE_HEAD(adc_wait);
- structclk*adc_clk;
- staticirqreturn_tadc_interrupt(intirq,void*dev_id)//中斷處理程序
- {
- if(flag==0)
- {
- buf=(readw(adc_dat0)&0x3ff);//讀取轉(zhuǎn)換完成的數(shù)據(jù)
- flag=1;
- wake_up_interruptible(&adc_wait);//喚醒等待其上的進(jìn)程
- printk("Readvalueis%ldn",buf);
- }
- returnIRQ_HANDLED;
- }
- staticstructfile_operationsadc_ops=
- {
- .owner=THIS_MODULE,
- .read=adc_read,
- .open=adc_open,
- .release=adc_release,
- };
- staticstructmiscdeviceadc_misc=
- {
- .name=ADC_NAME,
- .minor=ADC_MAJOR,
- .fops=&adc_ops,
- };
- staticint__initadc_init(void)
- {
- intret;
- adc_clk=clk_get(NULL,"adc");//獲取時(shí)鐘
- clk_enable(adc_clk);//使能時(shí)鐘
- ret=misc_register(&adc_misc);//注冊(cè)設(shè)備
- if(ret<0)
- {
- printk("registerdevicefailn");
- returnret;
- }
- adc_con=(unsignedlong)ioremap(0x58000000,4);
- adc_dat0=(volatileunsignedlong)ioremap(0x58000000+S3C2410_ADCDAT0,4);
- if(!(adc_con&adc_dat0))
- {
- printk("Failedtoioremapn");
- gotohandle;
- }
- printk("Initialized...n");
- returnSUCCESS;
- handle:
- misc_deregister(&adc_misc);
- return-1;
- }
- staticintadc_open(structinode*inode,structfile*file)//打開設(shè)備函數(shù)
- {
- //注冊(cè)中斷
- intret;
- //disable_irq(IRQ_ADC);
- //enable_irq(IRQ_ADC);
- ret=request_irq(IRQ_ADC,adc_interrupt,IRQF_SHARED,ADC_NAME,1);//注冊(cè)中斷IRQ_ADC在mach/irqs.h中定義
- if(ret<0)
- {
- printk("IRQ%dcannotrequestn",IRQ_ADC);
- returnret;
- }
- returnSUCCESS;
- }
- staticintadc_release(structinode*inode,structfile*file)//關(guān)閉設(shè)備函數(shù)
- {
- free_irq(IRQ_ADC,1);//釋放中斷
- returnSUCCESS;
- }
- staticssize_tadc_read(structfile*file,
- char*buffer,
- size_tlength,
- loff_t*offset)//設(shè)備讀取函數(shù)
- {
- writew((1<<14)|(0x31<<6),adc_con);//設(shè)置ADCCON
- writew((readw(adc_con)|0x1),adc_con);//啟動(dòng)AD轉(zhuǎn)換
- wait_event_interruptible(adc_wait,flag);
- flag=0;
- }
- staticint__exitadc_exit(void)//驅(qū)動(dòng)卸載函數(shù)
- {
- iounmap(adc_con);
- iounmap(adc_dat0);
- misc_deregister(&adc_misc);
- clk_disable(adc_clk);
- clk_put(adc_clk);
- printk("Theadcisunintializedn");
- returnSUCCESS;
- }
- module_init(adc_init);
- module_exit(adc_exit);
- MODULE_LICENSE("GPL");
評(píng)論