pwm子系统

news2024/9/22 17:20:03

一、系统框架

  内核的PWM core,向下对实际pwm控制器驱动,提供了pwm_chip,soc厂商编程控制器驱动,只需注册结构体,配置好private_data,实例化pwm_ops操作,编写具体函数即可。 向上为其他驱动调用提供了统一的接口,通过pwm_device,关联pwm_chip,其他驱动或者用户程序通过接口来操作pwm_device结构体。
  pwm_chip层实际上也是核心层的一部分,其主要对应一个pwm控制器,一个pwm控制器可包含多个pwm_device,针对一个pwm_chip,提供了访问pwm 控制器的方法,通过pwm_chip提供的方法,实现对pwm 控制器的配置
  在硬件层,pwm控制器驱动soc厂商已经写好,我们要做的是在设备树(或者是设备树插件)中开启控制器节点, 描述pwm设备节点,然后驱动中调用内核PWM提供的接口,来实现pwm驱动控制。
  

驱动程序分为两部分:

device部分:目前都是设备数陪住

driver部分:创建设备节点,并且提供open,close,read,write等接口供上层应用调用

二、核心层:

PWM framework非常简单,但它同样具备framework的基本特性:对上,为内核其它driver(Consumer)提供使用PWM功能的统一接口;对下,为PWM driver(Provider)提供driver开发的通用方法和API;内部,抽象并实现公共逻辑,屏蔽技术细节。下面我们通过它所提供的API,进一步认识PWM framework。

api声明见linux\include\linux\pwm.h

PWM linux framework源代码位于内核的 drivers/pwm 目录下,具体的路径如下所示:

drivers/pwm/
├── core.c--pwm子系统核心。
├── pwm-imx.c--imx的pwm_chip驱动。
└── sysfs.c--pwm子系统的pwm_class注册,pwm_chip属性,pwm_device属性等定义。

2.1、向PWM consumer提供的APIs:

对consumer而言,关注PWM的如下参数:频率,占空比,极性,开关(使能)

/* include/linux/pwm.h */
/**
 * pwm_config() - change a PWM device configuration
 * @pwm: PWM device
 * @duty_ns: "on" time (in nanoseconds)
 * @period_ns: duration (in nanoseconds) of one cycle
 *
 * Returns: 0 on success or a negative error code on failure.
 */
static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
			     int period_ns);
 
/**
 * pwm_enable() - start a PWM output toggling
 * @pwm: PWM device
 *
 * Returns: 0 on success or a negative error code on failure.
 */
static inline int pwm_enable(struct pwm_device *pwm);
 
/**
 * pwm_disable() - stop a PWM output toggling
 * @pwm: PWM device
 */
static inline void pwm_disable(struct pwm_device *pwm);
 
/**
 * pwm_set_polarity() - configure the polarity of a PWM signal
 * @pwm: PWM device
 * @polarity: new polarity of the PWM signal
 *
 * Note that the polarity cannot be configured while the PWM device is
 * enabled.
 *
 * Returns: 0 on success or a negative error code on failure.
 */
static inline int pwm_set_polarity(struct pwm_device *pwm,
				   enum pwm_polarity polarity);

上面的API都以struct pwm_device类型的指针为操作句柄,该指针抽象了一个PWM设备(consumer不需要关心其内部构成),那么怎么获得PWM句柄呢?使用如下的API:

/* include/linux/pwm.h */
 
struct pwm_device *pwm_get(struct device *dev, const char *con_id);
struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
void pwm_put(struct pwm_device *pwm);
 
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
                                   const char *con_id);//常用!!
void devm_pwm_put(struct device *dev, struct pwm_device *pwm);

 上述api中,常用struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, const char *con_id);

  • 参数:
    • pwm:PWM设备的指针
    • np: 要从中检索 PWM 设备的设备树节点。
    • con_id: PWM 消费者的字符串标识符。如果不需要,可以为 NULL。
  • 返回值:
    • 成功:指向表示 PWM 设备的 pwm_device 结构体的指针
    • 失败:错误指针(ERR_PTR)

2.2、向PWM provider提供的APIs

使用说明

初始化pwm chip:填充ops操作函数实例,就是厂家驱动代码

通过pwmchip_add接口注册到kernel中,之后的事情,pwm driver就不用操心了

int pwmchip_add(struct pwm_chip *chip);
int pwmchip_remove(struct pwm_chip *chip);

关键结构体的层级关系如下:

pwm_device仅仅表示一个pwm通道,pwm chip表示一个pwm控制器!!

/**
 * struct pwm_device - PWM channel object
 * @label: name of the PWM device
 * @flags: flags associated with the PWM device
 * @hwpwm: per-chip relative index of the PWM device
 * @pwm: global index of the PWM device
 * @chip: PWM chip providing this PWM device
 * @chip_data: chip-private data associated with the PWM device
 * @args: PWM arguments
 * @state: curent PWM channel state
 */
struct pwm_device {
	const char *label;
	unsigned long flags;
	unsigned int hwpwm;//这是 PWM 设备的相对索引,局部于芯片
	unsigned int pwm;//这是 PWM 设备的系统全局索引
	struct pwm_chip *chip;
	void *chip_data;
 
	struct pwm_args args;
	struct pwm_state state;
};

struct pwm_args { 
   unsigned int period; /* Device's nitial period */ 
   enum pwm_polarity polarity; 
}; 

struct pwm_state { 
   unsigned int period; /* PWM period (in nanoseconds) */ 
   unsigned int duty_cycle; /* PWM duty cycle (in nanoseconds) */ 
   enum pwm_polarity polarity; /* PWM polarity */ 
   bool enabled; /* PWM enabled status */ 
} 

/**
 * struct pwm_chip - abstract a PWM controller
 * @dev: device providing the PWMs
 * @list: list node for internal use
 * @ops: callbacks for this PWM controller
 * @base: number of first PWM controlled by this chip
 * @npwm: number of PWMs controlled by this chip
 * @pwms: array of PWM devices allocated by the framework
 * @of_xlate: request a PWM device given a device tree PWM specifier
 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
 */
struct pwm_chip {
	struct device *dev;//该pwm chip对应的设备,一般由pwm driver对应的platform驱动指定
	struct list_head list;//
	const struct pwm_ops *ops;//操作PWM设备的回调函数,后面会详细介绍。必须提供
	int base;//这是由此芯片控制的第一个 PWM 的编号。如果chip->base < 0,则内核将动态分配一个基数。在将该chip下所有pwm device组成radix tree时使用,只有旧的pwm_request接口会使用,因此忽略它吧,编写pwm driver不需要关心。
	unsigned int npwm;//该pwm chip可以支持的pwm channel(也可以称作pwm device由struct pwm_device表示)个数
	struct pwm_device *pwms;//保存所有pwm device的数组,kernel根据npwm自行分配,不需要driver关心。
 
	struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
					const struct of_phandle_args *args);
	unsigned int of_pwm_n_cells;
};

/**
 * struct pwm_ops - PWM controller operations
 * @request: optional hook for requesting a PWM
 * @free: optional hook for freeing a PWM
 * @config: configure duty cycles and period length for this PWM
 * @set_polarity: configure the polarity of this PWM
 * @capture: capture and report PWM signal
 * @enable: enable PWM output toggling
 * @disable: disable PWM output toggling
 * @apply: atomically apply a new PWM config. The state argument
 *	   should be adjusted with the real hardware config (if the
 *	   approximate the period or duty_cycle value, state should
 *	   reflect it)
 * @get_state: get the current PWM state. This function is only
 *	       called once per PWM device when the PWM chip is
 *	       registered.
 * @dbg_show: optional routine to show contents in debugfs
 * @owner: helps prevent removal of modules exporting active PWMs
 */
struct pwm_ops {
	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);//不再使用
	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);//不再使用。
	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
		      int duty_ns, int period_ns);
	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
			    enum pwm_polarity polarity);
	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
		       struct pwm_capture *result, unsigned long timeout);
	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
	void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
		     struct pwm_state *state);
	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
			  struct pwm_state *state);
#ifdef CONFIG_DEBUG_FS
	void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
#endif
	struct module *owner;
};
关键源码分析: 
/**
 * pwmchip_add() - register a new PWM chip
 * @chip: the PWM chip to add
 *
 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
 * will be used. The initial polarity for all channels is normal.
 *
 * Returns: 0 on success or a negative error code on failure.
 */
int pwmchip_add(struct pwm_chip *chip)
{
	return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
}

/**
 * pwmchip_add_with_polarity() - register a new PWM chip
 * @chip: the PWM chip to add
 * @polarity: initial polarity of PWM channels
 *
 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
 * will be used. The initial polarity for all channels is specified by the
 * @polarity parameter.
 *
 * Returns: 0 on success or a negative error code on failure.
 */
int pwmchip_add_with_polarity(struct pwm_chip *chip,
			      enum pwm_polarity polarity)
{
	struct pwm_device *pwm;
	unsigned int i;
	int ret;
 
	if (!chip || !chip->dev || !chip->ops || !chip->npwm)
		return -EINVAL;
 
	if (!pwm_ops_check(chip->ops))
		return -EINVAL;
 
	mutex_lock(&pwm_lock);
 
	ret = alloc_pwms(chip->base, chip->npwm);
	if (ret < 0)
		goto out;
 
	chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
	if (!chip->pwms) {
		ret = -ENOMEM;
		goto out;
	}
 
	chip->base = ret;
 
	for (i = 0; i < chip->npwm; i++) {
		pwm = &chip->pwms[i];
 
		pwm->chip = chip;
		pwm->pwm = chip->base + i;
		pwm->hwpwm = i;
		pwm->state.polarity = polarity;
 
		if (chip->ops->get_state)
			chip->ops->get_state(chip, pwm, &pwm->state);
 
		radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
	}
 
	bitmap_set(allocated_pwms, chip->base, chip->npwm);
 
	INIT_LIST_HEAD(&chip->list);
	list_add(&chip->list, &pwm_chips);
 
	ret = 0;
 
	if (IS_ENABLED(CONFIG_OF))
		of_pwmchip_add(chip);
 
out:
	mutex_unlock(&pwm_lock);
 
	if (!ret)
		pwmchip_sysfs_export(chip);//导出sys fs节点
 
	return ret;
}

这个也比较简单,根据这个pwm控制器的channel个数,创建相应的struct pwm_device,最终加到pwm_tree链表树中。这样所有的可以使用的pwm channel都被抽象成了struct pwm_device结构体,也是我们最终的操作结构体。 

三、设备驱动层:

四、应用层:

从第一章的系统框架可以看出:应用层使用PWM有两条路径可以选择

  • 调用设备驱动来获取核心层提供的数据

  • 利用sysfs文件系统直接调用核心层数据,这种方法可以不进行设备树的匹配和设备驱动的编写
导出chip1通道的0设备文件:echo 0 > /sys/class/pwm/pwmchip1/export
配置chip1通道0的周期: echo 10000000 > /sys/class/pwm/pwmchip1 /pwm0/period
配置chip1通道0的占空比:echo 4000000 >/sys/class/pwm/pwmchip1/pwm0/duty_cycle
配置片chip通道0使能: echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
配置片chip通道0禁能: echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
取消导出片chip通道0设备文件: echo 0 >/sys/class/pwm/pwmchip1/unexport

PWM 通道使用从 0 到pwm<n-1>的索引编号。这些数字是相对于芯片的。每个 PWM 通道导出都会在pwmchipN中创建一个pwmX目录,该目录与使用的export文件相同。X是导出的通道号。

完整的 PWM 框架 API 和 sysfs 描述可在内核源树中的Documentation/pwm.txt文件中找到。

/sys/class/pwm/
The pwm/ class sub-directory belongs to the Generic PWM Framework and provides a sysfs interface for using PWM channels

/sys/class/pwm/pwmchipN/
A /sys/class/pwm/pwmchipN directory is created for each probed PWM controller/chip where N is the base of the PWM chip.

/sys/class/pwm/pwmchipN/npwm
The number of PWM channels supported by the PWM chip.

/sys/class/pwm/pwmchipN/export
Exports a PWM channel from the PWM chip for sysfs control.Value is between 0 and /sys/class/pwm/pwmchipN/npwm - 1.

/sys/class/pwm/pwmchipN/unexport
Unexports a PWM channel.

/sys/class/pwm/pwmchipN/pwmX
A /sys/class/pwm/pwmchipN/pwmX directory is created for each exported PWM channel where X is the exported PWM channel number.

/sys/class/pwm/pwmchipN/pwmX/period
Sets the PWM signal period in nanoseconds.

/sys/class/pwm/pwmchipN/pwmX/duty_cycle
Sets the PWM signal duty cycle in nanoseconds.

/sys/class/pwm/pwmchipN/pwmX/enable
Enable/disable the PWM signal.

p17.扩展:为什么可以通过sysyfs操作pwm_哔哩哔哩_bilibili

五、debug方法:

xxx:/ # cat /sys/kernel/debug/pwm
platform/ff680030.pwm, 1 PWM device
 pwm-0   ((null)              ): period: 0 ns duty: 0 ns polarity: inverse
 
platform/ff680020.pwm, 1 PWM device
 pwm-0   ((null)              ): period: 0 ns duty: 0 ns polarity: inverse
 
platform/ff680010.pwm, 1 PWM device
 pwm-0   (backlight           ): requested enabled period: 1000000 ns duty: 723313 ns polarity: normal
xxx:/ #

ref:

Linux内核4.14版本——PWM子系统(1)_框架分析-CSDN博客

https://www.cnblogs.com/apachecn/p/18196572

https://wiki.luckfox.com/zh/Luckfox-Pico/Luckfox-Pico-PWM/

https://doc.embedfire.com/linux/imx8mm/linux_base/zh/latest/linux_app/pwm/pwm.html

https://www.cnblogs.com/toutiegongzhu/p/17697360.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2071796.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Toyota】 Avalon

文章目录 1、外观2、内饰3、2024 款配置对比2024 2.0L CVT 进取版 vs 2024 2.0L CVT 臻选版2024 2.0L CVT 臻选版 vs 2024 2.0L CVT 豪华版2024 2.0L CVT 臻选版 vs 2024 智能电混双擎 2.0L E-CVT 臻选版 &#xff08;纯油 vs 油电混&#xff09;2024 智能电混双擎 2.0L E-CVT …

appium学习记录

免责声明 本文内容仅供参考&#xff0c;将appuim与爬虫技术相结合可能违反某些app的使用条款和法律法规。作者不对因此产生的法律问题或技术风险负责。建议读者在进行爬取操作前&#xff0c;充分了解相关法律法规并确保合规。 1、初识appium 背景&#xff1a;部分APP需要反编译…

云计算第二阶段---DBA Day05-DAY07

DBA Day05 这周的内容涉及到的是各类数据库的服务配置与数据的备份与恢复操作. 环境准备: 设置 ip 地址 和主机名 安装mysql,mysql-server --->启动 yum -y install mysql mysql-server #装mysql环境包 systemclt start mysqld #启动服务 exit …

hackit 2018

源代码 const express require(express) var hbs require(hbs); var bodyParser require(body-parser); const md5 require(md5); var morganBody require(morgan-body); const app express(); var user []; //empty for nowvar matrix []; for (var i 0; i < 3; …

PySide6入门教程之六 | Main Window、Widge、Dtialog三大类型窗口的使用

前言 PySide6 是用于Python的一个跨平台GUI库&#xff0c;它提供了Qt框架的Python绑定。在PySide6中&#xff0c;QMainWindow,QWidget, 和 QDialog 都是非常常用的类&#xff0c;它们各自有特定的应用场景和功能。 &#x1f680;&#x1f680;&#x1f680; Pyside6实战教程专…

存储与传输/大小端字节序的概念、决定因素、给编程带来的困扰

文章目录 概述大小端分歧的类比为什么要关注字节序NET网络字节序什么时候必须转换字节序大小端字节序哪个优秀判断系统字节序类型字节序类型转换大小端内存监视和调试 谁决定了大小端模式CPU架构决定大小端操作系统影响大小端&#xff1f;编译器也影响大小端&#xff1f;可配置…

kafka发送消息-分区策略(消息发送到哪个分区中?是什么策略)

生产者发送消息的分区策略&#xff08;消息发送到哪个分区中&#xff1f;是什么策略&#xff09; 1、默认策略&#xff0c;程序自动计算并指定分区1.1、指定key&#xff0c;不指定分区1.2、不指定key&#xff0c;不指定分区 2、轮询分配策略RoundRobinPartitioner2.1、创建配置…

3.4-CoroutineScope/CoroutineContext:coroutineScope() 和 supervisorScope()

文章目录 coroutineScope()supervisorScope()总结 coroutineScope() coroutineScope() 和我们创建协程时的 CoroutineScope 名字是相同的&#xff0c;实际上它们也确实有所关联&#xff0c;为了方便理解我们先说下 coroutineScope() 是怎样的效果。 我们在使用 coroutineScop…

PPT分享:某集团公司供应链-销售与运营计划SOP

今天笔者给大家带来的是“供应链中的S&OP”&#xff0c;在分享PPT之前&#xff0c;咱们先了解下什么是“S&OP”。PPT下载链接见文末~ 供应链中的S&OP&#xff0c;全称为Sales and Operations Planning&#xff0c;即销售与运营计划&#xff0c;是一种跨职能的决策…

【Unity】移动端草海解决方案

草海是开放大世界渲染的必不可少的因素&#xff0c;Unity 原生的 Terrain 草海效率较低&#xff0c;而且无法与 RVT 结合起来&#xff0c;无法在移动端上实现。因此我们自己搓出来一套草海系统&#xff0c;使用 C# 多线程辅助运算&#xff0c;并能支持割草、烧草等进阶玩法。草…

springboot嵌入式数据库实践-H2内嵌数据库(文件、内存)

本文章记录笔者的嵌入式数据库简单实现&#xff0c; 记录简要的配置过程。自用文章&#xff0c;仅作参考。 目录 本文章记录笔者的嵌入式数据库简单实现&#xff0c; 记录简要的配置过程。自用文章&#xff0c;仅作参考。 嵌入式数据库 -------------------------------具…

数学基础(八)

一、假设检验 什么是假设&#xff1a; 对总体参数&#xff08;均值、比例等&#xff09;的具体数值所作的陈述。 什么是假设检验&#xff1a; 先对总体的参数提出某种假设&#xff0c;然后利用样本的信息判断假设是否成立的过程 假设检验的应用&#xff1a; 推广新的教育…

【C++ Primer Plus习题】4.10

问题: 解答: #include <iostream> #include <array> using namespace std;int main() {array<float, 3> grade;float average0;for (int i 0; i < 3; i){cout << "请输入第" << i 1 << "次跑40米的成绩:";cin &…

『功能项目』新输入系统【06】

我们打开上一篇04禁止射线穿透行为项目&#xff0c; 本章要做的事情是在Unity编辑器中添加 新输入系统 实现主角在场景中鼠标右键可以使主角 转向。 本次项目需要让Unity引擎重新启动所以先保存当前项目 再次打开项目后&#xff0c; 修改为Both 点击Apply前注意要先保存项目&a…

Apollo9.0 PNC源码学习之Planning模块—— Lattice规划(五):横向运动轨迹规划

参考文章: (1)自动驾驶规划理论与实践 (2)Apollo6.0代码Lattice算法详解——Part5: 生成横纵向轨迹 0 前言 横向运动轨迹规划主要对车辆方向盘转向的控制策略;求解方法分为基于s的5次多项式和基于二次规划的方法 2 基于s的5次多项式的横向运动轨迹的生成 纵向运动轨迹…

Linux环境使用docker搭建Navidrome本地个人音乐库并实现远程访问

文章目录 前言1. 安装Docker2. Docker镜像源添加方法3. 创建并启动Navidrome容器 前言 本文和大家分享一款目前在G站有11KStar的开源跨平台音乐服务器Navidrome&#xff0c;如何在Linux环境本地使用Docker部署&#xff0c;并结合cpolar内网穿透工具配置公网地址&#xff0c;实…

Docker安装Logstash,并结合logback实现ELK日志收集

拉取镜像 docker pull docker.elastic.co/logstash/logstash:8.14.3创建文件夹 mkdir /mnt/data/logstash创建默认文件 先不做目录挂载&#xff0c;run出一个容器 docker run -d --rm -it docker.elastic.co/logstash/logstash:8.14.3将config和pipeline从容器cp到宿主机 …

BEV学习---LSS-1:论文原理及代码串讲

目前在自动驾驶领域&#xff0c;比较火的一类研究方向是基于采集到的环视图像信息&#xff0c;去构建BEV视角下的特征完成自动驾驶感知的相关任务。所以如何准确的完成从相机视角向BEV视角下的转变就变得由为重要。目前感觉比较主流的方法可以大体分为两种&#xff1a; 1、显式…

Linux驱动.之字符设备驱动框架,及手动,自动注册创建/dev下设备节点详解(一)

一、Linux 字符设备驱动框架 Linux一切皆文件&#xff0c;通过VFS虚拟文件系统&#xff0c;把所有外设的细节都隐藏起来&#xff0c;最后都以文件的形态呈现于应用层&#xff0c; 操作设备&#xff0c;就像对文件的打开、关闭、读写、控制。这种方式完美的统一了对用户的接口&a…

WIN/MAC 图像处理软件Adobe Photoshop PS2024软件下载安装

目录 一、软件概述 1.1 基本信息 1.2 主要功能 二、系统要求 2.1 Windows 系统要求 2.2 macOS 系统要求 三、下载 四、使用教程 4.1 基本界面介绍 4.2 常用工具使用 4.3 进阶操作 一、软件概述 1.1 基本信息 Adobe Photoshop&#xff08;简称PS&#xff09;是一款…