spi 驱动-数据发送流程分析
总结
核心函数是spi_sync, 设备驱动->核心函数-> 控制器驱动
实例分析
(gdb) c
Continuing.Thread 115 hit Breakpoint 1, bcm2835_spi_transfer_one (master=0xffffffc07b8e6000, spi=0xffffffc07b911800, tfr=0xffffff8009f53c40) at drivers/spi/spi-bcm2835.c:534
534 {
(gdb) bt
#0 bcm2835_spi_transfer_one (master=0xffffffc07b8e6000, spi=0xffffffc07b911800, tfr=0xffffff8009f53c40) at drivers/spi/spi-bcm2835.c:534
#1 0xffffff80086cbe8c in spi_transfer_one_message (ctlr=0xffffffc07b8e6000, msg=0xffffff8009f53ca0) at drivers/spi/spi.c:1031
#2 0xffffff80086cc990 in __spi_pump_messages (ctlr=0xffffffc07b8e6000, in_kthread=<optimized out>) at drivers/spi/spi.c:1265
#3 0xffffff80086cccb8 in __spi_sync (spi=0xffffffc07b911800, message=0xffffff8009f53ca0) at drivers/spi/spi.c:3129
#4 0xffffff80086ccd04 in spi_sync (spi=0xffffffc07b911800, message=0xffffff8009f53ca0) at drivers/spi/spi.c:3165
#5 0xffffff80086ce14c in spidev_sync (spidev=<optimized out>, message=0xffffff8009f53ca0) at drivers/spi/spidev.c:112
#6 0xffffff80086ce2f0 in spidev_sync_write (len=<optimized out>, spidev=<optimized out>) at drivers/spi/spidev.c:132
#7 spidev_write (filp=<optimized out>, buf=<optimized out>, count=4, f_pos=<optimized out>) at drivers/spi/spidev.c:199
#8 0xffffff80082c919c in __vfs_write (file=0xffffffc072ea2500, p=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4, pos=0xffffff8009f53db0) at fs/read_write.c:485
#9 0xffffff80082c93e0 in vfs_write (file=0xffffffc072ea2500, buf=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4, pos=0xffffff8009f53db0) at fs/read_write.c:549
#10 0xffffff80082c9768 in ksys_write (fd=<optimized out>, buf=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4) at fs/read_write.c:599
#11 0xffffff80082c9810 in __do_sys_write (count=<optimized out>, buf=<optimized out>, fd=<optimized out>) at fs/read_write.c:611
#12 __se_sys_write (fd=1, buf=367353383776, count=4) at fs/read_write.c:608
#13 0xffffff80082c984c in __arm64_sys_write (regs=0xffffff8009f53ec0) at fs/read_write.c:608
#14 0xffffff8008099254 in __invoke_syscall (regs=0xffffff8009f53ec0, syscall_fn=0xffffff80082c9820 <__arm64_sys_write>) at arch/arm64/kernel/syscall.c:36
#15 0xffffff80080993a4 in invoke_syscall (regs=0xffffff8009f53ec0, scno=<optimized out>, sc_nr=<optimized out>, syscall_table=0xffffff80089c0778 <sys_call_table>) at arch/arm64/kernel/syscall.c:48
#16 0xffffff800809943c in el0_svc_common (regs=0xffffff8009f53ec0, scno=64, sc_nr=294, syscall_table=0xffffff80089c0778 <sys_call_table>) at arch/arm64/kernel/syscall.c:114
#17 0xffffff8008099524 in el0_svc_handler (regs=0xffffff8009f53ec0) at arch/arm64/kernel/syscall.c:160
#18 0xffffff8008084088 in el0_svc () at arch/arm64/kernel/entry.S:917
源码分析
1. 注册字符设备并提供操作函数 spidev_fops
static int __init spidev_init(void)status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
}static const struct file_operations spidev_fops = {.....write = spidev_write,....
};
2. 分析spidev_write 调用spi_sync
static ssize_t
spidev_write(struct file *filp, const char __user *buf,size_t count, loff_t *f_pos)
{struct spidev_data *spidev;ssize_t status = 0;unsigned long missing;spidev = filp->private_data;missing = copy_from_user(spidev->tx_buffer, buf, count);if (missing == 0)status = spidev_sync_write(spidev, count);....}
static inline ssize_t
spidev_sync_write(struct spidev_data *spidev, size_t len)
{struct spi_transfer t = {.tx_buf = spidev->tx_buffer,.len = len,.speed_hz = spidev->speed_hz,};struct spi_message m;spi_message_init(&m);spi_message_add_tail(&t, &m);return spidev_sync(spidev, &m);
}static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{int status;struct spi_device *spi;spi = spidev->spi;status = spi_sync(spi, message);return status;
}
3. 分析 spi_sync 调用 ctlr->transfer_one_message
int spi_sync(struct spi_device *spi, struct spi_message *message)
{ret = __spi_sync(spi, message);
}static int __spi_sync(struct spi_device *spi, struct spi_message *message)
{DECLARE_COMPLETION_ONSTACK(done);int status;struct spi_controller *ctlr = spi->controller;unsigned long flags;status = __spi_validate(spi, message);if (status != 0)return status;message->complete = spi_complete;message->context = &done;message->spi = spi;SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);if (ctlr->transfer == spi_queued_transfer) { //新方法status = __spi_queued_transfer(spi, message, false);} else { //legacy method //老方法status = spi_async_locked(spi, message);}if (status == 0) {/* Push out the messages in the calling context if we* can.*/if (ctlr->transfer == spi_queued_transfer) {SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync_immediate);SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync_immediate);__spi_pump_messages(ctlr, false); //处理spi 消息}wait_for_completion(&done);status = message->status;}message->context = NULL;return status;
}
static int __spi_queued_transfer(struct spi_device *spi,struct spi_message *msg,bool need_pump)
{struct spi_controller *ctlr = spi->controller;unsigned long flags;...list_add_tail(&msg->queue, &ctlr->queue); 将消息队列添加到队列的尾部if (!ctlr->busy && need_pump)kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);....return 0;
}
static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
{unsigned long flags;bool was_busy = false;int ret;/* Lock queue */spin_lock_irqsave(&ctlr->queue_lock, flags);/* Make sure we are not already running a message */if (ctlr->cur_msg) {spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* If another context is idling the device then defer */if (ctlr->idling) {kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Check if the queue is idle */if (list_empty(&ctlr->queue) || !ctlr->running) {if (!ctlr->busy) {spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Only do teardown in the thread */if (!in_kthread) {kthread_queue_work(&ctlr->kworker,&ctlr->pump_messages);spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}ctlr->busy = false;ctlr->idling = true;spin_unlock_irqrestore(&ctlr->queue_lock, flags);kfree(ctlr->dummy_rx);ctlr->dummy_rx = NULL;kfree(ctlr->dummy_tx);ctlr->dummy_tx = NULL;if (ctlr->unprepare_transfer_hardware &&ctlr->unprepare_transfer_hardware(ctlr))dev_err(&ctlr->dev,"failed to unprepare transfer hardware\n");if (ctlr->auto_runtime_pm) {pm_runtime_mark_last_busy(ctlr->dev.parent);pm_runtime_put_autosuspend(ctlr->dev.parent);}trace_spi_controller_idle(ctlr);spin_lock_irqsave(&ctlr->queue_lock, flags);ctlr->idling = false;spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Extract head of queue */ctlr->cur_msg =list_first_entry(&ctlr->queue, struct spi_message, queue);list_del_init(&ctlr->cur_msg->queue);if (ctlr->busy)was_busy = true;elsectlr->busy = true;spin_unlock_irqrestore(&ctlr->queue_lock, flags);mutex_lock(&ctlr->io_mutex);if (!was_busy && ctlr->auto_runtime_pm) {ret = pm_runtime_get_sync(ctlr->dev.parent);if (ret < 0) {pm_runtime_put_noidle(ctlr->dev.parent);dev_err(&ctlr->dev, "Failed to power device: %d\n",ret);mutex_unlock(&ctlr->io_mutex);return;}}if (!was_busy)trace_spi_controller_busy(ctlr);if (!was_busy && ctlr->prepare_transfer_hardware) {ret = ctlr->prepare_transfer_hardware(ctlr);if (ret) {dev_err(&ctlr->dev,"failed to prepare transfer hardware\n");if (ctlr->auto_runtime_pm)pm_runtime_put(ctlr->dev.parent);mutex_unlock(&ctlr->io_mutex);return;}}trace_spi_message_start(ctlr->cur_msg);if (ctlr->prepare_message) {ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);if (ret) {dev_err(&ctlr->dev, "failed to prepare message: %d\n",ret);ctlr->cur_msg->status = ret;spi_finalize_current_message(ctlr);goto out;}ctlr->cur_msg_prepared = true;}ret = spi_map_msg(ctlr, ctlr->cur_msg);if (ret) {ctlr->cur_msg->status = ret;spi_finalize_current_message(ctlr);goto out;}ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 调用控制器的传输函数if (ret) {dev_err(&ctlr->dev,"failed to transfer one message from queue\n");goto out;}out:mutex_unlock(&ctlr->io_mutex);/* Prod the scheduler in case transfer_one() was busy waiting */if (!ret)cond_resched();
}
4. spi controler "传输函数" 之间的相互调用
# ctlr->transfer = spi_queued_transfer; // 调用 ctlr->transfer_one_message
ctlr->transfer_one_message = spi_transfer_one_message; //ctlr->transfer_one
ctrl->transfer_one = bcm2835_spi_transfer_one;
相关文章:
spi 驱动-数据发送流程分析
总结 核心函数是spi_sync, 设备驱动->核心函数-> 控制器驱动 实例分析 (gdb) c Continuing.Thread 115 hit Breakpoint 1, bcm2835_spi_transfer_one (master0xffffffc07b8e6000, spi0xffffffc07b911800, tfr0xffffff8009f53c40) at drivers/spi/spi-bcm2835…...

平面分割--------PCL
平面分割 bool PclTool::planeSegmentation(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::ModelCoefficients::Ptr coefficients, pcl::PointIndices::Ptr inliers) {std::cout << "Point cloud data: " << cloud->points.size() <<…...

前端之深拷贝
前提: 就是在实际开发中,我有一个编辑的弹窗,可以查看和编辑,因为弹窗里面是一个步骤条,点击下一步就要向对应的接口发送请求,考虑到就比如我点击下一步,此次表箱信息其实不需要修改࿰…...

2024年 Java 面试八股文——SpringCloud篇
目录 1.Spring Cloud Alibaba 中的 Nacos 是如何进行服务注册和发现的? 2.Spring Cloud Alibaba Sentinel 的流量控制规则有哪些? 3.Spring Cloud Alibaba 中如何实现分布式配置管理? 4.Spring Cloud Alibaba RocketMQ 的主要特点有哪些&…...

linux C语言Makefile
ChatGPT 在Linux中使用Makefile来自动化C语言项目的构建过程是很普遍的实践。Makefile是一个包含了一系列构建目标及如何构建这些目标的依赖和规则的文本文件。 一个基本的Makefile例子可能会像这样: # 定义编译器 CCgcc# 定义编译选项 CFLAGS-I.# 定义可执行文件…...

pgvector扩展在IvorySQL Oracle兼容模式下的应用实践
向量数据库是生成式人工智能(GenAI)的关键组成部分。作为PostgreSQL的重要扩展,pgvector支持高达16000维的向量计算能力,使得PostgreSQL能够直接转化为高效的向量数据库。 IvorySQL基于PostgreSQL开发,因此它同样支持添加pgvector扩展。在Ora…...
c++ 线程概述
C中的线程是并发编程的重要组成部分,它允许程序同时执行多个任务。以下是对C线程的概述: 基本概念: 并发:意味着两个或多个任务同时执行。在单核CPU上,由于只有一个CPU,某一时刻只能执行一个任务࿰…...

纯血鸿蒙APP实战开发——短视频切换实现案例
短视频切换实现案例 介绍 短视频切换在应用开发中是一种常见场景,上下滑动可以切换视频,十分方便。本模块基于Swiper组件和Video组件实现短视频切换功能。 效果图预览 使用说明 上下滑动可以切换视频。点击屏幕暂停视频,再次点击继续播放…...

36.Docker-Dockerfile自定义镜像
镜像结构 镜像是将应用程序及其需要的系统函数库、环境、配置、依赖打包而成。 镜像是分层机构,每一层都是一个layer BaseImage层:包含基本的系统函数库、环境变量、文件系统 EntryPoint:入口,是镜像中应用启动的命令 其他:在…...

【webrtc】MessageHandler 4: 基于线程的消息处理:以Fake 收发包模拟为例
G:\CDN\rtcCli\m98\src\media\base\fake_network_interface.h// Fake NetworkInterface that sends/receives RTP/RTCP packets.虚假的网络接口,用于模拟发送包、接收包单纯仅是处理一个ST_RTP包 消息的id就是ST_RTP 类型,– 然后给到目的地:mediachannel处理: 最后消息消…...
C#运算符“/”使用方法
C#中,当需要对两个整数进行除法运算时,结果会被截断为整数部分,即使结果本应是一个小数。这是因为整数除法会丢弃小数部分,只保留整数部分。 要想保留小数部分,需要将至少其中一个操作数转换为float、double或者 deci…...

虚拟机网络桥接模式无法通信,获取到的ip为169.254.X.X
原因:VMware自动选择的网卡可能不对 解决:编辑-虚拟网络编辑器-更改桥接模式-选择宿主机物理网卡,断开虚拟机网络连接后重新连接即可...

【数据结构】初识数据结构
引入: 哈喽大家好,我是野生的编程萌新,首先感谢大家的观看。数据结构的学习者大多有这样的想法:数据结构很重要,一定要学好,但数据结构比较抽象,有些算法理解起来很困难,学的很累。我…...

相机知识的补充
一:镜头 1.1MP的概念 相机中MP的意思是指百万像素。MP是mega pixel的缩写。mega意为一百万,mega pixel 指意为100万像素。“像素”是相机感光器件上的感光最小单位。就像是光学相机的感光胶片的银粒一样,记忆在数码相机的“胶片”ÿ…...

在Linux操作系统中实现磁盘开机自动挂载
当一个分区创建好,然后文件系统创建完毕之后, 需要使用mount命令将分区挂载到空目录上,这个挂载关系是临时的,也就是说当重启机器的时候,硬盘分区于空目录之间的挂载关系就会解除。 磁盘于目录之间的挂载关系断开意味…...

单片机编程实例400例大全(100-200)
今天继续分享单片机编程实例第100-200例。 今天的实例会比前面100复杂一些,我大概看了下,很多都具备实际产品的参考价值。 今天继续分享单片机编程实例第100-200例。 今天的实例会比前面100复杂一些,我大概看了下,很多都具备实际…...

新兴游戏引擎Godot vs. 主流游戏引擎Unity和虚幻引擎,以及版本控制工具Perforce Helix Core如何与其高效集成
游戏行业出现一个新生事物——Godot,一个免费且开源的2D和3D游戏引擎。曾经由Unity和虚幻引擎(Unreal Engine)等巨头主导的领域如今迎来了竞争对手。随着最近“独特”定价模式的变化,越来越多的独立开发者和小型开发团队倾向于选择…...

Leetcode—1652. 拆炸弹【简单】
2024每日刷题(127) Leetcode—1652. 拆炸弹 实现代码 class Solution { public:vector<int> decrypt(vector<int>& code, int k) {int codeSize code.size();vector<int> ans(codeSize, 0);if(k 0) {return ans;}if(k > 0)…...
JAVASE---抽象类相关
instanceof 和类型转换 System.out.println(X instanceof Y );主要看X与Y之间是否存在父子(继承)关系,如果存在则编译可完成,否则无法 进行编译。 1.父类引用指向子类的对象 2.把子类转换为父类,向上转型; 3.把父类转…...
深入理解C++中的inline函数
在C编程中,我们经常会遇到inline关键字,它用于修饰函数,以建议编译器将该函数的调用替换为函数体的直接拷贝。这就是inline函数的基本概念。然而,inline函数并非真正意义上的函数,而只是一种"在调用点插入函数体&…...

Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...
生成 Git SSH 证书
🔑 1. 生成 SSH 密钥对 在终端(Windows 使用 Git Bash,Mac/Linux 使用 Terminal)执行命令: ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" 参数说明: -t rsa&#x…...
C++中string流知识详解和示例
一、概览与类体系 C 提供三种基于内存字符串的流,定义在 <sstream> 中: std::istringstream:输入流,从已有字符串中读取并解析。std::ostringstream:输出流,向内部缓冲区写入内容,最终取…...

NLP学习路线图(二十三):长短期记忆网络(LSTM)
在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...
重启Eureka集群中的节点,对已经注册的服务有什么影响
先看答案,如果正确地操作,重启Eureka集群中的节点,对已经注册的服务影响非常小,甚至可以做到无感知。 但如果操作不当,可能会引发短暂的服务发现问题。 下面我们从Eureka的核心工作原理来详细分析这个问题。 Eureka的…...

Reasoning over Uncertain Text by Generative Large Language Models
https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...

LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf
FTP 客服管理系统 实现kefu123登录,不允许匿名访问,kefu只能访问/data/kefu目录,不能查看其他目录 创建账号密码 useradd kefu echo 123|passwd -stdin kefu [rootcode caozx26420]# echo 123|passwd --stdin kefu 更改用户 kefu 的密码…...

人机融合智能 | “人智交互”跨学科新领域
本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...

MySQL 知识小结(一)
一、my.cnf配置详解 我们知道安装MySQL有两种方式来安装咱们的MySQL数据库,分别是二进制安装编译数据库或者使用三方yum来进行安装,第三方yum的安装相对于二进制压缩包的安装更快捷,但是文件存放起来数据比较冗余,用二进制能够更好管理咱们M…...
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...