思维导图

练习
1.编写LED灯的驱动,可以控制三个灯,应用程序中编写控制灯的逻辑,要使用自动创建设备节点机制
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct
{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ORD;
}gpio_t;
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define RCC 0x50000A28
#endif
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"
#include <linux/device.h>
unsigned int major; // 定义一个变量保存主设备号
char kbuf[128] = {0};
unsigned int *rcc;
gpio_t *led1;
gpio_t *led2;
gpio_t *led3;
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 failed\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')//开灯
led1->ORD |= (0x1 << 10);
else if (kbuf[1] == '0')//关灯
led1->ORD &= (~(0x1 << 10));
break;
case '2':
if (kbuf[1] == '1')
led2->ORD |= (0x1 << 10);
else if (kbuf[1] == '0')
led2->ORD &= (~(0x1 << 10));
break;
case '3':
if (kbuf[1] == '1')
led3->ORD |= (0x1 << 8);
else if (kbuf[1] == '0')
led3->ORD &= (~(0x1 << 8));
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,
.release = mycdev_close,
.read = mycdev_read,
.write = mycdev_write,
};
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, "mychrdev");
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, "mychrdev%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(cls);
}
}
printk("向上提交设备节点成功\n");
// 完成硬件寄存器物理内存的映射
led1 = ioremap(GPIOE, sizeof(gpio_t));
if (led1 == NULL)
{
printk("led1物理内存映射失败\n");
return -EFAULT;
}
led2 = ioremap(GPIOF, sizeof(gpio_t));
if (led2 == NULL)
{
printk("led2物理内存映射失败\n");
return -EFAULT;
}
led3 = ioremap(GPIOE, sizeof(gpio_t));
if (led3 == NULL)
{
printk("led3物理内存映射失败\n");
return -EFAULT;
}
rcc = ioremap(RCC, 4);
if (rcc == NULL)
{
printk("rcc物理内存映射失败\n");
return -EFAULT;
}
printk("物理内存映射成功\n");
//rcc使能
(*rcc) |= (0x3 << 4);
//硬件寄存器初始化
led1->MODER &= (~(0x3 << 20));
led1->MODER |= (0x1 << 20);
led2->MODER &= (~(0x3 << 20));
led2->MODER |= (0x1 << 20);
led3->MODER &= (~(0x3 << 16));
led3->MODER |= (0x1 << 16);
//默认关灯
led1->ORD &= (~(0x1 << 10));
led2->ORD &= (~(0x1 << 10));
led3->ORD &= (~(0x1 << 8));
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
// 取消物理内存的映射
iounmap(rcc);
iounmap(led1);
iounmap(led2);
iounmap(led3);
//销毁设备信息
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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *agrv[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请输入LED控制方式:(11(LED1开))> ");
fgets(buf, sizeof(buf), stdin); // 从终端输入数据到buf
buf[strlen(buf) - 1] = '\0'; // 将buf末尾的'\n'切换为'\0'
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
博客围绕ARM相关练习展开,包含思维导图。练习内容为编写LED灯驱动,可控制三个灯,在应用程序中编写控制灯的逻辑,且使用自动创建设备节点机制,还给出了相关代码文件如head.h、mychrdev.c、test.c。
2346

被折叠的 条评论
为什么被折叠?



