Linux驅動 12 LED驅動

2021-10-01 08:16:20 字數 4598 閱讀 9141

2. 呼叫led驅動

1.1 led管腳的呼叫配置函式

gpio_request:gpio申請函式

gpio_set_value:gpio賦值函式

s3c_gpio_cfgpin:gpio配置函式,比如配置成輸入模式或者輸出模式(三星平台)

s3c_gpio_output:gpio配置成輸出模式的巨集定義(三星平台)

本例中開發板上的控制led管腳的gpio為2.0引腳。

1.2 led驅動原始碼與注釋

led原始碼在linux驅動(11)中的裝置節點的基礎上更改的。

/*

name:led_driver.c

author:ethan

version:1.0

date:2019-12-8

*//*包含初始化巨集定義的標頭檔案,**中的module_init和module_exit再次檔案中)*/

#include

/*包含初始化載入模組的標頭檔案,**中的module_license在此標頭檔案中*/

#include

/*驅動註冊的標頭檔案,包含驅動的結構體以及註冊和解除安裝的函式*/

#include

/*註冊雜項裝置標頭檔案*/

#include

/*註冊裝置節點的檔案結構體*/

#include

/*linux中申請gpio的標頭檔案*/

#include

/*三星平台的gpio配置函式標頭檔案*/

/*三星平台exynos系列平台,gpio配置引數巨集定義標頭檔案*/

#include

#include

/*三星平台4412平台,gpio巨集定義標頭檔案*/

#include

/*宣告驅動名稱*/

#define driver_name "helloworld_ctl"

/*宣告裝置節點名稱*/

#define device_name "helloworld_ctl"

/*宣告是開源的,沒有核心版本限制*/

module_license

("dual bsd/gpl");

/*宣告作者*/

module_author

("ethan");

/*定義文件開啟函式*/

static

intdevice_node_open

(struct inode *inode,

struct file *file)

/*定義文件關閉函式*/

static

intdevice_node_release

(struct inode *inode,

struct file *file)

/*定義io控制函式*/

static

long

device_node_ioctl

(struct file *file,

unsigned

int cmd,

unsigned

long arg)

if(arg >1)

/*根據傳入的引數設定目標io的值*/

gpio_set_value

(exynos4_gpl2(0

),cmd)

;return0;

}/*定義檔案操作結構體*/

struct file_operations ops_device_node =

;/*定義雜項裝置節點結構體*/

struct miscdevice misc_device_node =

;/*定義驅動的probe函式*/

static

inthello_probe

(struct platform_device *pdv)

/*將目標io設定為輸出模式*/

s3c_gpio_cfgpin

(exynos4_gpl2(0

),s3c_gpio_output)

;/*初始化將目標io的輸出*/

gpio_set_value

(exynos4_gpl2(0

),0)

;/*註冊雜項裝置*/

misc_register

(&misc_device_node)

;return0;

}/*定義驅動的remove函式*/

static

inthello_remove

(struct platform_device *pdv)

/*定義驅動的shutdown函式*/

static

void

hello_shutdown

(struct platform_device *pdv)

/*定義驅動的suspend函式*/

static

inthello_suspend

(struct platform_device *pdv)

/*定義驅動的resume函式*/

static

inthello_resume

(struct platform_device *pdv)

/*定義驅動結構體*/

struct platform_driver helloworld_driver =};

/*編寫初始化函式*/

static

inthello_init

(void

)//編寫解除安裝函式

static

void

hello_exit

(void

)/*初始化函式*/

module_init

(hello_init)

;/*解除安裝函式*/

module_exit

(hello_exit)

;

1.3 led驅動載入步驟

步驟為下:

① 將led_driver.c和makefile檔案移動到linux系統中的某一資料夾下(任意地方或者新建都可以)。

② 執行命令:

make
執行完畢之後可以在該資料夾下生成.ko檔案,該檔案就是編譯生成的驅動。

③ 我們通過開發板來驗證該驅動是否編寫與編譯成功:

—>將.ko檔案拷貝到u盤中

—>用命令

mount /dev/sda1 /mnt/disk/
將u盤掛載。

—>可以用命令

ls /mnt/disk/
來列出已經編譯成功的.ko檔案

—>用載入命令,來載入我們編譯好的驅動

insmod /mnt/disk/led_driver.ko
載入結果為:

—>用解除安裝命令,來解除安裝剛裝好的helloworld驅動

rmmod led_driver
2.1 原始碼
/*

name:led_driver_call.c

author:ethan

version:1.0

date:2019-12-8

*//*呼叫列印函式printf*/

#include

/*基本系統資料型別*/

#include

/*系統呼叫函式標頭檔案,可以呼叫普通檔案、目錄、管道、socket、字元、塊的屬性*/

#include

/*定義了open函式*/

#include

/*定義了close函式*/

#include

/*定義了ioctl函式*/

#include

main()

else

/*關閉檔案*/

close

(fd)

;}

2.2 步驟

① 在linux系統中任意位置新建乙個資料夾:

mkdir led_driver_call
② 複製led_driver_call.c在新建的資料夾中

③ 利用交叉編譯器編譯該檔案:

arm-none-linux-gnueabi-gcc -o led_driver_call led_driver_call.c -static
④ 將生成的二進位制檔案拷貝到u盤中

⑤ 將u盤掛載到開發板中:

mount /dev/sda1 /mnt/disk/
⑥ 執行二進位制檔案即可呼叫led驅動:

/mnt/disk/led_driver_call
執行結果為:

在開發板上看到的結果是led先亮3s,然後熄滅3s,然後再次點亮。

linux驅動之 led驅動

練手,第乙個字元驅動.用模組載入方法 華清遠見 嵌入式linux裝置驅動開發詳解 的 拿來改的.編譯過程發現很多錯誤.最後發現 這本書帶的驅動 都是基於linux2.4的.目前我用的linux2.6,部分需要做修改.我的板子是 友善之臂的 2410.vmware ubuntu nfs交叉編譯 首先 ...

Linux驅動 LED驅動測試

環境 主機 fedora12 目標板 mini6410 目標板linux核心版本 2.6.38 實現功能 驅動目標板4個led.說明 led與6410引腳對應連線 led1 gpk4 led2 gpk5 led3 gpk6 led4 gpk7 驅動源 led driver.c cpp view pl...

linux驅動 1 LED驅動 dev led

led linux驅動程式 測試平台 xunlong orange pi zero 驅動程式以及makefile如下 include include include include include include static struct class sun8i opizero led class...