2022-10-10 LED灯驱动开发

首先是myled.c的代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define CNAME "myled"

//LED1引脚 
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0x50000A28

//LED2引脚
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define PHY_LED2_RCC 0x50000A28

//LED3引脚
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR 0X50006014
#define PHY_LED3_RCC 0x50000A28
 
int major;
char kbuf[128]={};  //定义数组用于存放和用户之间拷贝的数据
unsigned int *vir_moder1;
unsigned int *vir_moder2;
unsigned int *vir_odr1;
unsigned int *vir_odr2;
unsigned int *vir_rcc;
 
ssize_t mycdev_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    //size是用户期待读到的字节长度
    int ret;
    if(size>sizeof(kbuf))
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
 
    return size;
}
ssize_t mycdev_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size > sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("数据从用户向内核拷贝失败\n");
        return -EIO;
    }
 
    if(kbuf[0] == '1')
    {
        (*vir_odr1) |= (1<<10);
    }else if(kbuf[0]== '2')
    {
        (*vir_odr2) |= (1<<10);
    }
    else if(kbuf[0] == '3')
    {
        (*vir_odr1) |= (1<<8);
    }
    else
    {
        (*vir_odr1) &= ~(1<<10);
        (*vir_odr2) &= ~(1<<10);
        (*vir_odr1) &= ~(1<<8);
    }
    return size;
}
int mycdev_open (struct inode *inode, struct file *file)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     return 0;
}
int mycdev_release (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_release
};
static int __init demo_init(void)
{
 
    major=register_chrdev(0,CNAME,&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n",major);
    
    //moder 地址映射
    //GPIOE
    vir_moder1=ioremap(PHY_LED1_MODER,4);
    if(vir_moder1 ==NULL)
    {
        printk("vir_moder1 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_moder1 映射成功\n");
    //GPIOF
    vir_moder2=ioremap(PHY_LED2_MODER,4);
    if(vir_moder2 ==NULL)
    {
        printk("vir_moder2 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_moder2 映射成功\n");
 
    //odr 地址映射
    //GPIOE
    vir_odr1=ioremap(PHY_LED1_ODR,4);
    if(vir_odr1 ==NULL)
    {
        printk("vir_odr1 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_odr1 映射成功\n");
    //GPIOF
    vir_odr2=ioremap(PHY_LED2_ODR,4);
    if(vir_odr2 ==NULL)
    {
        printk("vir_odr2 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_odr2 映射成功\n");
 
    //rcc 地址映射
    vir_rcc=ioremap(PHY_LED1_RCC,4);
    if(vir_rcc ==NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");
    
    //LED1 init
    (*vir_moder1) &= (~(3<<20));
    (*vir_moder1) |= (1<<20);
    (*vir_odr1) &= (~(1<<10));
    (*vir_rcc) |= (1<<4);
 
    //LED2 init
    (*vir_moder2) &= (~(3<<20));
    (*vir_moder2) |= (1<<20);
    (*vir_odr2) &= (~(1<<10));
    (*vir_rcc) |= (1<<5);
 
    //LED3 init
    (*vir_moder1) &= (~(3<<16));
    (*vir_moder1) |= (1<<16);
    (*vir_odr1) &= (~(1<<8));
    
    
    return 0;
}
static void __exit demo_exit(void)
{
    unregister_chrdev(major,CNAME);
 
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
 
}
//module_init和module_exit是内核提供的宏
//告诉内核驱动的出口函数和出口函数的地址
module_init(demo_init);
module_exit(demo_exit);
//声明遵循GPL开源协议
MODULE_LICENSE("GPL");

然后是test.c的代码

#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=open("/dev/myled",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败");
        exit(-1);
    }
    printf("设备文件打开成功\n");

    //在终端输入
    printf("请输入控制命令 1:led1亮,2:led2亮,3:led3亮-- 0关灯\n");
    fgets(buf,sizeof(buf),stdin);
    buf[strlen(buf)-1]='\0';
    write(fd,buf,sizeof(buf));

    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("读取到的数据为:%s\n",buf);

    close(fd);
    return 0;
}

Makefile的代码

 modname ?= demo
 arch ?= arm
 
 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)
 
 all:
     make -C $(KERNELDIR) M=$(PWD) modules
 
 clean:
     make -C $(KERNELDIR) M=$(PWD) clean
                                                                                    
 obj-m:=$(modname).o

首先在ubuntu中对myled.c进行编译:make arch=arm modname=myled

然后对test.c进行编译 arm-linux-gnueabihf-gcc test.c   ,

然后将编译好的myled.ko 文件和a.out可执行文件 拷贝到挂载目录下 ~/nfs/rootfs/

串口部分就是

zui

最后的结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值