STM32MP157系统驱动:LED驱动开发

LED驱动开发编程实现灯的亮灭

  • hello.h
  • mycdev_led.c
  • makefile
  • main.c
  • 程序的自动化实现

hello.h

#ifndef __HEAD_H__
#define __HEAD_H__

// LED1------->PE10
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED_RCC 0X50000a28

// LED2------->PF10
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014

// LED3------->PE8
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014

#endif

mycdev_led.c

// 驱动程序
#include 
#include 
#include 
#include 
#include 
#include "head.h"

unsigned int major;

char kbuf[128] = "";

unsigned int *vir_moder_1;
unsigned int *vir_odr_1;

unsigned int *vir_rcc_1;

unsigned int *vir_moder_2;
unsigned int *vir_odr_2;

unsigned int *vir_moder_3;
unsigned int *vir_odr_3;

// 封装驱动中的操作方法
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 (size > sizeof(kbuf)) // 判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size); // 内核向用户拷贝数据
    if (ret)
    {
        printk("copy_to_user filed%d\n", __LINE__);
        return ret;
    }
    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;
    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 filed%d\n", __LINE__);
        return ret;
    }
    // 灯的亮灭驱动
    if (kbuf[0] == '1') // 开灯
    {
        (*vir_odr_1) |= (1 << 10);
        (*vir_odr_2) |= (1 << 10);
        (*vir_odr_3) |= (1 << 8);
    }
    else if (kbuf[0] == '0') // 关灯
    {
        (*vir_odr_1) &= (~(1 << 10));
        (*vir_odr_2) &= (~(1 << 10));
        (*vir_odr_3) &= (~(1 << 8));
    }

    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, "mycdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);

    /// 进行LED1的相关寄存器映射 
    vir_moder_1 = ioremap(PHY_LED1_MODER, 4);
    if (vir_moder_1 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_odr_1 = ioremap(PHY_LED1_ODR, 4);
    if (vir_odr_1 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    /// 进行LED2的相关寄存器映射 
    vir_moder_2 = ioremap(PHY_LED2_MODER, 4);
    if (vir_moder_2 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_odr_2 = ioremap(PHY_LED2_ODR, 4);
    if (vir_odr_2 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    /// 进行LED3的相关寄存器映射 
    vir_moder_3 = ioremap(PHY_LED3_MODER, 4);
    if (vir_moder_3 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_odr_3 = ioremap(PHY_LED3_ODR, 4);
    if (vir_odr_3 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }

    //
    vir_rcc_1 = ioremap(PHY_LED_RCC, 4);
    if (vir_rcc_1 == NULL)
    {
        printk("寄存器映射失败%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("寄存器映射成功\n");
    //
    (*vir_rcc_1) |= (0x1 << 4); // GPIOE,使能分开
    (*vir_rcc_1) |= (0x1 << 5); // GPIOF
    // 寄存器的初始化
    (*vir_moder_1) &= (~(3 << 20));
    (*vir_moder_1) |= (1 << 20);
    (*vir_odr_1) &= (~(1 << 10));
    printk("寄存器初始化成功\n");

    // 寄存器的初始化
    (*vir_moder_2) &= (~(3 << 20));
    (*vir_moder_2) |= (1 << 20);
    (*vir_odr_2) &= (~(1 << 10));
    printk("寄存器初始化成功\n");

    // 寄存器的初始化
    (*vir_moder_3) &= (~(3 << 16));
    (*vir_moder_3) |= (1 << 16);
    (*vir_odr_3) &= (~(1 << 8));
    printk("寄存器初始化成功\n");

    return 0;
}

// 出口函数,卸载内核模块执行此函数
static void __exit mycdev_exit(void)
{
    iounmap(vir_rcc_1);
    // 取消内存映射
    iounmap(vir_moder_1);
    iounmap(vir_odr_1);

    iounmap(vir_moder_2);
    iounmap(vir_odr_2);

    iounmap(vir_moder_3);
    iounmap(vir_odr_3);
    // 字符设备驱动的注销
    unregister_chrdev(major, "mycdev");
}

// 声明入口函数和出口函数的地址
module_init(mycdev_init);
module_exit(mycdev_exit);

// 声明此模块遵循GPL协议
MODULE_LICENSE("GPL");

makefile

arch?=arm
modname ?=mycdev_led

#指定当前内核源码路径
ifeq ($(arch),arm)
KERNELDIR?= /home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR:= /lib/modules/$(shell uname -r)/build
endif

#指定当前模块源码路径
PWD:= $(shell pwd)
KBUILD_EXTRA_SYMBOLS+=/home/ubuntu/DC22121_driver/day02/Module.symvers

all:
	make -C $(KERNELDIR) M=$(PWD) modules
#make modules作用是将内核源码中被选配为M的文件进行模块化编译
#make -C 路径  解析路径下的Makefile按照那个Makefile的规则进行编译
#M=$(PWD)确定进行模块化编译的源码文件路径

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

obj-m:=$(modname).o
#这个变量的作用是用于声明将demo.o单独进行模块化编译
    

main.c

// 应用程序
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mycdev", O_RDWR);
    if (fd < 0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入要进行的操作: 1(开灯) 0(关灯) >>>");
        fgets(buf, sizeof(buf), stdin); // 从终端获取一个字符串
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
    }

    close(fd);
    return 0;
}

程序的自动化实现

#!/bin/bash
echo "hello world"
make
arm-linux-gnueabihf-gcc demo_led.c
cp mycdev_led.ko ~/nfs/rootfs
cp a.out ~/nfs/rootfs

你可能感兴趣的:(stm32,c语言,嵌入式硬件,驱动开发)