This commit is contained in:
LiShanwenGit 2022-03-29 19:39:16 +08:00
parent 11b7c6fd37
commit e051395df0
5 changed files with 43 additions and 27 deletions

Binary file not shown.

View File

@ -67,12 +67,13 @@ static int led_write(struct file *filp, const char __user *buff, size_t count, l
else
printk(KERN_DEBUG"write led ok!!!\n"); //输出信息
*((volatile size_t*)gpioe_data) &= ~(1<<10) ;//清除GPIOE12状态
if(status)
if(status == 1)
*((volatile size_t*)gpioe_data) |= (1<<10);//设置GPIOE12状态1
return ret;
}
static struct file_operations led_ops = {
static struct file_operations led_ops =
{
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,

View File

@ -22,9 +22,9 @@ static int led_open(struct inode *inode, struct file *file)
{
/* GPIOE配置 */
*((volatile size_t*)gpioe_cfg1) &= ~(7<<8); //清除配置寄存器
*((volatile size_t*)gpioe_cfg1) |= (1<<8); //配置GPIOE12为输出模式
*((volatile size_t*)gpioe_cfg1) |= (1<<8); //配置GPIOE10为输出模式
*((volatile size_t*)gpioe_pul0) &= ~(3<<20); //清除上/下拉寄存器
*((volatile size_t*)gpioe_pul0) |= (1<<20); //配置GPIOE12为上拉模式
*((volatile size_t*)gpioe_pul0) |= (1<<20); //配置GPIOE10为上拉模式
printk(KERN_DEBUG"open led!!!\n");
return 0;
}
@ -38,7 +38,7 @@ static int led_close(struct inode *inode, struct file *filp)
static int led_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
int ret;
size_t status = *((volatile size_t*)gpioe_data);//获取GPIOE12状态
size_t status = *((volatile size_t*)gpioe_data);//获取GPIOE10状态
ret = copy_to_user(buff,&status,4); //将内核空间拷贝到用户空间buff
if(ret < 0)
printk(KERN_DEBUG"read error!!!\n"); //输出信息
@ -56,9 +56,9 @@ static int led_write(struct file *filp, const char __user *buff, size_t count, l
printk(KERN_DEBUG"write error!!!\n"); //输出信息
else
printk(KERN_DEBUG"write led ok!!!\n"); //输出信息
*((volatile size_t*)gpioe_data) &= ~(1<<10) ;//清除GPIOE12状态
*((volatile size_t*)gpioe_data) &= ~(1<<10) ;//清除GPIOE10状态
if(status)
*((volatile size_t*)gpioe_data) |= (1<<10);//设置GPIOE12状态1
*((volatile size_t*)gpioe_data) |= (1<<10);//设置GPIOE10状态1
return 0;
}

View File

@ -1,4 +1,4 @@
KERN_DIR = /home/lsw/licheepi/linux-5.7.1
KERN_DIR = /home/lsw/licheepi/nanopi/linux-5.7.1
all:
make -C $(KERN_DIR) M=$(shell pwd) modules

View File

@ -2,30 +2,39 @@
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> //含有ioremap函数iounmap函数
#include <asm/uaccess.h> //含有copy_from_user函数和含有copy_to_user函数
#include <linux/io.h> //含有ioremap函数iounmap函数
#include <linux/uaccess.h> //含有copy_from_user函数和含有copy_to_user函数
#include <linux/device.h> //含有类相关的设备函数
#include <linux/cdev.h>
#include <linux/platform_device.h> //包含platform函数
#include <linux/of.h> //包含设备树相关函数
#include <linux/of_platform.h>
#include <linux/of_address.h>
static dev_t led_dev_num; //定义一个设备号
static struct cdev *led_dev; //定义一个设备管理结构体指针
static struct class *led_class; //定义一个设备类
static struct device *led0; //定义一个设备
size_t *gpioe_cfg0; //存储虚拟地址到物理地址映射
size_t *gpioe_cfg1; //存储虚拟地址到物理地址映射
size_t *gpioe_data; //存储虚拟地址到物理地址映射
size_t *gpioe_pul0; //存储虚拟地址到物理地址映射
volatile unsigned long *gpioe_cfg0; //存储虚拟地址到物理地址映射
volatile unsigned long *gpioe_cfg1; //存储虚拟地址到物理地址映射
volatile unsigned long *gpioe_data; //存储虚拟地址到物理地址映射
volatile unsigned long *gpioe_pul0; //存储虚拟地址到物理地址映射
#define GPIOE_CFG0 (0x01C20890)
#define GPIOE_CFG1 (0x01C20894)
#define GPIOE_DATA (0x01C208A0)
#define GPIOE_PUL0 (0x01C208AC)
static int led_open(struct inode *inode, struct file *file)
{
/* GPIOE配置 */
*((volatile size_t*)gpioe_cfg1) &= ~(7<<16); //清除配置寄存器
*((volatile size_t*)gpioe_cfg1) |= (1<<16); //配置GPIOE12为输出模式
*((volatile size_t*)gpioe_pul0) &= ~(3<<16); //清除上/下拉寄存器
*((volatile size_t*)gpioe_pul0) |= (1<<12); //配置GPIOE12为上拉模式
*((volatile size_t*)gpioe_cfg1) &= ~(7<<8); //清除配置寄存器
*((volatile size_t*)gpioe_cfg1) |= (1<<8); //配置GPIOE10为输出模式
*((volatile size_t*)gpioe_pul0) &= ~(3<<20); //清除上/下拉寄存器
*((volatile size_t*)gpioe_pul0) |= (1<<20); //配置GPIOE10为上拉模式
printk(KERN_DEBUG"open led!!!\n");
return 0;
}
@ -40,15 +49,16 @@ static int led_close(struct inode *inode, struct file *filp)
static int led_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
int ret;
size_t status = *((volatile size_t*)gpioe_data);//获取GPIOE12状态
size_t status = *((volatile size_t*)gpioe_data);//获取GPIOE10状态
ret = copy_to_user(buff,&status,4); //将内核空间拷贝到用户空间buff
if(ret < 0)
printk(KERN_DEBUG"read error!!!\n"); //输出信息
else
printk(KERN_DEBUG"read led ok!!!\n"); //输出信息
return 0;
return ret;
}
static int led_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
{
int ret;
@ -58,9 +68,9 @@ static int led_write(struct file *filp, const char __user *buff, size_t count, l
printk(KERN_DEBUG"write error!!!\n"); //输出信息
else
printk(KERN_DEBUG"write led ok!!!\n"); //输出信息
*((volatile size_t*)gpioe_data) &= ~(1<<12) ;//清除GPIOE12状态
*((volatile size_t*)gpioe_data) &= ~(1<<10) ;//清除GPIOE10状态
if(status)
*((volatile size_t*)gpioe_data) |= (1<<12);//设置GPIOE12状态1
*((volatile size_t*)gpioe_data) |= (1<<10);//设置GPIOE10状态1
return 0;
}
@ -105,13 +115,16 @@ static int led_probe(struct platform_device *pdev)
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); //获取device中的GPIOE_CFG0
gpioe_cfg0 = ioremap(res->start,(res->end - res->start)+1);
gpioe_cfg0 = ioremap(res->start, res->end - res->start +1);
res = platform_get_resource(pdev, IORESOURCE_MEM, 1); //获取device中的GPIOE_CFG1
gpioe_cfg1 = ioremap(res->start,(res->end - res->start)+1);
gpioe_cfg1 = ioremap(res->start, res->end - res->start +1);
res = platform_get_resource(pdev, IORESOURCE_MEM, 2); //获取device中的GPIOE_DATA
gpioe_data = ioremap(res->start,(res->end - res->start)+1);
gpioe_data = ioremap(res->start, res->end - res->start +1);
res = platform_get_resource(pdev, IORESOURCE_MEM, 3); //获取device中的GPIOE_PUL0
gpioe_pul0 = ioremap(res->start,(res->end - res->start)+1);
gpioe_pul0 = ioremap(res->start, res->end - res->start +1);
return 0;
}
@ -122,7 +135,7 @@ static int led_remove(struct platform_device *pdev)
iounmap(gpioe_cfg1); //取消GPIOE_CFG1映射
iounmap(gpioe_data); //取消GPIOE_DATA映射
iounmap(gpioe_pul0); //取消GPIOE_PUL0映射
cdev_del(led_dev); //从内核中删除设备管理结构体
cdev_del(led_dev); //从内核中删除设备管理结构体
unregister_chrdev_region(led_dev_num,1); //注销设备号
device_destroy(led_class,led_dev_num); //删除设备节点
@ -132,10 +145,12 @@ static int led_remove(struct platform_device *pdev)
static struct of_device_id led_match_table[] = {
{.compatible = "lite200,led",},
{ },
};
static struct platform_device_id led_device_ids[] = {
{.name = "led",},
{ },
};
static struct platform_driver led_driver=