led.h
#ifndef __LED_H__
#define __LED_H__
#define PHY_MODER 0x50006000
#define PHY1_ODR 0x50006014
#define PHY1_RCC 0x50000A28
#define PHY2_MODER 0x50007000
#define PHY2_ODR 0x50007014
#define PHY3_MODER 0x50006000
#define PHY3_ODR 0x50006014
#endif
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include <linux/io.h>
#include "led.h"
unsigned int major;
char kbuf[128]={0};
//定义三个指针指向映射后的虚拟内存首地址
unsigned int *vir1_moder;
unsigned int *vir1_odr;
unsigned int *vir_rcc;
unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir3_moder;
unsigned int *vir3_odr;
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 *iof)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy to user filed\n");
return -EIO;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *iof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy from user filed\n");
return -EIO;
}
//根据用户空间发送来的指令实现灯的亮灭控制
if(kbuf[0]=='1')//亮灯
{
(*vir1_odr) |= (0X1<<10);
(*vir2_odr) |= (0X1<<10);
(*vir3_odr) |= (0X1<<8);
}
else if(kbuf[0]=='0')//灭灯
{
(*vir1_odr) &= (~(0X1<<10));
(*vir2_odr) &= (~(0X1<<10));
(*vir3_odr) &= (~(0X1<<8));
}
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,"mycdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//进行LED相关寄存器地址映射
vir1_moder=ioremap(PHY_MODER,4);
if(vir1_moder==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir1_odr=ioremap(PHY1_ODR,4);
if(vir1_odr==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir2_moder=ioremap(PHY_MODER,4);
if(vir2_moder==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir2_odr=ioremap(PHY1_ODR,4);
if(vir2_odr==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir3_moder=ioremap(PHY_MODER,4);
if(vir3_moder==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir3_odr=ioremap(PHY1_ODR,4);
if(vir3_odr==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc=ioremap(PHY1_RCC,4);
if(vir_rcc==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
printk("物理内存映射为虚拟内存成功\n");
//相关寄存器初始化
(*vir_rcc) |= (0x1<<4);//gpio使能
//LED1灯初始化
(*vir1_moder) &= (~(0x3<<20));//设置为输出
(*vir1_moder) |= (0x1<<20);
(*vir1_odr) &= (~(0x1<<10));//灭灯
//LED2灯初始化
(*vir2_moder) &= (~(0x3<<20));//设置为输出
(*vir2_moder) |= (0x1<<20);
(*vir2_odr) &= (~(0x1<<10));//灭灯
//LED3灯初始化
(*vir3_moder) &= (~(0x3<<16));//设置为输出
(*vir3_moder) |= (0x1<<16);
(*vir3_odr) &= (~(0x1<<8));//灭灯
printk("相关寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//字符设备驱动的注销
unregister_chrdev(major,"mycdev");
}
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>
int main(int argc, char const *argv[])
{
char buf[128]={0};
int fd=open("/dev/mycdev",O_RDWR);
if(fd<0)
{
printf("打开文件失败\n");
exit(-1);
}
while(1)
{
printf("输入控制码: 1 (亮灯) 0 (灭灯) >>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}