第一、二期銜接——3.1 字符驅動設備—LED設備驅動框架搭建

字符驅動設備之LED設備驅動框架搭建

  • 硬件平臺:韋東山嵌入式Linxu開發板(S3C2440.v3)
  • 軟件平臺:運行於VMware Workstation 12 Player下UbuntuLTS16.04_x64 系統
  • 參考資料:《嵌入式Linux應用開發手冊》
  • 開發環境:Linux 2.6.22.6 內核、arm-linux-gcc-3.4.5-glibc-2.3.6工具鏈


一、軟件系統的大致分層

在這裏插入圖片描述
我們從上往下分析:
①、在應用程序中:執行openread等函數時,實際上會觸發swi異常

②、在庫中:進行系統調用,執行swi指令(swi val,每個函數都有對應的val值)引起CPU的異常

③、在內核中:系統調用接口根據發生異常的原因,即val值執行虛擬文件系統中的sys_opensys_read等函數。

④、在驅動程序中:sys_opensys_read等函數 調用 對應驅動程序中的xxx_openxxx_read函數(這個類似於C++中的多態),執行實現不同的功能

二、Linux驅動程序的分類

1、字符設備(Character device)

  字符設備如其名所示,是以字節流的形式訪問設備的,如按鍵,串口,聲卡,觸摸屏等,存取時沒有緩存

2、塊設備(Block device)

  塊設備,即數據是以塊的形式存放的,所以通常在數據存放時會按照一定的格式(通過文件系統的類型定義),需要一定的緩存來支持。

3、網絡接口(Network interface)

  網絡接口有上述兩種驅動的特點,但是又有所不同。其訪問必須通過套接字,結合TCP/IP協議棧來使用

三、初步的代碼框架

我這個裏的編程方式是參考linux-2.6.22.6\drivers\leds\leds-s3c24xx.c目錄下的源碼,進行編寫的。

1、編寫open、write函數

新建一個驅動程序文件first_drv.c,參考內核的源碼編寫first_drv_open()、first_drv_write()函數

static int first_drv_open(struct inode *inode, struct file *file)
{
	printf("first_drv_open\n");
	return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	printf("first_drv_write\n");
	return 0;
}

2、如何告訴內核有這個驅動程序

2.1 構建file_operations()結構體

參考內核源碼,定義結構體,根據需求添加函數指針。
在這裏插入圖片描述

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,	   
};

2.2 定義驅動的入口函數first_drv_init()、註冊驅動register_chrdev()

內核源碼:
在這裏插入圖片描述

  • major:主設備號,先設置爲111
  • name:設備名稱,可自定義
  • fops:文件系統的接口指針,即上述定義的結構體名稱
static int first_drv_init(void)
{
	register_chrdev(111, "first_drv", &first_drv_fops); // 註冊, 告訴內核
	return 0;
}

2.3 通過module_init()修飾入口函數,讓內核怎麼知道哪個設備對應哪個的入口

module_init(first_drv_init);

2.4 添加出口函數first_drv_exit()module_exit()修飾入口函數,用於卸載驅動

static void first_drv_exit(void)
{
	unregister_chrdev(major, "first_drv"); 
}

module_exit(first_drv_exit);

3、Makefile文件編寫

參考內核源碼:

KERN_DIR = /work/system/linux-2.6.22.6

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= first_drv.o

4、完整文件

#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>

static int first_drv_open(struct inode *inode, struct file *file)
{
	printk("first_drv_open\n");
	return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	printk("first_drv_write\n");
	return 0;
}

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,   
};

/* 入口函數 */
static int first_drv_init(void)
{
	register_chrdev(252, "first_drv", &first_drv_fops); // 註冊, 告訴內核
	return 0;
}

/* 出口函數 */
static void first_drv_exit(void)
{
	unregister_chrdev(252, "first_drv"); 
}

/* 修飾 */
module_init(first_drv_init);
module_exit(first_drv_exit);

四、編譯文件

1、編譯

執行make命令後就生成如下文件
在這裏插入圖片描述

2、傳輸

利用NFS網絡文件繫系統,把.ko文件傳輸到開發板上
在這裏插入圖片描述

3、加載驅動

在開發板上的根文件系統下執行insmod first_drv.ko,加載驅動
cat /proc/devices可以看到加載驅動前,根文件系統下的驅動有:
在這裏插入圖片描述
加載驅動後:
在這裏插入圖片描述

五、測試文件

1、新建一個測試文件firsttest.c

代碼如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/xxx", O_RDWR);
	if (fd < 0)
	{
		printf("./firstdrvtest <on|off>");
	}
	return 0;
}

2、上傳到開發板的根文件系統,新建文件

根據代碼中的/dev/xxx執行新建命令mkdir /dev/xxx 111 0,執行應用程序./firstdrvtest,可以看到成功調用了驅動程序。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章