writing data on sd card

799 Views Asked by At

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);
      }
    }
1

There are 1 best solutions below

0
On

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