实现开发板三盏灯点亮熄灭

实现开发板三盏灯点亮熄灭

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  GPIOE  (0x50006000)
#define  GPIOF   (0x50007000)
  • 三盏灯逻辑 kbuf[0]:表示操作哪一盏灯 kbuf[1]:表示操作灯的状态
  • kbuf[0] = 1==> 操作LED1 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭
  • kbuf[0] = 2 ==> 操作LED2 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭
  • kbuf[0] = 3 ==> 操作LED3 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭

 

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uacce.h>
#include <linux/io.h>
#include "myled.h"

#define CNAME "myled"
unsigned int major = 0;
char kbuf[100] = "";

unsigned int *rcc_virt = NULL;
gpio_t *gpioe_virt = NULL;
gpio_t *gpiof_virt = NULL;

#define LED1_ON (gpioe_virt->ODR |= (0x1 << 10))
#define LED1_OFF (gpioe_virt->ODR &= (~(0x1 << 10)))
#define LED2_ON (gpiof_virt->ODR |= (0x1 << 10))
#define LED2_OFF (gpiof_virt->ODR &= (~(0x1 << 10)))
#define LED3_ON (gpioe_virt->ODR |= (0x1 << 8))
#define LED3_OFF (gpioe_virt->ODR &= (~(0x1 << 8)))

gpio_t gpio;

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
{
    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 from user is error\n");
        return -EIO;
    }
    switch (kbuf[0])
    {
    case 1:
        if (kbuf[1] == 1)
        {
            LED1_ON;
        }
        else
        {
            LED1_OFF;
        }
        break;
    case 2:
        if (kbuf[1] == 1)
        {
            LED2_ON;
        }
        else
        {
            LED2_OFF;
        }
        break;
    case 3:
        if (kbuf[1] == 1)
        {
            LED3_ON;
        }
        else
        {
            LED3_OFF;
        }
        break;
    default:
        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,
    .write = myled_write,
    .release = myled_close,
};

static int __init demo_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, CNAME, &fops);
    if (major < 0)
    {
        printk("register chrdev is error\n");
        return -EIO;
    }
    printk("major = %d\n", major);

    rcc_virt = ioremap(RCC_MP_AHB4ENSETR, 4);
    if (rcc_virt == NULL)
    {
        printk("ioremap rcc is error\n");
        return -EIO;
    }
    gpioe_virt = ioremap(GPIOE, sizeof(gpio));
    if (gpioe_virt == NULL)
    {
        printk("ioremap gpioe moder is error\n");
        return -EIO;
    }
    gpiof_virt = ioremap(GPIOF, sizeof(gpio));
    if (gpiof_virt == NULL)
    {
        printk("ioremap gpiof is error\n");
        return -EIO;
    }

    *rcc_virt |= (0x3 << 4);

    gpioe_virt->MODER &= (~(0x3 << 20));
    gpioe_virt->MODER |= (0x1 << 20);
    gpioe_virt->ODR |= (0x1 << 10);

    gpiof_virt->MODER &= (~(0x3 << 20));
    gpiof_virt->MODER |= (0x1 << 20);
    gpiof_virt->ODR |= (0x1 << 10);

    gpioe_virt->MODER &= (~(0x3 << 16));
    gpioe_virt->MODER |= (0x1 << 16);
    gpioe_virt->ODR |= (0x1 << 8);
    return 0;
}
static void __exit demo_exit(void)
{
    iounmap(gpiof_virt);
    iounmap(gpioe_virt);
    iounmap(rcc_virt);
    // 注销字符设备驱动
    unregister_chrdev(major, CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
#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 RCC_MP_AHB4ENSETR 0x50000A28
// #define GPIOE_MODER 0x50006000
// #define GPIOE_ODR 0x50006014

#define  GPIOE   (0x50006000)
#define  GPIOF   (0x50007000)

#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char buf[128] = "";
    int fd = -1;
    fd = open("/dev/myled", O_RDWR);
    if (fd == -1)
    {
        perror("open is error");
        exit(1);
    }
    // buf[0]=1 led1 buf[0]=2 led2 buf[0]=3 led3
    //  buf[1]=0灭  buf[1]=1亮
    while (1)
    {
        buf[0] = 1;
        buf[1] = 1;
        write(fd, buf, sizeof(buf));
        sleep(1);
        buf[1] = 0;
        write(fd, buf, sizeof(buf));
        sleep(1);

        buf[0] = 2;
        buf[1] = 1;
        write(fd, buf, sizeof(buf));
        sleep(1);
        buf[1] = 0;
        write(fd, buf, sizeof(buf));
        sleep(1);

        buf[0] = 3;
        buf[1] = 1;
        write(fd, buf, sizeof(buf));
        sleep(1);
        buf[1] = 0;
        write(fd, buf, sizeof(buf));
        sleep(1);
    }
    close(fd);
    return 0;
}

相关推荐

  1. DAY6 作业 串口控制

    2024-06-14 00:48:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 00:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 00:48:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 00:48:02       82 阅读
  4. Python语言-面向对象

    2024-06-14 00:48:02       91 阅读

热门阅读

  1. MySQL CHECK约束

    2024-06-14 00:48:02       22 阅读
  2. Android基础-运行时权限

    2024-06-14 00:48:02       30 阅读
  3. 013-Linux交换分区管理

    2024-06-14 00:48:02       30 阅读
  4. ios CCDelete.m

    2024-06-14 00:48:02       29 阅读
  5. 项目经验:别啥事都跟甲方讲

    2024-06-14 00:48:02       28 阅读
  6. 【设计模式之享元模式 -- C++】

    2024-06-14 00:48:02       29 阅读
  7. 文件已经删除但磁盘空间未释放

    2024-06-14 00:48:02       30 阅读
  8. TikTok限流封号要如何处理

    2024-06-14 00:48:02       30 阅读
  9. 关于自学编程的9点忠告

    2024-06-14 00:48:02       27 阅读
  10. vue中v-bind控制class和style

    2024-06-14 00:48:02       84 阅读
  11. 使用Python多线程批量压缩图片文件

    2024-06-14 00:48:02       30 阅读