驱动开发点亮led灯

头文件

c 复制代码
#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED_MODER 0X50006000
#define PHY_LED_ODR 0X50006014
#define PHY_LED_RCC 0X50000A28
#define PHY_LED_FMODER 0X50007000
#define PHY_LED_FODR 0X50007014


#endif

驱动代码

c 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include "head.h"
#include <linux/io.h>
#include <linux/device.h>

unsigned int major;
char kbuf[128] = {0};
unsigned int *vir_moder;
unsigned int *vir_fmoder;
unsigned int *vir_rcc;
unsigned int *vir_odr;
unsigned int *vir_fodr;
struct class *clz;
struct device *dev;

int myDev_open(struct inode *major, struct file *file)
{
    printk("%s:%s,%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t myDev_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;
    }
    switch (kbuf[0])
    {
    case '1'://pe10
        if (kbuf[1] == '1')
        {
            (*vir_odr) |= (0x1 << 10);
        }
        else if (kbuf[1] == '0')
        {
            (*vir_odr) &= (~(0x1 << 10));
        }
        break;
    case '2'://pe8
        if (kbuf[1] == '1')
        {
             (*vir_fodr) |= (0x1 << 10);
        }
        else if (kbuf[1] == '0')
        {
             (*vir_fodr) &= (~(0x1 << 10));
        }
        break;
    case '3'://pf10
        if (kbuf[1] == '1')
        {
             (*vir_odr) |= (0x1 << 8);
        }
        else if (kbuf[1] == '0')
        {
             (*vir_odr) &= (~(0x1 << 8));
        }
        break;
    default:
        break;
    }
    return 0;
}

ssize_t myDev_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;
    }
    return 0;
}

int myDev_close(struct inode *major, struct file *file)
{
    printk("myDev_close\n");
    return 0;
}

struct file_operations fop = {
    .open = myDev_open,
    .release = myDev_close,
    .read = myDev_read,
    .write = myDev_write};

static int __init mydev_init(void)
{
    int i = 0;
    major = register_chrdev(0, "myCharDev", &fop);
    if (major < 0)
    {
        printk("设备注册失败\n");
        return major;
    }
    printk("设备注册成功!major=%d\n", major);
    vir_moder = ioremap(PHY_LED_MODER, 4);
    if (vir_moder == NULL)
    {
        printk("内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_fmoder = ioremap(PHY_LED_FMODER, 4);
    if (vir_fmoder == NULL)
    {
        printk("内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_rcc = ioremap(PHY_LED_RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr = ioremap(PHY_LED_ODR, 4);
    if (vir_odr == NULL)
    {
        printk("内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_fodr = ioremap(PHY_LED_FODR, 4);
    if (vir_fodr == NULL)
    {
        printk("内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    // pe10
    (*vir_moder) &= (~(0x3 << 20));
    (*vir_moder) |= (0x1 << 20);
    // pe8
    (*vir_moder) &= (~(0x3 << 16));
    (*vir_moder) |= (0x1 << 16);
    // pf10
    (*vir_fmoder) &= (~(0x3 << 20));
    (*vir_fmoder) |= (0x1 << 20);

    (*vir_rcc) |= (0x1 << 4);
    (*vir_rcc) |= (0x1 << 5);

    (*vir_odr) &= (~(0x1 << 10));
    (*vir_odr) &= (~(0x1 << 8));
    (*vir_fodr) &= (~(0x1 << 10));
    clz = class_create(THIS_MODULE, "mydev");
    if (IS_ERR(clz))
    {
        printk("提交目录失败\n");
        return -PTR_ERR(clz);
    }
    printk("提交目录成功\n");
    for (i = 0; i < 3; i++)
    {
        dev = device_create(clz, NULL, MKDEV(major, i), NULL, "mydev%d", i);
        if (IS_ERR(dev))
        {
            printk("提交设备节点信息失败\n");
            return -PTR_ERR(clz);
        }
    }
    printk("提交设备节点信息成功\n");
    return 0;
}

static void __exit mydev_exit(void)
{
    printk("mydev_exit\n");
    iounmap(vir_moder);
    iounmap(vir_rcc);
    iounmap(vir_odr);
    iounmap(vir_fmoder);
    iounmap(vir_fodr);
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(clz, MKDEV(major, i));
    }
    class_destroy(clz);
    unregister_chrdev(major, "myCharDev");
}

module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");

测试代码

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


int main()
{
    char buf[128];
    int fd = open("/dev/mydev0", O_RDWR);
    if (fd < 0)
    {
        perror("open failed!\n");
        exit(-1);
    }
    printf("打开设备成功!\n");
    while (1)
    {
        printf("请输入led灯的逻辑》\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;
}
相关推荐
丁总学Java2 分钟前
ARM 架构(Advanced RISC Machine)精简指令集计算机(Reduced Instruction Set Computer)
arm开发·架构
2202_754421544 小时前
一个计算频率的模块
驱动开发·fpga开发
嵌入(师)1 天前
嵌入式驱动开发详解1(系统调用)
驱动开发
昵称p1 天前
杂项驱动开发
驱动开发·gpio子系统·杂项驱动开发
韦德斯1 天前
嵌入式Linux的RTC读写操作应用
linux·运维·c语言·arm开发·实时音视频
byte轻骑兵1 天前
嵌入式 ARM Linux 系统构成全解:从硬件到应用层层剖析
linux·arm开发·arm·嵌入式开发
7yewh1 天前
嵌入式硬件杂谈(四)-高速板PCB设计 高速信号全面讲解 蛇形线 等长线 差分对 阻抗对
驱动开发·嵌入式硬件·mcu·物联网·硬件工程·pcb工艺·精益工程
思尔芯S2C1 天前
面向未来的智能视觉参考设计与汽车架构,思尔芯提供基于Arm技术的创新方案
arm开发·架构·汽车·iot·fpga原型验证·prototyping·智慧视觉
lishing62 天前
Linux驱动开发(9):pinctrl子系统和gpio子系统--led实验
linux·运维·驱动开发
lishing62 天前
Linux驱动开发(7):使用设备树实现RGB 灯驱动
linux·驱动开发