2017-05-03 07:54 AM
Hello everyone,
I have never used SD card in my previous project. Today I change it. I use STM32L053 MCU and CubeMX. SPI is configured in the following way:
I have added FATfs(from MiddeWare). SD card has 2 GB capacity and it is SD type(not SDHC). It is connected to evalboard by sd adapter(like arduino). I have written power_on function, which you can see below. This function is called before infinite loop. I have expected 0x01 response but was 0xFF(I checked it by Logic analyzer). I don't know where is problem.static void SELECT(void)
{
HAL_GPIO_WritePin(GPIOB, CS_Pin, GPIO_PIN_RESET);
}
static void DESELECT(void)
{
HAL_GPIO_WritePin(GPIOB, CS_Pin, GPIO_PIN_SET);
}
static void xmit_spi(BYTE Data)
{
while (HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
HAL_SPI_Transmit(&hspi2, &Data, 1, 100);
}
static BYTE rcvr_spi(void)
{
unsigned char Dummy, Data;
Dummy = 0xFF;
Data = 0;
while ((HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY));
HAL_SPI_TransmitReceive(&hspi2, &Dummy, &Data, 1, 5000);
return Data;
}
void power_on(void)
{
unsigned char i, cmd_arg[6];
unsigned int Count = 0x10;
BYTE spi_status = 0;
DESELECT();
for(i = 0; i < 10; i++)
{
xmit_spi(0xFF);
}
SELECT();
cmd_arg[0] = (CMD0 | 0x40);
cmd_arg[1] = 0;
cmd_arg[2] = 0;
cmd_arg[3] = 0;
cmd_arg[4] = 0;
cmd_arg[5] = 0x95;
for (i = 0; i < 6; i++)
{
xmit_spi(cmd_arg[i]);
}
while ((rcvr_spi() != 0x01) && Count)
{
Count--;
}
DESELECT();
xmit_spi(0XFF);
PowerFlag = 1;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
EDIT: I could not formatted code better. Sorry.
Someone can help me?
Thank you in advance.
Best Regards.
#sd-card #fat #stml0