编写LED灯的驱动,创建三个设备文件,每个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制设备文件对应的这盏灯。
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
unsigned int moder;
unsigned int otyper;
unsigned int ospeedr;
unsigned int pupdr;
unsigned int idr;
unsigned int odr;
}gpio_t;
#define GPIOE 0X50006000
#define GPIOF 0X50007000
#define GPIOB 0X50003000
#define RCC 0X50000A28
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
应用程序
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
int a, b;
char buf[128] = {0};
int fd_led = open("/dev/led0", O_RDWR);
if (fd_led < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择灯功能\n");
printf("0(关灯) 1(开灯)>");
scanf("%d", &a);
if (a == 1)
{
ioctl(fd_led, LED_ON);
}
else if (a == 0)
{
ioctl(fd_led, LED_OFF);
}
}
close(fd_led);
return 0;
}
驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
gpio_t *vir_gpioe;
unsigned int *vir_rcc;
gpio_t *vir_gpiof;
int mycdev_open(struct inode *inode, struct file *file)
{
unsigned int aaa = MINOR(inode->i_rdev);
file->private_data = (void *)aaa;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which;
int ret = copy_from_user(&which, (void *)arg, 4);
if (ret)
{
printk("拷贝用户空间数据失败\n");
return -EIO;
}
unsigned int aaa = (int)file->private_data;
switch (cmd)
{
case LED_ON:
switch (aaa)
{
case 1:
vir_gpioe->odr |= (0x1 << 10);
break;
case 2:
vir_gpiof->odr |= (0x1 << 10);
break;
case 3:
vir_gpioe->odr |= (0x1 << 8);
break;
}
break;
case LED_OFF:
switch (aaa)
{
case 1:
vir_gpioe->odr &= (~(0x1 << 10));
break;
case 2:
vir_gpiof->odr &= (~(0x1 << 10));
break;
case 3:
vir_gpioe->odr &= (~(0x1 << 8));
break;
}
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret, i;
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
cdev_init(cdev, &fops);
if (major > 0)
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret = -PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备节点信息成功\n");
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if (vir_gpioe == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(RCC, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
printk("寄存器映射成功\n");
(*vir_rcc) |= (0x3 << 4);
vir_gpioe->moder &= (~(0x3 << 20));
vir_gpioe->moder |= (0x1 << 20);
vir_gpioe->odr &= (~(0x1 << 10));
vir_gpiof->moder &= (~(0x3 << 20));
vir_gpiof->moder |= (0x1 << 20);
vir_gpiof->odr &= (~(0x1 << 10));
vir_gpioe->moder &= (~(0x3 << 16));
vir_gpioe->moder |= (0x1 << 16);
vir_gpioe->odr &= (~(0x1 << 8));
printk("硬件寄存器初始化成功\n");
return 0;
out5:
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_gpioe);
iounmap(vir_rcc);
iounmap(vir_gpiof);
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");