SPI 在 STM32F103ZE 中读取数据为零

发布于 2024-11-27 18:42:41 字数 804 浏览 3 评论 0原文

我使用的是STM32F103ZE 我没有正确获取 SPI 数据。 主机正在正确传输。 但在发送非零值时始终读取为零。

主配置:(MSP430)

The master configuration is correct. (I tested it.)
Master Mode, MSB First, 8-bit SPI, 
Inactive state is high, SS grounded, 1 MHz clock, no dividers

从配置(STM32F103ZE)

    Using SPI2.
    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB
    SPI_InitStructure.SPI_CRCPolynomial = 7

任何人都有答案,

谢谢 哈里

I am using STM32F103ZE
I am not getting SPI data correctly.
Master is transmitting correctly.
But always read as zero where a non zero value has been sent.

Master config: (MSP430)

The master configuration is correct. (I tested it.)
Master Mode, MSB First, 8-bit SPI, 
Inactive state is high, SS grounded, 1 MHz clock, no dividers

Slave Config (STM32F103ZE)

    Using SPI2.
    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB
    SPI_InitStructure.SPI_CRCPolynomial = 7

Anybody have an ANSWER,

Thanks
Hari

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

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

发布评论

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

评论(2

一袭白衣梦中忆 2024-12-04 18:42:42

我知道,这个问题很老了。不过,由于我最近几天也遇到了同样的问题,我会尽力为未来的读者提供答案。

以下代码适用于 STM32F407,该代码用于 STM32 发现板。从数据表中我可以看到,SPI外设与STM32F103上的相同,所以
我希望代码无需修改即可在另一个微控制器上运行。

#include "stm32f4xx.h"

[... configure the pins SCK, MISO, MOSI and NSS ...]

// Configure the SPI as: Slave, 8 bit data, clock high when idle, capture on 
// 1st edge, baud rate prescaler 2, MSB first.
SPI1->CR1 = SPI_CR1_CPOL;
// No interrupts, no DMA and Motorola frame format.
SPI1->CR2 = 0;
// Enable the peripheral.
SPI1->CR1 |= SPI_CR1_SPE;

// Wait until some data has been sent to the slave and print it.
while ((SPI1->SR & SPI_SR_RXNE) == 0);
printf("Received: %d\n", SPI1->DR);

此初始化过程与问题中发布的代码有两点不同:

  1. 不要为具有 3 条线 SCK、MISO 和 MOSI 的普通 SPI 选择双向模式。
    MISO和MOSI都是单向线。

  2. 我使用硬件从机选择管理,即未设置位SSM。这样,
    SPI 外设可以自动检测设备何时被置位(引脚
    NSS 变低)并将
    将 MOSI 位存储在移位寄存器中。当读取了足够的位(8 或
    16 取决于所选的数据格式),
    标志RXNE被设置在状态寄存器中并且可以读取传输的值
    来自寄存器DR

希望有帮助。

I know, the question is quite old. Still, since I have faced the same problem the last days, I'll try to give an answer for future readers.

The following code works on the STM32F407, which is used on the STM32 discovery board. What I can see from the datasheet, the SPI peripheral is the same as on the STM32F103, so
I expect the code to run on the other microcontroller without modifications.

#include "stm32f4xx.h"

[... configure the pins SCK, MISO, MOSI and NSS ...]

// Configure the SPI as: Slave, 8 bit data, clock high when idle, capture on 
// 1st edge, baud rate prescaler 2, MSB first.
SPI1->CR1 = SPI_CR1_CPOL;
// No interrupts, no DMA and Motorola frame format.
SPI1->CR2 = 0;
// Enable the peripheral.
SPI1->CR1 |= SPI_CR1_SPE;

// Wait until some data has been sent to the slave and print it.
while ((SPI1->SR & SPI_SR_RXNE) == 0);
printf("Received: %d\n", SPI1->DR);

Two things are different in this initialization procedure from the code posted in the question:

  1. Do not select bidirectional mode for ordinary SPI with the 3 lines SCK, MISO and MOSI.
    Both MISO and MOSI are unidirectional lines.

  2. I use hardware slave select management, i.e. the bit SSM is not set. This way, the
    SPI peripheral can automatically detect when the device has been asserted (the pin
    NSS goes low) and will
    store the MOSI bits in a shift register. When enough bits have been read (8 or
    16 depending on the selected data format),
    the flag RXNE is set in the status register and the transmitted value can be read
    from the register DR.

Hope that helps.

娇俏 2024-12-04 18:42:42

我在从数据寄存器获取 0x00 值时遇到了完全相同的问题。

就我而言,问题是 MISO 线被设置为浮动输入。改成OType_PP后就可以了。这是我的STM32F429的配置代码:

void SPI1_Config(void){
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIOE->BSRRL |= GPIO_Pin_7;

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStruct);

SPI_Cmd(SPI1, ENABLE);}

和发送功能:

uint8_t SPI1_Send(uint8_t data){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, data);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

return SPI_I2S_ReceiveData(SPI1);}

I had exactly the same problem of getting 0x00 value from data register.

In my case the problem was that MISO line was set as floating input. It works after changing it to OType_PP. Here is my configuration code for STM32F429:

void SPI1_Config(void){
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIOE->BSRRL |= GPIO_Pin_7;

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStruct);

SPI_Cmd(SPI1, ENABLE);}

And sending function:

uint8_t SPI1_Send(uint8_t data){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, data);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

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