歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 【Linux驅動】TQ2440 LED驅動程序

【Linux驅動】TQ2440 LED驅動程序

日期:2017/3/1 9:42:57   编辑:Linux編程

★總體介紹

LED驅動程序主要實現了TQ2440開發板上的4個LED燈的硬件驅動,實現了對引腳GPIOB5、GPIOB6、GPIOB7、GPIOB8的高低電平設置(common-smdk.c中已經實現了對引腳的配置),利用測試程序調用該驅動程序,通過命令控制LED燈的亮滅。

Ubuntu下搭建TQ2440的程序下載環境 http://www.linuxidc.com/Linux/2011-03/32869.htm

Ubuntu 12.04(32位)下TQ2440開發板環境搭建 http://www.linuxidc.com/Linux/2014-04/100085.htm

★詳細介紹

1、驅動程序代碼:My_led.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/device.h>

#define DEVICE_NAME "My_led" /**加載模塊後執行cat/proc/devices中看到的設備名稱**/
#define Led_MAJOR 103 /**主設備號**/
#define LED_ON 1
#define LED_OFF 0

/**Led的控制引腳**/
static unsigned long led_table[ ] =
{
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};

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

static int My_led_ioctl(struct inode * inode, struct file * file,unsigned int cmd,unsigned long arg)
{
if(arg > 4)
{
return -1;
}
switch(cmd)
{
case LED_ON:
s3c2410_gpio_setpin(led_table[arg], 0);//設置指定引腳為輸出電平為0
return 0;
case LED_OFF:
s3c2410_gpio_setpin(led_table[arg], 1);//設置指定引腳為輸出電平為1
return 0;
default:
return -1;
}
}

static struct file_operations My_led_fops =
{
.owner = THIS_MODULE,
.open = My_led_open,
.ioctl = My_led_ioctl,
};

static struct class *led_class;

static int __init My_led_init(void)
{
int ret;
printk("My_led start\n");

/**注冊字符設備驅動程序**/
/**參數為主設備號、設備名字、file_operations結構**/
/**這樣主設備號就與file_operations聯系起來**/
ret = register_chrdev(Led_MAJOR, DEVICE_NAME, &My_led_fops);
if(ret < 0)
{
printk("can't register major number\n");
return ret;
}

//注冊一個類,使mdev可以在"/dev/目錄下建立設備節點"
led_class = class_create(THIS_MODULE, DEVICE_NAME);
if(IS_ERR(led_class))
{
printk("failed in My_led class.\n");
return -1;
}
device_create(led_class, NULL, MKDEV(Led_MAJOR,0), NULL, DEVICE_NAME);
printk(DEVICE_NAME "initialized\n");
return 0;

}

static void __exit My_led_exit(void)
{
unregister_chrdev(Led_MAJOR, DEVICE_NAME);
device_destroy(led_class, MKDEV(Led_MAJOR,0));//注銷設備節點
class_destroy(led_class);//注銷類
}

module_init(My_led_init);
module_exit(My_led_exit);

MODULE_LICENSE("GPL");

2、宏定義

#define DEVICE_NAME "My_led" //加載模塊後執行cat/proc/devices中看到的設備名稱

#define Led_MAJOR 103 //主設備號

#define LED_ON 1

#define LED_OFF 0 My_led_ioctl函數中要輸入的參數命令,LED_ON會執行打開燈的命令、LED_OFF執行關閉燈的命令

3、Led燈的引腳控制

定義一個led_table[ ] 數組,在後面調用時要方便些。

詳細說明一下S3C2410_GPB5是什麼意思:



如上圖所示,是關於S3C2410_GPB5相關的所有代碼。通過上面的代碼可以得出S3C2410_GPB5為37。實際上S3C2410_GPB5就是端口的編號,bank是分組的基號碼,offset是組內的偏移量。TQ2440開發板的GPIOB的第五個引腳為37。

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-06/103328p2.htm

Copyright © Linux教程網 All Rights Reserved