新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ARM-Linux自動(dòng)創(chuàng)建設(shè)備結(jié)點(diǎn)

ARM-Linux自動(dòng)創(chuàng)建設(shè)備結(jié)點(diǎn)

作者: 時(shí)間:2016-11-20 來(lái)源:網(wǎng)絡(luò) 收藏
硬件平臺(tái):FL2440

內(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)了

[cpp]view plaincopy
  1. #include
  2. #include
  3. #include
  4. #include/*創(chuàng)建設(shè)備節(jié)點(diǎn)*/
  5. #include
  6. #include/*定義DECLARE_WAIT_QUEUE_HEAD*/
  7. #include/*定義了irqreturn_t等*/
  8. #includeinterrupt.h>/*request_irqdisable_irqenable_irq*/
  9. #include
  10. #include
  11. #include/*其中包含了#include"mach/irqs.h"*/
  12. #includeadc.h>
  13. #include
  14. #defineADC_MAJOR102
  15. #defineADC_NAME"my_adc"
  16. #defineSUCCESS0
  17. staticintadc_open(structinode*,structfile*);
  18. staticintadc_release(structinode*,structfile*);
  19. staticint__initadc_init(void);
  20. staticint__exitadc_exit(void);
  21. staticssize_tadc_read(structfile*,char*,size_t,loff_t*);
  22. volatileunsignedlongadc_con;
  23. unsignedlongadc_dat0;
  24. intflag;//等待任務(wù)完成標(biāo)志
  25. unsignedlongbuf;//存放轉(zhuǎn)換完成的數(shù)據(jù)
  26. //聲明等待隊(duì)列
  27. DECLARE_WAIT_QUEUE_HEAD(adc_wait);
  28. structclk*adc_clk;
  29. staticirqreturn_tadc_interrupt(intirq,void*dev_id)//中斷處理程序
  30. {
  31. if(flag==0)
  32. {
  33. buf=(readw(adc_dat0)&0x3ff);//讀取轉(zhuǎn)換完成的數(shù)據(jù)
  34. flag=1;
  35. wake_up_interruptible(&adc_wait);//喚醒等待其上的進(jìn)程
  36. printk("Readvalueis%ldn",buf);
  37. }
  38. returnIRQ_HANDLED;
  39. }
  40. staticstructfile_operationsadc_ops=
  41. {
  42. .owner=THIS_MODULE,
  43. .read=adc_read,
  44. .open=adc_open,
  45. .release=adc_release,
  46. };
  47. staticstructmiscdeviceadc_misc=
  48. {
  49. .name=ADC_NAME,
  50. .minor=ADC_MAJOR,
  51. .fops=&adc_ops,
  52. };
  53. staticint__initadc_init(void)
  54. {
  55. intret;
  56. adc_clk=clk_get(NULL,"adc");//獲取時(shí)鐘
  57. clk_enable(adc_clk);//使能時(shí)鐘
  58. ret=misc_register(&adc_misc);//注冊(cè)設(shè)備
  59. if(ret<0)
  60. {
  61. printk("registerdevicefailn");
  62. returnret;
  63. }
  64. adc_con=(unsignedlong)ioremap(0x58000000,4);
  65. adc_dat0=(volatileunsignedlong)ioremap(0x58000000+S3C2410_ADCDAT0,4);
  66. if(!(adc_con&adc_dat0))
  67. {
  68. printk("Failedtoioremapn");
  69. gotohandle;
  70. }
  71. printk("Initialized...n");
  72. returnSUCCESS;
  73. handle:
  74. misc_deregister(&adc_misc);
  75. return-1;
  76. }
  77. staticintadc_open(structinode*inode,structfile*file)//打開設(shè)備函數(shù)
  78. {
  79. //注冊(cè)中斷
  80. intret;
  81. //disable_irq(IRQ_ADC);
  82. //enable_irq(IRQ_ADC);
  83. ret=request_irq(IRQ_ADC,adc_interrupt,IRQF_SHARED,ADC_NAME,1);//注冊(cè)中斷IRQ_ADC在mach/irqs.h中定義
  84. if(ret<0)
  85. {
  86. printk("IRQ%dcannotrequestn",IRQ_ADC);
  87. returnret;
  88. }
  89. returnSUCCESS;
  90. }
  91. staticintadc_release(structinode*inode,structfile*file)//關(guān)閉設(shè)備函數(shù)
  92. {
  93. free_irq(IRQ_ADC,1);//釋放中斷
  94. returnSUCCESS;
  95. }
  96. staticssize_tadc_read(structfile*file,
  97. char*buffer,
  98. size_tlength,
  99. loff_t*offset)//設(shè)備讀取函數(shù)
  100. {
  101. writew((1<<14)|(0x31<<6),adc_con);//設(shè)置ADCCON
  102. writew((readw(adc_con)|0x1),adc_con);//啟動(dòng)AD轉(zhuǎn)換
  103. wait_event_interruptible(adc_wait,flag);
  104. flag=0;
  105. }
  106. staticint__exitadc_exit(void)//驅(qū)動(dòng)卸載函數(shù)
  107. {
  108. iounmap(adc_con);
  109. iounmap(adc_dat0);
  110. misc_deregister(&adc_misc);
  111. clk_disable(adc_clk);
  112. clk_put(adc_clk);
  113. printk("Theadcisunintializedn");
  114. returnSUCCESS;
  115. }
  116. module_init(adc_init);
  117. module_exit(adc_exit);
  118. MODULE_LICENSE("GPL");



評(píng)論


技術(shù)專區(qū)

關(guān)閉