我正在尝试在STM32F303上使用SPI初始化SD卡,当我发送CMD0命令(0x40 0x00 0x00 0x00 0x00 0x95)时,在示波器上查看MOSI传输的信号时,我看到了不同的信号,如下图所示。请注意,SPI速度为281.25kBits/s。任何帮助将不胜感激。
以下是我用来测试MOSI输出的代码部分:
以下是我用来测试MOSI输出的代码部分:
uint8_t cmd0[6]={0x40,0x00,0x00,0x00,0x00,0x95};
FATFS_CS_HIGH;
HAL_SPI_Transmit(&hspi1, (uint8_t*)0xff, 1, 10);
for(int i=0;i<10;i++)
{
HAL_SPI_Transmit(&hspi1, (uint8_t*)0xff, 1, 10);
}
FATFS_CS_LOW;
while(1){
HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd0[0], 1, 10);
HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd0[1], 1, 10);
HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd0[2], 1, 10);
HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd0[3], 1,10);
HAL_SPI_Transmit(&hspi1,(uint8_t *) cmd0[4], 1, 10);
HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd0[5], 1, 10);
}
现在我发送了正确的命令并且可以在示波器上看到。然而,我从SD卡(SanDisk Ultra 40MB/s ...16GB)没有收到0x01响应,我一直收到0xff。我正在使用以下代码:
FATFS_CS_HIGH;
for (int i = 0; i < 10; i++) {
HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
}
FATFS_CS_LOW;
HAL_SPI_Transmit(&hspi1, cmd0, 6, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
for (int i = 0; i < 10; i++) {
HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
}
HAL_SPI_Receive(&hspi1, &c, 1, 10);