head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_PE_MODER 0x50006000
#define PHY_PF_MODER 0x50007000
#define PHY_PE_ODR 0x50006014
#define PHY_PF_ODR 0x50007014
#define PHY_RCC 0x50000A28
#endif
应用程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev", O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
return -1;
}
printf("打开设备文件成功\n");
while(1)
{
printf("请输入要进行的操作:0(关灯) 1 (开灯)>");
fgets(buf,sizeof(buf),stdin); //在终端读一个字符串
buf[strlen(buf) - 1] = '\0';
write(fd,buf,sizeof(buf));//将数据传递给内核
}
/*
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("buf: %s\n", buf);
*/
close(fd);
return 0;
}
驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
unsigned int major; //主设备号
char kbuf[128] = {}; //内核空间buf
//定义五个指针指向映射后的虚拟内存
unsigned int *vir_pe_moder;
unsigned int *vir_pf_moder;
unsigned int *vir_pe_odr;
unsigned int *vir_pf_odr;
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 *ubuf, size_t size, loff_t *lof)
{
//mycdev_read()函数中的ubuf和size分别是指向应用程序中read()函数中的buf, 和sizeof(buf)的值
printk("%s:%s:%d\n", __FILE__,__func__,__LINE__);
int ret;
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
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;
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy_from_user failed\n");
return -EIO;
}
if(kbuf[0] == '0')//关灯
{
//关灯逻辑
(*vir_pe_odr) &= (~(0x1 << 10)); //默认关灯
(*vir_pf_odr) &= (~(0x1 << 10));
(*vir_pe_odr) &= (~(0x1 << 8));
}
else if(kbuf[0] == '1')
{
//开灯逻辑
(*vir_pe_odr) |= (0x1 << 10);
(*vir_pf_odr) |= (0x1 << 10);
(*vir_pe_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);
//进行寄存器的地址映射
vir_pe_moder = ioremap(PHY_PE_MODER, 4);
if(vir_pe_moder == NULL)
{
printk("物理地址内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_pf_moder = ioremap(PHY_PF_MODER,4);
if(vir_pf_moder == NULL)
{
printk("物理地址内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_pe_odr = ioremap(PHY_PE_ODR, 4);
if(vir_pe_odr == NULL)
{
printk("物理地址内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_pf_odr = ioremap(PHY_PF_ODR, 4);
if(vir_pf_odr == NULL)
{
printk("物理地址内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_rcc = ioremap(PHY_RCC, 4);
if(vir_rcc == NULL)
{
printk("物理地址内存映射失败%d\n",__LINE__);
return -EFAULT;
}
printk("寄存器内存映射成功\n");
//LED寄存器初始化
(*vir_rcc) |= (0x3 << 4); //GPIOE和GPIOF控制器时钟使能
//PE10
(*vir_pe_moder) &= (~(0x3 << 20)); //MODER[21:20]->00
(*vir_pe_moder) |= (0x1 << 20); //MODER[21"20]->01
(*vir_pe_odr) &= (~(0x1 << 10)); //默认关灯
//PF10
(*vir_pf_moder) &= (~(0x3 << 20));
(*vir_pf_moder) |= (0x1 << 20);
(*vir_pf_odr) &= (~(0x1 << 10)); //默认关灯
//PE8
(*vir_pe_moder) &= (~(0x3 << 16));
(*vir_pe_moder) |= (0x1 << 16);
(*vir_pe_odr) &= (~(0x1 << 8)); //默认关灯
return 0;
}
static void __exit mycdev_exit(void)
{
//取消内存映射
iounmap(vir_pe_moder);
iounmap(vir_pf_moder);
iounmap(vir_pe_odr);
iounmap(vir_pf_odr);
iounmap(vir_rcc);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
实现效果