LED点灯实验

LED点灯实验_第1张图片

mycdev.c 

#include 
#include 
#include 
#include 
#include 
#include "myled.h"
#define CNAME "mycdev"

int major;
int kbuf[128]={};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;


unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir2_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 __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_odr)|=(1<<10);
    }
    else if(kbuf[0] == '2')
    {
        (*vir2_odr) |= (1<<10);
    }
      else if(kbuf[0] == '3')
    {
        (*vir_odr) |= (1<<8);
    }else
    {
        (*vir_odr) &=~(1<<10);
        (*vir2_odr) &=~(1<<10);
        (*vir_odr) &=~(1<<8);
    }
    
 
   return size;
}

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,CNAME,&fops);
    if(major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
     printk("字符设备驱动注册成功 major=%d\n",major);

     //物理地址映射
     vir_moder=ioremap(PHY_LED1_MODER,4);
     if(vir_moder == NULL)
     {
        printk("vir_moder 映射失败\n");
        return -ENOMEM;
     }
     printk("vir_moder 映射成功\n");

     vir_odr=ioremap(PHY_LED1_ODR,4);
     if(vir_odr == NULL)
     {
        printk("vir_odr 映射失败\n");
        return -ENOMEM;
     }
     printk("vir_odr 映射成功\n");

     vir_rcc=ioremap(PHY_LED1_RCC,4);
     if(vir_rcc == NULL)
     {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
     }
     printk("vir_rcc 映射成功\n");



vir2_moder=ioremap(PHY_LED2_MODER,4);
     if(vir2_moder == NULL)
     {
        printk("vir2_moder 映射失败\n");
        return -ENOMEM;
     }
     printk("vir2_moder 映射成功\n");

     vir2_odr=ioremap(PHY_LED2_ODR,4);
     if(vir2_odr == NULL)
     {
        printk("vir2_odr 映射失败\n");
        return -ENOMEM;
     }
     printk("vir2_odr 映射成功\n");

     vir2_rcc=ioremap(PHY_LED2_RCC,4);
     if(vir2_rcc == NULL)
     {
        printk("vir2_rcc 映射失败\n");
        return -ENOMEM;
     }
     printk("vir2_rcc 映射成功\n");

    //寄存器初始化

    (*vir_moder) &= ~(3<<20);
    (*vir_moder) |= (1<<20);
    (*vir_odr) &= ~(1<<10);
    (*vir_rcc) |= (1<<4);

     (*vir_rcc) |= (1<<5);

    //led2
    (*vir2_moder) &= ~(3 << 20);
    (*vir2_moder) |= (1 << 20);
    (*vir2_odr) &= ~(1<<10);

    //led3
    (*vir_moder) &= ~(3 << 16);
    (*vir_moder) |= (1 << 16);
    (*vir_odr) &= ~(1<<8);


    return 0;
}
static void __exit mycdev_exit(void)
{
     unregister_chrdev(major,CNAME);

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

myled.h

#ifndef __MYLED_H__
#define __MYLED_H__

#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR   0X50006014
#define PHY_LED1_RCC   0X50000A28

#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR   0X50007014
#define PHY_LED2_RCC   0X50000A28

#define PHY_LED3_MODER 0X50007000
#define PHY_LED3_ODR   0X50007014
#define PHY_LED3_RCC   0X50000A28
#endif

1.c

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

int main(int argc, char const *argv[])
{
    char buf[128]={};
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("设备文件打开成功\n");
    while (1)
    {
        printf("请输入命令:1.led1开灯 2.led2开灯 3.led3开灯  0.关灯\n");
        fgets(buf,sizeof(buf),stdin);

        buf[strlen(buf)-1] = '\0';
        write(fd,buf,sizeof(buf));

    } 
    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf)); 
    close(fd);
    return 0;
}

你可能感兴趣的:(嵌入式硬件,c语言)