全志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模式,电平为低
在这里插入图片描述

结束

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
编译 Linux 内核需要以下步骤: 1. 获取 Linux 内核源代码。你可以从官方网站或 GitHub 上下载最新的内核源代码。 2. 安装交叉编译工具链。如果你的开发环境是 x86 架构的,你需要安装交叉编译工具链来编译适用于 ARM 架构的内核。你可以使用 apt-get 命令或其他包管理器来安装工具链。例如,在 Ubuntu 或 Debian 上,你可以使用以下命令来安装: ``` sudo apt-get install gcc-arm-linux-gnueabihf ``` 3. 配置内核。进入内核源代码目录,使用以下命令来配置内核: ``` make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- orangepi_zero2_defconfig ``` 这将使用默认配置文件来配置内核。 4. 编译内核。使用以下命令来编译内核: ``` make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage dtbs modules ``` 这将编译内核镜像、设备树和内核模块。 5. 安装内核。将编译好的内核镜像和设备树文件复制到 SD 卡的 boot 分区中。例如,在 Ubuntu 或 Debian 上,你可以使用以下命令来安装: ``` sudo cp arch/arm/boot/zImage /media/boot/ sudo cp arch/arm/boot/dts/sun8i-h2-plus-orangepi-zero2.dtb /media/boot/ ``` 6. 配置引导加载程序。编辑 SD 卡的 boot 分区中的 boot.cmd 文件,并添加以下内容: ``` setenv bootargs console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait panic=10 fatload mmc 0:1 ${kernel_addr_r} zImage fatload mmc 0:1 ${fdt_addr_r} sun8i-h2-plus-orangepi-zero2.dtb bootz ${kernel_addr_r} - ${fdt_addr_r} ``` 这将配置引导加载程序以加载内核镜像和设备树,并指定内核参数。 7. 生成引导加载程序镜像。使用以下命令来生成引导加载程序镜像: ``` mkimage -C none -A arm -T script -d boot.cmd boot.scr ``` 这将生成一个名为 boot.scr 的文件,它是引导加载程序镜像。 8. 将引导加载程序镜像复制到 SD 卡的 boot 分区中: ``` sudo cp boot.scr /media/boot/ ``` 9. 将 SD 卡插入 Orange Pi Zero 2 开发板,并启动板子。如果一切正常,你应该看到内核启动信息在串口终端中输出。 注意:这只是一个简单的指南,实际的操作可能会因环境和设备而有所不同。请确保你熟悉 Linux 内核编译和 Orange Pi Zero 2 开发板的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Strange_Head

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值