驱动文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#include<linux/uaccess.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#define CNAME "myled"
char kbuf[128] = {0};
gpio_t* virt_gpioe = NULL;
gpio_t* virt_gpiof = NULL;
gpio_t* virt_gpioz = NULL;
unsigned int* virt_rcc = NULL;
unsigned int* virt_rcc2 =NULL;
#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 LED1_1_ON (virt_gpioe->ODR |= (0x1 << 5))
#define LED1_1_OFF (virt_gpioe->ODR &= (~(0x1 << 5)))
#define LED2_1_ON (virt_gpiof->ODR |= (0x1 << 6))
#define LED2_1_OFF (virt_gpiof->ODR &= (~(0x1 << 6)))
#define LED3_1_ON (virt_gpioe->ODR |= (0x1 << 7))
#define LED3_1_OFF (virt_gpioe->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)
{
//1.判断cmd switch(cmd)
//2.判断操作哪盏灯进行点亮 copy_from_user
int whitch;
int ret;
switch(cmd)
{
case LED_ON:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
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 LED_HE1:
LED1_1_ON;
break;
case LED_HE2:
LED2_1_ON;
break;
case LED_HE3:
LED3_1_ON;
break;
}
break;
case LED_OFF:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
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 LED_HE1:
LED1_1_OFF;
break;
case LED_HE2:
LED2_1_OFF;
break;
case LED_HE3:
LED3_1_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,
};
struct cdev *cdev;
dev_t devno;
unsigned int minor=0;
unsigned int major =0;//动态申请主设备号
struct class *cls;
struct device *dev;
//open
static int __init mycdev_init(void)
{
int ret,i;
//1.分配对象
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("分配对象空间失败\n");
ret=-ENOMEM;
goto ERR1;
}
printk("分配对象空间成功\n");
//2.初始化驱动对象
cdev_init(cdev,&fops);
//申请设备号
ret=alloc_chrdev_region(&devno,minor,3,"mycdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major=MAJOR(devno);//获取主设备号
minor=MINOR(devno);//获取次设备号
//3.注册对象
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("驱动对象注册进内核失败\n");
goto ERR3;
}
printk("驱动对象注册进内核成功\n");
//向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
//4.向上提交设备节点
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
goto ERR5;
}
}
printk("向上提交设备节点成功\n");
//3.打印主设备号
printk("register chrdev major=%d\n",major);
//4.将物理地址映射为虚拟地址
//4.1 将rcc地址映射
virt_rcc = ioremap(PHY_RCC,4);
if(virt_rcc == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
virt_rcc2 = ioremap(PHY_RCC2,4);
if(virt_rcc2 == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
//映射GPIOE地址
virt_gpioe = ioremap(PHY_GPIOE_ADDR,sizeof(gpio_t));
if(virt_gpioe== NULL)
{
printk("virt_gpioe ioremap is error\n");
return -ENOMEM;
}
//映射GPIOF地址
virt_gpiof = ioremap(PHY_GPIOF_ADDR,sizeof(gpio_t));
if(virt_gpiof== NULL)
{
printk("virt_gpiof ioremap is error\n");
return -ENOMEM;
}
//映射GPIOF地址
virt_gpioz = ioremap(PHY_GPIOZ_ADDR,sizeof(gpio_t));
if(virt_gpioz== NULL)
{
printk("virt_gpiof ioremap is error\n");
return -ENOMEM;
}
//5.对led1---->PE10引脚初始化 对led3---->PE8引脚初始化
*virt_rcc |= (0x1 << 4);//5.1 使能GPIOE组时钟[4]=1
virt_gpioe->MODER &= (~(0x3 << 20));//5.2 设置PE10引脚为输出模式 [21:20] = 01
virt_gpioe->MODER |= (0x1 << 20);
virt_gpioe->ODR &= (~(0x1 << 10)); //5.3 设置PE10引脚输出低电平
//6. 对led2---->PF10引脚初始化
*virt_rcc |= (0x1 << 5);//5.1 使能GPIOF组时钟[5]=1
virt_gpiof->MODER &= (~(0x3 << 20));//5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpiof->MODER |= (0x1 << 20);
virt_gpiof->ODR &= (~(0x1 << 10)); //5.3 设置PF10引脚输出低电平
//7.对led3---->PE8引脚初始化
virt_gpioe->MODER &= (~(0x3 << 16));//5.2 设置PE8引脚为输出模式 [17:16] = 01
virt_gpioe->MODER |= (0x1 << 16);
virt_gpioe->ODR &= (~(0x1 << 8)); //5.3 设置PE8引脚输出低电平
//5.对led1---->PE10引脚初始化 对led3---->PE8引脚初始化
*virt_rcc2 |= (0x1 << 0);//5.1 使能GPIOF组时钟[5]=1
virt_gpioz->MODER &= (~(0x3 << 10));//5.2 设置PE10引脚为输出模式 [21:20] = 01
virt_gpioz->MODER |= (0x1 << 10);
virt_gpioz->ODR &= (~(0x1 << 5)); //5.3 设置PE10引脚输出低电平
//6. 对led2---->PF10引脚初始化
virt_gpioz->MODER &= (~(0x3 << 12));//5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpioz->MODER |= (0x1 << 12);
virt_gpioz->ODR &= (~(0x1 << 6)); //5.3 设置PF10引脚输出低电平
//7.对led3---->PE8引脚初始化
virt_gpioz->MODER &= (~(0x3 << 14));//5.2 设置PE8引脚为输出模式 [17:16] = 01
virt_gpioz->MODER |= (0x1 << 14);
virt_gpioz->ODR &= (~(0x1 << 7)); //5.3 设置PE8引脚输出低电平
return 0;
ERR5:
//将前面提交成功的设备信息销毁
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),3);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
//2.取消地址映射
iounmap(virt_rcc);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
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");
源文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc,const char * argv[])
{
char buf[128] = {0};
int whitch;
int fd = -1;
fd = open("/dev/myled",O_RDWR);
if(fd == -1)
{
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 = LED_HE1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED_HE2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED_HE3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
}
close(fd);
return 0;
}