头文件
#ifndef __MYCDEV_H__
#define __MYCDEV_H__
typedef struct {
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int LCKR;
volatile unsigned int AFRL;
volatile unsigned int AFRH;
volatile unsigned int BRR;
volatile unsigned int res;
volatile unsigned int SECCFGR;
}gpio_t;
#define PHY_RCC 0x50000A28
#define PHY_RCC2 0x54000210
#define PHY_GPIOE 0x50006000
#define PHY_GPIOF 0x50007000
#define PHY_GPIOZ 0x54004000
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
LED4,
LED5,
LED6,
};
#endif
源文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/device.h>
#include "myled.h"
#include <linux/uaccess.h>
#include <linux/io.h>
#define CNAME "myled" //驱动的名字
#if 1
unsigned int major = 0; // 动态申请主设备号
#else
unsigned int major = 500; // 静态申请主设备号
#endif
struct cdev *my_cdev;
dev_t dev_number; // 设备号(主设备号/次设备号)
unsigned int baseminor = 0; // 次设备号起始值
unsigned int minor = 0; // 次设备号
struct class *cls;
struct device *dev;
unsigned int* virt_rcc;
unsigned int* virt_rcc2;
gpio_t* virt_gpioe;
gpio_t* virt_gpiof;
gpio_t* virt_gpioz;
#define LED1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))
#define LED4_ON (virt_gpioz->ODR |= (0x1 << 5))
#define LED4_OFF (virt_gpioz->ODR &= (~(0x1 << 5)))
#define LED5_ON (virt_gpioz->ODR |= (0x1 << 6))
#define LED5_OFF (virt_gpioz->ODR &= (~(0x1 << 6)))
#define LED6_ON (virt_gpioz->ODR |= (0x1 << 7))
#define LED6_OFF (virt_gpioz->ODR &= (~(0x1 << 7)))
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
//判断应用层传递过来的命令码 cmd
//判断操作哪盏灯进行点亮 copy_from_user
int whitch;
int res;
switch(cmd)
{
case LED_ON:
res = copy_from_user(&whitch,(void*)args,sizeof(args));
if(res)
{
printk("copy from user is error\n");
return -EIO;
}
switch(whitch)
{
case LED1:
LED1_ON;
break;
case LED2:
LED2_ON;
break;
case LED3:
LED3_ON;
break;
case LED4:
LED4_ON;
break;
case LED5:
LED5_ON;
break;
case LED6:
LED6_ON;
break;
}
break;
case LED_OFF:
res = copy_from_user(&whitch,(void*)args,sizeof(args));
if(res)
{
printk("copy from user is error\n");
return -EIO;
}
switch(whitch)
{
case LED1:
LED1_OFF;
break;
case LED2:
LED2_OFF;
break;
case LED3:
LED3_OFF;
break;
case LED4:
LED4_OFF;
break;
case LED5:
LED5_OFF;
break;
case LED6:
LED6_OFF;
break;
}
break;
}
return 0;
}
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
static int __init mycdev_init(void)
{
int ret, i;
// 1.分配字符设备驱动对象空间
my_cdev = cdev_alloc();
if (NULL == my_cdev)
{
printk("1.分配字符设备驱动对象空间失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("1.分配字符设备驱动对象空间成功\n");
// 2.初始化驱动对象结构体
cdev_init(my_cdev, &fops);
// 3.动态申请设备号
if (major == 0) //动态
{
ret = alloc_chrdev_region(&dev_number, baseminor, 6, CNAME);
if (ret)
{
printk("2.动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(dev_number);
minor = MINOR(dev_number);
printk("2.动态申请设备号成功\n");
}
else if (major > 0) //静态
{
ret = register_chrdev_region(MKDEV(major, minor), 6, CNAME);
if (ret)
{
printk("2.静态申请设备号失败\n");
goto ERR2;
}
printk("2.静态申请设备号成功\n");
}
// 4.注册字符设备驱动对象
ret = cdev_add(my_cdev, MKDEV(major, minor), 6);
if (ret)
{
printk("3.注册字符驱动设备对象失败\n");
goto ERR3;
}
printk("3.注册字符驱动设备对象成功\n");
// 5.向上提交目录
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls))
{
printk("4.向上提交目录失败\n");
ret = PTR_ERR(cls);
goto ERR4;
}
printk("4.向上提交目录成功\n");
// 6.向上提交字符设备信息
for (i = 0; i < 6; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("5.向上提交节点信息失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("5.向上提交设备节点成功\n");
//将物理地址映射为虚拟地址
//映射RCC地址
virt_rcc = ioremap(PHY_RCC,4);
if(NULL == virt_rcc)
{
printk("rcc ioremap error\n");
return -ENOMEM;
}
virt_rcc2 = ioremap(PHY_RCC2,4);
if(NULL == virt_rcc2)
{
printk("rcc2 ioremap error\n");
return -ENOMEM;
}
//映射GPIOE地址
virt_gpioe = ioremap(PHY_GPIOE,4);
if(NULL == virt_gpioe)
{
printk("gpioe ioremap error\n");
return -ENOMEM;
}
//映射GPIOF地址
virt_gpiof = ioremap(PHY_GPIOF,4);
if(virt_gpiof == NULL)
{
printk("gpiof ioremap is error\n");
return -ENOMEM;
}
//映射GPIOZ地址
virt_gpioz = ioremap(PHY_GPIOZ,4);
if(virt_gpioz == NULL)
{
printk("gpioz ioremap is error\n");
return -ENOMEM;
}
//RCC初始化
*virt_rcc |= (0x1 << 4);
*virt_rcc |= (0x1 << 5);
*virt_rcc2 |= (0x1 << 0);
//对LED1_1初始化,设置引脚功能
virt_gpioe->MODER &= (~(0x3 << 20));
virt_gpioe->MODER |= (0x1 << 20);
virt_gpioe->ODR &= (~(0x1 << 10));
//对LED1_2初始化,设置引脚功能
virt_gpiof->MODER &= (~(0x3<<20));
virt_gpiof->MODER |= (0x1<<20);
virt_gpiof->ODR &= (~(0x1<<10));
//对LED1_3初始化,设置引脚功能
virt_gpioe->MODER &= (~(0x3<<16));
virt_gpioe->MODER |= (0x1<<16);
virt_gpioe->ODR &= (~(0x1<<8));
//对LED2_1初始化,设置引脚功能
virt_gpioz->MODER &= (~(0x3 << 10));
virt_gpioz->MODER |= (0x1 << 10);
virt_gpioz->ODR &= (~(0x1 << 5));
//对LED2_2初始化,设置引脚功能
virt_gpioz->MODER &= (~(0x3 << 12));
virt_gpioz->MODER |= (0x1 << 12);
virt_gpioz->ODR &= (~(0x1 << 6));
//对LED2_3初始化,设置引脚功能
virt_gpioz->MODER &= (~(0x3 << 14));
virt_gpioz->MODER |= (0x1 << 14);
virt_gpioz->ODR &= (~(0x1 << 7));
return 0;
ERR5:
// 将前面提交成功的设备信息销毁
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
ERR4:
cdev_del(my_cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), 6);
ERR2:
kfree(my_cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
//取消映射
iounmap(virt_rcc);
iounmap(virt_rcc2);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
iounmap(virt_gpioz);
// 1.注销向上提交的字符设备信息
for (i = 0; i < 6; i++)
{
device_destroy(cls, MKDEV(major, i));
}
printk("1.注销向上提交的字符设备信息成功\n");
// 2.注销向上提交的目录信息
class_destroy(cls);
printk("2.注销向上提交的目录信息成功\n");
// 3.注销字符设备驱动对象
cdev_del(my_cdev);
printk("3.注销字符设备驱动对象成功\n");
// 4.释放申请到的设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
printk("4.释放申请到的设备号成功\n");
// 5.释放申请到的字符设备驱动对象空间
kfree(my_cdev);
printk("5.释放申请到的字符设备驱动对象空间成功\n");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "myled.h"
#include <sys/ioctl.h>
int main(int argc,const char * argv[])
{
char buf[128] = {0};
int fd = -1;
int whitch;
fd = open("/dev/myled0", O_RDWR);
if(-1 == fd)
{
perror("open is error\n");
return -1;
}
while(1)
{
whitch = LED1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED4;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED5;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED6;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
}
close(fd);
return 0;
}