实现LED点亮
demo.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int major;
char kbuf[128] = {0};
// 定义指针接收映射成功的虚拟内存首地址
unsigned int *vir_moder1;
unsigned int *vir_odr1;
unsigned int *vir_rcc1;
unsigned int *vir_moder2;
unsigned int *vir_odr2;
unsigned int *vir_rcc2;
unsigned int *vir_moder3;
unsigned int *vir_odr3;
unsigned int *vir_rcc3;
struct class *cls;
struct device *dev;
// 封装操作方法
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)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
int ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user filad\n");
return ret;
}
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)
{
printk("copy_from_user failed\n");
return ret;
}
switch (kbuf[0])
{
case '1':
if (kbuf[1] == '1') // 开灯
{
// 开灯逻辑
printk("开灯\n");
(*vir_odr1) |= (0X1 << 10);
}
else if (kbuf[1] == '0') // 关灯
{
// 关灯逻辑
printk("关灯\n");
(*vir_odr1) &= (~(0X1 << 10));
}
break;
case '2':
if (kbuf[1] == '1') // 开灯
{
// 开灯逻辑
printk("开灯\n");
(*vir_odr2) |= (0X1 << 10);
}
else if (kbuf[1] == '0') // 关灯
{
// 关灯逻辑
printk("关灯\n");
(*vir_odr2) &= (~(0X1 << 10));
}
break;
case '3':
if (kbuf[1] == '1') // 开灯
{
// 开灯逻辑
printk("开灯\n");
(*vir_odr1) |= (0X1 << 8);
}
else if (kbuf[1] == '0') // 关灯
{
// 关灯逻辑
printk("关灯\n");
(*vir_odr1) &= (~(0X1 << 8));
}
break;
default:
break;
}
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)
{
int i;
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录信息
cls = class_create(THIS_MODULE, "Mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录信息失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功\n");
// 向上提交设备信息
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(cls);
}
}
printk("向上提交设备节点信息成功\n");
// 完成硬件寄存器物理内存的映射
vir_moder1 = ioremap(PHY_LED1_MODER, 4);
if (vir_moder1 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_odr1 = ioremap(PHY_LED1_ODR, 4);
if (vir_odr1 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_rcc1 = ioremap(PHY_LED1_RCC, 4);
if (vir_rcc1 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
//LED2
vir_moder2 = ioremap(PHY_LED2_MODER, 4);
if (vir_moder2 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_odr2 = ioremap(PHY_LED2_ODR, 4);
if (vir_odr2 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_rcc2 = ioremap(PHY_LED2_RCC, 4);
if (vir_rcc2 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
//LED3
/* vir_moder3 = ioremap(PHY_LED3_MODER, 4);
if (vir_moder3 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_odr3 = ioremap(PHY_LED3_ODR, 4);
if (vir_odr3 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_rcc3 = ioremap(PHY_LED3_RCC, 4);
if (vir_rcc3 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}*/
printk("物理内存映射成功\n");
// 硬件寄存器的初始化
//LED1初始化
(*vir_moder1) &= (~(0X3 << 20)); // 设置为输出
(*vir_moder1) |= (0X1 << 20);
// rcc使能
(*vir_rcc1) |= (0X1 << 4);
// 默认关灯
(*vir_odr1) &= (~(0X1 << 10));
//LED2初始化
(*vir_moder2) &= (~(0X3 << 20)); // 设置为输出
(*vir_moder2) |= (0X1 << 20);
// rcc使能
(*vir_rcc2) |= (0X1 << 5);
// 默认关灯
(*vir_odr2) &= (~(0X1 << 10));
//LED3初始化
(*vir_moder1) &= (~(0X3 << 16)); // 设置为输出
(*vir_moder1) |= (0X1 << 16);
// rcc使能
(*vir_rcc1) |= (0X1 << 4);
// 默认关灯
(*vir_odr1) &= (~(0X1 << 16));
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消物理内存的映射
iounmap(vir_moder1);
iounmap(vir_odr1);
iounmap(vir_rcc1);
iounmap(vir_moder2);
iounmap(vir_odr2);
iounmap(vir_rcc2);
/*iounmap(vir_moder3);
iounmap(vir_odr3);
iounmap(vir_rcc3);*/
//销毁设备信息
int i;
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
// 字符设备驱动的注销
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char buf[128] = {};
int fd = open("/dev/mycdev0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("成功打开设备文件\n");
while(1)
{
printf("请选择灯控制方式:1/2/3(几号灯)0/1(开关灯)>");
fgets(buf,sizeof(buf),stdin);//从终端读一个字符串存放在buf
buf[strlen(buf)-1]='\0';
write(fd, buf, sizeof(buf));//将从终端得到的字符串传递到驱动
}
close(fd);
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define PHY_LED2_RCC 0X50000A28
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR 0X50006014
#define PHY_LED3_RCC 0X50000A28
#endif
运行结果: