微控制器到微控制器 SPI 通信

发布于 2024-09-03 08:17:54 字数 294 浏览 2 评论 0原文

我正在阅读一些内容,甚至在我的微控制器上找到了一个“主”SPI。这是我的问题,基本上,如果主设备想要初始化对从设备的写入,我们写入 SSPBUF,我们如何控制从设备的响应内容?在我看来,数据表似乎不太清楚这种情况下的事件顺序。

IE主机向SSPBUF中放入一个字符,这会启动SPI模块向从机发送数据,在移位过程中,从机返回一个字节。

在从机端,是否有东西告诉您有传入数据,您可以先写入 SSPBUF,然后接受数据?

或者,

在主机有机会发起传输之前,您是否必须将想要发回的第一个“返回值”写入 SSPBUF?

I was doing some reading and have even gotten a "master" SPI working on my microcontroller. Here is my question, basically if the master wants to initialize a write to the slave we write to the SSPBUF, how do we control what the slave responds with? The datasheet doesn't seem really clear to me the order of events in that case.

I.E. Master puts a char into the SSPBUF, this initiates the SPI module to send data to the slave, during the shift, the slave returns a byte.

In the slave side, is there something that tells you you have incoming data, and you can write to your SSPBUF first, THEN accept the data?

OR

Do you have to write to the SSPBUF the first "return value" you want sent back before the master can have an opportunity to initiate a transfer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

养猫人 2024-09-10 08:17:54

您想要在 GPIO 线上使用中断来处理从设备的片选。确保从机有足够的时间来处理该中断并在时钟启动之前加载传出数据寄存器。

某些 SPI 模块不允许使用 GPIO - 您需要检查 SPI 模块是否具有处理芯片选择的功能,或者仅根据 GPIO 活动启用模块。

You want to use an interrupt on the GPIO line which handles your slave's chip-select. Ensure there is enough time for the slave to process this interrupt and load the outgoing data register before the clock starts.

Some SPI modules preclude the use of GPIO - you will need to check if your SPI module has a function to handle chip select, or only enable the module based on the GPIO activity.

坦然微笑 2024-09-10 08:17:54

通常我过去使用 SPI 所做的就是从主机向从机发送 2 个字节,其间的延迟最小。主站发送:“X Y”,其中“X”是它希望从从站读取的变量,“Y”实际上只是一个虚拟变量,用于从从站输出响应。同时,当从机收到“X”时,它会获得一个中断,查找要放入其输出缓冲区中的值,当它收到“Y”时,对其数据包的响应将被计时到主机。

Typically what i have done in the past with SPI is I send 2 bytes from the master to the slave with a minimal delay in between. The master sends: "X Y" where "X" is the variable it wishes to read from the slave and "Y" is really just a dummy variable which is used to clock out the response from the slave. At the same time, the slave gets an interrupt when it recieves "X", looks up what value to put in its output buffer, and when it receives "Y", the response to its packet is clocked out to the master.

软甜啾 2024-09-10 08:17:54

无论您的微控制器是什么,三个可能是与接收 SPI 数据相关的 ISR,以及一个可以访问已接收数据并将其复制到局部变量中的寄存器。

Whatever your microcontroller is, three is likely to be an ISR associated with receipt of SPI data, and a register where the data that has been received can be accessed and copied into a local variable.

浅听莫相离 2024-09-10 08:17:54

首先您需要确认您的主机和从机具有共同的参数值,例如SPI模式和数据大小。 SPI 模式将决定 SPI 时钟线的空闲极性和数据采样沿是第一还是第二。数据大小将表明它是 8 位还是 16 位,或者根据芯片的不同可能有所不同。

现在,当主机通过芯片/从机选择引脚选择从机时,从机变为活动状态并等待时钟更改其状态,一旦时钟更改状态,从机将再次等待时钟线,以获得第一个或第二个边沿,具体取决于 SPI 模式选择。如果是第一个边沿从机采样 MOSI 线数据并放入其内部移位寄存器,如果从机还必须传输数据,则它必须在所选时钟边沿之前将数据保留在 MISO 线上。
根据数据大小,此过程将重复 8 或 16 次。完成后,主设备和从设备可以向其 CPU 生成中断以收集数据(如果是微控制器)。

First you need to confirm that your master and slave have common value of the parameters such as SPI mode and data size. SPI mode will decide the idle polarity of SPI clock line and the data sampling edge whether it is going to be first or second. Data size will tell whether it is 8-bit or 16-bit or may be something different depending upon chip.

Now, when master selects a slave through chip/slave select pin slave becomes active and waits for clock to change it's state, once clock changes state slave waits for clock line again for 1st or 2nd edge depending upon SPI mode selection. If it is 1st edge slave samples MOSI line data and puts into it's internal shift register, if slave has to transmit data also, it has to keep data on MISO line before the selected clock edge.
This process will be repeated till 8 or 16 times depending upon data size. Upon completion, master and slave can generate interrupt to their CPU to collect the data (in case of microcontroller).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文