应用程序:
#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, char const *argv[])
{
/* code */
char buf[128]={0};
int fd=open("/dev/demo0", O_RDWR);
if(fd<0){
printf("打开设备文件失败\n");
exit(-1);
}
printf("成功打开设备文件\n");
while(1){
printf("请输入要实现的逻辑》\n");
printf("11:LED1亮\n");
printf("10:LED1灭\n");
printf("21:LED2亮\n");
printf("20:LED2灭\n");
printf("31:LED3亮\n");
printf("30:LED3灭\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
驱动程序:
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define PHY_LED_RCC 0x50000A28
#endif
驱动文件
#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"
// extern int add(int i,int j);
int major;
char kbuf[128]={0};
struct class *cls;
struct device *dev;
//定义指针接收映射成功的虚拟内存首地址
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
// 封装操作方法
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)
{
int ret;
ret=copy_to_user(ubuf, kbuf, size);
if(ret){
printk("copy_to_user failed\n");
return ret;
}
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;
ret=copy_from_user(kbuf, ubuf, size);
if(ret){
printk("copy_from_user failed\n");
return ret;
}
//开关灯逻辑
//LED1
if(kbuf[0]=='1'&&kbuf[1]=='1'){
printk("开灯\n");
vir_gpioe->ODR|=(0x1<<10);
}else if(kbuf[0]=='1'&&kbuf[1]=='0'){
printk("关灯\n");
vir_gpioe->ODR&=(~(0x1<<10));
}
//LED2
if(kbuf[0]=='2'&&kbuf[1]=='1'){
printk("开灯\n");
vir_gpiof->ODR|=(0x1<<10);
}else if(kbuf[0]=='2'&&kbuf[1]=='0'){
printk("关灯\n");
vir_gpiof->ODR&=(~(0x1<<10));
}
//LED3
if(kbuf[0]=='3'&&kbuf[1]=='1'){
printk("开灯\n");
vir_gpioe->ODR|=(0x1<<8);
}else if(kbuf[0]=='3'&&kbuf[1]=='0'){
printk("关灯\n");
vir_gpioe->ODR&=(~(0x1<<8));
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
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, "demo0", &fops);
if (major < 0)
{
printk("字符设备驱动组测失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
//物理内存映射首地址
vir_gpioe=ioremap(GPIOE,sizeof(gpio_t));
if(vir_gpioe==NULL){
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_gpiof=ioremap(GPIOF,sizeof(gpio_t));
if(vir_gpiof==NULL){
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc=ioremap(PHY_LED_RCC,4);
if(vir_rcc==NULL){
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
printk("物理内存映射成功\n");
//硬件寄存器初始化
//gpioe、gpiof寄存器组使能
(*vir_rcc)|=(0x3<<4);
//LED1设置---PE10
vir_gpioe->MODER&=(~(0x3<<20));
vir_gpioe->MODER|=(0x1<<20);
vir_gpioe->ODR&=(~(0x1<<10));
//LED2设置---PF10
vir_gpiof->MODER&=(~(0x3<<20));
vir_gpiof->MODER|=(0x1<<20);
vir_gpiof->ODR&=(~(0x1<<10));
//LED3设置---PE8
vir_gpioe->MODER&=(~(0x3<<16));
vir_gpioe->MODER|=(0x1<<16);
vir_gpioe->ODR&=(~(0x1<<8));
//创建设备节点文件
//向上提交目录信息
cls=class_create(THIS_MODULE,"demo0");
if(IS_ERR(cls)){
printk("向上提交目录信息失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功");
//向上提交设备信息
int i;
for(i=0;i<3;i++){
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"demo%d",i);
if(IS_ERR(dev)){
printk("向上提交设备信息失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备信息成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//取消物理内存的映射
iounmap(vir_gpioe);
iounmap(vir_gpiof);
iounmap(vir_rcc);
//销毁设备信息
int i;
for(i=0;i<3;i++){
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
// 字符设备驱动注销
unregister_chrdev(major, "demo0");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");