字元裝置驅動(二)按鍵點燈
阿新 • • 發佈:2018-11-22
目錄
title: 字元裝置驅動(二)按鍵點燈
tags: linux
date: 2018-11-21 18:06:37
toc: true
---
字元裝置驅動(二)按鍵點燈
資料互動
使用者記憶體和核心記憶體是獨立的,在各自的地址空間實現。核心與使用者函式互動需要使用copy_from_user
和copy_to_user
.為什麼?點這個
- 直接使用那個地址很不安全,如果應用程式傳過來一個非法地址,就有可能panic系統。或者應用程式可以給出一個指標指向kernel地址(>PAGE_OFFSET),那樣就可以隨意訪問kernel page資料。
- 對於大多數有
MMU
的平臺,情況就有了一些變化:使用者空間傳過來的指標是在虛擬地址空間上的,它指向的虛擬地址空間很可能還沒有真正對映到實際的物理頁面上。
實體地址訪問
Linux 的驅動程式直接操作不了實體地址,需要使用虛擬地址去對映,使用函式ioremap
,釋放使用iounmap
gpio_va = ioremap(0x56000000, 0x100000);
iounmap(gpio_va);
查詢方式按鍵
目的: 通過查詢方式查到按鍵按下,列印按鍵值,如果按鍵被按下的話,點亮led.
驅動程式設計
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> volatile unsigned long *gpfcon; volatile unsigned long *gpfdat; volatile unsigned long *gpgcon; volatile unsigned long *gpgdat; static struct class *drv_class; static struct class_device *drv_class_dev; static int drv_open(struct inode *inode, struct file *file) { int minor = MINOR(inode->i_rdev); printk("drv_open=%d\n",minor); /* 配置GPF0,2為輸入引腳 */ *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2))); /* 配置GPG3,11為輸入引腳 */ *gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2))); /* 配置GPF4,5,6為輸出 */ *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2))); *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2))); return 0; } static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { //int minor = MINOR(file->f_dentry->d_inode->i_rdev); //printk("drv_write=%d\n",minor); int val; copy_from_user(&val, buf, count); // copy_to_user(); if (val == 1) { // 點燈 *gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); } else { // 滅燈 *gpfdat |= (1<<4) | (1<<5) | (1<<6); } return 0; } static ssize_t drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { /* 返回4個引腳的電平 */ unsigned char key_vals[4]; int regval; if (size != sizeof(key_vals)) return -EINVAL; /* 讀GPF0,2 */ regval = *gpfdat; key_vals[0] = (regval & (1<<0)) ? 1 : 0; key_vals[1] = (regval & (1<<2)) ? 1 : 0; /* 讀GPG3,11 */ regval = *gpgdat; key_vals[2] = (regval & (1<<3)) ? 1 : 0; key_vals[3] = (regval & (1<<11)) ? 1 : 0; copy_to_user(buf, key_vals, sizeof(key_vals)); return sizeof(key_vals); } static struct file_operations drv_fops = { .owner = THIS_MODULE, /* 這是一個巨集,推向編譯模組時自動建立的__this_module變數 */ .open = drv_open, .write = drv_write, .read = drv_read, }; static int major; static int drv_init(void) { int minor=0; major=register_chrdev(0, "drv", &drv_fops); // 註冊, 告訴核心 drv_class = class_create(THIS_MODULE, "drv"); drv_class_dev = class_device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xyz%d", minor); gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16); gpgdat = gpgcon + 1; return 0; } static void drv_exit(void) { unregister_chrdev(major, "drv"); // 解除安裝 class_device_unregister(drv_class_dev); class_destroy(drv_class); iounmap(gpfcon); iounmap(gpgcon); } module_init(drv_init); module_exit(drv_exit); MODULE_LICENSE("GPL");
測試程式設計
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main(int argc, char **argv) { int fd; int val = 1; unsigned char key_vals[4]; int cnt = 0; if (argc <= 1) {printf("nofile select !\n"); return ;} fd = open(argv[1], O_RDWR); if (fd < 0) { printf("can't open!\n"); } while (1) { read(fd, key_vals, sizeof(key_vals)); if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3]) { val=1; printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]); write(fd, &val, 4); } else { val=0; write(fd, &val, 4); } } return 0; }
測試
載入驅動
insmod dri.ko
測試執行
./test /dev/xyz0
按鍵按下,列印按鍵的同時點燈
執行後使用
ctrl+c
終止程式,使用ctrl+z
程式休眠,繼續輸入q
退出,使用ps
命令還能看到,但是使用top
命令看到使用cpu
為0,使用kill -9 pid
關閉程序CTRL+Z停止程序並放入後臺 jobs 顯示當前暫停的程序 fg %N 使第N個任務在前臺執行 bg %N 使第N個任務在後臺執行(%前有空格)
啟用後臺執行
./test /dev/xyz0 &
檢視下佔用率
top
,佔用99%
關閉程式才能解除安裝模組
ps 檢視任務pid kill -9 pid #關閉程序 rmmod dri.ko 解除安裝程式