驱动开发---点灯

head.h

#ifndef __HEAD_H__
#define __HEAD_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 RCC 0x50000A28
#define GPIOA 0x50002000
#define GPIOB 0x50003000
#define GPIOC 0x50004000
#define GPIOD 0x50005000
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define GPIOG 0x50008000
#define GPIOH 0x50009000
#define GPIOI 0x5000A000
#define GPIOJ 0x5000B000
#define GPIOK 0x5000C000
#define GPIOZ 0x54004000
#endif

驱动代码:

#include "head.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>
static int __init mycdev_init(void);
int major; // 用于保存主设备号
char kbuf[128] = { 0 };
gpio_t* led1;
gpio_t* led2;
gpio_t* led3;
unsigned int* 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* ubuf, size_t size, loff_t* lof)
{
    int ret;
    if (sizeof(kbuf) < size) {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret) {
        printk("copy_to_user filed\n");
        return -EIO;
    }

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_write(struct file* file, const char* ubuf, size_t size, loff_t* lof)
{
    int ret;
    // 判断用户空间的需求是否能被驱动满足,满足不了就给能给最大的
    if (sizeof(kbuf) < size) {
        size = sizeof(kbuf);
    }
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret) {
        printk("copy_from_user file\n");
        return -EIO;
    }
    // 开灯
    switch (kbuf[0]) {
    case '1':

        led1->ODR |= (0x1 << 10);
        led2->ODR &= (~(0x1 << 10));
        led3->ODR &= (~(0x1 << 8));
        break;
    case '2':
        led1->ODR &= (~(0x1 << 10));
        led2->ODR |= (0x1 << 10);
        led3->ODR &= (~(0x1 << 8));
        break;
    case '3':
        led1->ODR &= (~(0x1 << 10));
        led2->ODR &= (~(0x1 << 10));
        led3->ODR |= (0x1 << 8);
        break;
    case '4':
        led1->ODR |= (0x1 << 10);
        led2->ODR |= (0x1 << 10);
        led3->ODR |= (0x1 << 8);
        break;
    default:
        break;
    }

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
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, "mychrdev", &fops);
    if (major < 0) {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    // 映射结构体物理寄存器
    led1 = ioremap(GPIOE, sizeof(gpio_t));
    if (led1 == NULL) {
        printk("led1结构体映射表失败\n");
        return -EFAULT;
    }
    led2 = ioremap(GPIOF, sizeof(gpio_t));
    if (led2 == NULL) {
        printk("led1结构体映射表失败\n");
        return -EFAULT;
    }
    led3 = ioremap(GPIOE, sizeof(gpio_t));
    if (led3 == NULL) {
        printk("led1结构体映射表失败\n");
        return -EFAULT;
    }

    rcc = ioremap(RCC, 4);
    if (rcc == NULL) {
        printk("rcc寄存器地址映射表失败\n");
        return -EFAULT;
    }

    // led1寄存器初始化
    (*rcc) |= (0x1 << 4);
    led1->MODER &= (~(0x3 << 20));
    led1->MODER |= (0x1 << 20);
    led1->ODR &= (~(0x1 << 10));
    // led2初始化
    led2->MODER &= (~(0x3 << 20));
    led2->MODER |= (0x1 << 20);
    led2->ODR &= (~(0x1 << 10));
    // led3初始化
    led3->MODER &= (~(0x3 << 16));
    led3->MODER |= (0x1 << 16);
    led3->ODR &= (~(0x1 << 8));
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 取消结构体映射
    iounmap(led1);
    iounmap(led2);
    iounmap(led3);
    iounmap(rcc);
    // 字符设备驱动的注销
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

.c代码

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const* argv[])
{
    char buf[128] = { 0 };
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0) {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1) {
        printf("请输入控制命令---1:led1亮  2:led2亮  3:led3亮  4:全部点亮");
        fgets(buf, sizeof(buf), stdin); // 在终端读取
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
        }
    close(fd);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值