2.15编写led驱动,通过应用程序控制三盏灯亮灭

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
 
#define RCC_GPIO 0x50000a28
#define GPIOE_MODER 0x50006000
#define GPIOE_ODR 0x50006014
#define GPIOF_MODER 0x50007000
#define GPIOF_ODR 0x50007014
 
#endif 

mychrdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
 
unsigned int major;
char kbuf[128] = "";
unsigned int *gpioe_moder;
unsigned int *gpiof_moder;
unsigned int *gpioe_odr;
unsigned int *gpiof_odr;
unsigned int *rcc;
 
ssize_t mychrdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned int ret;
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user error\n");
        return -ret;
    }
    return 0;
}
ssize_t mychrdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned int ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user error\n");
        return -ret;
    }
    if (kbuf[0] == '0') // 关灯
    {
        (*gpioe_odr) &= (~(0x1 << 10));
        (*gpiof_odr) &= (~(0x1 << 10));
        (*gpioe_odr) &= (~(0x1 << 8));
    }
    else if (kbuf[0] == '1') // LED1开灯
    {
        (*gpioe_odr) |= (0x1 << 10);
        (*gpiof_odr) &= (~(0x1 << 10));
        (*gpioe_odr) &= (~(0x1 << 8));
    }
    else if (kbuf[0] == '2') // LED2开灯
    {
        (*gpioe_odr) &= (~(0x1 << 10));
        (*gpiof_odr) |= (0x1 << 10);
        (*gpioe_odr) &= (~(0x1 << 8));
    }
    else if (kbuf[0] == '3') // LED3开灯
    {
        (*gpioe_odr) &= (~(0x1 << 10));
        (*gpiof_odr) &= (~(0x1 << 10));
        (*gpioe_odr) |= (0x1 << 8);
    }
    return 0;
}
int mychrdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mychrdev_close(struct inode *indde, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
struct file_operations fops =
    {
        .open = mychrdev_open,
        .release = mychrdev_close,
        .read = mychrdev_read,
        .write = mychrdev_write,
};
 
static int __init mycdev_init(void)
{
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
    }
    printk("字符设备驱动注册成功 major=%d\n", major);
    // 进行LED寄存器的地址映射
    gpioe_moder = ioremap(GPIOE_MODER, 4);
    if (gpioe_moder == NULL)
    {
        printk("地址映射失败%d\n", __LINE__);
        return 0;
    }
    gpioe_odr = ioremap(GPIOE_ODR, 4);
    if (gpioe_odr == NULL)
    {
        printk("地址映射失败%d\n", __LINE__);
        return 0;
    }
 
    gpiof_moder = ioremap(GPIOF_MODER, 4);
    if (gpiof_moder == NULL)
    {
        printk("地址映射失败%d\n", __LINE__);
        return 0;
    }
    gpiof_odr = ioremap(GPIOF_ODR, 4);
    if (gpiof_odr == NULL)
    {
        printk("地址映射失败%d\n", __LINE__);
        return 0;
    }
    rcc = ioremap(RCC_GPIO, 4);
    if (rcc == NULL)
    {
        printk("地址映射失败%d\n", __LINE__);
        return 0;
    }
    printk("地址映射成功\n");
    // 硬件初始化
    // 使能时钟
    (*rcc) |= (0X3 << 4);
    // 设置PE10、PF10、PE8为输出
    (*gpioe_moder) &= (~(0X3 << 20));
    (*gpioe_moder) |= (0X1 << 20);
    (*gpiof_moder) &= (~(0X3 << 20));
    (*gpiof_moder) |= (0X1 << 20);
    (*gpioe_moder) &= (~(0X3 << 16));
    (*gpioe_moder) |= (0X1 << 16);
    // 默认输出低电平
    (*gpioe_odr) &= (~(0x1 << 10));
    (*gpiof_odr) &= (~(0x1 << 10));
    (*gpioe_odr) &= (~(0x1 << 8));
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 取消内存映射
    iounmap(gpioe_moder);
    iounmap(gpiof_moder);
    iounmap(gpioe_odr);
    iounmap(gpiof_odr);
    iounmap(rcc);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

 test.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "head.h"
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开失败\n");
        return -1;
    }
    while (1)
    {
        printf("请输入0/1/2/3> ");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
    }
    close(fd);
 
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值