实现LED驱动点灯

实现LED1/2/3点亮

头文件:

#ifndef __MYCDEV_H__
#define __MYCDEV_H__

//GPIOx组控制器结构体
typedef struct {
    volatile unsigned int MODER;   // 0x00
    volatile unsigned int OTYPER;  // 0x04
    volatile unsigned int OSPEEDR; // 0x08
    volatile unsigned int PUPDR;   // 0x0C
    volatile unsigned int IDR;     // 0x10
    volatile unsigned int ODR;     // 0x14
    volatile unsigned int BSRR;    // 0x18
    volatile unsigned int LCKR;    // 0x1C 
    volatile unsigned int AFRL;    // 0x20 
    volatile unsigned int AFRH;    // 0x24
    volatile unsigned int BRR;     // 0x28
    volatile unsigned int res;
    volatile unsigned int SECCFGR; // 0x30

}gpio_t;

//指定GPIOE组基地址
#define  PHY_GPIOE_ADDR  0x50006000
//指定GPIOF组基地址
#define  PHY_GPIOF_ADDR  0x50007000
//RCC基地址:0x50000A28
#define PHY_RCC_LED1 0x50000A28

//LED1亮
#define LED1_ON (virt_gpioe->ODR |= (0x1 << 10))
//LED1灭
#define LED1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
//LED2亮
#define LED2_ON (virt_gpiof->ODR |= (0x1 << 10))
//LED2灭
#define LED2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
//LED3亮
#define LED3_ON (virt_gpioe->ODR |= (0x1 << 8))
//LED3灭
#define LED3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))

#endif

源文件:

#include 
#include 
#include 
#include 
#include "mycdev.h"
#include 
#include 
 
#define CNAME "myled"
int major;
char kbuf[128] = {0};
//初始化rcc、GPIOE、GPIOF组控制器
unsigned int* virt_rcc;
gpio_t* virt_gpioe;
gpio_t* virt_gpiof;
struct class* c;
struct device* d;
 
int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t myled_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    if(size > sizeof(ubuf)) size = sizeof(ubuf);
    ret = copy_to_user(ubuf,kbuf,size);
    if(!ret)
    {
        printk("copy to user failed\n");
        return -EIO;
    }
    return size;
}

ssize_t myled_write(struct file *file,const char __user *buf,size_t size,loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    ret = copy_from_user(kbuf,buf,size);
    if(ret)
    {
        printk("copy from user failed\n");
        return -EIO;
    }
    printk("kbuf[0] = %c\n",kbuf[0]);
    //kbuf[0]:1~6,分别控制3盏灯的两个状态
    switch (kbuf[0])
    {
    case '1':
        LED1_ON;
        break;
    case '2':
        LED1_OFF;
        break;
    case '3':
        LED2_ON;
        break;
    case '4':
        LED2_OFF;
        break;
    case '5':
        LED3_ON;
        break;
    case '6':
        LED3_OFF;
        break;
    default:
        printk("Wrong command\n");
        break;
    }
    return size;
}

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

const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .release = myled_close,
};
static int __init mycdev_init(void)
{
    //1.注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0) //2.判断返回值
    {
        printk("register chrdev is error\n");
    }
    //3.打印主设备号
    printk("register chrdev major=%d\n",major);
    c = class_create(THIS_MODULE,"myled");
    if(IS_ERR(c))
    {
        PTR_ERR(c);
    }
    d = device_create(c,NULL,MKDEV(major,0),NULL,"myled");
    if(IS_ERR(d))
    {
        PTR_ERR(d);
    }
    //ioremap(0x5000)
    //4.将物理地址映射为虚拟地址
    //4.1 将rcc地址映射
    virt_rcc = ioremap(PHY_RCC_LED1,4);
    if(virt_rcc == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //4.2 将GPIOE地址映射
    virt_gpioe = ioremap(PHY_GPIOE_ADDR,sizeof(gpio_t));
    if(virt_gpioe == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    //4.3 将GPIOF地址映射
    virt_gpiof = ioremap(PHY_GPIOF_ADDR,sizeof(gpio_t));
    if(virt_gpiof == NULL)
    {
        printk("odr ioremap is error\n");
        return -ENOMEM;
    }
    //5.对LED1 PE10引脚初始化
    *virt_rcc |= (0x1 << 4);
    virt_gpioe->MODER &= (~(0x3 << 20));
    virt_gpioe->MODER |= (0x1 << 20);
    virt_gpioe->ODR &= (~(0x1) << 10);
    //对LED2 PF10引脚初始化
    *virt_rcc |= (0x1 << 5);
    virt_gpiof->MODER &= (~(0x3 << 20));
    virt_gpiof->MODER |= (0x1 << 20);
    virt_gpiof->ODR &= (~(0x1) << 10);
    //对LED3 PF8引脚初始化
    virt_gpioe->MODER &= (~(0x3 << 16));
    virt_gpioe->MODER |= (0x1 << 16);
    virt_gpioe->ODR &= (~(0x1) << 8);

    return 0;
}
 
static void __exit mycdev_exit(void)
{
    device_destroy(c,MKDEV(major,0));
    class_destroy(c);
    //6.取消地址映射
    iounmap(virt_rcc);
    iounmap(virt_gpioe);
    iounmap(virt_gpiof);
    class_destroy(c);
    //7.注销字符设备驱动
    unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

//241 myled

测试文件:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
int main(int argc,const char * argv[])
{
    char cmd[128] = {0};
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }
    while(1)
    {
        // 循环发送buf[0]:1~6,控制三盏灯循环亮灭
        printf("命令1:*********LED1亮*********\n");
        printf("命令2:*********LED1灭*********\n");
        printf("命令3:*********LED2亮*********\n");
        printf("命令4:*********LED2灭*********\n");
        printf("命令5:*********LED3亮*********\n");
        printf("命令6:*********LED3灭*********\n");
        printf("请输入命令>>>>>>>");
        fgets(cmd,sizeof(cmd),stdin);
        write(fd,cmd,1);
        //system("clear");
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(c语言,开发语言)