<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > OK6410A學(xué)習(xí)筆記三:嵌入式Linux驅(qū)動之LED驅(qū)動

          OK6410A學(xué)習(xí)筆記三:嵌入式Linux驅(qū)動之LED驅(qū)動

          作者: 時(shí)間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
          開發(fā)環(huán)境:
          Windows7 + vmware workstation 6.5 + Ubuntu9.10
          Linux Source Code: FORLINX_linux-3.0.1.tar.gz
          ARM CROSS GCC: arm-linux-gcc v4.3.2

          源碼:
          //s3c6410_led.c – driver file
          #include
          #include
          #include
          #include
          #include

          #define DEV_MAJOR 176
          #define DEV_NAME "s3c6410_leds"


          #define GPMCON 0x7F008820
          #define GPMDAT 0x7F008824
          #define GPMPUD 0x7F008828


          volatile unsigned long *gpmcon = NULL;
          volatile unsigned long *gpmdat = NULL;
          volatile unsigned long *gpmpud = NULL;


          static int s3c6410_led_open(struct inode *inode, struct file *filp)
          {
          //int ret;

          printk(KERN_ALERT "This is open function of s3c6410 led driver.n");

          *gpmpud &= 0xffffffaa; //set GPM0~3 as pull up enabled
          *gpmcon &= 0xffff1111; //set GPM0~3 as output
          *gpmdat &= 0xfffffff0; //set GPM0~3 to low level, which turn on leds

          return 0;

          }

          static ssize_t s3c6410_led_write(struct file *filp,const char __user *buf,size_t count,loff_t *ppos)
          {
          int val;

          //copy_from_user(&val,buf,count);

          printk(KERN_ALERT "This is write function of s3c6410 led driver.n");

          return count;
          }


          struct file_operations s3c6410_led_flops = {
          .owner = THIS_MODULE,
          .open = s3c6410_led_open,
          .write = s3c6410_led_write,
          };

          static int __init s3c6410_led_init(void)
          {
          int ret;

          //register led device driver into kernel
          ret = register_chrdev(DEV_MAJOR,DEV_NAME,&s3c6410_led_flops);

          //retriver the vritual address by ioremap
          gpmcon = (volatile unsigned long *)ioremap(GPMCON,4); //32-bit reg
          gpmdat = gpmcon + 1;
          gpmpud = gpmcon + 2;

          return 0;
          }

          static void __exit s3c6410_led_exit(void)
          {
          //unregister led device dirver from kernel
          unregister_chrdev(DEV_MAJOR,DEV_NAME);

          //iounmap
          iounmap(gpmcon);
          }

          module_init(s3c6410_led_init);
          module_exit(s3c6410_led_exit);

          MODULE_DESCRIPTION("This is led driver sample for OK6410A board.");
          MODULE_VERSION("1.0");
          MODULE_AUTHOR("");
          MODULE_LICENSE("Dual BSD/GPL");

          //s3c6410_led_test.c – test file
          #include
          #include

          int main(int argc,char* argv[])
          {
          int fd;

          fd = open("/dev/s3c6410_led",0);

          if(!fd){
          printf("open s3c6410 led failed.n");
          }else{
          printf("open s3c6410 led succeed.n");
          }
          return 0;
          }

          解析:
          1. 查看OK6410A開發(fā)板原理圖,4個(gè)LED燈接在GPM0~3四個(gè)GPIO上。
          2. 查看Samsung S3C6410X User’s Manual.pdf ,查看GPm端口操作說明,包括GPMCON,GPMDAT,GPMPUD三個(gè)寄存器的基地址以及配置信息,從而確定點(diǎn)亮LED燈和熄滅LED燈的操作。
          3. 編寫源代碼,包括驅(qū)動源碼和測試源碼。這里比較重要的地方是,Linux驅(qū)動是工作在保護(hù)模式下,無法直接操作寄存器,因此必須將步驟二中找到的寄存器基地址通過IO地址重映射,從而得到虛擬地址進(jìn)行操作,在驅(qū)動程序入口函數(shù)中使用ioremap()函數(shù)進(jìn)行地址重映射,在退出函數(shù)中使用iounmap()結(jié)束地址重映射。另外,如果想在加載驅(qū)動的同時(shí),讓系統(tǒng)自動新建設(shè)備節(jié)點(diǎn),則要將設(shè)備信息提供給系統(tǒng)內(nèi)核,Linux系統(tǒng)支持的mdev機(jī)制會根據(jù)驅(qū)動程序提供的信息新建設(shè)備節(jié)點(diǎn)。在上述源碼中并沒有實(shí)現(xiàn)這一功能。
          4. 編譯源碼
          驅(qū)動程序Makefile如下:
          obj-m:=s3c6410_led.o
          KERNELDIR?=/usr/src/linux-3.0.1

          default:
          $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules

          clean:
          rm -rf *.o *~ *.ko *.mod.c *.order *.symvers

          在終端下執(zhí)行如下命令:
          #make //編譯驅(qū)動程序,得到s3c6410_led.ko文件
          #arm-linux-gcc s3c6410_led_test.c –o s3c6410_led_test //編譯測試源碼

          5. 拷貝s3c6410_led.ko和s3c6410_led_test到SD卡中,給開發(fā)板上電




          評論


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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();