简体   繁体   English

Linux kernel,I2C SPI总线,还有时间片?

[英]Linux kernel, I2C SPI buses, and time slices?

Kind of a weird question, but I'm in a class about linux kernel driver development and the prof couldn't answer this.有点奇怪的问题,但我在 class 关于 linux kernel 驱动程序开发中,教授无法回答这个问题。

Let's suppose I have a finite state machine of some kind attached to an SPI or I2C bus (In real life this is actually an accelerometer, but the implementation isn't important).假设我有一个有限的 state 机器连接到 SPI 或 I2C 总线(在现实生活中,这实际上是一个加速度计,但实现并不重要)。 If I write a misc character driver for it, and insmod it, any user on the system can now access this device.如果我为它写一个 misc 字符驱动程序,并 insmod 它,系统上的任何用户现在都可以访问这个设备。

Let's suppose that I want to read 200 bytes of data from this FSM device.假设我想从这个 FSM 设备读取 200 字节的数据。 A read is performed by sending the register address of the data location, and performing a continuous read until I have my 200 bytes (program#1).通过发送数据位置的寄存器地址来执行读取,并执行连续读取直到我有 200 个字节(程序#1)。

The confusion comes in when I know my user level program is getting time-sliced.当我知道我的用户级程序被时间分割时,混乱就来了。 If another (program#2) user level program is also going to ask for 10 bytes from this accelerometer, it will address and continuously read until it has it's 10 bytes.如果另一个(程序#2)用户级程序也将向该加速度计请求 10 个字节,它将寻址并连续读取直到它达到 10 个字节。

If program#1 has run and read 50 bytes when it gets time-sliced out, and program#2 is run to completion, then when program#1 returns, the address it was reading from is corrupted, and it's reading data from the wrong address.如果程序#1 在被时间分片时已经运行并读取了 50 个字节,并且程序#2 运行完成,那么当程序#1 返回时,它正在读取的地址已损坏,并且它正在从错误的位置读取数据地址。

Time slices are invisible to the user.时间片对用户是不可见的。 As far as I know they're also invisible to the kernel driver code.据我所知,它们对于 kernel 驱动程序代码也是不可见的。 Is there some mechanism to avoid this?有什么机制可以避免这种情况吗? Atomic read and writes would make the most sense, but these I2C/SPI devices are slow by CPU standards.原子读写最有意义,但这些 I2C/SPI 设备按照 CPU 标准比较慢。 I can't imagine the CPU would sit idly by for the entire time just to ensure an I2C/SPI read/write is atomic?我无法想象 CPU 会一直闲置以确保 I2C/SPI 读/写是原子的?

The answer the prof gave me is that this problem has been solved for memory a long time ago, and linux is "probably doing something fancy under the hood" to make it happen.教授给我的答案是这个问题很久以前就已经为 memory 解决了,而 linux “可能在幕后做了一些奇特的事情”来实现它。 But that didn't really address the root of the question.但这并没有真正解决问题的根源。 I'd love a proper answer.我想要一个正确的答案。 If anyone can recommend better search terms to approach the question I would also appreciate it.如果有人可以推荐更好的搜索词来解决这个问题,我也会很感激。

Thank you谢谢

If I get your question right, the whole problem is about locking the device that is used by multiple programs (threads, etc.) and this post may have an answer to it.如果我答对了你的问题,那么整个问题就是锁定多个程序(线程等)使用的设备, 这篇文章可能会有答案。

Your professor was partially right.你的教授部分正确。 The problem was discovered a long time ago, it's called the race conditions .这个问题很早以前就被发现了,叫做race conditions But, the solution isn't provided by the kernel itself in a fancy way.但是,kernel 本身并没有以奇特的方式提供解决方案。 It is the user's responsibility to implement a mechanism that can prevent race conditions on devices or any other resources.用户有责任实施一种机制来防止设备或任何其他资源出现竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM