From bd6171d2d953342797a9cdc10b931131fcd0b65d Mon Sep 17 00:00:00 2001 From: Michael McMaster Date: Tue, 18 May 2021 21:13:21 +1000 Subject: [PATCH] Simplify SD card busy waiting --- src/firmware/bsp_driver_sd.c | 15 ++++++--------- src/firmware/disk.c | 25 ++++++++++++------------- src/firmware/sd.c | 8 ++++++++ src/firmware/sd.h | 1 + 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/firmware/bsp_driver_sd.c b/src/firmware/bsp_driver_sd.c index e972c98f..e69c7183 100755 --- a/src/firmware/bsp_driver_sd.c +++ b/src/firmware/bsp_driver_sd.c @@ -34,6 +34,7 @@ /* USER CODE BEGIN 0 */ /* Includes ------------------------------------------------------------------*/ #include "bsp_driver_sd.h" +#include "sd.h" /* Extern variables ---------------------------------------------------------*/ @@ -283,15 +284,8 @@ uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumO { while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY) {} - HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); - while (cardState == HAL_SD_CARD_PROGRAMMING) - { - // Wait while the SD card is writing buffer to flash - // The card may remain in the RECEIVING state (even though it's programming) if - // it has buffer space to receive more data available. - - cardState = HAL_SD_GetCardState(&hsd); - } + // Wait while the SD card has no buffer space + while (sdIsBusy()) {} HAL_SD_WriteBlocks_Data(&hsd, pData + (i * 512)); } @@ -312,6 +306,9 @@ uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumO SD_state = MSD_OK; } + // Wait while the SD card is in the PROGRAMMING state. + while (sdIsBusy()) {} + HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) { diff --git a/src/firmware/disk.c b/src/firmware/disk.c index c9c7ae45..45b18aa0 100755 --- a/src/firmware/disk.c +++ b/src/firmware/disk.c @@ -693,13 +693,13 @@ void scsiDiskPoll() { // Wait while keeping BSY. } - } + } HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_SENDING) { cardState = HAL_SD_GetCardState(&hsd); - } + } // We've finished transferring the data to the FPGA, now wait until it's // written to he SCSI bus. @@ -787,14 +787,11 @@ void scsiDiskPoll() while (j < sectors && !scsiDev.resetFlag) { if (sdActive && - HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY) + HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY && + !sdIsBusy()) { - HAL_SD_CardStateTypeDef tmpCardState = HAL_SD_GetCardState(&hsd); - if (tmpCardState != HAL_SD_CARD_PROGRAMMING) - { - j += sdActive; - sdActive = 0; - } + j += sdActive; + sdActive = 0; } if (!sdActive && ((prep - j) > 0)) { @@ -850,14 +847,12 @@ void scsiDiskPoll() } } - HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); - while ((cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) && + while (sdIsBusy() && s2s_elapsedTime_ms(dmaFinishTime) < 180) { // Wait while the SD card is writing buffer to flash // The card may remain in the RECEIVING state (even though it's programming) if // it has buffer space to receive more data available. - cardState = HAL_SD_GetCardState(&hsd); } if (!disconnected && @@ -875,7 +870,11 @@ void scsiDiskPoll() clearBSY = process_MessageIn(0); // Will go to BUS_FREE state but keep BSY asserted. } - cardState = HAL_SD_GetCardState(&hsd); + // Wait while the SD card is writing buffer to flash + // The card may remain in the RECEIVING state (even though it's programming) if + // it has buffer space to receive more data available. + while (sdIsBusy()) {} + HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) { // Wait while the SD card is writing buffer to flash diff --git a/src/firmware/sd.c b/src/firmware/sd.c index 1f2a27bc..0eff83d3 100755 --- a/src/firmware/sd.c +++ b/src/firmware/sd.c @@ -214,3 +214,11 @@ int sdInit() return result; } +// Return 1 if the SD card has no buffer space left for writes and/or is +// in the programming state. +int sdIsBusy() +{ + // Busy while DAT0 is held low. + return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8) == 0; +} + diff --git a/src/firmware/sd.h b/src/firmware/sd.h index b82f1192..5a1d79a1 100755 --- a/src/firmware/sd.h +++ b/src/firmware/sd.h @@ -38,5 +38,6 @@ void sdReadDMA(uint32_t lba, uint32_t sectors, uint8_t* outputBuffer); int sdReadDMAPoll(uint32_t remainingSectors); void sdCompleteTransfer(); +int sdIsBusy(); #endif -- 2.38.5