LED驱动开发编程实现灯的亮灭
- hello.h
- mycdev_led.c
- makefile
- main.c
- 程序的自动化实现
hello.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED_RCC 0X50000a28
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#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);
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;
}
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;
}
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);
(*vir_rcc_1) |= (0x1 << 5);
(*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);
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;
}
程序的自动化实现
echo "hello world"
make
arm-linux-gnueabihf-gcc demo_led.c
cp mycdev_led.ko ~/nfs/rootfs
cp a.out ~/nfs/rootfs