文章目录
- 前言
- 1、分离设计
- 驱动程序分析---程序分层
- 通用驱动程序---面向对象
- 个性化驱动程序---分离
- APP 程序分析
前言
韦东山嵌入式Linux驱动开发基础知识学习笔记
文章中大多内容来自韦东山老师的文档,还有部分个人根据自己需求补充的内容
视频教程地址:https://www.bilibili.com/video/BV14f4y1Q7ti
上一章在驱动中引入面向对象和分层的设计思想
在驱动里简单来说面向对象就是抽象一个结构体作为设备的类,然后将结构体成员作为对象
而分层设计思想简单来说就是将 内核相关的操作和硬件强相关操作分离 达到内核相关操作通用,硬件相关操作个性化目的
本节将介绍分离思想
【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED驱动框架–面向对象、分层设计思想
1、分离设计
▲面向对象、分层、分离思想体现
将使能、操作GPIO的流程抽象成统一的接口,LED的init可以去调用这些接口,其中添加struct led_resource
结构体,其中保存了LED需要的资源即GPIO的group & pin
,实现初始化。一系列操作相当于封装了GPIO的寄存器相关操作供设备使用
驱动程序分析—程序分层
通用驱动程序—面向对象
led_opr.h
:包含led设备结构体类
#ifndef _LED_OPR_H
#define _LED_OPR_H
struct led_operations {
int (*init) (int which);
int (*ctl) (int which, char status);
int (*exit) (void);
};
struct led_operations *get_board_led_opr(void);
#endif
leddrv.c
:给APP提供接口
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include "led_opr.h"
#define LED_NUM 2
static int major = 0;
static struct class *led_class;
struct led_operations *p_led_opr;
#define MIN(a, b) (a < b ? a : b)
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
char status;
struct inode *inode = file_inode(file);
int minor = iminor(inode);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(&status, buf, 1);
p_led_opr->ctl(minor, status);
return 1;
}
static int led_drv_open (struct inode *node, struct file *file)
{
int minor = iminor(node);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
p_led_opr->init(minor);
return 0;
}
static int led_drv_close (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
static int __init led_init(void)
{
int err;
int i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "100ask_led", &led_drv);
led_class = class_create(THIS_MODULE, "100ask_led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "led");
return -1;
}
for (i = 0; i < LED_NUM; i++)
device_create(led_class, NULL, MKDEV(major, i), NULL, "100ask_led%d", i);
p_led_opr = get_board_led_opr();
return 0;
}
static void __exit led_exit(void)
{
int i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
p_led_opr->exit();
for (i = 0; i < LED_NUM; i++)
device_destroy(led_class, MKDEV(major, i));
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
unregister_chrdev(major, "100ask_led");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
个性化驱动程序—分离
board_A_led
:包含LED所需要的资源信息
#include "led_resource.h"
static struct led_resource led_resources = {
.num = 2,
.pin[0] = GROUP_PIN(0,10),
.pin[1] = GROUP_PIN(6,8),
};
struct led_resource *get_led_resouce(void)
{
return &led_resources;
}
chip_demo_gpio.c
:包含GPIO相关操作、使能函数,LED设备对象从这里获得接口
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <asm/io.h>
#include "led_opr.h"
#include "led_resource.h"
static volatile unsigned int *RCC_PLL4CR;
static volatile unsigned int *RCC_MP_AHB4ENSETR;
static volatile unsigned int *GPIOA_MODER;
static volatile unsigned int *GPIOA_BSRR;
static volatile unsigned int *GPIOG_MODER;
static volatile unsigned int *GPIOG_BSRR;
struct led_resource *led_rsc;
static int board_demo_led_init (int which)
{
if (!led_rsc)
{
led_rsc = get_led_resouce();
}
if (!RCC_PLL4CR)
{
RCC_PLL4CR = ioremap(0x50000000 + 0x894, 4);
RCC_MP_AHB4ENSETR = ioremap(0x50000000 + 0xA28, 4);
GPIOA_MODER = ioremap(0x50002000 + 0x00, 4);
GPIOA_BSRR = ioremap(0x50002000 + 0x18, 4);
GPIOG_MODER = ioremap(0x50008000 + 0x00, 4);
GPIOG_BSRR = ioremap(0x50008000 + 0x18, 4);
}
switch(GROUP(led_rsc->pin[which]))
{
case 0:
{
printk("init pin of group A ...\n");
*RCC_PLL4CR |= (1<<0);
while ((*RCC_PLL4CR & (1<<1)) == 0);
*RCC_MP_AHB4ENSETR |= (1<<GROUP(led_rsc->pin[which]));
*GPIOA_MODER &= ~(3 << (PIN(led_rsc->pin[which])*2) );
*GPIOA_MODER |= (1<< (PIN(led_rsc->pin[which])*2) );
break;
}
case 6:
{
printk("init pin of group G ...\n");
*RCC_PLL4CR |= (1<<0);
while ((*RCC_PLL4CR & (1<<1)) == 0);
*RCC_MP_AHB4ENSETR |= (1<<GROUP(led_rsc->pin[which]));
*GPIOG_MODER &= ~((PIN(led_rsc->pin[which])*2));
*GPIOG_MODER |= ((PIN(led_rsc->pin[which])*2));
break;
}
default:
printk("not support %d\n", led_rsc->pin[which]);
}
return 0;
}
static int board_demo_led_ctl (int which, char status)
{
printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(led_rsc->pin[which]), PIN(led_rsc->pin[which]));
switch(GROUP(led_rsc->pin[which]))
{
case 0:
{
printk("set pin of group A ...\n");
*GPIOA_BSRR = ( 1<< (16*status + PIN(led_rsc->pin[which])));
break;
}
case 6:
{
printk("set pin of group G ...\n");
*GPIOG_BSRR = ( 1<< (16*status + PIN(led_rsc->pin[which])));
break;
}
default:
printk("not support %d\n", led_rsc->pin[which]);
}
return 0;
}
static int board_demo_led_exit (void)
{
if(RCC_PLL4CR)
{
iounmap(RCC_PLL4CR);
iounmap(RCC_MP_AHB4ENSETR);
iounmap(GPIOA_MODER);
iounmap(GPIOA_BSRR);
iounmap(GPIOG_MODER);
iounmap(GPIOG_BSRR);
}
return 0;
}
static struct led_operations board_demo_led_opr = {
.init = board_demo_led_init,
.ctl = board_demo_led_ctl,
.exit = board_demo_led_exit,
};
struct led_operations *get_board_led_opr(void)
{
return &board_demo_led_opr;
}
APP 程序分析
ledtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int fd;
char status;
if (argc != 3)
{
printf("Usage: %s <dev> <on | off>\n", argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
if (0 == strcmp(argv[2], "on"))
{
status = 1;
write(fd, &status, 1);
}
else
{
status = 0;
write(fd, &status, 1);
}
close(fd);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)