如何从 C 中的串行(SPI)连接读取数据? [英] How to read data from a serial (SPI) connection in C?

查看:16
本文介绍了如何从 C 中的串行(SPI)连接读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序将安装在 Linux MCU (Raspberry Pi) 上,该程序将读取来自另一个 MCU(我将自己构建的国产)的串行数据.

I am trying to write a program that will be installed on a Linux MCU (Raspberry Pi) that will read serial data coming to it from yet another MCU (something homegrown that I will build myself).

我研究了如何做到这一点,并认为我有大局",但仍然缺少一些东西.一方面,我需要启用内核模块并让自己访问设备:

I have researched how to do this and think I have the "big picture" but still missing a few things. For one, I need to enable the kernel module and give myself access to the device:

sudo modprobe spi_bcm2708
sudo chown `id -u`.`id -g` /dev/spidev0.*

从那里我可以使用 这个著名的 C 文件 来测试 SPI 连接.

From there I can use this famous C file to test the SPI connection.

所以现在连接已经存在,但我仍然需要从中读取串行数据.根据 this SO question的答案,我只需要:

So now the connection is there, but I still need to read serial data from it. According to the answer to this SO question, I just need to:

...打开相关端口进行读取,并使用read()获取数据.

...open the relevant port for reading, and use read() to get the data.

但是,该答案中的代码片段似乎是通过设备驱动程序打开端口,而不是通过 SPI.

However the code snippet in that answer seems to be opening up a port through a device driver, not via SPI.

我的理解是我可以使用 SPI 从连接的 MCU 设备中读取数据,并且我不需要为该设备编写自己的设备驱动程序.这是真的?如果是这样,我将如何从 SPI 连接中读取数据,该代码与上面链接的问题答案中的代码有何不同?

My understanding was that I could use SPI to read data from the connected MCU device, and that I would not need to write my own device driver for that device. Is this true? If so, how would I read from an SPI connection, and how would that code be different than the code in the answer to the question linked above?

如果我的理解不正确,并且我确实通过"了设备驱动程序,为什么我首先需要驱动程序?这不正是SPI希望规避的吗?提前致谢.

And if my understanding is incorrect, and I do read "through" a device driver, why do I need the driver in the first place? Isn't that what SPI hopes to circumvent? Thanks in advance.

推荐答案

我正在尝试编写一个程序,该程序将安装在 Linux MCU (Raspberry Pi) 上,该程序将读取来自另一个 MCU(我将自己构建的国产)的串行数据.

I am trying to write a program that will be installed on a Linux MCU (Raspberry Pi) that will read serial data coming to it from yet another MCU (something homegrown that I will build myself).

[MCU 不是正确使用的术语.Raspberry Pi 使用 SoC(片上系统).微控制器会更便宜比 SoC 更简单的设备.如果要使用 TLA,则使用 SBC,单板计算机.]

[MCU is not the proper term to use. The Raspberry Pi uses a SoC (System on a Chip). A microcontroller would be a cheaper & simpler device than a SoC. If you want to use a TLA, then use SBC, single board computer.]

[您误用了术语串行端口"和串行数据".今天,由于 PC 的普及,串口"已经专门指 EIA/RS-232 异步串行连接.SPI.USB.I2C、TWI、SATA 不应被称为串行"连接,除非您解释它们是如何工作的.]

[You are misusing the terms "serial port" and "serial data". Today, because of the ubiquity of PCs, "serial port" has come to exclusively refer to EIA/RS-232 asynchronous serial connections. SPI. USB. I2C, TWI, SATA et cetera should not be referred to as "serial" connections unless you are explaining how they work.]

在 Linux 中,SPI 设备驱动程序通常被实现为平台驱动程序"而不是字符驱动程序.因此这样的驱动程序不会有文件操作,或 fops, 执行 open(), read(), write()close(). 这样的操作是对于目标设备,平台设备连接到系统.因此,平台设备在/dev中没有目标设备的设备节点.SPI与USB和PCI属于同一类别;它们都是总线,通常作为平台驱动程序实现.

In Linux the SPI device driver is often implemented as a *platform driver" rather than a character driver. Therefore such a driver would not have file operations, or fops, to perform open(), read(), write() or close(). Such operation are for target devices, which the platform device connects to the system. As a consequence, platform devices do not have device nodes in /dev like target devices do. SPI is in the same category as USB and PCI; they are all buses and typically implemented as platform drivers.

我的理解是我可以使用 SPI 从连接的 MCU 设备中读取数据,并且我不需要为该设备编写自己的设备驱动程序.这是真的吗?

My understanding was that I could use SPI to read data from the connected MCU device, and that I would not need to write my own device driver for that device. Is this true?

答案取决于您使用的内核是否有一个 SPI 字符设备暴露给您的用户程序使用.但如果 SPI 驱动程序是平台驱动程序,则必须实现自定义 SBC 的设备驱动程序.该目标设备需要 /dev 中的节点、分配的主要和次要编号以及与这些编号关联的驱动程序.该驱动程序将利用 SPI 驱动程序提供的平台操作或使用 Linux SPI API 来执行传输.SPI 及其驱动程序只是在此处理器和目标设备之间传输数据的管道.与 SATA 和 PCI 一样,用户很少意识到这些将外围设备连接到计算机的(内部)总线.

The answer depends on whether the kernel you use has a SPI char device exposed for your user program to use. But if the SPI driver is a platform driver, then a device driver for your custom SBC would have to be implemented. This target device would need a node in /dev, major and minor numbers assigned and a driver associated with those numbers. This driver would utilize the platform operations that the SPI driver provides or use the Linux SPI API to perform the transfers. The SPI and its driver are merely conduits for transferring data between this processor and the target device. LIke SATA and PCI, the user is rarely aware of these (internal) buses that connect peripheral devices to the computer.

linux/drivers/spi/spi_bcm2708.c 是平台驱动.它没有 fops 来支持/执行 open()read()write()>close() 操作.它将自己注册为 SPI 主机,因此其他(目标)驱动程序可以将 SPI API 用于其服务.

linux/drivers/spi/spi_bcm2708.c is a platform driver. It has no fops to support/perform open(), read(), write() or close() operations. It registers itself as a SPI master, so other (target) drivers can use the SPI API for its services.

IMO,您最好在 RPI 和您的自定义 SBC 之间实施 EIA/RS-232 链接.如果使用非规范(原始)传输,那么如果/当您转换/升级到 SPI 连接时,您编写的代码的 99% 可能是可重用的.没有流量控制的 3 线串行连接类似于 SPI 连接,但没有强加主/从层次结构、更简单的硬件接口和更长的电缆长度.

IMO you would be better off implementing an EIA/RS-232 link between the RPI and your custom SBC. If non-canonical (raw) transfers were used, then probably 99% of the code you write will be reusable if/when you convert/upgrade to a SPI connection. A 3-wire serial connection with no flow control is similar to a SPI connection, but with no master/slave hierarchy imposed, a simpler HW interface, and longer possible cable lengths.

请注意,您可能无法使用您装配的任何电缆实现长 SPI 距离的快速传输速率.SPI 的 10 Mbps 速率通常在具有接地层和短走线的多层板上实现.

Beware that you may not be able to achieve fast transmission rates with long SPI distances using whatever cable you rig up. The 10s of Mbps rates for SPI are typically achieved on multilayer boards with ground planes and short traces.

这篇关于如何从 C 中的串行(SPI)连接读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆