嵌入式ARM平臺下的Linux字符設備驅動實例_第1頁
嵌入式ARM平臺下的Linux字符設備驅動實例_第2頁
嵌入式ARM平臺下的Linux字符設備驅動實例_第3頁
嵌入式ARM平臺下的Linux字符設備驅動實例_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、嵌入式 ARM 平臺下的 Linux 字符設備驅動實例(目6.1 下面以一個名為S3C2440_leds.c 的簡單控制目標板”LED 亮滅的驅動為例進行分析。標板為天嵌TQ2440 ; Linux2.6.25.8 ) 。主要功能是通過應用程序調用該驅動來按制目標板的四個 LED 燈的亮滅。驅動源程序如下:#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h&g

2、t;#include <asm/irq.h>#include <asm/arch/regs-gpio.h>#include <asm/hardware.h>cat /proc/devices ”命令看到#define DEVICE_NAME "leds" /* 加載模式后,執行”的設備名稱*/#define LED_MAJOR231/* 主設備號*/* 應用程序執行ioctl(fd, cmd, arg) 時的第 2 個參數 */#define IOCTL_LED_ON0#define IOCTL_LED_OFF1 /* 用來指定 LED

3、所用的 GPIO 引腳 */ static unsigned long led_table = S3C2410_GPB5,S3C2410_GPB6,S3C2410_GPB7,S3C2410_GPB8,;/* 用來指定 GPIO 引腳的功能:輸出 */ static unsigned int led_cfg_table = S3C2410_GPB5_OUTP,S3C2410_GPB6_OUTP,S3C2410_GPB7_OUTP,S3C2410_GPB8_OUTP,;/* 應用程序對設備文件/dev/leds 執行 open() 時,* 就會調用 s3c24xx_leds_open */ sta

4、tic int s3c24xx_leds_open(struct inode *inode, struct file *file)int i;for(i=0; i<4; i+)s3c2410_gpio_cfpin(led_tablei, led_cfg_tablei)return 0;/* 應用程序對設備文件/dev/leds 執行 iotcl() 時,* 就會調用 s3c24xx_leds_iotcl*/static int s3c24xx_leds_iotcl(struct inode *inode, struct file *file, unsigned int cmd, unsi

5、gned long arg)if (arg > 4)return -EINV AL;switch(cmd) case IOCTL_LED_ON:s3c2410_gpio_setpin(led_tablearg, 0);return 0;case IOCTL_LED_OFF:s3c2410_gpio_setpin(led_tablearg, 1);return 0;default:return -EINV AL;/* 這個結構是字符設備驅動程序的核心* 當應用程序操作設備文件時調用的open、 read 等函數,* 最終會調用這個結構中指定的對應函數static sturct s3c24x

6、x_leds_fops = .owner = THIS_MODULE,.open = s3c24xx_leds_open,.ioctl = s3c24xx_leds_ioctl;/* 模塊的初始化函數*/static int _init s3c24xx_leds_init(void)int ret;ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);if(ret < 0)printk(DEVICE_NAME "can't register major numbern");r

7、eturn ret;printk(DEVICE_NAME "initializedn");return 0;/* 模塊的撤銷函數*/static void _exit s3c24xx_leds_exit(void)unregister_chrdev(LED_MAJOR, DEVICE_NAME);/* 指定驅動程序的初始化函數和卸載函數*/module_init(s3c24xx_leds_init);module_exit(s3c24xx_leds_exit);/* 加入描述信息*/MODULE_AUTHOR("ckz");MODULE_DESCRIPT

8、ION("S3C2440 LED Driver");MODULE_LICENSE("GPL");寫好的驅動放在內核源文件目錄下: linux2.6.25.8#drivers/char/6.2 以模塊形式編譯及加載修改同目錄下的“ Kconfig ”文件,在合適的地方添加如下內容:Config S3C2440_LEDStristate “ S3C2440 LEDS Driver ”depends on ARCH_S3C2440helpLEDS on S3C2440然后再通目錄下修改“ Makefile ” ,添加如下內容:Obj-$(CONFIG_ S3C

9、2440_LEDS)+= S3C2440_leds o添加完成以上內容之后,輸入 #make menuconfig ,然后配置如下Device Drivers -Character devices -><M> S3C2440 LEDS Driver將其選擇為“ M ” ,然后保存配置,編譯出內核鏡像燒寫到開發板中。然后再使用命令#make SUBDIR=drivers/char modules ,編譯出驅動模塊,在內核目錄下的“drivers/char”下面,名為 S3c2440_leds.Ko ,將其復制到開發板中的 /lib目錄中。 加載、卸載驅動到目標系統中。在/lib

10、目錄下:* insmod S3C2440_leds.KO /* 加載驅動 */* rmmod S3C2440_leds.KO /* 卸載驅動 */6.3 下面編寫簡單的應用程序來測試剛才的驅動程序,新建名為 leds.c 的文件#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>int main(int argc, char *argv)int on;int led_no;int fd;if (argc != 3 | sscanf(argv1, "%d", &led_no) != 1 | sscanf(argv2,"%d", &on) != 1 |on < 0 | on > 1 | led_no < 1 | led_no > 4) fprintf(stderr, "Usage: leds led_no 0|1n");exit(1);fd = open("/dev/GPIO-Control", 0);if (fd < 0) perror("open device l

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論