上篇文章写的是2440的应用程序,这篇文章总结下学习2440LED驱动程序的经验吧。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1,模块加载的驱动函数,一定要是int型,即static int xxx_init();并且最终要有返回值,即return 0;
2,每个非void函数最好都要有返回值,即return 0或其他,否则会引起太多警告,甚至会出现segmentation fault
3,编译内核时,若出现无bound.h及MAX_NR_ZONES宏定义,用make prepare 解决
4. copy from user函数包含于asm/uaccess.h中
5.字符串比较一定要用strcmp函数,与整数比较区分开
6.驱动操作用的基本是虚拟地址,开启了mmu,所以要进行地址映射,ioremap()iounmap()
7.自动创建设备节点的前提是,文件系统中及busybox中一定要配置,否则无法创建,在/etc/init.d/rcS中添加
mount -t tmpfs mdev /dev
mkdir /dev/pts
mount -t devpts devpts /dev/pts
mount -t sysfs sysfs /sys
mount -a
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
而且,随着内核版本的不同,设备的创建有区别。device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “char_dev”),其中,myclass为创建的类名及用struct class类的,在你加入设备时udev就会判断,然后建立节点。主要函数:
virtual_disk_class = class_create(THIS_MODULE, "VirtualDisk");
device_create( virtual_disk_class, NULL, devno, 0, "VirtualDisk");
device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
class_destroy(i2c_dev_class);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
以下为实验成功的源代码
驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/device.h>
//#include <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>
static struct class *ledsdrv_class;
//static struct class_device *ledsdrv_class_dev;
MODULE_LICENSE("GPL");
volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;
static int leds_drv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置GPB5,6,8,10为输出 */
*gpbcon &= ~( (0x3<<(10*2)) | (0x3<<(8*2)) | (0x3<<(6*2)) | (0x3<<(5*2)) );
*gpbcon |= ( (0x1<<(10*2)) | (0x1<<(8*2)) | (0x1<<(6*2)) | (0x1<<(5*2)) );
return 0;
}
static ssize_t leds_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//printk("first_drv_write\n");
copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1)
{
// 点灯
*gpbdat &= ~((1<<5) | (1<<6) | (1<<8) | (1<<10));
}
else
{
// 灭灯
*gpbdat |=(1<<5) | (1<<6) | (1<<8) | (1<<10);
}
return 0;
}
static struct file_operations leds_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = leds_drv_open,
.write = leds_drv_write,
};
int major;
static int leds_drv_init(void)
{
struct device * dev;
major = register_chrdev(0, "leds_drv", &leds_drv_fops); // 注册, 告诉内核
ledsdrv_class = class_create(THIS_MODULE, "ledsdrv");
dev= device_create(ledsdrv_class, NULL, MKDEV(major, 0), NULL, "leds"); /* /dev/leds */
if(dev==NULL)
{
printk("create dev fail!!!!");
return 0;
}
gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);
gpbdat = gpbcon + 1;
return 0;
}
static void leds_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
device_destroy(ledsdrv_class,MKDEV(major, 0));
class_destroy(ledsdrv_class);
iounmap(gpbcon);
}
module_init(leds_drv_init);
module_exit(leds_drv_exit);
试验程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* ledstest on
* ledstest off
*/
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/leds", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}