编写led驱动,通过应用程序控制三盏灯亮灭
main.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128] = "";
int fd = open("/dev/mychrdev", O_RDWR);
if (fd < 0)
{
printf("打开失败\n");
return -1;
}
while (1)
{
printf("请输入0/1/2/3> ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
unsigned int major;
char kbuf[128] = "";
unsigned int *gpioe_moder;
unsigned int *gpiof_moder;
unsigned int *gpioe_odr;
unsigned int *gpiof_odr;
unsigned int *rcc;
ssize_t mychrdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned int ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user error\n");
return -ret;
}
return 0;
}
ssize_t mychrdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned int ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user error\n");
return -ret;
}
if (kbuf[0] == '0') // 关灯
{
(*gpioe_odr) &= (~(0x1 << 10));
(*gpiof_odr) &= (~(0x1 << 10));
(*gpioe_odr) &= (~(0x1 << 8));
}
else if (kbuf[0] == '1') // LED1开灯
{
(*gpioe_odr) |= (0x1 << 10);
(*gpiof_odr) &= (~(0x1 << 10));
(*gpioe_odr) &= (~(0x1 << 8));
}
else if (kbuf[0] == '2') // LED2开灯
{
(*gpioe_odr) &= (~(0x1 << 10));
(*gpiof_odr) |= (0x1 << 10);
(*gpioe_odr) &= (~(0x1 << 8));
}
else if (kbuf[0] == '3') // LED3开灯
{
(*gpioe_odr) &= (~(0x1 << 10));
(*gpiof_odr) &= (~(0x1 << 10));
(*gpioe_odr) |= (0x1 << 8);
}
return 0;
}
int mychrdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int mychrdev_close(struct inode *indde, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops =
{
.open = mychrdev_open,
.release = mychrdev_close,
.read = mychrdev_read,
.write = mychrdev_write,
};
static int __init mycdev_init(void)
{
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
}
printk("字符设备驱动注册成功 major=%d\n", major);
// 进行LED寄存器的地址映射
gpioe_moder = ioremap(GPIOE_MODER, 4);
if (gpioe_moder == NULL)
{
printk("地址映射失败%d\n", __LINE__);
return 0;
}
gpioe_odr = ioremap(GPIOE_ODR, 4);
if (gpioe_odr == NULL)
{
printk("地址映射失败%d\n", __LINE__);
return 0;
}
gpiof_moder = ioremap(GPIOF_MODER, 4);
if (gpiof_moder == NULL)
{
printk("地址映射失败%d\n", __LINE__);
return 0;
}
gpiof_odr = ioremap(GPIOF_ODR, 4);
if (gpiof_odr == NULL)
{
printk("地址映射失败%d\n", __LINE__);
return 0;
}
rcc = ioremap(RCC_GPIO, 4);
if (rcc == NULL)
{
printk("地址映射失败%d\n", __LINE__);
return 0;
}
printk("地址映射成功\n");
// 硬件初始化
// 使能时钟
(*rcc) |= (0X3 << 4);
// 设置PE10、PF10、PE8为输出
(*gpioe_moder) &= (~(0X3 << 20));
(*gpioe_moder) |= (0X1 << 20);
(*gpiof_moder) &= (~(0X3 << 20));
(*gpiof_moder) |= (0X1 << 20);
(*gpioe_moder) &= (~(0X3 << 16));
(*gpioe_moder) |= (0X1 << 16);
// 默认输出低电平
(*gpioe_odr) &= (~(0x1 << 10));
(*gpiof_odr) &= (~(0x1 << 10));
(*gpioe_odr) &= (~(0x1 << 8));
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消内存映射
iounmap(gpioe_moder);
iounmap(gpiof_moder);
iounmap(gpioe_odr);
iounmap(gpiof_odr);
iounmap(rcc);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
head.h
#ifndef __HEAD__H__
#define __HEAD__H__
#define RCC_GPIO 0x50000a28
#define GPIOE_MODER 0x50006000
#define GPIOE_ODR 0x50006014
#define GPIOF_MODER 0x50007000
#define GPIOF_ODR 0x50007014
#endif // ! __HEAD__H__