#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED13_MODER 0x50006000
#define PHY_LED13_ODR 0x50006014
#define PHY_LED123_RCC 0x50000A28
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128]={};
int fd1=open("/dev/mycdev0",O_RDWR);
if(fd1<0)
{
printf("打开设备文件1失败\n");
exit(-1);
}
printf("打开设备文件1成功\n");
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(buf[0]=='0')
{
break;
}
write(fd1,buf,sizeof(buf));
}
close(fd1);
return 0;
}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int major;
char kbuf[128]={0};
unsigned int *vir_led13_moder;
unsigned int *vir_led13_odr;
unsigned int *vir_led123_rcc;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
struct class *cls;
struct device *dev;
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 *ubuf,size_t size,loff_t* lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return ret;
}
return 0;
}
ssize_t mycdev_write(struct file* file,const char* ubuf,size_t size,loff_t* lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return ret;
}
if(kbuf[0]=='1')
{
(*vir_led13_odr) ^= (0x1<<10);
}
else if (kbuf[0]=='2')
{
(*vir_led13_odr) ^= (0x1<<8);
}
else if (kbuf[0]=='3')
{
(*vir_led2_odr) ^= (0x1<<10);
}
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,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
cls = class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("提交目录信息失败\n");
return -PTR_ERR(cls);
}
printk("目录提交成功\n");
int i;
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("提交设备节点失败\n");
return -PTR_ERR(dev);
}
}
printk("提交设备节点成功\n");
vir_led123_rcc=ioremap(PHY_LED123_RCC,4);
vir_led13_moder=ioremap(PHY_LED13_MODER,4);
vir_led13_odr=ioremap(PHY_LED13_ODR,4);
vir_led2_moder=ioremap(PHY_LED2_MODER,4);
vir_led2_odr=ioremap(PHY_LED2_ODR,4);
(*vir_led123_rcc) |= (0x3<<4);
(*vir_led13_moder) &= (~(0x33<<16));
(*vir_led13_moder) |= (0x11<<16);
(*vir_led13_odr) &= (~(0x5<<8));
(*vir_led2_moder) &= (~(0x3<<20));
(*vir_led2_moder) |= (0x1<<20);
(*vir_led2_odr) &= (~(0x1<<10));
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
iounmap(vir_led123_rcc);
iounmap(vir_led13_moder);
iounmap(vir_led13_odr);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");