1. 程式人生 > 其它 ># 實驗二 按鍵OK6410-A開發板LINUX3.0.1(嵌入式開發)

# 實驗二 按鍵OK6410-A開發板LINUX3.0.1(嵌入式開發)

實驗二 按鍵

一、實驗目的

1.熟悉linux系統,學會簡單linux指令

2.熟悉OK6410-A開發板的燒入步驟

3.熟悉ARM暫存器,地址等。

4.系統性的瞭解UBOOT和linux核心,yaffs2系統映像等知識

二、實驗儀器

開發機環境

      作業系統:ubuntu 20.04
      交叉編譯環境:arm-linux-gcc 4.6.4 
      6410板子核心原始碼:linux-3.0.1 

目標板環境

	OK6410-A     linux-3.0.1 

環境配置看這裡

三、實驗內容(原理)

1.硬體部分

四、實驗步驟

1.編寫驅動程式

driver_key.c

//driver_key.c
#include <linux/kernel.h>  
 #include <linux/module.h>  
 #include <linux/init.h>  
 #include <linux/fs.h>  
 #include <linux/gpio.h>  
 #include <linux/types.h> 
 #include <linux/cdev.h>  
 #include <linux/interrupt.h>  
 #include <linux/sched.h>  
 #include <linux/device.h>  
 #include <linux/poll.h>  
 #include <linux/semaphore.h>  
 #include <linux/timer.h>  
 #include <asm/irq.h>  
 #include <asm/uaccess.h>  
 #include <mach/hardware.h>  
 #include <mach/irqs.h>  
   MODULE_LICENSE("GPL");  
 #define DEVICE_NAME "keyint"  
 #define KEYNUM 6  
 dev_t devid;  
 //static DEFINE_SEMAPHORE(key_lock);  //declare a mutex lock for keyint  
   //定義一個訊號量  
 struct semaphore key_lock;  
 static struct fasync_struct *key_async;   
 static struct timer_list key_timer;  
 struct key_irq_desc {  
     int irq;        //irq num  
     unsigned long flags;    //irq flags,identified the way of irq here,eq.edge,level  
     char *name;        //irq name  
 };  
 static struct key_irq_desc key_irqs[] = {  
     //下降沿產生中斷  
     {IRQ_EINT(0), IRQF_TRIGGER_FALLING, "KEY1"},  
     {IRQ_EINT(1), IRQF_TRIGGER_FALLING, "KEY2"},  
     {IRQ_EINT(2), IRQF_TRIGGER_FALLING, "KEY3"},  
     {IRQ_EINT(3), IRQF_TRIGGER_FALLING, "KEY4"},  
     {IRQ_EINT(4), IRQF_TRIGGER_FALLING, "KEY5"},  
     {IRQ_EINT(5), IRQF_TRIGGER_FALLING, "KEY6"},  
 };  
 /*define a waiting queue here*/  
 static DECLARE_WAIT_QUEUE_HEAD(key_waitq);  
 /*define a event flag ev_press*/  
 static volatile int ev_press = 0;  
 static volatile int press_cnt[KEYNUM] = {0,0,0,0,0,0};  
 /*中斷處理函式*/  
 static irqreturn_t keys_interrupt(int irq, void *dev_id)  
 { 
     volatile int *press_cnt = (volatile int *) dev_id;  
        /*set the pressed key flag(must do here due to not be static value)*/  
     *press_cnt = *press_cnt + 1;  
     //延時10ms後執行定時器處理函式  
     mod_timer(&key_timer,jiffies+HZ/100);        //start timer after 10ms  
     return IRQ_RETVAL(IRQ_HANDLED);  
 } 
//定時器處理函式  
 static void key_timer_func(unsigned long data)  
 {
     ev_press = 1;  
     //喚醒等待佇列  
     wake_up_interruptible(&key_waitq);  
     kill_fasync(&key_async, SIGIO, POLL_IN);  
 }  
 static int key_fasync(int fd, struct file *filp, int on)  
 {  
     printk("Function key_fasync\n");  
     return fasync_helper(fd,filp,on,&key_async);  
 }  
 static unsigned key_poll(struct file *file, poll_table *wait)  
 {  
     unsigned int mask=0;  
     //指明要使用的等待佇列  
     poll_wait(file,&key_waitq,wait);  
    //返回掩碼  
     if(ev_press)  
     mask |= POLL_IN | POLLRDNORM; 
     printk("poll wait\n"); 
     return mask;  
 }  
 static int key_open(struct inode *inode, struct file *file)  
 {  
     int num;  
     if(file->f_flags & O_NONBLOCK) {  
       if(down_trylock(&key_lock)) return -EBUSY;  
     }  
     else {  
       down(&key_lock);  
     }  
     //為每個按鍵註冊中斷處理程式  
     for(num=0;num<KEYNUM;num++) {  
       request_irq(key_irqs[num].irq, keys_interrupt, key_irqs[num].flags, key_irqs[num].name, (void *)&press_cnt[num]);  
     } 
     return 0;  
 }
 static int key_close(struct inode *inode, struct file *file)  
 { 
     int num;  
     //釋放中斷號  
     for(num=0;num<6;num++) {  
       free_irq(key_irqs[num].irq, (void *)&press_cnt[num]);  
     }  
     up(&key_lock);  
     printk("key_close free irqs\n");  
     return 0;  
 }  
 static int key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)  
 {  
 //    unsigned int err;  
     //判斷是阻塞讀還是非阻塞讀  
     if(filp->f_flags & O_NONBLOCK) {  
       if(!ev_press)  return -EAGAIN;  
     } 
     else {  
       /*if ev_press==0,then sleep*/  
       /*阻塞,當有按鍵按下時(中斷)被喚醒*/  
       wait_event_interruptible(key_waitq,ev_press);  
     }  
     //阻塞結束,有鍵按下了  
     ev_press = 0;  
     //拷貝資料到使用者空間  
     copy_to_user(buff,(const void *)press_cnt,min(sizeof(press_cnt),count));  
     memset((void *)press_cnt,0,sizeof(press_cnt));  
 //    printk("read and clean press_cnt\n");  
     return 1;  
 }  
 static struct file_operations key_ops = {  
     .owner     = THIS_MODULE,  
     .open     = key_open,  
     .release = key_close,  
     .read     = key_read,  
     .poll     = key_poll,  
     .fasync     = key_fasync,  
 };  
 static struct cdev *cdev_keyint;  
 static struct class *keyint_class;  
 //模組初始化函式  
 static int __init s3c6410_keyint_init(void) {  
     int val; 
     /*timer initial */  
     init_timer(&key_timer);  
     key_timer.function = key_timer_func;  
     add_timer(&key_timer);  
         /*初始化訊號量*/  
        init_MUTEX(&key_lock);  
     /*register device*/  
     val = alloc_chrdev_region(&devid,0,1,DEVICE_NAME);  
     if(val) {  
       return -1;  
       printk("register keyint error\n");  
     }  
     cdev_keyint = cdev_alloc();  
     cdev_init(cdev_keyint, &key_ops);  
     cdev_keyint->owner = THIS_MODULE;  
     cdev_keyint->ops   = &key_ops;  
     val = cdev_add(cdev_keyint,devid,1); 
     if(val) {
       return -1;  
       printk("add device error\n");  
     }  
     keyint_class = class_create(THIS_MODULE,DEVICE_NAME);  
     device_create(keyint_class,NULL,devid,NULL,"%s",DEVICE_NAME);  
     printk("KEY initialezed ^_^\n");  
     return 0;  
 }  
 static void __exit s3c6410_keyint_exit(void)  
 { 
      cdev_del(cdev_keyint);  
     device_destroy(keyint_class,devid);  
     class_destroy(keyint_class);  
     unregister_chrdev_region(devid,1); 
 } 
 module_init(s3c6410_keyint_init);  
 module_exit(s3c6410_keyint_exit); 

2.編寫Makefile檔案

ifneq ($(KERNELRELEASE),)
obj-m := driver_key.o
else 
KDIR := /home/kk/Desktop/forlinx/linux-3.0.1
all:
	make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
	 rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif

3.編寫執行檔案

app_key.c

//app_key.c
#include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <sys/ioctl.h>  
 int main(int argc, char **argv)  
 {  
     int fd;  
     int val;  
     int i;  
     int key_value[6];  
     fd = open("/dev/keyint",0);  
     if(fd<0) {  
       printf("open devie error\n");  
       return -1;  
     }  
     while(1) {  
       val = read(fd,key_value, sizeof(key_value));  
       if(val<0) {  
         printf("read error\n");  
         continue;  
       }  
       for (i=0;i<6;i++) {  
          if(key_value[i])  
          printf("KEY%d pressed\n",(i+1),key_value[i]);  
       }   
     }  
     close(fd);  
     return 0;  
 }  

4.編譯驅動程式與測試程式

 #make

將編譯生成 driver_key.ko等檔案

#arm-linux-gcc  app_key.c  -o  anjian

將生成anjian可執行檔案

最後呈現以下檔案

5.修改系統核心檔案

在原有到核心中,按鍵的GPIO口被佔用,需要進行相應到修改才能達到預期到效果,首先需要做的是安裝libncurses 的相關軟體,來實現對核心到編寫
核心編寫過程:
找到核心

make menuconfig

按照下面一路選擇
Device Drivers
Input device support
keyboards
有GPIO Buttons
把這個選項去掉

make zImage

生成zImage映象檔案
之後重新燒寫

6.格式化SD卡,把 SD 卡格式化為 FAT32 格式。

7.用SD_Writer將 mmc.bin 燒寫到 SD 卡中

1.以管理員身份執行
2.點選”Scan”,這個步驟是自動搜尋 SD 卡所在碟符。如果"Scan"沒有正確設定 SD 卡所在碟符,就需要手動 調整 SD Volume,把碟符號調整為 SD 卡所在碟符(比如說,PC 的 USB 口接了兩個或者兩個以上的 U 盤或 者 SD 卡,就有可能錯誤到掃描 SD 卡碟符)。
3.將”SD Type”更改為 auto。這個步驟是為了讓 SD_Writer 自動識別 SD 卡型別。
4.將”OS Type”更改為 Linux。這個步驟是選擇要燒寫的系統型別。
5.點選”Select Boot”, 選擇適合自己開發板的 mmc.bin
mmc_ram128.bin 適用於 128M 記憶體的開發板
mmc_ram256.bin 適用於 256M 記憶體的開發板
6.點選”Program”,出現”It’s OK”表示操作成功。

8.拷貝系統檔案

首先,將 u-boot.bin 拷貝到 SD 卡中。
u-boot_ram128.bin 專門用於 128M 記憶體開發板。
u-boot_ram256.bin 專門用於 256M 記憶體開發板。
將與開發板對應的 u-boot 拷貝到 SD 卡中。接著在 SD 卡中將檔名改為u-boot.bin 即可。

然後,將 zImage 拷貝到 SD 卡中。zImage 是 Linux 的核心映像檔案。

最後,將 rootfs.yaffs2 拷貝到 SD 卡中。
rootfs.yaffs2-nand256m 專門用於 128M 記憶體,256M NandFlash開發板。
rootfs.yaffs2-nand2g 專門用於 256M 記憶體,1G 或 2G 或者 4G Nandflash 的開發板

9.拷貝驅動程式與測試程式

將driver_led.ko與test拷貝到SD卡上

10.燒寫Linux到開發板的NandFlash

步驟 1. 將製作好的 SD 卡插入開發板 SD 的插槽。

步驟 2. 接好 5V 直流電源(飛凌提供此電源,請使用飛凌提供的電源)。

步驟 3. 撥碼開關設定為 SD 卡啟動。

撥碼開關在底板SD 卡啟動的撥碼開關設定如下:

引腳號 Pin8 Pin7 Pin6 Pin5 Pin4 Pin3 Pin2 Pin1
引腳定義 SELNAND OM4 OM3 OM2 OM1 GPN15 GPN14 GPN13
SD卡啟動 1 1 1 1 1 0 0 0

11. 測試

1.開啟終端
2.載入驅動
#insmod /sdcard/driver_key.ko
3.建立裝置檔案
#mknod /dev/my_led c 240 0
4.測試
./anjian
5.解除安裝驅動
#rmmod driver_key

五、實驗程式(包括流程圖)

1.編寫驅動程式

driver_key.c

//driver_key.c
#include <linux/kernel.h>  
 #include <linux/module.h>  
 #include <linux/init.h>  
 #include <linux/fs.h>  
 #include <linux/gpio.h>  
 #include <linux/types.h> 
 #include <linux/cdev.h>  
 #include <linux/interrupt.h>  
 #include <linux/sched.h>  
 #include <linux/device.h>  
 #include <linux/poll.h>  
 #include <linux/semaphore.h>  
 #include <linux/timer.h>  
 #include <asm/irq.h>  
 #include <asm/uaccess.h>  
 #include <mach/hardware.h>  
 #include <mach/irqs.h>  
   MODULE_LICENSE("GPL");  
 #define DEVICE_NAME "keyint"  
 #define KEYNUM 6  
 dev_t devid;  
 //static DEFINE_SEMAPHORE(key_lock);  //declare a mutex lock for keyint  
   //定義一個訊號量  
 struct semaphore key_lock;  
 static struct fasync_struct *key_async;   
 static struct timer_list key_timer;  
 struct key_irq_desc {  
     int irq;        //irq num  
     unsigned long flags;    //irq flags,identified the way of irq here,eq.edge,level  
     char *name;        //irq name  
 };  
 static struct key_irq_desc key_irqs[] = {  
     //下降沿產生中斷  
     {IRQ_EINT(0), IRQF_TRIGGER_FALLING, "KEY1"},  
     {IRQ_EINT(1), IRQF_TRIGGER_FALLING, "KEY2"},  
     {IRQ_EINT(2), IRQF_TRIGGER_FALLING, "KEY3"},  
     {IRQ_EINT(3), IRQF_TRIGGER_FALLING, "KEY4"},  
     {IRQ_EINT(4), IRQF_TRIGGER_FALLING, "KEY5"},  
     {IRQ_EINT(5), IRQF_TRIGGER_FALLING, "KEY6"},  
 };  
 /*define a waiting queue here*/  
 static DECLARE_WAIT_QUEUE_HEAD(key_waitq);  
 /*define a event flag ev_press*/  
 static volatile int ev_press = 0;  
 static volatile int press_cnt[KEYNUM] = {0,0,0,0,0,0};  
 /*中斷處理函式*/  
 static irqreturn_t keys_interrupt(int irq, void *dev_id)  
 { 
     volatile int *press_cnt = (volatile int *) dev_id;  
        /*set the pressed key flag(must do here due to not be static value)*/  
     *press_cnt = *press_cnt + 1;  
     //延時10ms後執行定時器處理函式  
     mod_timer(&key_timer,jiffies+HZ/100);        //start timer after 10ms  
     return IRQ_RETVAL(IRQ_HANDLED);  
 } 
//定時器處理函式  
 static void key_timer_func(unsigned long data)  
 {
     ev_press = 1;  
     //喚醒等待佇列  
     wake_up_interruptible(&key_waitq);  
     kill_fasync(&key_async, SIGIO, POLL_IN);  
 }  
 static int key_fasync(int fd, struct file *filp, int on)  
 {  
     printk("Function key_fasync\n");  
     return fasync_helper(fd,filp,on,&key_async);  
 }  
 static unsigned key_poll(struct file *file, poll_table *wait)  
 {  
     unsigned int mask=0;  
     //指明要使用的等待佇列  
     poll_wait(file,&key_waitq,wait);  
    //返回掩碼  
     if(ev_press)  
     mask |= POLL_IN | POLLRDNORM; 
     printk("poll wait\n"); 
     return mask;  
 }  
 static int key_open(struct inode *inode, struct file *file)  
 {  
     int num;  
     if(file->f_flags & O_NONBLOCK) {  
       if(down_trylock(&key_lock)) return -EBUSY;  
     }  
     else {  
       down(&key_lock);  
     }  
     //為每個按鍵註冊中斷處理程式  
     for(num=0;num<KEYNUM;num++) {  
       request_irq(key_irqs[num].irq, keys_interrupt, key_irqs[num].flags, key_irqs[num].name, (void *)&press_cnt[num]);  
     } 
     return 0;  
 }
 static int key_close(struct inode *inode, struct file *file)  
 { 
     int num;  
     //釋放中斷號  
     for(num=0;num<6;num++) {  
       free_irq(key_irqs[num].irq, (void *)&press_cnt[num]);  
     }  
     up(&key_lock);  
     printk("key_close free irqs\n");  
     return 0;  
 }  
 static int key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)  
 {  
 //    unsigned int err;  
     //判斷是阻塞讀還是非阻塞讀  
     if(filp->f_flags & O_NONBLOCK) {  
       if(!ev_press)  return -EAGAIN;  
     } 
     else {  
       /*if ev_press==0,then sleep*/  
       /*阻塞,當有按鍵按下時(中斷)被喚醒*/  
       wait_event_interruptible(key_waitq,ev_press);  
     }  
     //阻塞結束,有鍵按下了  
     ev_press = 0;  
     //拷貝資料到使用者空間  
     copy_to_user(buff,(const void *)press_cnt,min(sizeof(press_cnt),count));  
     memset((void *)press_cnt,0,sizeof(press_cnt));  
 //    printk("read and clean press_cnt\n");  
     return 1;  
 }  
 static struct file_operations key_ops = {  
     .owner     = THIS_MODULE,  
     .open     = key_open,  
     .release = key_close,  
     .read     = key_read,  
     .poll     = key_poll,  
     .fasync     = key_fasync,  
 };  
 static struct cdev *cdev_keyint;  
 static struct class *keyint_class;  
 //模組初始化函式  
 static int __init s3c6410_keyint_init(void) {  
     int val; 
     /*timer initial */  
     init_timer(&key_timer);  
     key_timer.function = key_timer_func;  
     add_timer(&key_timer);  
         /*初始化訊號量*/  
        init_MUTEX(&key_lock);  
     /*register device*/  
     val = alloc_chrdev_region(&devid,0,1,DEVICE_NAME);  
     if(val) {  
       return -1;  
       printk("register keyint error\n");  
     }  
     cdev_keyint = cdev_alloc();  
     cdev_init(cdev_keyint, &key_ops);  
     cdev_keyint->owner = THIS_MODULE;  
     cdev_keyint->ops   = &key_ops;  
     val = cdev_add(cdev_keyint,devid,1); 
     if(val) {
       return -1;  
       printk("add device error\n");  
     }  
     keyint_class = class_create(THIS_MODULE,DEVICE_NAME);  
     device_create(keyint_class,NULL,devid,NULL,"%s",DEVICE_NAME);  
     printk("KEY initialezed ^_^\n");  
     return 0;  
 }  
 static void __exit s3c6410_keyint_exit(void)  
 { 
      cdev_del(cdev_keyint);  
     device_destroy(keyint_class,devid);  
     class_destroy(keyint_class);  
     unregister_chrdev_region(devid,1); 
 } 
 module_init(s3c6410_keyint_init);  
 module_exit(s3c6410_keyint_exit); 

2.編寫Makefile檔案

ifneq ($(KERNELRELEASE),)
obj-m := driver_key.o
else 
KDIR := /home/kk/Desktop/forlinx/linux-3.0.1
all:
	make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
	 rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif

3.編寫執行檔案

app_key.c

//app_key.c
#include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <sys/ioctl.h>  
 int main(int argc, char **argv)  
 {  
     int fd;  
     int val;  
     int i;  
     int key_value[6];  
     fd = open("/dev/keyint",0);  
     if(fd<0) {  
       printf("open devie error\n");  
       return -1;  
     }  
     while(1) {  
       val = read(fd,key_value, sizeof(key_value));  
       if(val<0) {  
         printf("read error\n");  
         continue;  
       }  
       for (i=0;i<6;i++) {  
          if(key_value[i])  
          printf("KEY%d pressed\n",(i+1),key_value[i]);  
       }   
     }  
     close(fd);  
     return 0;  
 }  

4.流程圖

六、執行結果

按鍵按下的螢幕顯示:

七、心得體會