调试一个芯片与STM32的SPI接口时,遇到一个十分奇怪的问题。发送数据应该正常,接收数据出错。我在接收时读取的是寄存器内的数值,读出来的数据总是发生移位。比如寄存器默认值05E00000,我读出来的时候就变成了02F00000。SPI配置部分代码如下:
GPIOB:
/* Enable GPIO clock for SPI_MASTER */
RCC_APB2PeriphClockCmd(ADAS1000_SPI_GPIO_CLK | ADAS_SPI_GPIO_AF_CLK, ENABLE);
/* Configure SPI_MASTER pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = ADAS_SPI_SCK_PIN | ADAS_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(ADAS1000_SPI_GPIO_PORT, &GPIO_InitStructure);
/* Configure SPI_MASTER pins: MISO */
GPIO_InitStructure.GPIO_Pin = ADAS1000_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(ADAS1000_SPI_GPIO_PORT, &GPIO_InitStructure);
/* Configure SPI_MASTER pins NSS(PB.12) as GPIO */
GPIO_InitStructure.GPIO_Pin = ADAS1000_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADAS1000_SPI_GPIO_PORT, &GPIO_InitStructure);
SPI:
/* Enable SPI_MASTER clock */
RCC_APB1PeriphClockCmd(ADAS1000_SPI_CLK, ENABLE);
/* Configure SPI_MASTER */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;// Clock is high when idle;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;// Data is shifted out on SDO on the falling edges of SCLK and sampled on the rising edge.
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //ADAS CS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(ADAS1000_SPI, &SPI_InitStructure);
SPI写入:
/* Set the Chip select pin to low to begin transmission. */
ADAS_SPI_CS_LOW;
for(i = 0; i < BytesNumber; i++)
{
/* Wait for SPI_MASTER Tx buffer empty */
while (SPI_I2S_GetFlagStatus(ADAS1000_SPI, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI_MASTER data */
SPI_I2S_SendData(ADAS1000_SPI, data[i]);
/* Make sure the byte was writted correctly */
/* Wait for SPI_MASTER Rx buffer empty */
while (SPI_I2S_GetFlagStatus(ADAS1000_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI_MASTER data */
SPI_I2S_ReceiveData(ADAS1000_SPI);
}
/* Set the Chip select pin to high to terminate transmission. */
ADAS_SPI_CS_HIGH;
SPI读取:
/* Set the Chip select pin to low to begin transmission. */
ADAS1000_SPI_CS_LOW;
for(i = 0; i < BytesNumber; i++)
{
/* Wait for SPI_MASTER Tx buffer empty */
while (SPI_I2S_GetFlagStatus(ADAS1000_SPI, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI_MASTER data */
SPI_I2S_SendData(ADAS1000_SPI, 0x00);
/* Wait for SPI_MASTER Tx buffer empty */
while (SPI_I2S_GetFlagStatus(ADAS1000_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI_MASTER data */
data[i] = (u8)SPI_I2S_ReceiveData(ADAS1000_SPI);
}
/* Set the Chip select pin to high to terminate transmission. */
ADAS1000_SPI_CS_HIGH;
我觉得各方面的设置应该都没有问题了,可是为什么读取还是出错呢?请高手指点一下。一般来说,这是由于主机和从机CPHA设置为一样导致的。但是,如果我把设置改为SPI_CPHA_2Edge,则根本无法读出数据。被弄晕头了,请坛子的大侠指点。 |