新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > ARM嵌入式LINUX設(shè)備驅(qū)動設(shè)計入門學(xué)習(xí)

ARM嵌入式LINUX設(shè)備驅(qū)動設(shè)計入門學(xué)習(xí)

作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
經(jīng)過一段時間的學(xué)習(xí)之后,也開發(fā)了一些小型的驅(qū)動,正如我之前一篇中寫到得,現(xiàn)在我就來寫一下在ARM嵌入式LINUX下如何設(shè)計驅(qū)動的框架。

在這里我用的板子是micro2440板子,板子上的linux版本是2.6.13。因為我在前一篇介紹了驅(qū)動編程的兩種框架設(shè)計,所以現(xiàn)在我就來分別寫一下這兩種框架設(shè)計的程序。

本文引用地址:http://butianyuan.cn/article/201611/318469.htm

開發(fā)平臺:RED HAT LINUX 9(Linux 2.4.18)

開發(fā)板:micro2440(友善之臂)(Linux 2.6.13)

交叉編譯工具:arm-linux-gcc-3.4.1

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.4內(nèi)核驅(qū)動框架:

static int __init leds_init(void)
{
int result;
int i;

result = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops);
if(result < 0){
printk(DEVICE_NAME "cant register major number/n");
return result;
}

//static devfs_handle_t devfs_handle;
//devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, LED_MAJOR, &leds_fops, NULL);

/*

*之所以不用devfs_register,而用devfs_mk_cdev,原因是因為在linux2.6內(nèi)核里沒有devfs_register函數(shù),而改用*devfs_mk_cdev

*/
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

for(i = 0; i < 4; i++){
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);
}

printk(DEVICE_NAME "initialized/n");
return 0;
}

static void __exit leds_exit(void)
{
devfs_remove(DEVICE_NAME);
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.6內(nèi)核驅(qū)動框架:

static int __init leds_init(void)
{
int result;
int i;
dev_t dev = 0;

dev = MKDEV(LED_MAJOR, 0);
result = register_chrdev_region(dev, 1, DEVICE_NAME);
if (result < 0)
{
printk(KERN_WARNING "DEMO: cant get major %d/n", LED_MAJOR);
return result;
}

LED_devices = kmalloc(sizeof(struct DEMO_dev), GFP_KERNEL);
if (!LED_devices)
{
result = -ENOMEM;
goto fail;
}
memset(LED_devices, 0, sizeof(struct DEMO_dev));

cdev_init(&LED_devices->cdev, &leds_fops);
LED_devices->cdev.owner = THIS_MODULE;
LED_devices->cdev.ops = &leds_fops;
result = cdev_add (&LED_devices->cdev, dev, 1);
if(result)
{
printk(KERN_NOTICE "Error %d adding DEMO/n", result);
goto fail;
}
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

for(i = 0; i < 4; i++){
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);
}

printk(DEVICE_NAME "initialized/n");
return 0;
fail:
leds_exit();
return result;

}

static void __exit leds_exit(void)
{
dev_t devno = MKDEV(LED_MAJOR, 0);

if (LED_devices)
{
devfs_remove(DEVICE_NAME);
cdev_del(&LED_devices->cdev);
kfree(LED_devices);
}

unregister_chrdev_region(devno,1);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

在這里我事先都寫好了文件才做結(jié)構(gòu)體,和操作函數(shù)ioctl,下面我就來介紹介紹

static int leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd){
case 0:
case 1:
if(arg > 4){
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}



關(guān)鍵詞: ARMLINUX設(shè)備驅(qū)

評論


技術(shù)專區(qū)

關(guān)閉