实现三盏灯点亮
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define LED1_MODER 0X50006000
#define LED1_ODR 0x50006014
#define LED1_RCC 0x50000A28
#define LED2_MODER 0X50007000
#define LED2_ODR 0x50007014
#define LED2_RCC 0x50000A28
#define LED3_MODER 0X50006000
#define LED3_ODR 0x50006014
#define LED3_RCC 0x50000A28
#endif
mychrdev.c
#include
#include
#include
#include
#include
#include "head.h"
unsigned int major;
char kbuf[128] = {0};
unsigned int *vir1_moder;
unsigned int *vir1_odr;
unsigned int *vir1_rcc;
unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir2_rcc;
unsigned int *vir3_moder;
unsigned int *vir3_odr;
unsigned int *vir3_rcc;
// 封装操作方法
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;
ret = copy_to_user(ubuf, kbuf, size);
if (ret < 0)
{
printk("copy to user err\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret < 0)
{
printk("copy to user err\n");
return -EIO;
}
if (kbuf[0] == '1')
{
// 点亮led
*vir1_odr |= (0x1 << 10);
*vir2_odr |= (0x1 << 10);
*vir3_odr |= (0x1 << 8);
}
else
{
// 熄灭led
*vir1_odr &= (~(0x1 << 10));
*vir2_odr &= (~(0x1 << 10));
*vir3_odr &= (~(0x1 << 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,
};
int add(int a, int b)
{
return a + b;
}
EXPORT_SYMBOL_GPL(add);
// 入口函数 安装内核模块时执行
static int __init mycdev_init(void)
{
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备注册失败!\n");
return 0;
}
printk("字符设备注册成功!%d\n", major);
// 地址映射
vir1_moder = ioremap(LED1_MODER, 4);
if (vir1_moder == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir2_moder = ioremap(LED2_MODER, 4);
if (vir2_moder == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir3_moder = ioremap(LED3_MODER, 4);
if (vir3_moder == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir1_odr = ioremap(LED1_ODR, 4);
if (vir1_odr == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir2_odr = ioremap(LED2_ODR, 4);
if (vir2_odr == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir3_odr = ioremap(LED3_ODR, 4);
if (vir3_odr == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir1_rcc = ioremap(LED1_RCC, 4);
if (vir1_rcc == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir2_rcc = ioremap(LED2_RCC, 4);
if (vir2_rcc == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
vir3_rcc = ioremap(LED3_RCC, 4);
if (vir3_rcc == NULL)
{
printk("物理地址映射到虚拟地址失败!%d\n", __LINE__);
return -EFAULT;
}
// rcc初始化
*vir1_rcc |= (0x1 << 4);
*vir2_rcc |= (0x1 << 5);
*vir3_rcc |= (0x1 << 4);
// gpio初始化
*vir1_moder &= (~(0x3 << 20));
*vir1_moder |= (0x1 << 20);
*vir2_moder &= (~(0x3 << 20));
*vir2_moder |= (0x1 << 20);
*vir3_moder &= (~(0x3 << 16));
*vir3_moder |= (0x1 << 16);
// 默认关灯
*vir1_odr &= (~(0x1 << 10));
*vir2_odr &= (~(0x1 << 10));
*vir3_odr &= (~(0x1 << 8));
return 0;
}
// 出口函数 卸载内核模块执行
static void __exit mycdev_exit(void)
{
// 取消映射
iounmap(vir1_rcc);
iounmap(vir1_moder);
iounmap(vir1_odr);
iounmap(vir2_rcc);
iounmap(vir2_moder);
iounmap(vir2_odr);
iounmap(vir3_rcc);
iounmap(vir3_moder);
iounmap(vir3_odr);
// 字符设备注销
unregister_chrdev(major, "mychrdev");
return;
}
// 用于声明当前内核模块入口函数的地址
module_init(mycdev_init);
// 用于声明当前内核模块出口函数的地址
module_exit(mycdev_exit);
// 声明当前内核模块遵循GPL协议
MODULE_LICENSE("GPL");
test.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while (1)
{
printf("请选择灯的状态(1)点亮 (0)熄灭\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}