编写驱动代码实现LED灯点亮

一、head.h

#ifndef __HEAD_H__
#define __HEAD_H__

typedef struct
{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
} gpio_t;

#define PHY_RCC_ADDR  0X50000A28
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000

#endif

二、test.c

#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, const char *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }

    printf("打开字符设备文件成功\n");
    while (1)
    {
        printf("请输入需要控制的LED灯[1(LED1) 2(LED2) 3(LED3)]以及状态[0(关灯) 1(开灯)]>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        // 向设备文件中写
        write(fd, buf, sizeof(buf));
    }
    close(fd);
    return 0;
}

三、demo.c

#include 
#include 
#include 
#include 
#include 
#include "head.h"

unsigned int major;
char kbuf[128] = {};

// 定义指针指向映射后的虚拟内存
unsigned int *vir_rcc;
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;

// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_release(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d", __FILE__, __func__, __LINE__);
    int ret;
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d", __FILE__, __func__, __LINE__);
    int ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    switch (kbuf[0])
    {
    case '1':
        if (kbuf[1] == '0') 
            vir_led1->ODR &= (~(0x1 << 10));
        else 
            vir_led1->ODR |= (0x1 << 10);
        break;
    case '2':              
        if (kbuf[1] == '0') 
            vir_led2->ODR &= (~(0x1 << 10));
        else 
            vir_led2->ODR |= (0x1 << 10);
        break;
    case '3':              
        if (kbuf[1] == '0') 
            vir_led3->ODR &= (~(0x1 << 8));
        else 
            vir_led3->ODR |= (0x1 << 8);
        break;
    }
    return 0;
}
// 定义操作方法结构体对象
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_release};
// 入口函数,安装内核模块时执行
static int __init mycdev_init(void)
{
    // static 修饰当前函数只能在本文件使用
    // int 函数的返回值类型

    // 注册字符设备驱动
    // static inline int register_chrdev(unsigned int major, const char *name,
    //			  const struct file_operations *fops)
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);

    // 寄存器地址映射
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_led1 = ioremap(PHY_LED1_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_led2 = ioremap(PHY_LED2_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_led3 = ioremap(PHY_LED3_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    // 寄存器初始化
    (*vir_rcc) |= (0x3 << 4);

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

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

    vir_led3->MODER &= (~(0x3 << 16));
    vir_led1->MODER |= (0x1 << 16);
    vir_led1->ODR &= (~(0x1 << 8));
    printk("寄存器初始化成功\n");

    return 0;
}
static void __exit mycdev_exit(void)
{
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(vir_rcc);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(驱动,linux,驱动开发)