demo.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 *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_rcc;
unsigned int major;
char kbuf[128]={};
//封装操作方法
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)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret;
//将内核空间数据拷贝到用户空间
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user failed!\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;
}
printk("kbuf=%s\n",kbuf);
//led1
if(kbuf[0] == '0')//关灯
{
printk("kbuf[0]=%c\n",kbuf[0]);
//关灯逻辑
(*vir_odr) &= (~(0x1<<10));
}
if(kbuf[0] == '1')//开灯
{
//开灯逻辑
(*vir_odr) |= (0x1<<10);
}
//led2
if(kbuf[1] == '0')//关灯
{
//关灯逻辑
(*vir_led2_odr) &= (~(0x1<<10));
}
if(kbuf[1] == '1')//开灯
{printk("kbuf[1]=%c\n",kbuf[1]);
//开灯逻辑
(*vir_led2_odr) |= (0x1<<10);
}
//led3
if(kbuf[2] == '0')//关灯
{
//关灯逻辑
(*vir_odr) &= (~(0x1<<8));
}
if(kbuf[2] == '1')//开灯
{printk("kbuf[2]=%c\n",kbuf[2]);
//开灯逻辑
(*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);
//进行寄存器地址的映射
vir_moder = ioremap(PHY_LED1_3_MODER,4);
if(vir_moder == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_odr = ioremap(PHY_LED1_3_ODR,4);
if(vir_odr == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led2_moder = ioremap(PHY_LED2_MODER,4);
if(vir_led2_moder == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led2_odr = ioremap(PHY_LED2_ODR,4);
if(vir_led2_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控制器使能
(*vir_moder) &= (~(0x3 << 20));//MODER[21:20]->00
(*vir_moder) &= (~(0x3 << 16));//MODER[17:16]->00
(*vir_moder) |= (0x1 << 20);//MODER[21:20]->01 PE10
(*vir_moder) |= (0x1 << 16);//MODER[17:16]->01 PE8
(*vir_led2_moder) &= (~(0x3 << 20));//MODER[21:20]->00
(*vir_led2_moder) |= (0x1 << 20);//MODER[21:20]->01 PF10
(*vir_odr) &= (~(0x1 << 10));
(*vir_odr) &= (~(0x1 << 8));
(*vir_led2_odr) &= (~(0x1 << 10));
return 0;
}
//出口函数,卸载内核模块时执行
static void __exit mycdev_exit(void)
{
//#define __exit __section(".exit.text")
//__exit指定出口函数保存在。exit.text段中
//取消映射内存
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
iounmap(vir_rcc);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
//用于声明入口函数
module_init(mycdev_init);
//用于声明出口函数
module_exit(mycdev_exit);
//声明当前内核模块遵循GPL协议(许可证)
MODULE_LICENSE("GPL");
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED1_3_MODER 0x50006000
#define PHY_LED1_3_ODR 0x50006014
#define PHY_RCC 0x50000A28
#endif
text.c
#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';//因为读到的字符串在最后一个是执行命令的换行符,所以要把最后一个字符替换成'\0'
write(fd,buf,sizeof(buf));//将数据传递给内核(将用户空间的buf拷贝到内核空间)
}
close(fd);
return 0;
}
Makefile
modname ?= demo
arch ?= arm
ifeq ($(arch),arm)
#保存UBUNTU内核源码路径
KERNELDIR := /home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61/
else
#KERBELDIR保存开发板内核源码路径
KERNELDIR := /lib/modules/$(shell uname -r)/build
endif
#PWD保存当前内核模块的路径
PWD := $(shell pwd)
all:
#make modules是模块化编译命令
#make -C $(KERNLEDIR) 执行make之前先切换到KERNELDIR对应的路径
#M=$(PWD)表示进行模块化编译的路径是PWD保存的路径
make -C $(KERNELDIR) M=$(PWD) modules
clean:
#编译清除
make -C $(KERNELDIR) M=$(PWD) clean
#将obj-m保存的文件单独链接为内核模块
obj-m := $(modname).o