cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L431 SDMMC not working after STOP2

AlfRomeo
Associate III

Originally posted as a reply in this old thread:

https://community.st.com/t5/stm32-mcus-products/using-sdmmc-sdio-and-fatfs-for-reading-sd-card-for-stm32l471vgt6/m-p/690770

 

How did you solve it in the end? I also encountered this problem

1 ACCEPTED SOLUTION

Accepted Solutions

@AlfRome wrote:
This mode wakes up in LPTIM interrupt, and after waking up, SystemClock_Config() is called again

Did you read the Solution in the other thread?

He said that SystemClock_Config() was not sufficient - he also had to call PeriphCommonClock_Config() after exiting the sleep mode:

https://community.st.com/t5/stm32-mcus-products/using-sdmmc-sdio-and-fatfs-for-reading-sd-card-for-stm32l471vgt6/m-p/690770/highlight/true#M253528

 

 

 

View solution in original post

6 REPLIES 6
AlfRomeo
Associate III

Isn't the HAL library I am using only SystemClock_Cnfig() for configuring clocks?


@AlfRomeo wrote:

How did you solve it in the end? 


He described it in his post immediately before yours - the one marked as The Solution:

https://community.st.com/t5/stm32-mcus-products/using-sdmmc-sdio-and-fatfs-for-reading-sd-card-for-stm32l471vgt6/m-p/690770/highlight/true#M253528

 


@AlfRomeo wrote:

Isn't the HAL library I am using only SystemClock_Cnfig() for configuring clocks?


We have no idea what you are using!

Probably best if you start a new thread of your own, and provide a full description of your system (code, schematics, your problem symptoms, what debugging you've done so far, etc) there:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

You can always provide a link back to this thread for reference, if required.

#define _SD_POW_ON HAL_GPIO_WritePin(GPIOB, SD_EN_Pin, GPIO_PIN_RESET)
#define _SD_POW_OFF HAL_GPIO_WritePin(GPIOB, SD_EN_Pin, GPIO_PIN_SET)

#define _delSD(en)                     \
    do                                 \
    {                                  \
        f_mount(NULL, "0:", (en & 1)); \
        closeSD();                    \
        sdInfo.busy = _IDLE;           \
    } while (0)


int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_LPTIM1_Init();
    MX_USART3_UART_Init();
    MX_RTC_Init();
    MX_SPI1_Init();
    MX_SDMMC1_SD_Init();
    MX_FATFS_Init();
    _VDP_ON; 
    _SD_POW_ON; // SD POWER OPEN
    HAL_Delay(100);
    fm25v_init(); // FM25V Init
    // fm25v_SafeInit();
    scanSD(); // Get SD card capacity
    closeSD();//close SD
    
    __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hrtc, RTC_IT_WUT);
    while (1)
    {
        sd_test();
        EnterStop2ModeLPTIM();
    }
}


void sd_test(void)
{
    openSD();
    FRESULT ret;
    FIL *sdFile = NULL;
    BYTE *buffer = NULL;
    UINT bytesWritten = 0;
    sdFile = (FIL *)malloc_mem(sizeof(FIL));
    if (sdFile == NULL)
    {
        return;
    }
    BYTE writeBuffer[] = "\r\n\r\nhello world!\r\nasdfgdsagdfasgdsa\r\n";
    ret = f_mount(&SDFatFS, "0:", 0);
    if (ret != FR_OK && ret != FR_NO_FILESYSTEM)
    {
        printf("f_mount error! ret: %d\r\n", ret);
        free_mem(sdFile);
        return;
    }
    else if (ret == FR_NO_FILESYSTEM)
    {
        printf("FATFS file system not detected, perform formatting...\r\n");
        ret = f_mkfs("0:", 0, 4096, buffer, _MAX_SS);
        if (ret == FR_OK)
        {
            printf("Format successful!\r\n");
            f_mount(NULL, "0:", 0);
            ret = f_mount(&SDFatFS, "0:", 0);
        }
        else
        {
            printf("Format failed!\r\n");
            free_mem(sdFile);
            return;
        }
    }
    else
    {
        printf("f_mount success!\r\n");
    }
    printf("\r\n ========== write test ==========\r\n");
    ret = f_open(sdFile, "hello.txt", FA_CREATE_ALWAYS | FA_WRITE);
    if (ret == FR_OK)
    {
        printf("open file sucess!\r\n");
        ret = f_write(sdFile, writeBuffer, sizeof(writeBuffer), &bytesWritten);
        if (ret == FR_OK)
        {
            printf("write \"%s\" success!\r\nwrite len:%d\r\n", writeBuffer, bytesWritten);
            f_close(sdFile);
            _delSD(0);
        }
        else
        {
            printf("write error! ret:%d \r\n", ret);
            f_close(sdFile);
            free_mem(sdFile);
            return;
        }
    }
    else
    {
        printf("ret = %d\r\n", ret);
        printf("open file error!\r\n");
        f_close(sdFile);
        free_mem(sdFile);
        return;
    }
    free_mem(sdFile);
}

void closeSD(void)
{
    // sdSdmmcIO();
    HAL_SD_MspDeInit(&hsd1); 
    _SD_POW_OFF; 
    // sdSta = STA_NOINIT;
}

void openSD(void)
{
    // sdSdmmcInit();
    HAL_SD_MspInit(&hsd1);
    // BSP_SD_Init();
    _SD_POW_ON;
}

void EnterStop2ModeLPTIM(void) 
{
    // printf("Start to enter Stop2 mode\r\n");
    __HAL_RCC_PWR_CLK_ENABLE();
    HAL_RCCEx_WakeUpStopCLKConfig(RCC_STOP_WAKEUPCLOCK_MSI);
    HAL_LPTIM_MspInit(&hlptim1);                     
    HAL_LPTIM_TimeOut_Start_IT(&hlptim1, 65535, 65535);
    HAL_SuspendTick();
    HAL_DBGMCU_EnableDBGStopMode();
    __HAL_RCC_PWR_CLK_ENABLE();                 
    HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); 

}

void ExitStop2ModeLPTIM(void)
{
    HAL_LPTIM_MspDeInit(&hlptim1);
    SystemClock_Config();         
    HAL_ResumeTick();
    SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;
    // printf("In the EXTI to wake up!\r\n");
}

void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim) 
{
  ExitStop2ModeLPTIM();
}

This is my code, which is generated by cubemx using STM32L431 code. The logic of the code is to first call the openSD() function in while (1), reconfigure the IO port of SDMMC, and then turn on the power supply of the SD card. After the SD card is successfully written, it will call _delSD (0); SDMMC will be enabled inside, and SD card power will be turned off, then enter STOP2 mode. This mode wakes up in LPTIM interrupt, and after waking up, SystemClock_Cnfig() is called again; Then my question is why FR-DISK-ERR (1) is returned in f_open?

微信图片_20241016081043.png微信图片_20241016083129.png


@AlfRome wrote:
This mode wakes up in LPTIM interrupt, and after waking up, SystemClock_Config() is called again

Did you read the Solution in the other thread?

He said that SystemClock_Config() was not sufficient - he also had to call PeriphCommonClock_Config() after exiting the sleep mode:

https://community.st.com/t5/stm32-mcus-products/using-sdmmc-sdio-and-fatfs-for-reading-sd-card-for-stm32l471vgt6/m-p/690770/highlight/true#M253528

 

 

 

There is no PeriphCommonClock_Config() function in CUBEMX

Then you'll have to write one yourself.

As noted in the other thread:

"the sdmmc runs on a clock that doesnt get started by SystemClock_Config()"

You should be able to verify whether that is actually what's happening in your case.

Find out where that clock does get started in your code.

Make sure that also happens after you exit STOP2.