华清远见上海中心22071

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"

#define CNAME "myled"
int major;
char kbuf[128] = {0};
volatile unsigned int* virt_rcc;
gpio_t*  virt_GPIOE;
gpio_t* virt_GPIOF;


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 __user *ubuf, size_t size, loff_t *loffs)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //校验传输数据大小,如果用户空间传输数据的大小大于内核空间,需要更正传输数据大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    ret = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy from user is error\n");
        return -EIO;
    }
    return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //1.校验传输数据大小,如果用户空间传输数据的大小大于内核空间,需要更正传输数据大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy from user is error\n");
        return -EIO;
    }
    printk("copy from user kbuf = %s\n",kbuf);
    if(kbuf[0] == '1') 
    {
        if(kbuf[1] == '1')
        {
            virt_GPIOE->ODR |= (0x1 << 10);
            virt_GPIOE->ODR |= (0x1 << 8);
            virt_GPIOF->ODR |= (0x1 << 10);
        }
        else if(kbuf[1] == '0')
        {
            virt_GPIOE->ODR &= (~(0x1 << 10));
             virt_GPIOE->ODR &= (~(0x1 << 8));
              virt_GPIOF->ODR &= (~(0x1 << 10));
        }
    }
    return size;
}

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


const 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,CNAME,&fops);
    if(major < 0)
    {
        printk("register chrdev is error\n");
        return major;
    }    
    printk("major=%d\n",major);

    virt_rcc = ioremap(PHY_RCC,4);
    if(NULL == virt_rcc)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    
    virt_GPIOE = ioremap(GPIOE,sizeof(gpio_t));
    if(NULL == virt_GPIOE)
    {
        printk("GPIOE is error\n");
        return -ENOMEM;
    }

    virt_GPIOF = ioremap(GPIOF,sizeof(gpio_t));
    if(NULL == virt_GPIOF)
    {
        printk("GPIOF is error\n");
        return -ENOMEM;
    }
  

    
    *virt_rcc |= (0x1 << 4);
    *virt_rcc |= (0x1 << 5);
    
    virt_GPIOE->MODER &= (~(0x3 << 20));
    virt_GPIOE->MODER |= (0x1 << 20);
    
    virt_GPIOE->MODER &= (~(0x3 << 16));
    virt_GPIOE->MODER |= (0x1 << 16);

    virt_GPIOF->MODER &= (~(0x3 << 20));
    virt_GPIOF->MODER |= (0x1 << 20);

    return 0;
}


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

 

#ifndef __LED_H__
#define __LED_H__


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

#define PHY_RCC 0x50000A28
#define GPIOE 0x50006000
#define GPIOF 0x50007000

#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

char buf[128] = {""};
int main(int argc, char const *argv[])
{
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(-1 == fd)
    {
        perror("open is error");
        exit(1);
    }

    while(1)
    {
        buf[0] = '1';
        write(fd,buf,sizeof(buf));
        buf[1] = '1';
        write(fd,buf,sizeof(buf));
        sleep(1);
        buf[1] = '0';
        write(fd,buf,sizeof(buf));
        sleep(1);
    }

    close(fd);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值