cancel
Showing results for 
Search instead for 
Did you mean: 

FatFs: SD_ioctl doesn't support the CTRL_TRIM command

Steve Neves
Associate II

I want to enable trim support that is baked into FatFs, however the SD_ioctl function provided by ST does not include many of the FatFs commands, only what is listed below. Has anyone implemented this before? I plan on doing data logging on a micro SD card and want to ensure the longest life possible. Thanks.

/* USER CODE BEGIN beforeIoctlSection */
/* can be used to modify previous code / undefine following code / add new code */
/* USER CODE END beforeIoctlSection */
/**
  * @brief  I/O control operation
  * @param  lun : not used
  * @param  cmd: Control code
  * @param  *buff: Buffer to send/receive control data
  * @retval DRESULT: Operation result
  */
#if _USE_IOCTL == 1
DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
{
  DRESULT res = RES_ERROR;
  BSP_SD_CardInfo CardInfo;
 
  if (Stat & STA_NOINIT) return RES_NOTRDY;
 
  switch (cmd)
  {
  /* Make sure that no pending write process */
  case CTRL_SYNC :
    res = RES_OK;
    break;
 
  /* Get number of sectors on the disk (DWORD) */
  case GET_SECTOR_COUNT :
    BSP_SD_GetCardInfo(&CardInfo);
    *(DWORD*)buff = CardInfo.LogBlockNbr;
    res = RES_OK;
    break;
 
  /* Get R/W sector size (WORD) */
  case GET_SECTOR_SIZE :
    BSP_SD_GetCardInfo(&CardInfo);
    *(WORD*)buff = CardInfo.LogBlockSize;
    res = RES_OK;
    break;
 
  /* Get erase block size in unit of sector (DWORD) */
  case GET_BLOCK_SIZE :
    BSP_SD_GetCardInfo(&CardInfo);
    *(DWORD*)buff = CardInfo.LogBlockSize / SD_DEFAULT_BLOCK_SIZE;
    res = RES_OK;
    break;
  default:
    res = RES_PARERR;
  }
 
  return res;
}
#endif /* _USE_IOCTL == 1 */

1 REPLY 1
jaakjensen
Associate III

I think you can just add another case called "CTRL_TRIM" and then instruct the BSP to erase whatever blocks are requested. You can call HAL_SD_Erase(&hsd_sdmmc, st, ed) instead of BSP_SD_Erase();

The return value of this function when the command CTRL_TRIM is used is not checked by ff.c / ff.h so I don't believe error checking is required.

  /* Tell the disk to erase the requested sectors */
  case CTRL_TRIM :
	  tmp = buff;
	  st = *tmp;
	  ed = *(tmp+1);
	  BSP_SD_Erase(0, st, ed-st);
	  res = RES_OK;
	  break;