上一篇我们写了一个字符设备的框架,因此我们可以拿过来用:2—linux字符设备进阶篇
我们根据这几个寄存器,来对io口进行操作
static unsigned long gpfcon; //定义全局变量
static unsigned long gpfdat; //定义全局变量
gpfcon= ioremap(0x56000000, 0x100000);
gpfdat = gpfcon + 1;
在linux中,我们不能直接访问物理地址,而是间接把物理地址映射出来再操作
ioremap(cookie,size)
cookie :映射的物理地址
size :内存大小
老规矩,有映射就有取消映射:
iounmap(gpfcon);
在出口函数,取消映射
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
在open函数里面我们对io口进行配置,在write进行对led的开关led操作
.open()函数
static int led_open(struct inode *inode, struct file *file)
{
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}
初始化gpf 4 5 6,配置为输出模式。
.write()函数
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
copy_from_user(&val, buf, count);
if (val == 1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
}
else
{
*gpfdat |= (1<<4) | (1<<5) | (1<<6);
}
return 0;
}
当用户空间write的数据val为1时,灯灭。val为0时,灯亮
我们不能直接使用buf,需要用copy_from_user()函数,从用户空间拷贝
copy_from_user(to,from,count);
to : 目的
from:源
count :大小有从用户空间拷贝,自然就有拷贝到用户空间
copy_to_user(to, from, count)
to:目的
from:源
count:大小
老规矩,makefile,make,拷贝到开发板,insmod led.ko
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s \n" , argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}
编译,拷贝到开发板
./led_test on
灯全亮
major = register_chrdev(0, "led", &led_fops);
//int register_chrdev(unsigned int major, const char *name,const struct file_operations *fops)
这个函数没有指定次设备号,所以主设备号下的所有次设备号(0~255)都是对应这个led_fops,非常的霸道,但是我喜欢
major:主设备号,如果写0,则让内核自动分配主设备号
name:名字
fops:自己编写的fops
******ledc.c*******
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct class *led_class;
static struct class_device *led_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_open(struct inode *inode, struct file *file)
{
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
}
else
{
*gpfdat |= (1<<4) | (1<<5) | (1<<6);
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
int major;
static int led_init(void)
{
major = register_chrdev(0, "led_drv", &led_fops);
firstdrv_class = class_create(THIS_MODULE, "leddrv");
firstdrv_class_dev = class_device_create(led_class,NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void first_drv_exit(void)
{
unregister_chrdev(major, "led_drv");
class_device_unregister(led_class_dev);
class_destroy(led_class);
iounmap(gpfcon);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");