mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include "myled.h"
#define CNAME "myled"
struct cdev *cdev;
dev_t devt;
int major;
int minor;
unsigned int count = 3;
struct class *cls;
struct device *dev;
char kbuf[128] = {};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
volatile unsigned int *vir_rcc;
int mycdev_open(struct inode* inode,struct file* file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file* file,char __user *ubuf,size_t size,loff_t *loff)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_wirte(struct file* file,const char __user *ubuf,size_t size,loff_t *loff)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret;
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy form user is error\n");
return -EIO;
}
switch(minor)
{
case 0:
if(kbuf[0] == '1')
{
vir_led1->ODR |=(0x1 << 10);
}
else if(kbuf[0] == '0')
{
vir_led1->ODR &=(~(0x1 << 10));
}
break;
case 1:
if(kbuf[0] == '1')
{
vir_led1->ODR |=(0x1 << 10);
}
else if(kbuf[0] == '0')
{
vir_led1->ODR &=(~(0x1 << 10));
}
break;
case 2:
if(kbuf[0] == '1')
{
vir_led1->ODR |=(0x1 << 10);
}
else if(kbuf[0] == '0')
{
vir_led1->ODR &=(~(0x1 << 10));
}
break;
}
return 0;
}
int mycdev_close(struct inode* inode,struct file* file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_wirte,
.release = mycdev_exit,
};
//初始化函数
int led_init(void)
{
vir_rcc = ioremap(PHY_RCC,4);
if(vir_rcc == NULL)
{
printk("vir_rcc error\n");
return -ENOMEM;
}
//LED1物理地址映射
vir_led1 = ioremap(PHY_GPIOE,sizeof(gpio_t));
if(vir_led1 == NULL)
{
printk("vir_led1 error\n");
return -ENOMEM;
}
//LED2物理地址映射
vir_led2 = ioremap(PHY_GPIOF,sizeof(gpio_t));
if(vir_led2 == NULL)
{
printk("vir_led2 error\n");
return -ENOMEM;
}
//LED1物理地址映射
vir_led3 = ioremap(PHY_GPIOE,sizeof(gpio_t));
if(vir_led3 == NULL)
{
printk("vir_led3 error\n");
return -ENOMEM;
}
//初始化
*vir_rcc |= (0x3 << 4);
vir_led1->MODER &= (~(0x3 << 20));
vir_led1->MODER |= (0x1 << 20);
vir_led1->ODR &= (~(0x1 << 10));
vir_led2->MODER &= (~(0x3 << 20));
vir_led2->MODER |= (0x1 << 20);
vir_led2->ODR &= (~(0x1 << 10));
vir_led3->MODER &= (~(0x3 << 16));
vir_led3->MODER |= (0x1 << 16);
vir_led3->ODR &= (~(0x1 << 8));
return 0;
}
//入口
static int __init mycdev_init(void)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret;
int i;
cdev = cdev_alloc(); //分配字节设备驱动
if(cdev == NULL)
{
printk("cdev alloc error\n");
goto ERR1;
}
cdev_init(cdev,&fops); //初始化
ret = alloc_chrdev_region(&devt,minor,count,CNAME); //申请设备号
if(ret)
{
printk("alloc_chrdev_region\n");
goto ERR2;
}
major = MAJOR(devt);
minor = MINOR(devt);
ret = cdev_add(cdev,MKDEV(major,minor),count); //注册字符设备驱动
if(ret)
{
printk("cdev_add error\n");
goto ERR3;
}
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls))
{
printk("class_create error\n");
return PTR_ERR(cls);
goto ERR4;
}
for(i = 0;i < 3;i++)
{
dev = device_create(cls,MKDEV(major,minor),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("device_create error\n");
return PTR_ERR(cls);
goto ERR5;
}
}
led_init();
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),count);
ERR2:
kfree(cdev);
ERR1:
return -ENOMEM;
}
//出口
static void __exit mycdev_exit(void)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int i;
for(i = 0;i < 3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
//取消映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
//注销字符设备驱动
cdev_del(cdev);
//释放设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//释放动态申请空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//许可证
MODULE_LICENSE("GPL");
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
char buf[128] = {0};
int main(int argc, char const *argv[])
{
int fd = -1;
fd = open("/dev/myled", O_RDWR);
if (-1 == fd)
{
perror("open is error\n");
exit(1);
}
while (1)
{
buf[0] = '1';
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = '0';
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = '2';
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = '0';
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = '3';
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = '0';
write(fd, buf, sizeof(buf));
sleep(1);
}
read(fd, buf, sizeof(buf));
close(fd);
return 0;
}
myled.h
#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;
}gpio_t;
// typedef enum{
// LED1,
// LED2,
// LED3
// }led_t;
#define PHY_GPIOE 0x50006000
#define PHY_GPIOF 0x50007000
#define PHY_RCC 0x50000A28
//命令码封装
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#endif