当前位置: 首页 > news >正文

全志orangepi-zero2驱动编写2,控制电平高低

使用驱动编写控制高低电平

可看我前俩篇文章:
【1】全志orangepi-zeor2驱动编写
【2】驱动函数框架详解

检索芯片手册关键信息

知道GPIO基地址
在这里插入图片描述
知道PC偏移地址
在这里插入图片描述
知道想要控制的端口的信息
在这里插入图片描述

知道数据位如何操作
在这里插入图片描述

代码实操

驱动代码

#include <linux/fs.h>               //file_operations声明
#include <linux/module.h>           //module_init module_exit声明
#include <linux/init.h>             //__init__exit 宏声明
#include <linux/device.h>           //class device 声明
#include <linux/uaccess.h>          //copy_from_user的头文件
#include <linux/types.h>            //设备号dev_t类型声明
#include <asm/io.h>                 //ioremap iounmap的头文件static struct class *pin5_class;
static struct device *pin5_class_dev;static dev_t devno;            //设备号
static int major =231;       //主设备号
static int minor =0;          //次设备号
static char *module_name="pin5";   //模块名volatile unsigned int* GPIOBASE =  NULL;
volatile unsigned int* GPIOPC   =  NULL;
volatile unsigned int* GPIODAT  =  NULL;//ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
static ssize_t pin5_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{printk("pin5_read\n");return 0;
}static int pin5_open(struct inode *inode,struct file *file)
{printk("pin5_open\n");      //内核打印函数,和printf类似*GPIOPC &= ~(0x6 << 20);         //把bit22~bit20 配置成001 ,为输出模式*GPIOPC |= (0x1 << 20);return 0;
}static ssize_t pin5_write(struct file *file,const char __user *buf,size_t count, loff_t *ppos)
{char user_cmd;printk("pin5_write\n");//获取用户空间write的值copy_from_user(&user_cmd,buf,count);//根据值来操控io口,高电平或者低电平if(user_cmd == '1'){*GPIODAT |= 0x01 << 5;printk("pin5_set\n");}else if(user_cmd == '0'){*GPIODAT &= ~(0x01 << 5);printk("pin5_reset\n");}else{printk("undo\n");}
static struct file_operations pin5_fops = {.owner = THIS_MODULE,.open  = pin5_open,.write = pin5_write,.read  = pin5_read,
};
//static int __init
static int  pin5_drv_init(void)
{int ret;devno = MKDEV(major,minor);             //创建设备号ret   = register_chrdev(major, module_name,&pin5_fops);         //注册驱动 告诉内核 把这个驱动加入到>内核链表当中pin5_class=class_create(THIS_MODULE,"myfirstdemo");             //让代码在dev自动生成设备pin5_class_dev =device_create(pin5_class,NULL,devno,NULL,module_name);          //创建设备文件//映射虚拟地址//GPIO基地址GPIOBASE =  (volatile unsigned int *)ioremap(0x0300B000,4);//GPIOPC地址GPIOPC   =  (volatile unsigned int *)ioremap(0x0300B048,4);//GPIO数据地址GPIODAT  =  (volatile unsigned int *)ioremap(0x0300B058,4);return 0;
}static void pin5_drv_exit(void)
{iounmap(GPIOBASE);iounmap(GPIOPC);iounmap(GPIODAT);device_destroy(pin5_class,devno);class_destroy(pin5_class);unregister_chrdev(major, module_name);
}module_init(pin5_drv_init);
module_exit(pin5_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("SHUN-GE");

用户空间测试代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main()
{int fd;int cmd;fd = open("/dev/pin5",O_RDWR);if(fd < 0){printf("open fail\n");}else {printf("open success\n");}scanf("%d",&cmd);if(cmd == 1){fd = write(fd,"1",1);printf("%d=cmd \n",cmd);}else if(cmd == 0){fd = write(fd,"0",1);printf("%d=cmd \n",cmd);}
}

结果展示

PC5为OUT模式,电平为1
在这里插入图片描述
PC5为OUT模式,电平为低
在这里插入图片描述

结束

如有问题,欢迎提出,共同进步。

http://www.lryc.cn/news/135186.html

相关文章:

  • 软考高级系统架构设计师系列之:论文典型试题写作要点和写作素材总结系列文章四
  • 06.利用Redis实现点赞功能
  • 【React】生命周期和钩子函数
  • 无涯教程-TensorFlow - 优化器
  • 基于AQS+双向链表实现队列先进先出
  • 无涯教程-Perl - time函数
  • CUDA Bug<三>当__global__函数出现里面所有输出的数组都随机赋值了
  • 甜椒叶病害识别(Python代码,pyTorch框架,深度卷积网络模型,很容易替换为其它模型,带有GUI识别界面)
  • Python爬虫——scrapy_日志信息以及日志级别
  • 微信小程序 echarts 画多个横向柱状图
  • 【二叉树】572. 另一棵树的子树
  • 220V转5V芯片三脚芯片-AH8652
  • windows系统丢失mfc120u.dll的解决方法
  • css 实现电梯导航
  • 【Spring Boot】Spring Retry减少1000 行代码讲解
  • 【数据结构OJ题】相交链表
  • 【华为OD机试】最小传输时延I【2023 B卷|200分】
  • Android13 网络 Adb 默认开启
  • Git分享-规范/建议/技巧
  • vue3文件下载功能
  • Python调用文心一言的API
  • 【计算机网络八股】计算机网络(一)
  • 记录一次arcgis engine开发版本引入问题
  • 2023年Java毕业设计怎样选题,有哪些注意事项,300道Java毕业设计题目
  • 算法-滑动窗口-串联所有单词的子串
  • 2023年7月京东美妆护肤品小样行业数据分析(京东数据挖掘)
  • 记录Taro巨坑,找不到sass类型定义文件
  • CS1988|C#无法在异步方法中使用ref,in,out类型的参数的问题
  • ubuntu开机失败——ACPI Error
  • 搭建开发环境-操作系统篇(一键搭建开发环境)