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

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

问题描述

我想写,将在Linux MCU(树莓派),将读取的串行数据来从另一个MCU(自产自销的东西,我会自己建)安装的程序。

我已经研究如何做到这一点,并认为我有大画面,但还缺少一些东西。首先,我需要启用内核模块,并给自己访问设备:

 须藤modprobe的spi_bcm2708
须藤CHOWN`ID -u`.`id -g` /dev/spidev0.*

从那里我可以使用<一个href=\"http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob_plain;f=Documentation/spi/spidev_test.c\"相对=nofollow>这个著名的C文件测试SPI连接。

所以,现在的连接是存在的,但我仍然需要从中读出的串行数据。根据答案<一href=\"http://stackoverflow.com/questions/8600275/how-do-i-read-data-from-serial-port-in-linux-using-c\">this SO质疑,我只需要:


  

...打开阅读相关端口,并使用阅读()来获取数据。


然而,在这个问题的答案在code片段似乎通过设备驱动程序开放的端口,而不是通过SPI。

我的了解的是,我可以使用SPI读取所连接的MCU设备的数据,而且我会的的需要写我自己的设备驱动程序的设备。这是真的?如果是这样,我如何从一个SPI连接读书,怎么会是code比在回答上面链接的问题code有何不同?

如果我的理解是不正确的,我也穿越的设备驱动程序读取,为什么我需要摆在首位的驱动程序?这不就是SPI希望什么规避?先谢谢了。


解决方案

  

我想写,将在Linux MCU(树莓派),将读取的串行数据来从另一个MCU(自产自销的东西,我会自己建)安装的程序。


[ MCU 的不正确的长期使用。树莓PI(片上系统)使用的SoC。微控制器将是一个更便宜和放大器;不是一个简单的SoC器件。如果你想使用TLA,然后用单板机,单板计算机。]

[你是滥用术语串口和串行数据。今天,由于电脑的普及的串口已经到了专指EIA / RS-232异步串行连接。 SPI。 USB。 I2C,TWI,SATA的等等的不应该被称为串行连接,除非你解释他们是如何工作的。]

在Linux上的SPI设备驱动程序通常作为*平台的驱动程序,而不是一个的字符驱动程序的,因此这样的驱动程序不会有文件操作,或 FOPS 的,执行的的open()阅读()的write()的close()的。这样的操作是为目标设备,其中所述平台设备连接到系统。因此,平台设备没有像目标设备在的/ dev 设备节点做的。SPI处于同一类别作为USB和PCI;他们都是公交车,通常为平台的驱动程序实现


  

我的理解是,我可以使用SPI从连接MCU器件读取数据,而我不需要写我自己的设备驱动程序的设备。这是真的吗?


问题的答案取决于你使用的内核是否暴露为用户程序使用的SPI字符设备。但如果SPI驱动是一个平台驱动程序,然后为定制的SBC的设备驱动器将不得不被实现。该目标设备将需要在的/ dev 节点,指定主次编号,并与这些数字相关的驱动程序。此驱动程序将利用该SPI驱动提供平台操作或使用Linux SPI API来进行传输。所述SPI和它的驱动仅是该处理器和目标设备之间的数据传输导管。像SATA和PCI,用户很少觉察到外围设备连接到计算机上,这些(内部)总线。

的Linux /司机/ SPI / spi_bcm2708.c 是一个平台的驱动程序。它没有的 FOPS 的支持/执行的的open()阅读()的write()关闭()的操作。它自己注册为SPI主机,所以其他的(目标)驱动程序可以使用的SPI API为其服务。

IMO你会过得更好实施RPI和您的自定义SBC之间的EIA / RS-232连接。如果使用了不规范的(原始)传输,那么很可能在99 $ C $%的C你写的将是可重复使用的,如果/当你转换/升级到SPI连接。无流控制的3线串行连接,类似于一个SPI连接,但没有实行主/从层次结构,一个简单的硬件接口,并且不再可能的电缆长度。

当心,你可能无法使用任何连接线,你搭起实现与长SPI距离快速的传输速率。 Mbps的速率为SPI的10S通常在多层电路板与地面飞机和短痕迹实现。

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.*

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

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:

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

However the code snippet in that answer seems to be opening up a port through a device driver, not via 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?

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.

解决方案

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 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.]

[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.]

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.

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?

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 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 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.

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天全站免登陆