驱动代码
#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/io.h>
#include "head.h"
struct cdev *cdev;
unsigned int major=500;
unsigned int minor=0;
dev_t devno; //设备号
struct class *cls;
struct device *dev;
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
unsigned int *virf_moder;
unsigned int *virf_odr;
int mycdev_open(struct inode *inode,struct file *file)
{
unsigned int min = MINOR(inode->i_rdev);
file->private_data = (void *)min;
printk("%s,%s,%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned int min = (int)file->private_data;
switch (min)
{
case 0:
(*vir_odr) ^= (0x1<<(10));
break;
case 1:
(*virf_odr) ^= (0x1<<(10));
break;
case 2:
(*vir_odr) ^= (0x1<<(8));
break;
default:
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;
//1.分配字符设备驱动对象空间 cdev_alloc
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret=-EFAULT;
goto out1;
}
printk("申请字符设备驱动对象空间成功\n");
//2.字符设备驱动对象部分初始化 cdev_init
cdev_init(cdev,&fops);
//3.申请设备号 register_chrdev_region/alloc_chrdev_region
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");
//4.注册字符设备驱动对象 cdev_add()
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录
cls=class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点
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(cls);
goto out5;
}
}
printk("向上提交节点信息成功\n");
//映射物理寄存器
vir_moder=ioremap(PHY_LED1_MODER,4);
if(vir_moder==NULL)
{
printk("MODER寄存器地址映射表失败\n");
return -EFAULT;
}
vir_odr=ioremap(PHY_LED1_ODR,4);
if(vir_odr==NULL)
{
printk("ODR寄存器地址映射表失败\n");
return -EFAULT;
}
vir_rcc=ioremap(PHY_RCC,4);
if(vir_rcc==NULL)
{
printk("RCC寄存器地址映射表失败\n");
return -EFAULT;
}
virf_moder=ioremap(PHY_LED2_MODER,4);
if(virf_moder==NULL)
{
printk("MODER寄存器地址映射表失败\n");
return -EFAULT;
}
virf_odr=ioremap(PHY_LED2_ODR,4);
if(virf_odr==NULL)
{
printk("ODR寄存器地址映射表失败\n");
return -EFAULT;
}
printk("寄存器地址映射成功\n");
//寄存器初始化
(*vir_rcc) |= (0x3<<4);//rcc使能
(*vir_moder) &= (~(0X3<<20));//设置为输出
(*vir_moder) |=(0x1<<20);
(*vir_odr) &= (~(0x1<<10));//led1灭灯
(*vir_odr) |= (0x1<<8);//led3亮灯
(*virf_moder) &= (~(0X3<<20));//设置为输出
(*virf_moder) |=(0x1<<20);
(*virf_odr) |= (0x1<<10);//led2亮灯
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_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
iounmap(virf_moder);
iounmap(virf_odr);
int i;
//1.销毁设备信息 device_destroy
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//2.销毁目录 class_destroy
class_destroy(cls);
//3.注销对象 cdev_del()
cdev_del(cdev);
//4.释放设备号 unregister_chrdev_region()
unregister_chrdev_region(MKDEV(major,minor),3);
//5.释放对象空间 kfree()
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
程序代码
#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;
int fd;
while(1)
{
printf("请输入控制命令--1:开/关灯1 2:开/关灯2 3:开/关灯3\n");
scanf("%d",&a);
switch (a)
{
case 1:
fd = open("/dev/myled0",O_RDWR);
ioctl(fd,LED_ON,a);
break;
case 2:
fd = open("/dev/myled1",O_RDWR);
ioctl(fd,LED_ON,a);
break;
case 3:
fd = open("/dev/myled2",O_RDWR);
ioctl(fd,LED_ON,a);
break;
default:
break;
}
close(fd);
}
return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define LED_ON _IO('l',1)
#endif