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

查看:142
本文介绍了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.

我查看了

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->CR1寄存器中设置SSMSSI位.这些芯片中的SPI非常简单,对于轮询传输,您需要设置SSMSSISPEMSTR,正确的格式(LSBFIRSTCPOLCPHA)和适当的波特率(SPIx->CR1)中的(BR),您一切顺利.

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