点灯
demo.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
unsigned int major =0;
char kbuf[100]="";
//定义RCC虚拟地址
unsigned int *rcc_vir=NULL;
//定义GPIOE虚拟地址
gpio_t *gpioe_vir = NULL;
//宏定义LED1的点亮和熄灭
#define LED1_ON (gpioe_vir->ODR |=(0x1<<10))
#define LED1_OFF (gpioe_vir->ODR &=(~(0x1<<10)))
#define LED2_ON (gpioe_vir->ODR |=(0x1<<8))
#define LED2_OFF (gpioe_vir->ODR &=(~(0x1<<8)))
ssize_t myled_read(struct file *file, char __user *ubuf, size_t size, loff_t *loffs)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//如果用户想读的数据大小,大于内核空间的数据大小,更正用户读的数据大小
if(size > sizeof(kbuf)) size = sizeof(kbuf);
//将内核空间的数据,拷贝到用户空间
ret = copy_to_user(ubuf,kbuf,size);
if(ret){
printk("copy to user is error\n");
return -EIO;
}
//printk("kbuf=%s\n",kbuf);
return size;
}
int myled_open(struct inode *inode,struct file* file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t myled_write(struct file* file,const char __user *ubuf,size_t size,loff_t *loffs)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size>sizeof(kbuf)) size=sizeof(kbuf);
ret = copy_from_user(kbuf,ubuf,size);
if(ret){
printk("copy to user is error\n");
return -EIO;
}
if(kbuf[0]==1){
LED1_ON;
LED2_OFF;
}
else{
LED2_ON;
LED1_OFF;
}
//LED2_ON;
//LED1_ON;
return size;
}
int myled_close(struct inode *inode,struct file* file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops={
.open=myled_open,
.write=myled_write,
.release=myled_close,
};
static int __init mycdev_init(void)
{
major = register_chrdev(0,CNAME,&fops);
if(major<0){
printk("register chrdev is error\n");
return -EIO;
}
printk("major=%d\n",major);
//RCC地址映射
rcc_vir = ioremap(RCC_MP_AHB4ENSETR,4);
if(rcc_vir==NULL){
printk("ioremap rcc is error\n");
return -EIO;
}
//GPIOE地址映射
gpioe_vir = ioremap(GPIOE,sizeof(gpio_t));
if(gpioe_vir==NULL){
printk("ioremap rcc is error\n");
return -EIO;
}
//LED1灯初始化
*rcc_vir |= (0x1 << 4);//使能GPIOE组控制器[4] = 1
gpioe_vir->MODER &= (~(0x3 << 20));//设置PE10引脚为输出模式 [21:20] = 01
gpioe_vir->MODER |= (0x1 << 20);
gpioe_vir->ODR &= (~(0x1 << 10)); //设置PE10引脚输出低电平,LED1熄灭 [10] = 0
gpioe_vir->MODER &= (~(0x3 << 16));//设置PE10引脚为输出模式 [21:20] = 01
gpioe_vir->MODER |= (0x1 << 16);
gpioe_vir->ODR &= (~(0x1 << 8)); //设置PE10引脚输出低电平,LED1熄灭 [10] = 0
return 0;
}
static void __exit mycdev_exit(void)
{
//iounmap(gpioe_vir);//取消GPIOE_ODR寄存器地址映射
iounmap(gpioe_vir);//取消GPIOE_MODER寄存器地址映射
iounmap(rcc_vir);//取消rcc寄存器地址映射
//注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
myled.h
//宏定义寄存器物理地址
#define RCC_MP_AHB4ENSETR 0x50000A28 //rcc寄存器的物理地址
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define GPIOE (0x50006000)
#define GPIOF (0x50007000)
#endif
test.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "myled.h"
int main(int argc,const char * argv[])
{
char buf[128] = "";
int fd = -1;
fd = open("/dev/myled",O_RDWR); //打开
if(fd == -1){
perror("open is error");
exit(1);
}
//buf[0] = 0 灯熄灭 buf[0] = 1 灯点亮
while(1)
{
buf[0] = 1;
write(fd,buf,sizeof(buf)); //写
memset(buf,0,sizeof(buf));
sleep(1);
read(fd,buf,sizeof(buf));
printf("buf=%c\n",buf[0]);
sleep(1);
buf[0] = 0;
write(fd,buf,sizeof(buf)); //写
sleep(1);
}
close(fd); //关闭
return 0;
}
Makefile
#指定架构
ARCH ?=
#指定变量名字
MODNAME ?=
ifeq ($(ARCH),arm)
#指定arm架构编译内核 路径
KERNELDIR := /home/ubuntu/linux-5.10.61/
else
#指定x86架构编译内核 路径
KERNELDIR := /lib/modules/$(shell uname -r)/build
endif
#开启一个终端执行pwd命令
PWD := $(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
obj-m := $(MODNAME).o