day2驱动点灯

驱动程序

#include 
#include 
#include 
#include 
#include "led.h"
unsigned int major; // 定义一个变量保存主设备号
char kbuf[128] = {0};

gpio_t *GPIOE;
gpio_t *GPIOF;
unsigned int *RCC;

// 封装操作方法
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)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    long ret;
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    return 0;
}

void led_ctrl(char a, char b)
{
    unsigned int t;
    if (b == '0')
    {
        t = 0x0;
    }
    if (b == '1')
    {
        t = 0x1;
    }
    if (a == '1')
    {
        // led1
        GPIOE->ODR &= (~(0x1 << 10));
        GPIOE->ODR |= (t << 10);
    }
    if (a == '2')
    {
        // led2
        GPIOF->ODR &= (~(0x1 << 10));
        GPIOF->ODR |= (t << 10);
    }
    if (a == '3')
    {
        // led3
        GPIOE->ODR &= (~(0x1 << 8));
        GPIOE->ODR |= (t << 8);
    }
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    long ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    // 判断用户输入的数据,进行不同的硬件逻辑执行

    led_ctrl(kbuf[0], kbuf[1]);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
    .open = mycdev_open,
    .release = mycdev_close,
    .read = mycdev_read,
    .write = mycdev_write,
};

static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("注册字符设备驱动失败\n");
        return major;
    }
    printk("注册字符设备驱动成功major=%d\n", major);

    // 进行寄存器地址的映射-----------------------------------------------
    GPIOE = ioremap(PHY_GPIOE, 4);
    if (GPIOE == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }

    GPIOF = ioremap(PHY_GPIOF, 4);
    if (GPIOF == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }

    RCC = ioremap(PHY_RCC, 4);
    if (RCC == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }

    printk("映射物理内存成功\n");
    // 硬件寄存器的初始化
    (*RCC) |= (0X3 << 4); // rcc使能

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

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

    GPIOE->MODER &= (~(0x3 << 16));
    GPIOE->MODER |= (0x1 << 16);
    GPIOE->ODR |= (~(0x1 << 8));

    printk("硬件寄存器初始化成功\n");
    return 0;
}

static void __exit mycdev_exit(void)
{
    ionumap(PHY_GPIOE);
    ionumap(PHY_GPIOF);
    ionumap(PHY_RCC);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

头文件

#ifndef __LED_H__
#define __LED_H__

typedef struct
{
    unsigned int MODER;   // 00
    unsigned int OTYPER;  // 04
    unsigned int OSPEEDR; // 08
    unsigned int PUPDR;   // 0C
    unsigned int IDR;     // 10
    unsigned int ODR;     // 14
} gpio_t;

#define PHY_GPIOE 0X50006000
#define PHY_GPIOF 0X50007000
#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_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入控制命令:0(关灯)1(开灯)>");
        fgets(buf, sizeof(buf), stdin); // 从终端输入数据到buf
        buf[strlen(buf) - 1] = '\0';    // 将buf末尾的'\n'切换为'\0'
        write(fd, buf, sizeof(buf));
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(arm开发)