I try to read a file from SD Card using SPI and FatFS and I have a problem with ACMD41. Previously, the CMD0 and CMD8 give me correct response and I assume that I have a new v2 type. When I send ACMD41 first time I get 0x01 response for both CMD55 and CMD41, so I send it the next time and I don't receive on the CMD55 other response than 0xff in 16 CLK cycles, so the initialization ends up returning an error. I have tried to write my own files to initialize card and used ready library which I have found on the Internet. I also think that there's no problem with SD card because I have tried four of them.
Below is the code, maybe sb will be able to find the solution or give me some advice or link to proven source..
/*
* sd_spi.c
*
* Created on: 26.11.2017
* Author: jaras
*/
#include "sd_spi.h"
#include <string.h>
uint8_t SDSPI_SendCMD(SPI_HandleTypeDef *phandle, uint8_t cmd, uint32_t arg, uint8_t crc) {
uint8_t buf[6];
buf[0] = cmd | 0x40;
buf[1] = (arg >> 24) & 0xff;
buf[2] = (arg >> 16) & 0xff;
buf[3] = (arg >> 8) & 0xff;
buf[4] = arg & 0xff;
buf[5] = crc;
if(HAL_SPI_Transmit(phandle, buf, 6, 1000) != HAL_OK) {
return 1;
}
return 0;
}
uint8_t SDSPI_Response(SPI_HandleTypeDef *phandle, uint8_t *buf, uint16_t size) {
uint8_t tx = 0xff;
uint8_t rx = 0xff;
uint8_t i = 0;
while(rx == 0xff) {
if(HAL_SPI_TransmitReceive(phandle, &tx, &rx, 1, 1000) != HAL_OK) {
return 1;
}
i++;
if(i > 8) {
return 2;
}
}
*buf = rx;
for(uint16_t k = 1; k < size; k++) {
if(HAL_SPI_TransmitReceive(phandle, &tx, &rx, 1, 1000) != HAL_OK) {
return 1;
}
*(buf + k) = rx;
}
return 0;
}
uint8_t SDSPI_CMD(SPI_HandleTypeDef *phandle, uint8_t cmd, uint32_t arg, uint8_t crc,
uint8_t *response, uint8_t size) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_RESET);
uint8_t res = SDSPI_SendCMD(phandle, cmd, arg, crc);
if(res > 0) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
return 1;
}
res = SDSPI_Response(phandle, response, size);
if(res > 0) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
return 2;
}
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
uint8_t tx = 0xff;
if(HAL_SPI_Transmit(phandle, &tx, 1, 1000) != HAL_OK) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
return 3;
}
return 0;
}
uint8_t SDSPI_ACMD(SPI_HandleTypeDef *phandle, uint8_t cmd, uint32_t arg, uint8_t crc,
uint8_t *response, uint8_t size) {
uint8_t value0;
uint8_t rx = 0;
uint8_t res = SDSPI_CMD(phandle, 55, 0, 0x65, &rx, 1);
value0=rx;
if(res > 0) {
return 1;
}
if((rx & 0xf4) > 0) {
return 2;
}
res = SDSPI_CMD(phandle, cmd, arg, crc, response, size);
if(res > 0) {
return 3;
}
return 0;
}
uint8_t SDSPI_Init(SPI_HandleTypeDef *phandle) {
uint8_t value0;
uint8_t value1;
uint8_t value2;
uint8_t value3;
uint8_t value4;
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
HAL_Delay(10);
uint8_t buf[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
if(HAL_SPI_Transmit(phandle, buf, 10, 1000) != HAL_OK) {
return 1; //spi error
}
uint8_t res = SDSPI_CMD(phandle, 0, 0, 0x95, buf, 1);
value0 = buf[0];
if(res > 0) {
return 1; //command error
}
if(buf[0] != 1) {
return 2; //not initialized
}
uint8_t type = 0;
uint8_t block = 0;
res = SDSPI_CMD(phandle, 8, 0x01aa, 0x87, buf, 5);
value0 = buf[0];
value1 = buf[1];
value2 = buf[2];
value3 = buf[3];
value4 = buf[4];
if(res > 0) {
type = 1;
}
if(buf[0] != 1) {
type = 1;
}
if((buf[3] & 0x0f) != 1 || buf[4] != 0xaa) {
return 3; //initialization error
}
uint8_t stat = 0;
uint32_t tickstart = 0;
if(type == 0) {
stat = 1;
tickstart = HAL_GetTick();
while(stat > 0) {
if((HAL_GetTick()-tickstart) >= 1000) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
return 4; //timeout
}
res = SDSPI_ACMD(phandle, 41, 0x40000000, 0x77, &stat, 1);
if(res > 0) {
return 5; //not supported
}
}
res = SDSPI_CMD(phandle, 58, 0, 0x75, buf, 5);
if(res > 0) {
return 6; //not supported
}
if(buf[0] > 0) {
return 7;
}
if((buf[1] & 0x04) > 0) {
block = 1;
}
}
if(type == 1) {
stat = 1;
tickstart = HAL_GetTick();
while(stat > 0) {
if((HAL_GetTick()-tickstart) >= 1000) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
stat = 0;
type = 2;
}
res = SDSPI_ACMD(phandle, 41, 0, 0xff, &stat, 1);
if(res > 0) {
stat = 0;
type = 2;
}
}
}
if(type == 2) {
stat = 1;
tickstart = HAL_GetTick();
while(stat > 0) {
if((HAL_GetTick()-tickstart) >= 1000) {
HAL_GPIO_WritePin(SDSPI_CSPORT, SDSPI_CSPIN, GPIO_PIN_SET);
return 8; //timeout
}
res = SDSPI_CMD(phandle, 1, 0, 0xff, &stat, 1);
if(res > 0) {
return 9; //error
}
}
}
if(block == 0) {
res = SDSPI_CMD(phandle, 16, 512, 0xff, buf, 1);
if(res > 0) {
return 10; //not supported
}
if(buf[0] > 0) {
return 11; //error
}
}
return 0;
}
/*
* sd_spi.h
*
* Created on: 26.11.2017
* Author: jaras
*/
#ifndef SD_SPI_H_
#define SD_SPI_H_
#include "stm32f3xx_hal.h"
#include "gpio.h"
#define SDSPI_CSPORT CS_PIN_GPIO_Port
#define SDSPI_CSPIN CS_PIN_Pin
uint8_t SDSPI_Init(SPI_HandleTypeDef *phandle);
uint8_t SDSPI_ReadInfo(SPI_HandleTypeDef *phandle, uint16_t *sector, uint32_t *capacity);
uint8_t SDSPI_ReadBlock(SPI_HandleTypeDef *phandle, uint32_t lba, uint8_t *buf, uint16_t size);
uint8_t SDSPI_WriteBlock(SPI_HandleTypeDef *phandle, uint32_t lba, uint8_t *buf, uint16_t size);
#endif /* SD_SPI_H_ */
The problem was at SD Card, it hasn"t initialized both on SPI and SDIO witg ready FatFS library and SDIO files generated by CubeMX. I've bought a new card and now it works fine on SDIO. I haven"t checked it on SPI yet, but I suspect there is problem with formatting