SPI 无法读取前 6 个字节 [英] SPI fails to read first 6 bytes

查看:42
本文介绍了SPI 无法读取前 6 个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 STM32F051 MCU 上的 SPI 模块有很多问题.我已将其配置为主设备以驱动从属闪存模块(这并不重要).

I'm having a lot of issues with SPI module on my STM32F051 MCU. I've got it configured as a master to drive a slave flash memory module (that doesn't really matter).

我正在尝试从内存中读取 8 个字节,这是读取数据"消息的结构:

I'm trying to read 8 bytes from memory, this is how the 'read data' message is structured:

消息的前 4 个字节被发送,接下来的 8 个字节被接收.第一个字节是读取数据"操作码,后面三个是数据地址,在这种情况下等于 0.

First 4 bytes of the message are transmitted, next 8 are received. First byte is 'read data' opcode, three following are data address and equal 0 in this case.

代码:

memset(out, 0x00, 256);
memset(in, 0x00, 256);
out[0] = OPCODE_READ;
out[1] = 0x00;
out[2] = 0x00;
out[3] = 0x00;
uint32_t len = 4 + size;  // size == 8

spi_select(M25P80);
HAL_SPI_TransmitReceive(&hspi1, out, in, len, TIMEOUT);
delay_ms(BYTE_SPEED_MS * 5); // Needed because ^ finishes before physically 
                             // transmitting the data. Nevermind the 5, it 
                             // was picked experimentally 
spi_deselect(M25P80);

信号(黄色 - 时钟,红色 - 味噌):

Signal (yellow - clock, red - miso):

在 488 bits/s 传输 4 个字节需要 4 * 1E3/(488/8) = 65.5 ms.然后接待开始.内存立即开始传输 [0xFF...0xFF],但 'in' 缓冲区的内容是:

At 488 bits/s transmitting 4 bytes takes 4 * 1E3 / (488 / 8) = 65.5 ms. Then the reception starts. Memory starts transmitting [0xFF...0xFF] right away, but contents of the 'in' buffer are:

[0x00 0x00 0x00 0x00] [0x00 0x00 0x00 0x00 0x00] 0xFF 0xFF 0x00...0x00
^ zero because this   ^ should be 0xFF           ^ correct data
  is the part where
  data was being sent
  to the memory

所以前六个字节的数据只是丢失.我是唯一一个对 STM 的 SPI 模块有如此困难的人吗?

So first six bytes of data are just lost. Am I the only one who's having such a hard time with STM's SPI module?

我给自己买了一个不同的评估板,它的 MCU (STM32F030) 略有不同,但它变得更奇怪了:

I've gotten myself a different eval board with a slightly different MCU (STM32F030) and it gets even weirder:

[0x02 0x02 0x02 0x02] 
0x00 0x02 0x00 0x00 0xFF 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x00...0x00

尽管我必须提到我在这个 MCU 上使用了不同的编译器.

Although I must mention that I'm using a different compiler with this MCU.

编辑 2:

我部分让它工作的方式是使用 16 位模式和 SPI.这修复了这个特定的错误,但 STM32 的 SPI 有更多类似的奇怪之处.

The way I partially got it to work is using 16-bit mode with SPI. This fixed this particular bug, but there are more similar oddities with STM32's SPI.

编辑 3:

SPI 初始化代码:

void MX_SPI1_Init(void)
{

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
  HAL_SPI_Init(&hspi1);

}

推荐答案

你确定 SPI 的初始化是对的?也许您的时钟极性或相位设置在主从之间不匹配?观看时钟设置.

Are you sure that the initialization of SPI is right? Maybe your Clock polarity or phase settings does not match between Master and Slave? Take a watch to ClockSettings.

请出示您的 SPI 初始化代码!

Please show your SPI-Initialization-Code!

这篇关于SPI 无法读取前 6 个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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