Stm32f7-Disco write data to SD card with Fatfs

Stm32f7-Disco write data to SD card with Fatfs. This post show how to write data to Sd card by Fatfs. 

1. Insert your micro-SD card to sd slot.

2. Pin out setting

RCC -> High speed clock -> Crystal ceramic resonator
Up system clock to 216Mhz
SDMMC1->Mode->SD 4 bits wide bus
Fatfs->Sd card check.

 3. SDMMC1 configuration

 

4.Code generation and editon

In Src/main.c add code

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_FATFS_Init();
  MX_SDMMC1_SD_Init();
FATFS SDFatFs; 
  FIL MyFile;    

  char SDPath[4]; 
  FRESULT res;                                          
  uint32_t byteswritten;                
  uint8_t wtext[] = “This is test of Fatfs with STM32F7-Discon”;
  if(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0)
  {
    if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
    {
         Error_Handler();
    }
    else
    {
        if(f_open(&MyFile, “sd_test.txt”, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
        {
          Error_Handler();
        }
        else
        {
          res = f_write(&MyFile, wtext, sizeof(wtext)-1, (void *)&byteswritten);
        char buf[]=”Hello Worldn”;
          res = f_write(&MyFile,buf, sizeof(buf)-1, (void *)&byteswritten);
        char buf1[]=”I am Duong”;
          res = f_write(&MyFile,buf1, sizeof(buf1)-1, (void *)&byteswritten);
           BSP_LED_On(LED1);
          if((byteswritten == 0) || (res != FR_OK))
          {
            Error_Handler();
          }
          else
          {
            f_close(&MyFile);

          }
      }
    }
  }

  FATFS_UnLinkDriver(SDPath);

  while (1)
  {
  }
}

 

Video

 

Sponsored Links: