10.10号作业

#ifndef __MYLED_H__
#define __MYLED_H__
#define PHY_LED1_MODE 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
 
#define PHY_LED2_MODE 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED2_RCC 0x50000A28
 
#define PHY_LED3_MODE 0x50006000
#define PHY_LED3_ODR 0x50006014
#define PHY_LED3_RCC 0x50000A28
#endif
#include 
#include 
#include 
#include 
#include 
#include "myled.h"
#define CNAME "mycdev"
int major;           //接收主设备号
char kbuf[128] = {}; //存放和用户之间的拷贝的数据
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led1_rcc;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led2_rcc;
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_led3_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    if (kbuf[0] == '1') //开灯
        (*vir_led1_odr) |= (1 << 10);
    else if (kbuf[0] == '2')
        (*vir_led2_odr) |= (1 << 10);
    else if (kbuf[0] == '3')
        (*vir_led3_odr) |= (1 << 8);
    else if (kbuf[0] == '0')
    {
        (*vir_led1_odr) &= ~(1 << 10);
        (*vir_led2_odr) &= ~(1 << 10);
        (*vir_led3_odr) &= ~(1 << 8);
    }
    return size;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};
static int __init demo_init(void)
{
    major = register_chrdev(0, CNAME, &fops); //驱动注册
    if (major < 0)
    {
        printk("字符串设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    /***LED1****/
    vir_led1_moder = ioremap(PHY_LED1_MODE, 4);
    if (vir_led1_moder == NULL)
    {
        printk("vir_led1_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led1_moder 映射成功\n");
 
    vir_led1_odr = ioremap(PHY_LED1_ODR, 4);
    if (vir_led1_odr == NULL)
    {
        printk("vir_led1_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led1_odr 映射成功\n");
 
    vir_led1_rcc = ioremap(PHY_LED1_RCC, 4);
    if (vir_led1_rcc == NULL)
    {
        printk("vir_led1_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led1_rcc 映射成功\n");
    //寄存器初始化
    (*vir_led1_moder) &= ~(3 << 20);
    (*vir_led1_moder) |= (1 << 20);
    (*vir_led1_odr) &= ~(1 << 10);
    (*vir_led1_rcc) |= (1 << 4);
 
    /***LED2****/
    vir_led2_moder = ioremap(PHY_LED2_MODE, 4);
    if (vir_led2_moder == NULL)
    {
        printk("vir_led2_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led2_moder 映射成功\n");
 
    vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
    if (vir_led2_odr == NULL)
    {
        printk("vir_led2_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led2_odr 映射成功\n");
 
    vir_led2_rcc = ioremap(PHY_LED2_RCC, 4);
    if (vir_led2_rcc == NULL)
    {
        printk("vir_led2_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led2_rcc 映射成功\n");
    //寄存器初始化
    (*vir_led2_moder) &= ~(3 << 20);
    (*vir_led2_moder) |= (1 << 20);
    (*vir_led2_odr) &= ~(1 << 10);
    (*vir_led2_rcc) |= (1 << 5);
 
    /***LED3****/
    vir_led3_moder = ioremap(PHY_LED3_MODE, 4);
    if (vir_led3_moder == NULL)
    {
        printk("vir_led3_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led3_moder 映射成功\n");
 
    vir_led3_odr = ioremap(PHY_LED3_ODR, 4);
    if (vir_led3_odr == NULL)
    {
        printk("vir_led3_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led3_odr 映射成功\n");
 
    vir_led3_rcc = ioremap(PHY_LED3_RCC, 4);
    if (vir_led3_rcc == NULL)
    {
        printk("vir_led3_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led3_rcc 映射成功\n");
    //寄存器初始化
    (*vir_led3_moder) &= ~(3 << 16);
    (*vir_led3_moder) |= (1 << 16);
    (*vir_led3_odr) &= ~(1 << 8);
    (*vir_led3_rcc) |= (1 << 4);
    return 0;
}
static void __exit demo_exit(void)
{
    unregister_chrdev(major, CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    int fd = open("/dev/mychdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("打开设备文件成功\n");
    //从终端读取
    while (1)
    {
        printf("请输入命令 1:开启LED1\t2:开启LED2\t3:开启LED3\t0:关灯\n");
        fgets(buf, sizeof(buf), stdin);
        if (buf[0] != '1' && buf[0] != '2' && buf[0] != '3' && buf[0] != '0')
        {
            printf("输入错误\n");
            continue;
        }
        //吃掉换行符
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
        //清空buf
        memset(buf, 0, sizeof(buf));
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(c++,c#,c语言)