1

I am using a SD card for data logging. I am using the free fatfs file system from chan and SPI to communicate with the SD card. However I ran into problems when reenserting the card. It no longer works. More specifically the f_mount() function fails in in the disk_initialize() function because it is not recognized as a SDv2 card, altough it is. The first time the function is called (when booting the STM32 controller) this function returns that the card is a SDv2, after taking out the sd card, reinserting and then mounting it again it fails to detect the card.

I have tried to umount it before using f_unmount(), also I cleaned the memory of the FATFs object on the stask.

Before each write I enable powersupply to the sd card and disable it after finish of writing. This works also perfectly fine. as long as I do not reinsert the card. it seems so strange. I also tryied to reinit the SPI before each f_mount. Also does not help.

Following code works fine , but after reinserton of sd card it works no longer:

HAL_SPI_DeInit(&hspi1);

HAL_GPIO_WritePin(__SD_EN_GPIO_Port, __SD_EN_Pin, GPIO_PIN_RESET);
osDelay(10);
while(LightisIniting() == true);

/* reset physical driver */
memset(&SDFatFs, 0, sizeof(FATFS));
f_unmount("0");
osDelay(1);

/* check filenumber */
if ((errorcode = SD_Read_Filenumber()) != NO_ERROR) {
    return errorcode;
}
if ((errorcode = SD_Write_Filenumber()) != NO_ERROR) {
    return errorcode;
}


static uint8_t SD_Read_Filenumber()
{
    char init_data[20];
    uint8_t new_file_flag = 0;
    uint8_t errorcode = 0;
    uint16_t bytesread;
    char * pEnd;
 
    errorcode = SD_InitFATFS();
    if (errorcode == NO_ERROR) errorcode = SD_File_Open(&SDFileInit, "DATA.INI", FA_OPEN_ALWAYS | FA_READ);
    if (errorcode == NO_ERROR) errorcode = SD_File_Read(&SDFileInit, init_data, 20, &bytesread);
    if (errorcode == ERROR_SD_READ_DATA_LEN)
    {
        filenumber = 0; // empty (new) data.ini-file, so set filenumber to 0
        new_file_flag = 1;
        errorcode = NO_ERROR;
    }
    else
    {
        if (errorcode == 0) filenumber = strtol(init_data, &pEnd, 10);
    }
    if (errorcode == NO_ERROR) errorcode = f_close(&SDFileInit);
    if ((errorcode == NO_ERROR) && (new_file_flag)) errorcode = SD_Write_Filenumber();
    if (errorcode > 0) filenumber = 0;
    return errorcode;
}
 
static uint8_t SD_Write_Filenumber()
{
    char init_data[20];
    uint8_t errorcode = 0;
 
    errorcode = SD_InitFATFS();
    snprintf(init_data, 10, "%u", filenumber);
    if (errorcode == NO_ERROR) errorcode = SD_File_Open(&SDFileInit, "DATA.INI", FA_CREATE_ALWAYS | FA_WRITE);
    if (errorcode == NO_ERROR) errorcode = SD_File_Write(&SDFileInit, init_data, strlen(init_data));
    if (errorcode == NO_ERROR) errorcode = f_close(&SDFileInit);
    return errorcode;
}
 
int SD_Initialize() {
    uint8_t errorcode = 0;
 
    HAL_GPIO_WritePin(__SD_EN_GPIO_Port, __SD_EN_Pin, GPIO_PIN_RESET);
    osDelay(1);
    while(LightisIniting() == true);
 
    /* check filenumber */
    if ((errorcode = SD_Read_Filenumber()) == NO_ERROR) {
        filenumber++;
        if ((errorcode = SD_Write_Filenumber()) != NO_ERROR) {
            return errorcode;
        }
    }
    else {
        return errorcode;
    }
    return 0;
}
  • OK found the error. The problem was in the hardware. The MISO (=DO) pin of the SD card needs to be pulled up to +3.3V or the init process can fail . This solved it. So nothing to do with the software – Benjamin Graef Jul 14 '21 at 10:46

0 Answers0