Makefile
arch?=arm
modname?=demo
ifeq ($(arch),"arm")
KERNELDIR:=/home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR=/lib/modules/5.3.0-28-generic/build
endif
PWD:=$(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
obj-m:=$(modname).o
Head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
#endif
Demo.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, const char *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mycdev", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while(1){
memeset(buf,0,sizeof(buf));
printf("输入LEDX_ON开灯,输入LEDX_OFF关灯\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
Mydev.c
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include"head.h"
char kbuf[128]={0};
unsigned int *vir1_moder;
unsigned int *vir1_odr;
unsigned int *vir1_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 __user *ubuf, size_t size, loff_t *lof)
{
int ret;
if(size>sizeof(kbuf)){
size=sizeof(kbuf);
}
ret = copy_to_user(ubuf,kbuf,size);
if(ret){
printk("copy_from_user fail%d\n",__LINE__);
}
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 *lof)
{
int ret;
if(size>sizeof(kbuf)){
size=sizeof(kbuf);
}
ret = copy_from_user(kbuf,ubuf,size);
if(ret){
printk("copy_from_user fail%d\n",__LINE__);
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(strcmp(kbuf,"LED1_ON")==0){
(*vir1_odr)|=(0x1<<10);
}
else if(strcmp(kbuf,"LED1_OFF")==0){
(*vir1_odr)&=(~(0x1<<10));
}
if(strcmp(kbuf,"LED2_ON")==0){
(*vir2_odr)|=(0x1<<10);
}
else if(strcmp(kbuf,"LED2_OFF")==0){
(*vir2_odr)&=(~(0x1<<10));
}
if(strcmp(kbuf,"LED3_ON")==0){
(*vir3_odr)|=(0x1<<8);
}
else if(strcmp(kbuf,"LED3_OFF")==0){
(*vir3_odr)&=(~(0x1<<8));
}
return 0;
}
int mycdev_release (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_release,
};
unsigned int major;
static int __init mycdev_init(void)
{
major=register_chrdev(0,"mycdev",&fops);
if(major<0) {
printk("字符设备注册失败\n");
}
printk("字符设备注册成功 major=%d\n",major);
vir1_moder=ioremap(PHY_LED1_MODER,4);
if(vir1_moder==NULL){
return -ENOMEM;
}
vir1_odr=ioremap(PHY_LED1_ODR,4);
if(vir1_odr==NULL){
return -ENOMEM;
}
vir1_rcc=ioremap(PHY_LED1_RCC,4);
if(vir1_rcc==NULL){
return -ENOMEM;
}
vir2_moder=ioremap(PHY_LED2_MODER,4);
if(vir2_moder==NULL){
return -ENOMEM;
}
vir2_odr=ioremap(PHY_LED2_ODR,4);
if(vir2_odr==NULL){
return -ENOMEM;
}
vir3_moder=ioremap(PHY_LED3_MODER,4);
if(vir3_moder==NULL){
return -ENOMEM;
}
vir3_odr=ioremap(PHY_LED3_ODR,4);
if(vir3_odr==NULL){
return -ENOMEM;
}
printk("寄存器影射成功\n");
(*vir1_moder)&=(~(0x3<<20));
(*vir1_moder)|=(0x1<<20);
(*vir1_odr)|=(0x1<<10);
(*vir1_rcc)|=(0x1<<4);
(*vir2_moder)&=(~(0x3<<20));
(*vir2_moder)|=(0x1<<20);
(*vir2_odr)|=(0x1<<10);
(*vir1_rcc)|=(0x1<<5);
(*vir3_moder)&=(~(0x3<<16));
(*vir3_moder)|=(0x1<<16);
(*vir3_odr)|=(0x1<<8);
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,"mycdev");
iounmap(vir1_moder);
iounmap(vir1_rcc);
iounmap(vir1_odr);
iounmap(vir2_moder);
iounmap(vir2_odr);
iounmap(vir3_moder);
iounmap(vir3_odr);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");