2.4内核注册驱动要用:
int register_chrdev (unsigned int major, const char *name, struct file_operations *fops);
2.4内核注销驱动要用:
int unregister_chrdev( unsigned int major, const char *name );
2.4内核驱动注册完后,要用以下代码创建设备文件
static devfs_handle_t devfs_handle;
devfs_handle = devfs_register( NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
BUTTON_MAJOR,&sbc2410_buttons_fops, NULL);
2.4内核驱动要用以下代码移除设备文件:
devfs_unregister( devfs_handle);
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
2.6驱动i注册设备号要用:
(1)如果主设备号事先知道,要用:
int register_chrdev_region( dev_t first, unsigned int count, char *name );
(2)如果主设备号为0,则要用动态分配:
int alloc_chrdev_region( dev_t *dev, unsigned int firstminor,
unsigned int count, char *name );
2.6释放设备号要用:
void unregister_chrdev_region( dev_t first, unsigned int count );
2.6内核字符设备驱动注册要用:
struct cdev *my_cdev = cdev_alloc();
my_cdev->ops = &chr_fops;
void cdev_init( struct cdev *cdev, struct file_operations *fops);
int cdev_add( struct cdev *dev, dev_t num, unsigned int count);
2.6内核字符设备驱动移除要用:
void cdev_del( struct cdev *dev );
2.6内核驱动注册完后,要用以下代码创建设备文件
devfs_mk_cdev( MKDEV(LED_MAJOR, LED_MINOR),
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
2.6内核驱动要用以下代码移除设备文件:
devfs_remove(DEVICE_NAME);
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
以上也可以用命令创建设备文件:
mknod /dev/设备文件名 字符设备(c是字符设备,b是块设备) 主设备号 次设备号
例如:mknod /dev/testChar c 100 0
删除设备入口:
rm /dev/testChar
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
2.4驱动总体编写框架:
static int __init my_init(void)
{
//注册设备驱动
register_chrdev (unsigned int major, const char *name, struct file_operations *fops);
//创建设备文件
static devfs_handle_t devfs_handle;
devfs_handle = devfs_register( NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
BUTTON_MAJOR,&sbc2410_buttons_fops, NULL);
}
static int __exit my_exit(void)
{
//移除设备文件
devfs_unregister( devfs_handle);
//注销设备驱动
unregister_chrdev( unsigned int major, const char *name );
}
module_init( my_init );
module_exit( my_exit );
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
2.6驱动总体编写框架:
static int __init my_init(void)
{
//分配设备编号
if(主设备号)
{
sbc2440_leds_dev = MKDEV (LED_MAJOR, LED_MINOR);
result = register_chrdev_region (sbc2440_leds_dev, count, DEVICE_NAME);
}
else
{
result = alloc_chrdev_region (&sbc2440_leds_dev, LED_MINOR, count, DEVICE_NAME);
LED_MAJOR = MAJOR (sbc2440_leds_dev);
}
//注册字符设备驱动
sbc2440_leds_cdev = cdev_alloc();
if (sbc2440_leds_cdev != NULL)
{
cdev_init (sbc2440_leds_cdev, &sbc2440_leds_fops);
sbc2440_leds_cdev->ops = &sbc2440_leds_fops;
sbc2440_leds_cdev->owner = THIS_MODULE;
if (cdev_add (sbc2440_leds_cdev, sbc2440_leds_dev, count) )
printk (KERN_NOTICE "Someting wrong when adding sbc2440_leds_cdev!\n");
else
printk ("Success adding sbc2440_leds_cdev!\n");
}
//创建设备文件
devfs_handle = devfs_register( NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
BUTTON_MAJOR,&sbc2410_buttons_fops, NULL);
}
static int __exit my_exit(void)
{
//移除设备文件
devfs_remove(DEVICE_NAME);
//注销字符设备
cdev_del (sbc2440_leds_cdev);
//释放设备编号:
unregister_chrdev_region (sbc2440_leds_dev, count);
}
module_init( my_init );
module_exit( my_exit );
------------------------
驱动程序的编译:
驱动程序在编译之前,所使用的内核必须要经过编译,否则驱动程序不能编译。
驱动有用函数
set_irq_type函数
在set_irq_type(irq,type)中的type如下:
#define IRQT_NOEDGE (0)
#define IRQT_RISING (__IRQT_RISEDGE) 上升沿有效
#define IRQT_FALLING (__IRQT_FALEDGE) 下升沿有效
#define IRQT_BOTHEDGE (__IRQT_RISEDGE|__IRQT_FALEDGE) 双边沿有效
#define IRQT_LOW (__IRQT_LOWLVL) 低电平有效
#define IRQT_HIGH (__IRQT_HIGHLVL) 高电平有效
#define IRQT_PROBE (1 << 4)
按键驱动总结:
set_gpio_mode_user(k->gpio_port, GPIO_MODE_IN );
up = read_gpio_bit(k->gpio_port);
s3c2410_gpio_cfgpin(k->gpio_port, k->gpio_set);
set_irq_type( k->irq_no, IRQT_RISING );
以上这段代码的功能是:
第一条是设置GPIO端口的使用模式,在此为输入模式,总共有4种模式,分别为:
1,GPIO_MODE_IN(输入模式)
2,GPIO_MODE_OUT(输出模式)
3,GPIO_MODE_ALT0(第三功能)
4,GPIO_MODE_ALT1(第四功能)
第二条是读取gpio端口的信号。
第三条是初始化端口
第四条是设置中断触发方式。
实现了从io口读取信号,在此为上升沿读取有效。
在申请中断之前,必须对端口进行初始化,否则端口不能使用。在中断处理程序的最后,还要对端口进行复位,即恢复到申请中断前初始化时的状态。
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
S_IRUSR
Permits(允许) the file's owner to read it.
S_IWUSR
Permits the file's owner to write to it.
S_IRGRP
Permits the file's group to read it.
S_IWGRP
Permits the file's group to write to it.
阅读(1060) | 评论(0) | 转发(0) |