From 1004efa300d98d017a06c0bb582e8872df14a135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 27 Sep 2016 15:21:48 +0200 Subject: [PATCH] Moved insertion check and write protection check to functions. --- .../shimatta_sdio-driver.c | 79 ++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/FATFS/shimatta_sdio_driver/shimatta_sdio-driver.c b/FATFS/shimatta_sdio_driver/shimatta_sdio-driver.c index 14bee8a..239862e 100644 --- a/FATFS/shimatta_sdio_driver/shimatta_sdio-driver.c +++ b/FATFS/shimatta_sdio_driver/shimatta_sdio-driver.c @@ -42,23 +42,29 @@ int SDIO_send_CMD3(uint16_t* rca); int SDIO_send_CMD0(); CMD8_RESP_t SDIO_send_CMD8(); +void initDetectandProtectionPins(); +int checkNotInserted(); // Returns 0 if inserted! +int checkWriteProtection(); // returns 0 if write protected + + //BYTE rxtxbuffer[1<IDR & INS_PIN) == INS_ACTIVE_LEVEL)) - return STA_NODISK; -#endif /* SDIO_ENABLE_INS */ - if (cardInfo.type == CARD_NONE) { - return STA_NOINIT; + DSTATUS returnval = 0; + if (checkNotInserted()) { + returnval |= STA_NODISK; } -#if SDIO_ENABLE_WRITEPROT==1 - if (!((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == WRITEPROT_ACTIVE_LEVEL)) - return STA_PROTECT; -#endif /* SDIO_ENABLE_WRITEPROT */ - return 0; + if (cardInfo.type == CARD_NONE) { + returnval |= STA_NOINIT; + } + if (checkWriteProtection()) { + returnval |= STA_PROTECT; + } + return returnval; } + + DSTATUS SDIO_initialize(){ CMD8_RESP_t res8; ACMD41_RESP_t resa41; @@ -68,20 +74,14 @@ DSTATUS SDIO_initialize(){ SDIO_InitModule(); + initDetectandProtectionPins(); -#if SDIO_ENABLE_WRITEPROT==1 - WRITEPROT_PORT->PUPDR |= ((WRITEPROT_PULLUP ? 1 : 0)<PUPDR |= ((INS_PULLUP? 1 : 0)<IDR & INS_PIN) == INS_ACTIVE_LEVEL)) - return STA_NODISK; -#endif /* SDIO_ENABLE_INS */ + if (checkNotInserted()) { + return STA_NOINIT | STA_NODISK; + } + SDIO_send_CMD0(); res8 = SDIO_send_CMD8(); @@ -126,11 +126,10 @@ DSTATUS SDIO_initialize(){ //TODO: Set 4 bit mode //TODO: -#if SDIO_ENABLE_WRITEPROT==1 - if (!((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == WRITEPROT_ACTIVE_LEVEL)) + if (checkWriteProtection()) { return STA_PROTECT; -#endif /* SDIO_ENABLE_WRITEPROT */ - return 0; + } else + return 0; } DRESULT SDIO_disk_read(BYTE *buff, DWORD sector, UINT count){ return RES_OK; @@ -236,7 +235,7 @@ int SDIO_getResp(uint8_t expectedCMD, uint8_t typeOfAns, uint32_t *responseBuffe while (1) { if (SDIO->STA & SDIO_STA_CMDREND) break; //Correct Respone Received - //Exclude ACMD41 from valid CRC check + //Exclude ACMD41 and CMD2 from valid CRC check if ((SDIO->STA & SDIO_STA_CCRCFAIL)) { if(expectedCMD == 0x3f) { //This command does not have a CRC...Doushite.... @@ -356,3 +355,29 @@ CMD8_RESP_t SDIO_send_CMD8() { }while(retry-- > 0); return CMD8_RESP_TIMEOUT; } + +void initDetectandProtectionPins() { +#if SDIO_ENABLE_WRITEPROT==1 + WRITEPROT_PORT->PUPDR |= ((WRITEPROT_PULLUP ? 1 : 0)<PUPDR |= ((INS_PULLUP? 1 : 0)<IDR & INS_PIN) == INS_ACTIVE_LEVEL ? 0 : 1); +#else + return 0; // Assume Card is inserted +#endif +} + +int checkWriteProtection() { +#if SDIO_ENABLE_WRITEPROT + return ((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == WRITEPROT_ACTIVE_LEVEL ? 1 : 0); +#else + return 0; // Assume Card is not write protected +#endif +}