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; res = RES_OK; break; default: res = RES_PARERR; } return res; }
/** * @brief Returns the medium capacity. * @param lun: Logical unit number * @param block_num: Number of total block number * @param block_size: Block size * @retval Status (0: Ok / -1: Error) */ int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) { HAL_SD_CardInfoTypedef info; int8_t ret = -1; if(BSP_SD_IsDetected() != SD_NOT_PRESENT) { BSP_SD_GetCardInfo(&info); *block_num = (info.CardCapacity)/STORAGE_BLK_SIZ - 1; *block_size = STORAGE_BLK_SIZ; ret = 0; } return ret; }
/** * @brief Returns the medium capacity. * @param lun: Logical unit number * @param block_num: Number of total block number * @param block_size: Block size * @retval Status (0: Ok / -1: Error) */ int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) { HAL_SD_CardInfoTypedef info; int8_t ret = -1; if(k_StorageGetStatus (MSD_DISK_UNIT) == 1) { BSP_SD_GetCardInfo(&info); *block_num = (info.CardCapacity)/STORAGE_BLK_SIZ - 1; *block_size = STORAGE_BLK_SIZ; ret = 0; } return ret; }
DRESULT TM_FATFS_SD_SDIO_disk_ioctl(BYTE cmd, void *buff) { DRESULT res = RES_ERROR; SD_CardInfo CardInfo; /* Check if init OK */ if (Stat & STA_NOINIT) { return RES_NOTRDY; } switch (cmd) { /* Make sure that no pending write process */ case CTRL_SYNC : res = RES_OK; break; /* Size in bytes for single sector */ case GET_SECTOR_SIZE: *(WORD *)buff = SD_BLOCK_SIZE; res = RES_OK; break; /* Get number of sectors on the disk (DWORD) */ case GET_SECTOR_COUNT : BSP_SD_GetCardInfo(&CardInfo); *(DWORD *)buff = CardInfo.CardCapacity / SD_BLOCK_SIZE; res = RES_OK; break; /* Get erase block size in unit of sector (DWORD) */ case GET_BLOCK_SIZE : *(DWORD*)buff = SD_BLOCK_SIZE; break; default: res = RES_PARERR; } return res; }
/** * @brief SD Demo * @param None * @retval None */ void SD_demo (void) { uint8_t SD_state = SD_RESPONSE_NO_ERROR; __IO uint8_t prev_status = 0; SD_SetHint(); SD_state = BSP_SD_Init(); /* Check if the SD card is plugged in the slot */ if(BSP_SD_IsDetected() == SD_PRESENT) { BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Connected ", LEFT_MODE); } else { BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Not Connected", LEFT_MODE); } BSP_LCD_SetTextColor(LCD_COLOR_BLACK); if(SD_state != SD_RESPONSE_NO_ERROR) { BSP_LCD_DisplayStringAt(20, 100, (uint8_t *)"SD INITIALIZATION : FAIL.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 100, (uint8_t *)"SD INITIALIZATION : OK.", LEFT_MODE); SD_state = BSP_SD_GetCardInfo(&CardInfo); if(SD_state != SD_RESPONSE_NO_ERROR) { BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD GET CARD INFO : FAIL.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD GET CARD INFO : OK.", LEFT_MODE); SD_state = BSP_SD_Erase(BLOCK_START_ADDR, (BLOCKSIZE * NUM_OF_BLOCKS)); /* Verify that SD card is ready to use after the Erase */ SD_state |= BSP_SD_GetStatus(); if(SD_state != SD_RESPONSE_NO_ERROR) { BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD ERASE : FAILED.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD ERASE : OK.", LEFT_MODE); /* Fill the buffer to write */ Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF); SD_state = BSP_SD_WriteBlocks((uint32_t *)aTxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS); if(SD_state != SD_RESPONSE_NO_ERROR) { BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD WRITE : FAILED.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD WRITE : OK.", LEFT_MODE); SD_state = BSP_SD_ReadBlocks((uint32_t *)aRxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS); if(SD_state != SD_RESPONSE_NO_ERROR) { BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD READ : FAILED.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD READ : OK.", LEFT_MODE); if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) { BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)"SD COMPARE : FAILED.", LEFT_MODE); BSP_LCD_DisplayStringAt(20, 190, (uint8_t *)"SD Test Aborted.", LEFT_MODE); } else { BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)"SD TEST : OK.", LEFT_MODE); } } } } } } while (1) { /* Check if the SD card is plugged in the slot */ if(BSP_SD_IsDetected() != SD_PRESENT) { if(prev_status == 0) { prev_status = 1; BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Not Connected", LEFT_MODE); } } else if (prev_status == 1) { BSP_SD_Init(); BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Connected ", LEFT_MODE); prev_status = 0; } if(CheckForUserInput() > 0) { return; } } }