驅動開發3 簡單的led驅動

2021-10-06 10:11:38 字數 3869 閱讀 8710

一、字元裝置驅動框架

字元裝置驅動的編寫主要就是驅動對應的open、close、read。。。其實就是file_operations結構體的成員變數的實現。

二、驅動模組的載入與解除安裝

1、linux驅動程式有兩種存在形式:

編譯到kernel裡面,也就是zimage

編譯為模組,.ko。

tip:

1、編譯驅動的時候需要用到linux核心原始碼!因此要解壓縮linux核心原始碼,編譯linux核心原始碼。得到zimage和.dtb。需要使用編譯後的到的zimage和dtb啟動系統

2、從sd卡啟動,sd卡燒寫了uboot。uboot通過tftp從ubuntu裡面獲取zimage和dtb,rootfs也是通過nfs掛在。

3、設定bootcmd和bootargs

setenv bootargs root=/dev/nfs nfsroot=192.168.3.112:/rootfs rw console=ttysac2,115200 init=/linuxrc

setenv bootcmd tftp 41000000 uimage ; tftp 42000000 exynos4412-fs4412.dtb ; bootm 41000000 - 42000000

4、將編譯出來的.ko檔案放到根檔案系統裡面。

載入驅動會用到載入命令:insmod,modprobe。

移除驅動使用命令rmmod。對於乙個新的模組使用modprobe載入的時候需要先呼叫一下depmod命令。

5,驅動模組載入成功以後可以使用lsmod檢視一下。

6,解除安裝模組使用rmmod命令

三、字元裝置的註冊與登出

1、我們需要向系統註冊乙個字元裝置,使用函式register_chrdev。

2、解除安裝驅動的時候需要登出掉前面註冊的字元裝置,使用函式unregister_chrdev,登出字元裝置。

四、裝置號

1,linux核心使用dev_t。(用來儲存裝置號)

typedef __kernel_dev_t dev_t;

typedef __u32 __kernel_dev_t;

typedef unsigned int __u32;

2、linux核心將裝置號分為兩部分:

主裝置號和次裝置號。

主裝置號占用前12位,次裝置號占用低20位。(根據不同核心占用位數不同)

3、裝置號的操作函式,

a 、用巨集從dev_t獲取主裝置號和次裝置號

major(dev_t),

minor(dev_t)。

b、用函式把主裝置號和次裝置號構成dev_t,

mkdev(major,minor)

五、file_operations的具體實現主要注意ioctl,read,write,open,release函式實現使用

六、字元裝置驅動框架的搭建

#include

#include

#include

#include

#include

#include

#include

static

int major =

200;

static

int minor=0;

static dev_t devno;

static

struct class *cls;

static

struct device *test_device;

/* 暫存器實體地址 */

#define gpx2con 0x11000c40

#define gpx2dat 0x11000c44

#define gpx1con 0x11000c20

#define gpx1dat 0x11000c24

#define gpf3con 0x114001e0

#define gpf3dat 0x114001e4

/* 位址對映後的虛擬位址指標 */

static

int*pgpx2con ;

static

int*pgpx2dat;

static

int*pgpx1con ;

static

int*pgpx1dat;

static

int*pgpf3con ;

static

int*pgpf3dat;

void

fs4412_led_on

(int num)

}void

fs4412_led_off

(int num)

}static

int led_open (

struct inode *inode,

struct file *filep)

static

intled_release

(struct inode *inode,

struct file *filep)

static ssize_t led_read

(struct file *filep,

char __user *buf, size_t len, loff_t *pos)

static ssize_t led_write

(struct file *filep,

const

char __user *buf, size_t len, loff_t *pos)if(

copy_from_user

(&led_num,buf,len)

)return

-efault;

fs4412_led_on

(led_num)

;printk

("led_num =%d \n"

,led_num)

;return0;

}//編寫fop結構體

static

struct file_operations hello_ops=

;//編寫載入函式

static

void

fs4412_led_init

(void

)//編寫解除安裝函式

static

intled_init

(void)

test_device =

device_create

(cls,

null

,devno,

null

,"led");

//自動建立裝置節點if(

is_err

(test_device)

)fs4412_led_init()

;return0;

}void

fs4412_led_unmap

(void

)static

void

led_exit

(void

)module_license

("gpl");

//指定許可

module_init

(led_init)

;//指定載入函式

module_exit

(led_exit)

;//指定解除安裝函式

七、測 試

1、載入驅動。

modprobe led.ko

2,進入/dev檢視裝置檔案,

lsmod。/dev/chrdevbase。

3、測試

簡單的led驅動程式設計

基於ok6410 led驅動程式 vim led.c include include include include include include include led.h include define gpmcon 0x7f008820 define gpmdat 0x7f008824 uns...

驅動篇 乙個簡單的led驅動

1.構造裝置結構體 struct light dev cdev結構體 struct cdev 2.設定裝置資訊 struct light dev light devp 設定裝置結構體變數 int light major light major 設定主裝置號3.設定並填充file operations...

Linux驅動開發 LED流水燈驅動示例

include include include include include include include include include include include include include include define device name flowled define led ...