头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#define RCC_AHB4_ENSETR 0X50000A28
#define GPIOE_MODER 0X50006000
#define GPIOE_ODR 0X50006014
#define GPIOF_MODER 0X50007000
#define GPIOF_ODR 0X50007014
#endif
驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include"head.h"
int major;
//定义一些变量接收映射后的虚拟地址
unsigned int *gpioe_moder;
unsigned int *gpioe_odr;
unsigned int *gpiof_moder;
unsigned int *gpiof_odr;
unsigned int *gpio_rcc;
char kbuf[128]={0};
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 *off)
{
int ret;
//判断kbuf的大小,如果<size,把size的数值修改为kbuf的大小
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 *off)
{
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')
{
(*gpioe_odr) ^= (1<<10);
}
if(kbuf[0]=='2')
{
(*gpiof_odr) ^= (1<<10);
}
if(kbuf[0]=='3')
{
(*gpioe_odr) ^= (1<<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);
//进行物理地址的映射
gpioe_moder=ioremap(GPIOE_MODER,4);
if(gpioe_moder==NULL)
{
printk("MODER寄存器映射失败\n");
return -ENOMEM;
}
gpioe_odr=ioremap(GPIOE_ODR,4);
if(gpioe_odr==NULL)
{
printk("ODR寄存器映射失败\n");
return -ENOMEM;
}
gpiof_moder=ioremap(GPIOF_MODER,4);
if(gpioe_moder==NULL)
{
printk("MODER寄存器映射失败\n");
return -ENOMEM;
}
gpiof_odr=ioremap(GPIOF_ODR,4);
if(gpiof_odr==NULL)
{
printk("ODR寄存器映射失败\n");
return -ENOMEM;
}
gpio_rcc=ioremap(RCC_AHB4_ENSETR,4);
if(gpio_rcc==NULL)
{
printk("RCC寄存器映射失败\n");
return -ENOMEM;
}
printk("寄存器物理地址映射成功\n");
//寄存器的初始化
(*gpio_rcc) |= (1<<4);//rcc使能
(*gpio_rcc) |= (1<<5);//rcc使能
(*gpioe_moder) &= (~(3<<20));//设置为输出模式
(*gpioe_moder) |= (1<<20);
(*gpioe_odr) &= (~(1<<10));//输出低电平
(*gpioe_moder) &= (~(3<<16));//设置为输出模式
(*gpioe_moder) |= (1<<16);
(*gpioe_odr) &= (~(1<<8));//输出低电平
(*gpiof_moder) &= (~(3<<20));//设置为输出模式
(*gpiof_moder) |= (1<<20);
(*gpiof_odr) &= (~(1<<10));//输出低电平
printk("寄存器初始化成功\n");
return 0;
}
//出口函数
static void __exit mycdev_exit(void)
{
//取消物理内存的映射
iounmap(gpioe_moder);
iounmap(gpioe_odr);
iounmap(gpiof_moder);
iounmap(gpiof_odr);
iounmap(gpio_rcc);
//字符设备驱动的注销
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序
#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,2,3)->");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}