头文件:
myled.h
#ifndef __MYLED_H__
#define __MYLED_H__
// //PE10
// #define PHY_LED1_MODER 0X50006000
// #define PHY_LED1_ODR 0X50006014
// #define PHY_LED1_RCC 0X50000A28
// //PF10
// #define PHY_LED2_MODER 0X50007000
// #define PHY_LED2_ODR 0X50007014
// #define PHY_LED2_RCC 0X50000A28
// //PE8
// #define PHY_LED3_MODER 0X50006000
// #define PHY_LED3_ODR 0X50006014
// #define PHY_LED3_RCC 0X50000A28
//PB6
#define PHY_FMQ_ADDR 0X50003000
//PE9
#define PHY_FS_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
#define FsON _IO('a', 1)
#define FsOFF _IO('a', 0)
#define FmqON _IO('a', 1)
#define FmqOFF _IO('a', 0)
#endif
风扇驱动FS_mode.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include"myled.h"
#include "stm32mp1xx_gpio.h"
#define CNAME "mycdev"
int major;//定义变量接收主设备号
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_fs;
unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
//size参数是用户期待读到的字节长度
int ret;
if(size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if( ret )
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("数据从用户向内核拷贝失败\n");
return -EIO;
}
return size;
}
//close()
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case FsON:
vir_fs->ODR |= (1 << 9);
break;
case FsOFF:
vir_fs->ODR &= ~(1 << 9);
break;
default:
printk("功能码错误\n");
break;
}
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,
.unlocked_ioctl=ioctl,
.release=mycdev_close,
};
int all_led_init(void)
{
//进行物理地址映射
vir_fs = ioremap(PHY_FS_ADDR,sizeof(gpio_t));
if(vir_fs == NULL)
{
printk("vir_fs 映射失败\n");
return -ENOMEM;
}
printk("vir_fs映射成功\n");
vir_rcc = ioremap(PHY_RCC_ADDR,4);
if(vir_rcc == NULL)
{
printk("vir_rcc映射失败\n");
return -ENOMEM;
}
printk("vir_rcc映射成功\n");
//寄存器的初始化
vir_fs->MODER &= ~(3<<18);
vir_fs->MODER |= (1<<18);
vir_fs->ODR &= ~(1<<9);
(*vir_rcc) |= (1<<4);
return 0;
}
//入口函数,当驱动安装的时候执行
static int __init demo_init(void)
{
//动态注册字符设备驱动
major=register_chrdev(0, CNAME, &fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//向上提交节点目录
cls = class_create(THIS_MODULE, "FS");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//创建设备节点
dev = device_create(cls, NULL, MKDEV(major,0), NULL, "myfs");
if(IS_ERR(dev))
{
printk("创建节点失败\n");
return PTR_ERR(dev);
}
printk("创建节点成功\n");
//寄存器初始化
all_led_init();
return 0;
}
//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
//销毁节点
device_destroy(cls, MKDEV(major,0));
//销毁目录
class_destroy(cls);
//取消物理地址映射
iounmap(vir_fs);
iounmap(vir_rcc);
//注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
蜂鸣器驱动
FMQ_demo.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include"myled.h"
#include "stm32mp1xx_gpio.h"
#define CNAME "mycdev"
int major;//定义变量接收主设备号
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_fmq;
unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
//size参数是用户期待读到的字节长度
int ret;
if(size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if( ret )
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("数据从用户向内核拷贝失败\n");
return -EIO;
}
return size;
}
//close() //PB6
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case FmqON:
vir_fmq->ODR |= (1 << 6);
break;
case FmqOFF:
vir_fmq->ODR &= ~(1 << 6);
break;
default:
printk("功能码错误\n");
break;
}
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,
.unlocked_ioctl=ioctl,
.release=mycdev_close,
};
int all_led_init(void)
{
//进行物理地址映射
vir_fmq = ioremap(PHY_FMQ_ADDR,sizeof(gpio_t));
if(vir_fmq == NULL)
{
printk("vir_fmq 映射失败\n");
return -ENOMEM;
}
printk("vir_fmq映射成功\n");
vir_rcc = ioremap(PHY_RCC_ADDR,4);
if(vir_rcc == NULL)
{
printk("vir_rcc映射失败\n");
return -ENOMEM;
}
printk("vir_rcc映射成功\n");
//寄存器的初始化
vir_fmq->MODER &= ~(3 << 12);
vir_fmq->MODER |= (1 << 12);
vir_fmq->ODR &= ~(1 << 6);
(*vir_rcc) |= (1 << 1);
return 0;
}
//入口函数,当驱动安装的时候执行
static int __init demo_init(void)
{
//动态注册字符设备驱动
major=register_chrdev(0, CNAME, &fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//向上提交节点目录
cls = class_create(THIS_MODULE, "FMQ");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//创建设备节点
dev = device_create(cls, NULL, MKDEV(major,0), NULL, "myfmq");
if(IS_ERR(dev))
{
printk("创建节点失败\n");
return PTR_ERR(dev);
}
printk("创建节点成功\n");
//寄存器初始化
all_led_init();
return 0;
}
//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
//销毁节点
device_destroy(cls, MKDEV(major,0));
//销毁目录
class_destroy(cls);
//取消物理地址映射
iounmap(vir_fmq);
iounmap(vir_rcc);
//注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
主函数:test.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc, char const *argv[])
{
char buf[128]={};
int fd1 = open("/dev/myfs",O_RDWR);
int fd2 = open("/dev/myfmq",O_RDWR);
int which;
if(fd1<0 || fd2<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
//在终端输入
while(1)
{
printf("请选择要操作:\n1-->风扇启动\n2-->风扇关闭\n3-->蜂鸣器启动\n4-->蜂鸣器关闭\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0'; //吃换行
if(buf[0] == '1')
ioctl(fd1, FsON, &which);
else if(buf[0] == '2')
ioctl(fd1, FsOFF, &which);
else if(buf[0] == '3')
ioctl(fd2, FmqON, &which);
else if(buf[0] == '4')
ioctl(fd2, FmqOFF, &which);
}
close(fd1);
close(fd2);
return 0;
}
Makefile
modname ?= demo
arch ?= arm
KBUILD_EXTRA_SYMBOLS+=/home/ubuntu/lk/驱动开发/day2/1/Module.symvers
ifeq ($(arch),arm)#arm架构
KERNELDIR := /home/ubuntu/fsmp1a/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR:= /lib/modules/$(shell uname -r)/build #当前x86架构格式路径
#KERNELDIR是一个变量,指向内核源码目录
endif
PWD:=$(shell pwd)
#PWD指向当前驱动目录的一个变量
all:
make -C $(KERNELDIR) M=$(PWD) modules
#make -C $(KERNELDIR)
#进入内核顶层目录下,读取对应的Makefile文件,然后执行make
#M=$(PWD) :指定边沿模块的路径为当前驱动路径
#make modules :模块化编译
#进入内核顶层目录使用其中的Makefile对当前文件进行模块化编译
clean:
make -C $(KERNELDIR) M=$(PWD) clean
#清除编译
obj-m:=$(modname).o
#指定当前编译生成的模块名字为demo demo.c ==> demo.ko