1. 驱动:
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define RCC_MP_AHB4ENSETR 0X50000A28
#define GPIOE_MODER 0X50006000
#define GPIOE_ODR 0X50006014
#define GPIOF_MODER 0X50007000
#define GPIOF_ODR 0X50007014
#endif
ctu.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int major;
char kbuf[128] = {0};
unsigned int *vir_gpioe_moder;
unsigned int *vir_gpioe_odr;
unsigned int *vir_gpiof_moder;
unsigned int *vir_gpiof_odr;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
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 filad\n");return ret;}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 filed\n");return ret;}if (kbuf[0] == 0) {if (kbuf[1] == '1')(*vir_gpioe_odr) |= (0X1 << 10); else if (kbuf[1] == '0')(*vir_gpioe_odr) &= (~(0X1 << 10)); }else if (kbuf[0] == 1) {if (kbuf[1] == '1')(*vir_gpiof_odr) |= (0X1 << 10); else if (kbuf[1] == '0')(*vir_gpiof_odr) &= (~(0X1 << 10)); }else if (kbuf[0] == 2) {if (kbuf[1] == '1')(*vir_gpioe_odr) |= (0X1 << 9); else if (kbuf[1] == '0')(*vir_gpioe_odr) &= (~(0X1 << 9)); }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)
{int i;major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("字符设备驱动注册失败\n");return major;}printk("字符设备驱动注册成功major=%d\n", major);cls = class_create(THIS_MODULE, "mycdev");if (IS_ERR(cls)){printk("向上提交目录信息失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功\n");for (i = 0; i < 3; i++){dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);if (IS_ERR(dev)){printk("向上提交设备节点失败\n");return -PTR_ERR(cls);}}printk("向上提交设备节点信息成功\n");vir_gpioe_moder = ioremap(GPIOE_MODER, 4);if (vir_gpioe_moder == NULL){printk("GPIOE_MODER映射失败%d\n", __LINE__);return -EFAULT;}vir_gpioe_odr = ioremap(GPIOE_ODR, 4);if (vir_gpioe_odr == NULL){printk("GPIOE_ODR映射失败%d\n", __LINE__);return -EFAULT;}vir_gpiof_moder = ioremap(GPIOF_MODER, 4);if (vir_gpiof_moder == NULL){printk("GPIOF_MODER映射失败%d\n", __LINE__);return -EFAULT;}vir_gpiof_odr = ioremap(GPIOF_ODR, 4);if (vir_gpiof_odr == NULL){printk("GPIOF_ODR映射失败%d\n", __LINE__);return -EFAULT;}vir_rcc = ioremap(RCC_MP_AHB4ENSETR, 4);if (vir_rcc == NULL){printk("RCC_MP_AHB4ENSETR映射失败%d\n", __LINE__);return -EFAULT;}printk("物理内存映射成功\n");(*vir_gpioe_moder) &= (~(0X3 << 20)); (*vir_gpioe_moder) |= (0X1 << 20);(*vir_gpiof_moder) &= (~(0X3 << 20)); (*vir_gpiof_moder) |= (0X1 << 20);(*vir_gpioe_moder) &= (~(0X3 << 18)); (*vir_gpioe_moder) |= (0X1 << 18);(*vir_rcc) |= (0X1 << 4);(*vir_gpioe_odr) &= (~(0X1 << 10));(*vir_gpioe_odr) &= (~(0X1 << 9));(*vir_gpiof_odr) &= (~(0X1 << 10));return 0;
}
static void __exit mycdev_exit(void)
{iounmap(vir_gpioe_moder);iounmap(vir_gpiof_moder);iounmap(vir_gpioe_odr);iounmap(vir_gpiof_odr);iounmap(vir_rcc);int i;for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}class_destroy(cls);unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2. 程序:
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{char buf[128] = {'\0'};int fds[3] = {open("/dev/mycdev0", O_RDWR),open("/dev/mycdev1", O_RDWR),open("/dev/mycdev2", O_RDWR)};if (fds[0] < 0 || fds[1] < 0 || fds[2] < 0){printf("打开设备文件失败\n");exit(-1);}printf("成功打开设备文件\n");while (1){for (char i = 0; i < 3; i++){printf("请选择LED%d控制方式:1(开灯)0(关灯)>", i + 1);fgets(buf + 1, sizeof(buf) - 1, stdin); buf[strlen(buf + 1) - 1] = '\0';buf[0] = i;write(fds[i], buf, sizeof(buf)); }}for (char i = 0; i < 3; i++)close(fds[i]);return 0;
}
3. Makefile
modname ?= ctu
arch ?= arm
ifeq ($(arch),arm)
KERNELDIR:=/home/isis/CLionProjects/stm32mp1-openstlinux-5.10-dunfell-mp1-21-11-17/sources/arm-ostl-linux-gnueabi/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build
endifPWD:=$(shell pwd)
KBUILD_EXTRA_SYMBOLS += /home/isis/CLionProjects/test/model2/Module.symvers
all:make -C $(KERNELDIR) M=$(PWD) modules
test:test.carm-linux-gnueabihf-gcc test.c -o testclean:
make -C $(KERNELDIR) M=$(PWD) clean
obj-m:=$(modname).o