STM32F1 - 在裸机上使用主 SPI [英] STM32F1 - Using master SPI on bare metal

查看:25
本文介绍了STM32F1 - 在裸机上使用主 SPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试移植我的一些 AVR 代码来驱动一个简单的 SPI LCD 到 ARM 作为学习练习(我对 ARM 很陌生).为此,我只需要在主模式下使用 SPI.

I've been trying to port some of my AVR code to drive a simple SPI LCD to ARM as a learning exercise (I'm very new to ARM in general). For this I just need to use SPI in master mode.

我查看了我的设备 (STM32F103C8) 的数据表并发现我需要的 SPI1 引脚、SCK 和 MOSI 分别映射为 PA5 和 PA7 的替代功能以及其他外设(第 29 页).我的理解是,为了在这些引脚上使用 SPI 功能,我需要确保禁用映射到同一引脚的任何其他内容.然而,当查看外设时钟控制寄存器的默认值时,其他功能似乎已被禁用.

I looked in the datasheet for my device (STM32F103C8) and found that the SPI1 pins I need, SCK and MOSI are mapped as alternative functions of PA5 and PA7, respectively, along with other peripherals (pg.29). My understanding is that in order to use the SPI function on these pins, I need to make sure that anything else mapped to the same pin is disabled. When looking at the defaults for the peripheral clock control register, however, it looks like the other features are already disabled.

我查看了 参考手册,包括部分25.3.3 - 配置SPI 处于主模式.首先,我在 APB2ENR 中启用了 SPI1 主时钟,然后按照本节中的步骤根据我的需要配置 SPI1.我还更改了 PA5/7 的设置,将它们的模式设置为Alternate Function Output push-pull"(9.1.4).最后,我通过设置 CR1_SPE 启用了 SPI1.

I looked at the SPI section in the reference manual, including section 25.3.3 - Configuring the SPI in master mode. First I enabled the SPI1 master clock in APB2ENR and followed the steps in this section to configure SPI1 to my needs. I also changed the settings for PA5/7 to set their mode to "Alternate Function Output push-pull" (9.1.4). Finally, I enabled SPI1 by setting CR1_SPE.

从我的阅读中,我认为通过在如上配置SPI后将值加载到SPI1数据寄存器中,数据会被移出.但是,写入数据后,SPI状态寄存器中的TXE标志永远不会置位,表明我写入的数据只是坐在那里.

From my reading, I had thought that by loading a value into the SPI1 data register after configuring SPI as above, the data would be shifted out. However, after writing the data, the TXE flag in the SPI status register never becomes set, indicating that the data I wrote into it is just sat there.

在这一点上,我假设还有一些我没有正确配置的东西.例如,我不能 100% 确定如何处理 PA5/7 引脚.我试图从数据表中了解我能做些什么,但我一无所获.在它起作用之前还有什么需要做的吗?

At this point, I'm assuming that there is something else I've failed to configure correctly. For example, I'm not 100% sure about what to do with the PA5/7 pins. I've tried to understand what I can from the datasheets, but I'm not getting anywhere. Is there anything else that needs to be done before it'll work?

推荐答案

我几乎可以肯定你没有在 SPIx 中设置 SSMSSI 位->CR1 寄存器.这些芯片中的SPI非常简单,对于轮询传输,您需要设置SSMSSISPEMSTR>、SPIx 中正确的格式(LSBFIRSTCPOLCPHA)和正确的波特率(BR)->CR1,你可以开始了.

I'm almost certain that you did not set SSM and SSI bits in SPIx->CR1 register. SPI in these chips is pretty simple, for the polled transfers you need to set SSM, SSI, SPE, MSTR, correct format (LSBFIRST, CPOL, CPHA) and proper baudrate (BR) in SPIx->CR1 and you're good to go.

这篇关于STM32F1 - 在裸机上使用主 SPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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