Moved insertion check and write protection check to functions.

This commit is contained in:
Mario Hüttel 2016-09-27 15:21:48 +02:00
parent 9c8eb5cc41
commit 1004efa300

View File

@ -42,23 +42,29 @@ int SDIO_send_CMD3(uint16_t* rca);
int SDIO_send_CMD0(); int SDIO_send_CMD0();
CMD8_RESP_t SDIO_send_CMD8(); CMD8_RESP_t SDIO_send_CMD8();
void initDetectandProtectionPins();
int checkNotInserted(); // Returns 0 if inserted!
int checkWriteProtection(); // returns 0 if write protected
//BYTE rxtxbuffer[1<<BLOCKSIZE]; //Data RX and TX Buffer not needed anymore. thanks to DMA //BYTE rxtxbuffer[1<<BLOCKSIZE]; //Data RX and TX Buffer not needed anymore. thanks to DMA
SDInfo_t cardInfo; // = {.type = CARD_NONE}; SDInfo_t cardInfo; // = {.type = CARD_NONE};
DSTATUS SDIO_status(){ DSTATUS SDIO_status(){
#if SDIO_ENABLE_INS==1 DSTATUS returnval = 0;
if (!((INS_PORT->IDR & INS_PIN) == INS_ACTIVE_LEVEL)) if (checkNotInserted()) {
return STA_NODISK; returnval |= STA_NODISK;
#endif /* SDIO_ENABLE_INS */
if (cardInfo.type == CARD_NONE) {
return STA_NOINIT;
} }
#if SDIO_ENABLE_WRITEPROT==1 if (cardInfo.type == CARD_NONE) {
if (!((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == WRITEPROT_ACTIVE_LEVEL)) returnval |= STA_NOINIT;
return STA_PROTECT; }
#endif /* SDIO_ENABLE_WRITEPROT */ if (checkWriteProtection()) {
return 0; returnval |= STA_PROTECT;
}
return returnval;
} }
DSTATUS SDIO_initialize(){ DSTATUS SDIO_initialize(){
CMD8_RESP_t res8; CMD8_RESP_t res8;
ACMD41_RESP_t resa41; ACMD41_RESP_t resa41;
@ -68,20 +74,14 @@ DSTATUS SDIO_initialize(){
SDIO_InitModule(); SDIO_InitModule();
initDetectandProtectionPins();
#if SDIO_ENABLE_WRITEPROT==1
WRITEPROT_PORT->PUPDR |= ((WRITEPROT_PULLUP ? 1 : 0)<<WRITEPROT_PIN*2);
#endif /* SDIO_ENABLE_WRITEPROT */
#if SDIO_ENABLE_INS==1
INS_PORT->PUPDR |= ((INS_PULLUP? 1 : 0)<<INS_PIN*2);
#endif /* SDIO_ENABLE_INS */
__DSB();
#if SDIO_ENABLE_INS==1 if (checkNotInserted()) {
if (!((INS_PORT->IDR & INS_PIN) == INS_ACTIVE_LEVEL)) return STA_NOINIT | STA_NODISK;
return STA_NODISK; }
#endif /* SDIO_ENABLE_INS */
SDIO_send_CMD0(); SDIO_send_CMD0();
res8 = SDIO_send_CMD8(); res8 = SDIO_send_CMD8();
@ -126,10 +126,9 @@ DSTATUS SDIO_initialize(){
//TODO: Set 4 bit mode //TODO: Set 4 bit mode
//TODO: //TODO:
#if SDIO_ENABLE_WRITEPROT==1 if (checkWriteProtection()) {
if (!((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == WRITEPROT_ACTIVE_LEVEL))
return STA_PROTECT; return STA_PROTECT;
#endif /* SDIO_ENABLE_WRITEPROT */ } else
return 0; return 0;
} }
DRESULT SDIO_disk_read(BYTE *buff, DWORD sector, UINT count){ DRESULT SDIO_disk_read(BYTE *buff, DWORD sector, UINT count){
@ -236,7 +235,7 @@ int SDIO_getResp(uint8_t expectedCMD, uint8_t typeOfAns, uint32_t *responseBuffe
while (1) { while (1) {
if (SDIO->STA & SDIO_STA_CMDREND) break; //Correct Respone Received 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 ((SDIO->STA & SDIO_STA_CCRCFAIL)) {
if(expectedCMD == 0x3f) { if(expectedCMD == 0x3f) {
//This command does not have a CRC...Doushite.... //This command does not have a CRC...Doushite....
@ -356,3 +355,29 @@ CMD8_RESP_t SDIO_send_CMD8() {
}while(retry-- > 0); }while(retry-- > 0);
return CMD8_RESP_TIMEOUT; return CMD8_RESP_TIMEOUT;
} }
void initDetectandProtectionPins() {
#if SDIO_ENABLE_WRITEPROT==1
WRITEPROT_PORT->PUPDR |= ((WRITEPROT_PULLUP ? 1 : 0)<<WRITEPROT_PIN*2);
#endif /* SDIO_ENABLE_WRITEPROT */
#if SDIO_ENABLE_INS==1
INS_PORT->PUPDR |= ((INS_PULLUP? 1 : 0)<<INS_PIN*2);
#endif /* SDIO_ENABLE_INS */
__DSB();
}
int checkNotInserted() {
#if SDIO_ENABLE_INS
return ((INS_PORT->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
}