cancel
Showing results for 
Search instead for 
Did you mean: 

How to integrate SPI sd card on STM32L452CC?

MFrie.6
Associate III

I have a custom board with the l452cc controller. I am trying to run an sd card with fatfs through spi. I have generated cubemx fatfs files, but there is no interface layer for spi to fatfs. Are there some examples I can reference off of, or what is the best way to approach this?

Thanks.

19 REPLIES 19
MFrie.6
Associate III

Thanks for the input, I'll see how far I get and ask back if I have questions. Thanks

Is FatFS able to handle if SD card's sector size is not 512?

If you make it work, please also post your tech details here.

MFrie.6
Associate III

Well I'm making some progress. I implimented the NUCLEO-L452RE example project basically. 'BSP_SD_INIT()' returns success. f_mount however returns 'FR_NO_FILESYSTEM'. Sd card is a 16GB formatted with exFat currently. I have tried formatting it with FAT32 as well, with no success. Is this a low level problem, or is there something fundamentally wrong with my implimentation. Here is the section of applicable code.

static void SDCard_Config(void) { 
  /* Initialize the SD */
  if (BSP_SD_Init() != MSD_OK) {
#if DEBUG
    debug_printf("BSP_SD_INIT_FAILED\r\n");
#endif
  } else {
#if DEBUG
    debug_printf("BSP_SD_INIT_SUCCESS\r\n");
#endif
  }
}

void MX_FATFS_Init(void)
{
  /*## FatFS: Link the USER driver ###########################*/
  retUSER = FATFS_LinkDriver(&SD_Driver, USERPath);
 
  /* USER CODE BEGIN Init */
  /* additional user code for init */
  /* USER CODE END Init */
}
MX_FATFS_Init();
  SDCard_Config();
  FIL fil;        /* File object */
  char line[100]; /* Line buffer */
  FRESULT fr;     /* FatFs return code */
 
  /* Give a work area to the default drive */
  fr = f_mount(&SD_FatFs, "", 1);
  if (fr) {
    __NOP();
  }
 
  /* Open a text file */
  fr = f_open(&fil, "message.txt", FA_READ);
  if (fr) {
    __NOP();
  }
 
  /* Read every line and display it */
  while (f_gets(line, sizeof line, &fil)) {
    debug_printf(line);
  }
 
  /* Close the file */
  f_close(&fil);

Thanks

MFrie.6
Associate III

Here is a logic capture of the above code. You will have to rename 'SD_init.sal.txt' to 'SD_init.sal', thanks to a forum quirk.

MFrie.6
Associate III

So apparently 'Check boot record signature' is what is firing the error.

if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3;	/* Check boot record signature (always placed here even if the sector size is >512) */

It would almost seem like I have a spi driver problem, but if I look in 'fs->win' there appears to be some random data lines 440 to 461 in the sector. So not sure if it is a driver problem or not.

MFrie.6
Associate III

Ok so it was a driver problem. I haven't pinned it down yet, but it has to do with the dma, because when I use normal spi mode, it will do a basic read write. So the process to get an sd card working is this:

Generate fatfs files with cubemx

Copy BSP package in from the adafruit sd shield example

Write a spi driver, and tie it into the BSP package

In theory it should then work. I may have missed a few details, so feel free to ask if you have any questions.

HDaji.1
Senior

Are you able to read/write file with large size, like a few tens or even hundreds bytes?

How do you format the SD card? What is the sector size?

MFrie.6
Associate III

I haven't tried much besides a basic hello world text file. I will see here at some point. The card is formatted with FAT, and I have tried exFat too and that works. I will post back after I have tried some bigger files.

MFrie.6
Associate III

Well I don't know if this gets what you are wanting to know but here is a piece of code that I ran.

  BYTE buf[2048];
  memset(buf, 0x4c, sizeof(buf));
  UINT br, bw;
  fr = f_write(&fil, buf, sizeof(buf), &bw);

The file when I look at it is filled with 0x4c, so it seems like it is working.