0

I use a simple code in stm32 for SD card.

The code is written in "int main(void)" section. I am trying to configure a SD card so that as soon as the STM32F750vbt6 turn on, a file is created in the SD card and a text is written in the file.

when I turn the STM32F750vbt6 on, this does not happen and nothing is written in the SD card but when the code reaches to the "while" section (I have written an LED blink code inside the while section) and I reset the micro controller using RST pin of the STM32F750vbt6, the device works properly and a text is written in the sd card.

How can I fix this? Why is there a need for resetting the device for the code to work?

this is my code :

const char wtext[] = "hi world";
      int main(void)
    {
        FRESULT res; /* FatFs function common result code */
        uint32_t byteswritten, bytesread; /* File write/read counts */
      MPU_Config();
      HAL_Init();
      SystemClock_Config();
      MX_GPIO_Init();
      MX_SDMMC1_SD_Init();
      MX_FATFS_Init();
      if(retSD == 0)
        {
         if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
         {
        if(f_open(&MyFile, "file.txt", FA_CREATE_ALWAYS | FA_WRITE) ==FR_OK)
        {
        f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten);
        f_close(&MyFile);
        }
        }
        }
        FATFS_UnLinkDriver(SDPath);
      while (1)
      {
          HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
          HAL_Delay(2000);
          HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
          HAL_Delay(2000);
      }
    }
yaser kh
  • 11
  • 2
  • Do all the functions get the necessary time to execute? Try putting a delay function after every action with SD card and see if anything happens. Could be initialization of SD requires extra time, and by the time you reboot, it has just set up. – Ilya Jun 28 '22 at 12:06
  • I did it, but it didn't work. I think some section of stm32 are not activated at first, And after I reset the device, those sections are activated,Like the part of the clock that is related to sd card – yaser kh Jun 29 '22 at 04:26
  • I believe SD MMC Init is supposed to activate the clock. Besides, if it didn't, why would things work when you manually reset the thing. Where you have various "if ... ==FR_OK" you add else statements that activates LED and then goes "while(1);". If you see LED light up and sit like that, it will mean one of the if statements failed. Then start removing while(1) one by one until you figure out which exactly while(1) it was - which if....FR_OK failed. Also, I don't see any initialization for retSD (but since it compiles, it must be initialized somewhere?) – Ilya Jun 29 '22 at 06:23
  • I finally found the problem. I pulled up the data and cmd ports and the problem was solved – yaser kh Jul 04 '22 at 05:00
  • 1
    Doesn't explain why it worked after reset...but hey, write your own answer to this question below and select it. – Ilya Jul 04 '22 at 08:16

1 Answers1

0

The problem was that the device did not work from the ports. The data and CMD ports must be connected as pull-up

yaser kh
  • 11
  • 2