驱动点亮3栈灯的实现2

 myled.h:

#ifndef __MYLED_H__
#define __MYLED_H__

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;

#define PHY_LEDE_BASE   0x50006000
#define PHY_LEDF_BASE   0x50007000
#define PHY_LED_RCC     0x50000a28


#define LED1_ON (virt_led1_3_base->ODR |= (1<<10))
#define LED1_OFF (virt_led1_3_base->ODR &= ~(1<<10))

#define LED2_ON (virt_led2_base->ODR |= (1<<10))
#define LED2_OFF (virt_led2_base->ODR &= ~(1<<10))

#define LED3_ON (virt_led1_3_base->ODR |= (1<<8))
#define LED3_OFF (virt_led1_3_base->ODR &= ~(1<<8))


enum{
    LED1,
    LED2,
    LED3,
};

#endif

mycdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "mycdev"



struct cdev *cdev;
int major = 0;
int minor = 0;

const int count = 3;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
int curminor;
gpio_t *virt_led1_3_base; // gpioe10 gpioe8
gpio_t *virt_led2_base;   // gpioe8
unsigned int *virt_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    curminor = MINOR(inode->i_rdev);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *offs)
{
    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 data to user error\n");
        return -EIO;
    }
    return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *offs)
{
    int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    //校验大小
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    //完成数据传输
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy data from user error\n");
        return -EIO;
    }
    switch (curminor)
    {
    case LED1:
        kbuf[0]=='1'?LED1_ON:LED1_OFF;
        break;
    case LED2:
        kbuf[0]=='1'?LED2_ON:LED2_OFF;
        break;
    case LED3:
        kbuf[0]=='1'?LED3_ON:LED3_OFF;
        break;
    }
    return size;
}
int mycdev_release(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_release};
int led_init(void)
{
    // 1.地址映射
    virt_led1_3_base = ioremap(PHY_LEDE_BASE, sizeof(gpio_t));
    if (virt_led1_3_base == NULL)
    {
        printk("ioreamap virt_led1_3_base register error\n");
        return -ENOMEM;
    }
    virt_led2_base = ioremap(PHY_LEDF_BASE, sizeof(gpio_t));
    if (virt_led2_base == NULL)
    {
        printk("ioreamap virt_led2_base register error\n");
        return -ENOMEM;
    }
    virt_rcc = ioremap(PHY_LED_RCC, 4);
    if (virt_rcc == NULL)
    {
        printk("ioreamap rcc register error\n");
        return -ENOMEM;
    }
    // 2.初始化led
    *virt_rcc |= (1 << 4); // rcc gpioe enable
    *virt_rcc |= (1 << 5); // rcc gpiof enable

    virt_led1_3_base->MODER &= ~(3 << 20);
    virt_led1_3_base->MODER |= (1 << 20); // output
    virt_led1_3_base->ODR &= ~(1 << 10);  // led1 off

    virt_led2_base->MODER &= ~(3 << 20);
    virt_led2_base->MODER |= (1 << 20); // output
    virt_led2_base->ODR &= ~(1 << 10);  // led2 off

    virt_led1_3_base->MODER &= ~(3 << 16);
    virt_led1_3_base->MODER |= (1 << 16); // output
    virt_led1_3_base->ODR &= ~(1 << 8);  // led3 off

    return 0;
}
void led_deinit(void)
{
    virt_led1_3_base->ODR &= ~(1 << 10); // led1 off
    virt_led2_base->ODR &= ~(1 << 10); // led2 off
    virt_led1_3_base->ODR &= ~(1 << 8); // led3 off
    iounmap(virt_rcc);
    iounmap(virt_led1_3_base);
    iounmap(virt_led2_base);
}
static int __init mycdev_init(void)
{
    int ret;
    int i;
    dev_t devno;
    // 1.分配对象
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("alloc cdev memory error\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    // 2.对象初始化
    cdev_init(cdev, &fops);
    // 3.申请设备号
    if (major > 0)
    {
        ret = register_chrdev_region(MKDEV(major, minor), count, CNAME);
        if (ret)
        {
            printk("static:alloc device number error\n");
            goto ERR2;
        }
    }
    else if (major == 0)
    {
        ret = alloc_chrdev_region(&devno, minor, count, CNAME);
        if (ret)
        {
            printk("dynamic:alloc device number error\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    // 4.注册
    ret = cdev_add(cdev, MKDEV(major, minor), count);
    if (ret)
    {
        printk("register char device driver error\n");
        goto ERR3;
    }
    // 5.自动创建设备结点
    cls = class_create(THIS_MODULE, CNAME);
    if (IS_ERR(cls))
    {
        printk("class create error\n");
        goto ERR4;
    }
    for (i = 0; i < count; i++)
    {

        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("device create error\n");
            goto ERR5;
        }
    }
    led_init();
    return 0; //!!!!!千万不能删除
ERR5:
    for (--i; i >= 0; i--)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), count);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    int i;
    led_deinit();
    for (i = 0; i < count; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);

    cdev_del(cdev);

    unregister_chrdev_region(MKDEV(major, minor), count);

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

test.c:

#include <head.h>

int main(int argc,const char * argv[])
{
    int fd;
    char buf[128]={0};

    if((fd=open("/dev/mycdev0",O_RDWR))==-1){
        PRINT_ERR("open mycdev error");   
    }
    //从终端将数据输入到应用程序的buf中
    printf("input > ");
    fgets(buf,sizeof(buf),stdin);
    buf[strlen(buf)-1]='\0';

    //将buf的数据写入到内核空间
    write(fd,buf,sizeof(buf));
    //将内核空间的数据读取到用户空间
    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("get kernel data=%s\n",buf);
    close(fd);
    return 0;
}

Makefile:


arch?=x86
modname?=demo

ifeq ($(arch),arm)
KERNELDIR:=/home/ubuntu/linux-5.10.61 #在开发板上安装
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build #在Ubuntu上安装
endif
PWD:=$(shell pwd)
all:
	make -C $(KERNELDIR) M=$(PWD) modules

install:
	cp *.ko a.out ~/nfs/rootfs/

clean:
	make -C $(KERNELDIR) M=$(PWD) clean


obj-m:=$(modname).o
#指定编译的模块名是demo

实现现象:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值