接收时 STM32F303 SPI 中的奇怪数据字节顺序

问题描述 投票:0回答:0

我无法理解当我试图通过 STM32F303 读取 SPI 数据时发生的事情。在我的示波器上,我看到下一个序列:0x9c、0、0、0。但我以不同的顺序接收它们。 这是我的阅读功能:

volatile uint32_t wctr;
#define WAITX(x)  do{wctr = 0; while((x) && (++wctr < 360000)) IWDG->KR = IWDG_REFRESH; if(wctr==360000){ DBG("timeout"); return 0;}}while(0)

int spi_read(uint8_t *data, uint32_t n){
    if(spi_status != SPI_READY){
        DBG("not ready");
        return 0;
    }
    if(!spi_waitbsy()) return 0;
    // clear SPI Rx FIFO
    (void) SPI2->DR;
    while(SPI2->SR & SPI_SR_RXNE) (void) SPI2->DR;
    for(uint32_t x = 0; x < n; ++x){
        WAITX(!(SPI2->SR & SPI_SR_TXE));
        SPIDR = 0;
        WAITX(!(SPI2->SR & SPI_SR_RXNE));
        data[x] = SPIDR;
        USB_sendstr("rd got "); USB_sendstr(uhex2str(data[x]));
        newline();
    }
    return 1;
}

在终端我得到:

rd got 0x00
rd got 0x00
rd got 0x00
rd got 0x9c
Sregx10=0x9c000000
rd got 0x00
rd got 0x00
rd got 0x9c
rd got 0x00
Sregx10=0x9c0000
rd got 0x00
rd got 0x9c
rd got 0x00
rd got 0x00
Sregx10=0x9c00
rd got 0x9c
rd got 0x00
rd got 0x00
rd got 0x00
Sregx10=0x9c
rd got 0x00
rd got 0x00
rd got 0x00
rd got 0x9c
Sregx10=0x9c000000

Rx FIFO 中似乎有些垃圾,清理也无济于事。 当我尝试通过 DMA 接收数据时,我遇到了同样的问题。为什么总是在第一次读取时我在最后一个地方有第一个数据字节?

这里是使用 DMA 的接收:

Sregn9=8
00 00 00 fa 29 82 00 00
Sregn9=8
00 00 fa 29 82 00 00 00
Sregn9=8
00 fa 29 82 00 00 00 00
Sregn9=8
fa 29 82 00 00 00 00 00

正确的数据顺序是 0xfa、0x29、0x82、0(正如我在示波器上看到的那样)。但是STM32每次都以不同的方式接收数据,在实际数据之前有3、2、1或0个零。

这是这个特定 MCU 的错误,还是我的代码有问题?我怎样才能在主数据之前省略这个可变数量的零?

stm32 spi fifo
© www.soinside.com 2019 - 2024. All rights reserved.