led.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include"led.h"
#define CNAME "mycdev"
int major;
char kbuf[128]={};
unsigned int *vir_moder;
unsigned int *vir_moder2;
unsigned int *vir_moder3;
unsigned int *vir_odr;
unsigned int *vir_odr2;
unsigned int *vir_odr3;
unsigned int *vir_rcc;
unsigned int *vir_rcc2;
unsigned int *vir_rcc3;
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)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
if(kbuf[0]=='1')
{
(*vir_odr) |= (1<<10);
(*vir_odr2) |= (1<<10);
(*vir_odr3) |= (1<<8);
}
else
{ (*vir_odr) &= ~(1<<10);
(*vir_odr2) &= ~(1<<10);
(*vir_odr3) &= ~(1<<8);
}
if(kbuf[0]=='2')
{
(*vir_odr2) |= (1<<10);
}
else
{
(*vir_odr2) &= ~(1<<10);
}
if(kbuf[0]=='3')
{
(*vir_odr3) |= (1<<8);
}
else
{
(*vir_odr3) &= ~(1<<8);
}
return size;
}
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 demo_init(void)
{
major=register_chrdev(0,CNAME,&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
vir_moder=ioremap(PHY_LED1_MODER,4);
if(vir_moder==NULL)
{
printk("vir_moder 映射失败\n");
return -ENOMEM;
}
printk("vir_moder 映射成功\n");
vir_moder2=ioremap(PHY_LED2_MODER,4);
if(vir_moder2==NULL)
{
printk("vir_moder 映射失败\n");
return -ENOMEM;
}
printk("vir_moder 映射成功\n");
vir_moder3=ioremap(PHY_LED3_MODER,4);
if(vir_moder3==NULL)
{
printk("vir_moder 映射失败\n");
return -ENOMEM;
}
printk("vir_moder 映射成功\n");
vir_odr=ioremap(PHY_LED1_ODR,4);
if(vir_odr==NULL)
{
printk("vir_odr 映射失败\n");
return -ENOMEM;
}
printk("vir_odr 映射成功\n");
vir_odr2=ioremap(PHY_LED2_ODR,4);
if(vir_odr2==NULL)
{
printk("vir_odr 映射失败\n");
return -ENOMEM;
}
printk("vir_odr 映射成功\n");
vir_odr3=ioremap(PHY_LED3_ODR,4);
if(vir_odr3==NULL)
{
printk("vir_odr 映射失败\n");
return -ENOMEM;
}
printk("vir_odr 映射成功\n");
vir_rcc=ioremap(PHY_LED1_RCC,4);
if(vir_rcc==NULL)
{
printk("vir_rcc 映射失败\n");
return -ENOMEM;
}
printk("vir_rcc 映射成功\n");
vir_rcc2=ioremap(PHY_LED2_RCC,4);
if(vir_rcc2==NULL)
{
printk("vir_rcc 映射失败\n");
return -ENOMEM;
}
printk("vir_rcc 映射成功\n");
vir_rcc3=ioremap(PHY_LED3_RCC,4);
if(vir_rcc3==NULL)
{
printk("vir_rcc 映射失败\n");
return -ENOMEM;
}
printk("vir_rcc 映射成功\n");
(*vir_moder) &= ~(3<<20);
(*vir_moder) |= (1<<20);
(*vir_odr) &= ~(1<<10);
(*vir_rcc) |= (1<<4);
(*vir_moder2) &= ~(3<<20);
(*vir_moder2) |= (1<<20);
(*vir_odr2) &= ~(1<<10);
(*vir_rcc2) |= (1<<5);
(*vir_moder3) &= ~(3<<16);
(*vir_moder3) |= (1<<16);
(*vir_odr3) &= ~(1<<8);
(*vir_rcc3) |= (1<<4);
return 0;
}
static void __exit demo_exit(void)
{
unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
led.h
#ifndef __MYLED_H__
#define __MYLED_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define PHY_LED2_RCC 0X50000A28
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR 0X50006014
#define PHY_LED3_RCC 0X50000A28
#endif
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>
int main(int argc, char const *argv[])
{
char buf[128]={};
int fd=open("/dev/mychrdev",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
printf("请输入控制命令 1,2,3:开灯 0:关灯\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
memset(buf,0,sizeof(buf));
read(fd,buf,sizeof(buf));
printf("读取到的数据为%s\n",buf);
close(fd);
return 0;
}