2023.7.24(Driver lighting day_1)

头文件

#ifndef __HEAD_H__
#define __HEAD_H__

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
}gpio_t;
 
//基地址
#define PHY_LED1_ADDR 0x50006000
#define PHY_LED2_ADDR 0x50007000
#define PHY_LED3_ADDR 0x50006000
#define PHY_RCC 0x50000A28

#endif

主函数

#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
	char buf[128]={0};
	int fd=open("/dev/mychrdev",O_RDER);
	if(fd<0)
	{
		printf("设备打开失败\n");
		exit(-1);
	}
	while(1)
	{
		printf("请输入你要点亮的LED灯 1 2 3");
		fgets(buf,sizeof(buf),stdin);
		printf("请输入终端控制命令 0(关灯) 1(开灯)");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		write(fd,buf,sizeof(buf));
	}
	close(fd);
	return 0;
}

内核函数封装

#include
#include
#include
#include
#include"head.h"

unsigned int major;//定义一个变量保存设备号
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;

char kbuf[128]={0};

//封装方法
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<0)
	{
		printk("copy_to_user filed\n");
		return -EIO;
	}
	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<0)
	{
		printk("copy_from_user filed\n");
		return -EIO;
	}
	//判断用户输入的数据,进行硬件逻辑执行
	
	if(kbuf[0]=='1')
	{
		if(kbuf[1]=='0')
		{
		vir_led1->ODR |=(0x1 << 10);
		}
		else if(kbuf[1]=='1')
		{
			vir_led1->ODR &=(~(0x1 << 10));
		}
	}
	else if(kbuf[0]=='2')
	{
		if(kbuf[1]=='0')
		{
		vir_led2->ODR |=(0x1 << 10);
		}
		else if(kbuf[1]=='1')
		{
			vir_led2->ODR &=(~(0x1 << 10));
		}
	}
	else if(kbuf[0]=='3')
	{
		if(kbuf[1]=='0')
		{
		vir_led3->ODR |=(0x1 << 8);
		}
		else if(kbuf[1]=='1')
		{
			vir_led3->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",__FILE__,__func__,__LINE__);
	return 0;
}
//定义一个操作方法结构体变量并且初始化
struct file_operations fops={
    .open=mycdev_open,
    .release=mycdev_close,
    .read=mycdev_read,
    .write=mycdev_write,
};
int led_init(void)
{
    //进行寄存器地址的映射
    vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
    if(vir_led1==NULL)
    {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
    }
vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
    if(vir_led2==NULL)
    {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
    }
	vir_led3=ioremap(PHY_LED3_ADDR,sizeof(gpio_t));
    if(vir_led3==NULL)
    {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL)
    {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
    }
    printk("映射物理内存成功\n");
    //寄存器的初始化
    (*vir_rcc) |=(0x3 << 4); //rcc使能
	vir_led1->MODER &=(~(0x3 << 20));
	vir_led1->MODER |=(0x1 << 20);
	vir_led1->ODR |=(0x1 << 10);

	vir_led2->MODER &=(~(0x3 << 20));
	vir_led2->MODER |=(0x1 << 20);
	vir_led2->ODR |=(0x1 << 10);

	vir_led3->MODER &=(~(0x3 << 16));
	vir_led3->MODER |=(0x1 << 16);
	vir_led3->ODR |=(0x1 << 8);
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static int __init mycdev_init(void)
{
    //注册字符设备驱动
    major=register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("注册字符设备驱动失败\n");
        return major;
    }
    printk("注册字符设备驱动成功major=%d\n",major);
	 led_init();
	 return 0;
}
static void __exit mycdev_exit(void)
{
    //注销字符设备驱动
	iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(vir_rcc);
    unregister_chrdev(major,"mychrdev");
	printk("字符设备注销\n");

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

2023.7.24(Driver lighting day_1)_第1张图片

 

你可能感兴趣的:(c#)