[Linux驱动之路] 驱动设计的思想_面向对象_分层_分离—程序扩展

2023-05-16

韦东山老师的Linux驱动设计基础课程的p128 5_101
驱动设计的思想_面向对象_分层_分离
这课在开发板上实作练习,可控制imx6ull_pro板子的LED开关。

基于韦老师代码的基础上,更改如下代码:

led_resource.h

#ifndef _LED_RESOURCE_H
#define _LED_RESOURCE_H

/* GPIO3_0 */
/* bit[31:16] = group */
/* bit[15:0]  = which pin */
#define GROUP(x) (x>>16)
#define PIN(x)   (x&0xFFFF)
#define GROUP_PIN(g,p) ((g<<16) | (p))

#define GPIO5_IO3 1

/* GPIO3_0 */
// BIT[31:16] == group
// bit[15:0]  == pin number
struct led_resource {
	int pin;
};

struct led_resource *get_led_resource(void);


#endif

board_A_led.c

#include "led_resource.h"

static struct led_resource board_A_led = {
	.pin = GROUP_PIN(5,3), //GPIO5_IO3
};

struct led_resource *get_led_resource(void)
{
	return &board_A_led;
}

chip_demo_gpio.c

#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 struct led_resource *led_rsc;

#ifdef GPIO5_IO3
static volatile unsigned int *CCM_CCGR1 = NULL;
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = NULL;
static volatile unsigned int *GPIO5_GDIR = NULL;
static volatile unsigned int *GPIO5_DR = NULL;
#endif

/*******内部函数************/
static int _board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
	unsigned int value;
	printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
	if(which == 0)
	{
		if(!CCM_CCGR1)
		{
			CCM_CCGR1 								= ioremap(0x20C406C, 4);
			IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);
			GPIO5_GDIR								= ioremap(0x020AC000 + 0x4 , 4);
			GPIO5_DR 								= ioremap(0x020AC000, 4);
		}

		/* GPIO5_IO03 */ 
		/* a. 使能 GPIO5 
		* set CCM to enable GPIO5 
		* CCM_CCGR1[CG15] 0x20C406C 
		* bit[31:30] = 0b11 
		*/
		*CCM_CCGR1 |= (0x3 << 30); 

		/* b. 设置 GPIO5_IO03 用于 GPIO 
		* set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 
		* to configure GPIO5_IO03 as GPIO 
		* IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 0x2290014 
		* bit[3:0] = 0b0101 alt5 
		*/
		value = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
		value &= ~0xF; 
		value |= 0x5; 
		*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = value;
		

		/* c. 设置 GPIO5_IO03 作为 output 引脚 
		* set GPIO5_GDIR to configure GPIO5_IO03 as output 
		* GPIO5_GDIR 0x020AC000 + 0x4 
		* bit[3] = 0b1 
		*/ 
		*GPIO5_GDIR |= (1 << 3);
	}

	
	return 0;
}

static int _board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
	printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
	if(which == 0)
	{
		if(status) //on
		{
			/* d. 设置 GPIO5_DR 输出低电平 
			* set GPIO5_DR to configure GPIO5_IO03 output 0 
			* GPIO5_DR 0x020AC000 + 0 
			* bit[3] = 0b0 
			*/ 
			*GPIO5_DR &= ~(1 << 3); 
			
		}
		else
		{
			/* e. 设置 GPIO5_IO3 输出高电平 
			* set GPIO5_DR to configure GPIO5_IO03 output 1 
			* GPIO5_DR 0x020AC000 + 0 
			* bit[3] = 0b1 
			*/
			*GPIO5_DR |= (1 << 3); 

		}
	
	}
	return 0;
}

/************外部函数******************/

static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
	unsigned int value;
	
	if (!led_rsc)
	{
		led_rsc = get_led_resource();
	}
	
	printk("init gpio: group %d, pin %d\n", GROUP(led_rsc->pin), PIN(led_rsc->pin));
	switch(GROUP(led_rsc->pin))
	{
		case 0:
		{
			printk("init pin of group 0 ...\n");
			break;
		}
		case 1:
		{
			printk("init pin of group 1 ...\n");
			break;
		}
		case 2:
		{
			printk("init pin of group 2 ...\n");
			break;
		}
		case 3:
		{
			printk("init pin of group 3 ...\n");
			break;
		}
		case 4:
		{
			printk("init pin of group 4 ...\n");
			break;
		}
		case 5:
		{
			printk("init pin of group 5 ...\n");
			_board_demo_led_init(which);
			break;			
		}
	
	return 0;
}
}
	

static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
	printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(led_rsc->pin), PIN(led_rsc->pin));
	switch(GROUP(led_rsc->pin))
	{
		case 0:
		{
			printk("set pin of group 0 ...\n");
			_board_demo_led_ctl(PIN(led_rsc->pin), status);
			break;
		}
		case 1:
		{
			printk("set pin of group 1 ...\n");
			break;
		}
		case 2:
		{
			printk("set pin of group 2 ...\n");
			break;
		}
		case 3:
		{
			printk("set pin of group 3 ...\n");
			break;
		}
		case 4:
		{
			printk("init pin of group 4 ...\n");
			break;
		}
		case 5:
		{
			printk("init pin of group 5 ...\n");
			_board_demo_led_ctl(which, status);
			break;			
		}
	}
	return 0;
}

static struct led_operations board_demo_led_opr = {
	.num = 1,
	.init = board_demo_led_init,
	.ctl  = board_demo_led_ctl,
};

struct led_operations *get_board_led_opr(void)
{
	return &board_demo_led_opr;
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Linux驱动之路] 驱动设计的思想_面向对象_分层_分离—程序扩展 的相关文章

  • linux基础命令

    1 curl amp wget 使用curl或wget命令 xff0c 不用离开终端就可以下载文件 如你用curl xff0c 键入curl O后面跟一个文件路径 wget则不需要任何选项 下载的文件在当前目录 代码如下 curl O we
  • find_in_set

    1 in查询相当于多个or条件的叠加 xff0c 例如 xff1a select from user where user id in 1 2 3 等效于 select from user where user id 61 1 or use
  • 集成Cortex-M0内核-- Integration and Implementation Manual手册学习

    根据使用场景 xff0c 配置并集成一个Cortex M0的内核 xff0c 暂时不涉及的实现的部分 目录 阅读手册 Chapter1 Introduction 1 1 About the processor 1 2 About integ
  • 在NVIDIA NX 配置OpenCV多版本冲突和解决的总结

    Nvidia Jetson NX 环境 直接刷JetPack5 1的镜像 xff0c 会得到如下环境 Ubuntu20 04cuda11 4TensorRT8 4cudnn8 4opencv4 5 4 而且这些源一般是从nv xxxx等源下
  • 一款入门级的飞控CC3D(一)

    很多在校的朋友想自己动手做一款旋翼无人机 xff0c 但是零件采购下来要花费不少大洋 xff0c 装配完成后又需要进行软件硬件调试 所以很多想做极客的梦就扼杀在摇篮里 本期我将开始更新一款入门级的飞控CC3D 首先 xff0c 放上CC3D
  • 朱刘算法(Directed Minimum Spanning Tree/Directed MST/Minimum Arborescence/Optimum Branchings)

    概念 最小树形图 xff1a 有向图所分离出的有向生成树 亦称为最小树形图 xff0c 其应满足以下条件 xff1a 1 恰好有一个入度为0的点 xff0c 称为根结点 2 其他结点的入度均为1 3 可以从根结点到达其他结点 既然要找最小生
  • 仿真复现文章推荐

    以下学长推荐的文章 xff1a 人脸识别 xff1a SphereFace Deep Hypersphere Embedding for Face Recognition 手势姿态 xff1a OpenPose 3D人脸建模 xff1a L
  • 查看GPU显存 使用率

    watch n 0 2 nvidia smi 主要关注GPU Util Memory Usage 0 2表示每隔0 2秒刷新一次终端的显示结果 上面的表格中 xff1a 第一栏的Fan xff1a N A是风扇转速 xff0c 从0到100
  • scipy.ndimage.zoom

    最近邻 选择离它所映射到的位置最近的输入像素的灰度值为插值结果 最临近插值 3X3 的256级灰度图 xff0c 也就是高为3个象素 xff0c 宽也是3个象素的图像 xff0c 每个象素的取值可以是 0 xff0d 255 xff0c 代
  • torch.manual_seed()

    torch manual seed args seed 为CPU设置种子用于生成随机数 xff0c 以使得结果是确定的 if args cuda torch cuda manual seed args seed 为当前GPU设置随机种子 x
  • python torch.optim.SGD

    torch optim sgd学习参数 torch入门笔记14 Torch中optim的sgd Stochastic gradient descent 方法的实现细节 pytorch中使用torch optim优化神经网络以及优化器的选择
  • python zero_grad()

    有两种方式直接把模型的参数梯度设成0 xff1a model span class hljs preprocessor zero span grad optimizer span class hljs preprocessor zero s
  • torch.topk

    torch kthvalue input k dim 61 None keepdim 61 False out 61 None gt Tensor LongTensor k xff1a 第k个最小元素 返回第k个最小元素 input k d
  • torch.normal()

    torch normal means std out 61 None 返回一个张量 xff0c 包含从给定参数means std的离散正态分布中抽取随机数 均值means是一个张量 xff0c 包含每个输出元素相关的正态分布的均值 std是
  • 台式机ubuntu18.04 x86_64 简单ROS版本安装及其他库编译

    本教程是用于只安装ros melodic ros base的情况下 xff0c 为了避免安装opencv3 2 xff0c 而只保留一个opencv3 4 10 xff0c 而一步步安装rqt xff0c cv bridge xff0c r
  • ubuntu 当前文件夹 文件个数

    ls l grep 34 34 wc l
  • python [:,::-1]

    In span class hljs number 33 span t 61 np array span class hljs string 1 2 3 4 5 6 7 8 9 span In span class hljs number
  • numpy.floor

    numpy floor x out 61 None where 61 True casting 61 39 same kind 39 order 61 39 K 39 dtype 61 None subok 61 True signatur
  • Perfdog玩转内存泄漏

    背景交代 最近QC同学在跑游戏的过程中发现玩的时间久了游戏会发生闪退 xff0c 经过和开发人员讨论后又搜集了一些信息 xff0c 最后排除了功能性bug的原因 一 判断是否是内存泄露 拿到真机 xff0c USB连接 xff0c 杀掉多余
  • LCD1602知识详解(很详尽的)

    1602液晶知识详解 xff1a 1 1602液晶基础 VSS xff1a 电源地信号引脚 xff1b VDD xff1a 电源信号引脚 xff1b VEE xff1a 液晶对比度调节引脚 xff0c 接0 5V以调节液晶的显示对比度 xf

随机推荐