mychrdev.c:
c
#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]={0};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
unsigned int *vir_moder2;
unsigned int *vir_odr2;
unsigned int *vir_rcc2;
// 封装操作方法
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)
{
int ret;
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user err\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
//从用户拷贝
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user err\n");
return -EIO;
}
if(kbuf[0]=='1')//PE10开灯
{
//开灯逻辑
(*vir_odr) |= (0X1<<10);
}
else if(kbuf[0]=='0')
{
//关灯逻辑
(*vir_odr) &= (~(0X1<<10));
}
if(kbuf[1]=='1')//PF10开灯
{
//开灯逻辑
(*vir_odr2) |= (0X1<<10);
}
else if(kbuf[0]=='0')
{
//关灯逻辑
(*vir_odr2) &= (~(0X1<<10));
}
if(kbuf[2]=='1')//PE8开灯
{
//开灯逻辑
(*vir_odr) |= (0X1<<8);
}
else if(kbuf[0]=='0')
{
//关灯逻辑
(*vir_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, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//进行GPIE相关寄存器的内存映射
vir_odr=ioremap(LED1_ODR,4);
if(vir_odr==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_rcc=ioremap(LED1_RCC,4);
if(vir_rcc==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_moder=ioremap(LED1_MODER,4);
if(vir_moder==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
printk("寄存器内存映射成功\n");
//进行GPIOF相关寄存器的内存映射
vir_odr2=ioremap(LED2_ODR,4);
if(vir_odr2==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_rcc2=ioremap(LED2_RCC,4);
if(vir_rcc2==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_moder2=ioremap(LED2_MODER,4);
if(vir_moder2==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
printk("寄存器内存映射成功\n");
//PE10硬件寄存器的初始化
//RCC使能
(*vir_rcc) |= (0X1<<4);
//设置PE10为输出
(*vir_moder) &= (~(0X3<<20));
(*vir_moder) |= (0X1<<20);
//默认关灯
(*vir_odr) &= (~(0X1<<10));
//PF10硬件寄存器的初始化
//RCC使能
(*vir_rcc2) |= (0X1<<5);
//设置PF10为输出
(*vir_moder2) &= (~(0X3<<20));
(*vir_moder2) |= (0X1<<20);
//默认关灯
(*vir_odr2) &= (~(0X1<<10));
//PE8硬件寄存器的初始化
//设置PE10为输出
(*vir_moder) &= (~(0X3<<16));
(*vir_moder) |= (0X1<<16);
//默认关灯
(*vir_odr) &= (~(0X1<<8));
return 0;
}
static void __exit mycdev_exit(void)
{
//取消物理内存映射
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
//取消物理内存映射
iounmap(vir_moder2);
iounmap(vir_odr2);
iounmap(vir_rcc2);
// 字符设备驱动注销
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c:
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/mychrdev", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while (1)
{
printf("输入指令控制开/关灯(LED1|LED2|LED3)==>0(OFF)/1(ON)>");
fgets(buf, sizeof(buf), stdin); // 在终端输入数据传递到buf
buf[strlen(buf) - 1] = '\0'; // 替换末尾的'\n'
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
head.h:
c
#ifndef __HEAD_H__
#define __HEAD_H__
//PE10
#define LED1_RCC 0X50000A28
#define LED1_MODER 0X50006000
#define LED1_ODR 0X50006014
//PF10
#define LED2_RCC 0X50000A28
#define LED2_MODER 0X50007000
#define LED2_ODR 0X50007014
#endif
实验结果: