驱动开发——点灯实验

头文件

#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED_RCC 0x50000a28

#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014

#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014

#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014

#endif

驱动代码

#include 
#include 
#include 
#include 
#include "head.h"
#include 
unsigned int major;

char kbuf[128] = {0};

unsigned int *vir_rcc;

unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;

unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;

unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;

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)
{
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return ret;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (kbuf[0] == '1')
    {
        if(kbuf[1] == '1')
            (*vir_led1_odr) |= (1 << 10);
        else if(kbuf[1] == '0')
            (*vir_led1_odr) &= (~(1 << 10));
    }
    else if (kbuf[0] == '2')
    {
        if(kbuf[1] == '1')
            (*vir_led2_odr) |= (1 << 10);
        else if(kbuf[1] == '0')
            (*vir_led2_odr) &= (~(1 << 10));
    }
    else if (kbuf[0] == '3')
    {
        if(kbuf[1] == '1')
            (*vir_led3_odr) |= (1 << 8);
        else if(kbuf[1] == '0')
            (*vir_led3_odr) &= (~(1 << 8));
    }
    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,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};
static int __init mycdev_init(void)
{
    major = register_chrdev(0, "mycdev", &fops);
    if (major < 0)
    {
        printk("cdev create failed\n");
        return major;
    }
    printk("cdev create success majoer=%d\n", major);

    vir_rcc = ioremap(PHY_LED_RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led1_moder = ioremap(PHY_LED1_MODER, 4);
    if (vir_led1_moder == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led1_odr = ioremap(PHY_LED1_ODR, 4);
    if (vir_led1_odr == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2_moder = ioremap(PHY_LED2_MODER, 4);
    if (vir_led2_moder == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
    if (vir_led2_odr == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3_moder = ioremap(PHY_LED3_MODER, 4);
    if (vir_led3_moder == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3_odr = ioremap(PHY_LED3_ODR, 4);
    if (vir_led3_odr == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    (*vir_rcc) |= (3 << 4);

    (*vir_led1_moder) &= (~(3 << 20));
    (*vir_led1_moder) |= (1 << 20);
    (*vir_led1_odr) |= (~(1 << 10));

    (*vir_led2_moder) &= (~(3 << 20));
    (*vir_led2_moder) |= (1 << 20);
    (*vir_led2_odr) |= (~(1 << 10));

    (*vir_led3_moder) &= (~(3 << 16));
    (*vir_led3_moder) |= (1 << 16);
    (*vir_led3_odr) |= (~(1 << 8));

    return 0;
}
static void __exit mycdev_exit(void)
{
    unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mycdev", O_RDWR);
    if (fd < 0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入LED灯号(1、2、3)和灯的亮灭(0、1)>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
    }
    close(fd);
    return 0;
}

驱动开发——点灯实验_第1张图片

 

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