//}}} //{{{ DSTATUS SD_status (BYTE lun) { Stat = STA_NOINIT; if (BSP_SD_GetStatus() == MSD_OK) Stat &= ~STA_NOINIT; return Stat; }
DRESULT SD_write(const BYTE *buff, DWORD sector, BYTE count) { uint32_t timeout = 100000; DWORD scratch [BLOCK_SIZE / 4]; /* Alignment ensured, need enough stack */ uint8_t SD_state = MSD_OK; if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */ { while (count--) { memcpy (scratch, &buff[count * BLOCK_SIZE], BLOCK_SIZE); SD_state = BSP_SD_WriteBlocks_DMA((uint32_t*)scratch, (uint64_t)((sector + count) * BLOCK_SIZE), BLOCK_SIZE, 1); while(BSP_SD_GetStatus() != SD_TRANSFER_OK) { if (timeout-- == 0) { return RES_ERROR; } } } } else { SD_state = BSP_SD_WriteBlocks_DMA((uint32_t*)buff, (uint64_t)(sector * BLOCK_SIZE), BLOCK_SIZE, count); while(BSP_SD_GetStatus() != SD_TRANSFER_OK) { if (timeout-- == 0) { return RES_ERROR; } } } if (SD_state == MSD_OK) { return RES_OK; } return RES_ERROR; }
/** * @brief Gets Disk Status * @param None * @retval DSTATUS: Operation status */ DSTATUS SD_status(void) { Stat = STA_NOINIT; if(BSP_SD_GetStatus() == 0) { Stat &= ~STA_NOINIT; } return Stat; }
/** * @brief Gets Disk Status * @param pdrv: Physical drive number * @retval DSTATUS: Operation status */ DSTATUS SD_status(BYTE pdrv) { Stat = STA_NOINIT; if(BSP_SD_GetStatus() == MSD_OK) { Stat &= ~STA_NOINIT; } return Stat; }
/** * @brief Writes data into the medium. * @param lun: Logical unit number * @param buf: pointer to data buffer * @param blk_addr: Logical block address * @param blk_len: Blocks number * @retval Status (0 : Ok / -1 : Error) */ int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { int8_t ret = -1; uint32_t timeout = 100000; if(k_StorageGetStatus (MSD_DISK_UNIT) == 1) { BSP_SD_WriteBlocks((uint32_t *)buf, blk_addr * STORAGE_BLK_SIZ, STORAGE_BLK_SIZ, blk_len); while(BSP_SD_GetStatus() != SD_TRANSFER_OK) { if (timeout-- == 0) { return (-1); } } ret = 0; } return ret; }
DSTATUS TM_FATFS_SD_SDIO_disk_status(void) { Stat = STA_NOINIT; /* Check SDCARD status */ if (BSP_SD_GetStatus() == MSD_OK) { Stat &= ~STA_NOINIT; } else { Stat |= STA_NOINIT; } /* Check if write enabled */ if (SDCARD_IsWriteEnabled()) { Stat &= ~STA_PROTECT; } else { Stat |= STA_PROTECT; } return Stat; }
/** * @brief Checks whether the medium is ready. * @param lun: Logical unit number * @retval Status (0: Ok / -1: Error) */ int8_t STORAGE_IsReady(uint8_t lun) { static int8_t prev_status = 0; int8_t ret = -1; if(BSP_SD_IsDetected() != SD_NOT_PRESENT) { if(prev_status < 0) { BSP_SD_Init(); prev_status = 0; } if(BSP_SD_GetStatus() == SD_TRANSFER_OK) { ret = 0; } } else if(prev_status == 0) { prev_status = -1; } return ret; }
/** * @brief Checks whether the medium is ready. * @param lun: Logical unit number * @retval Status (0: OK / -1: Error) */ int8_t STORAGE_IsReady(uint8_t lun) { static int8_t prev_status = 0; int8_t ret = -1; // if(BSP_SD_IsDetected() != SD_NOT_PRESENT) { if(prev_status < 0) { BSP_SD_Init(); prev_status = 0; } if(BSP_SD_GetStatus() == MSD_OK) { ret = 0; } } // else if(prev_status == 0) // { // prev_status = -1; // } return ret; }
/** * @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; } } }