2424#include < boards.h>
2525
2626#define USE_DEBUG_MODE 0
27+ #define USE_YIELD 0
2728
2829// ==============================================================================
2930// #define SDHC_PROCTL_DTW_4BIT 0x01
3031#define CMD8_RETRIES 10
31- #define BUSY_TIMEOUT_MILLIS 500
32+ #define BUSY_TIMEOUT_MILLIS 750
3233// ==============================================================================
3334#define CMD_RESP_NONE SDIO_CMD_WAIT_NO_RESP
3435#define CMD_RESP_R1 SDIO_CMD_WAIT_SHORT_RESP // normal response
@@ -137,7 +138,8 @@ static void _panic(const char *message, uint32_t code)
137138 delay (250 );
138139 }
139140}
140- /* ===========================================================================
141+ /* ===========================================================================*/
142+ #if USE_YIELD
141143void yield (void )
142144{
143145 uint32_t val = SDIO->STA ;
@@ -169,7 +171,8 @@ void yield(void)
169171 _panic (" - DMA: Data Transmission Error " , val);
170172 }
171173 // Serial.write('.');
172- }*/
174+ }
175+ #endif
173176// =============================================================================
174177// Error function and macro.
175178inline bool setSdErrorCode (uint8_t code, uint32_t line) {
@@ -191,11 +194,11 @@ void sdhc_isr() {
191194// -----------------------------------------------------------------------------
192195static bool cardCommand (uint16_t xfertyp, uint32_t arg)
193196{
194- #if USE_DEBUG_MODE
197+ #if USE_DEBUG_MODE==2
195198 Serial.print (" cardCommand: " ); Serial.print (xfertyp&SDIO_CMD_CMDINDEX); Serial.print (" , arg: " ); Serial.print (arg, HEX);
196199#endif
197200 uint8_t resp = sdio_cmd_send (xfertyp, arg); // returns non-zero if fails, zero if OK
198- #if USE_DEBUG_MODE
201+ #if USE_DEBUG_MODE==2
199202 Serial.print (" , resp: " ); Serial.print (resp, HEX);
200203 Serial.print (" , SDIO->STA: " ); Serial.print (SDIO->STA , HEX); Serial.print (" , cmd_resp: " ); Serial.print (SDIO->RESP [0 ], HEX);
201204 if ( (xfertyp&SDIO_CMD_WAIT_LONG_RESP)==SDIO_CMD_WAIT_LONG_RESP ) {
@@ -330,6 +333,7 @@ static bool dmaTrxStart(uint8_t* buf, uint32_t n, uint8_t dir)
330333static bool dmaTrxEnd (bool multi_block)
331334{
332335 if ( !waitDmaStatus () ) {
336+ DBG_PRINT ();
333337 return sdError (SD_CARD_ERROR_DMA);
334338 }
335339 if ( waitTimeout (isBusyTransferComplete) ) {
@@ -536,6 +540,9 @@ uint32_t SdioCard::kHzSdClk() {
536540/* ---------------------------------------------------------------------------*/
537541bool SdioCard::readBlock (uint32_t lba, uint8_t * buf)
538542{
543+ #if USE_DEBUG_MODE
544+ Serial.print (" readBlock: " ); Serial.println (lba); // Serial.print(", buf: "); Serial.println((uint32_t)buf, HEX);
545+ #endif
539546 // prepare SDIO and DMA for data read transfer
540547 dmaTrxStart ((uint32_t )buf & 3 ? (uint8_t *)aligned : buf, 512 , TRX_RD);
541548 // send command to start data transfer
@@ -560,6 +567,11 @@ bool SdioCard::readBlock(uint32_t lba, uint8_t* buf)
560567/* ---------------------------------------------------------------------------*/
561568bool SdioCard::readBlocks (uint32_t lba, uint8_t * buf, size_t n)
562569{
570+ #if USE_DEBUG_MODE
571+ Serial.print (" readBlocks: " ); Serial.print (lba);
572+ // Serial.print(", buf: "); Serial.print((uint32_t)buf, HEX);
573+ Serial.print (" , " ); Serial.println (n);
574+ #endif
563575 if ((uint32_t )buf & 3 ) {
564576 for (size_t i = 0 ; i < n; i++, lba++, buf += 512 ) {
565577 if (!readBlock (lba, buf)) {
@@ -672,9 +684,12 @@ uint8_t SdioCard::type() {
672684/* ---------------------------------------------------------------------------*/
673685bool SdioCard::writeBlock (uint32_t lba, const uint8_t * buf)
674686{
687+ #if USE_DEBUG_MODE
688+ Serial.print (" writeBlock: " ); Serial.println (lba); // Serial.print(", buf: "); Serial.println((uint32_t)buf, HEX);
689+ #endif
675690 uint8_t * ptr = (uint8_t *)buf;
676691 if (3 & (uint32_t )ptr) {
677- // Serial.print("writeBlock: "); Serial.print(lba);
692+ Serial.print (" writeBlock: " ); Serial.print (lba);
678693 Serial.print (" , buf: " ); Serial.print ((uint32_t )ptr, HEX);
679694 // memcpy(aligned, buf, 512);
680695 register uint8_t * src = (uint8_t *)ptr;
@@ -701,6 +716,11 @@ bool SdioCard::writeBlock(uint32_t lba, const uint8_t* buf)
701716/* ---------------------------------------------------------------------------*/
702717bool SdioCard::writeBlocks (uint32_t lba, const uint8_t * buf, size_t n)
703718{
719+ #if USE_DEBUG_MODE
720+ Serial.print (" writeBlocks: " ); Serial.print (lba);
721+ // Serial.print(", buf: "); Serial.print((uint32_t)buf, HEX);
722+ Serial.print (" , " ); Serial.println (n);
723+ #endif
704724 if (3 & (uint32_t )buf) { // misaligned buffer address, write single blocks
705725 for (size_t i = 0 ; i < n; i++, lba++, buf += 512 ) {
706726 if (!writeBlock (lba, buf)) {
@@ -709,7 +729,6 @@ bool SdioCard::writeBlocks(uint32_t lba, const uint8_t* buf, size_t n)
709729 }
710730 return true ;
711731 }
712- // Serial.print("writeBlocks: "); Serial.print(lba); Serial.print(", "); Serial.print(n);
713732 if (yieldTimeout (isBusyCMD13)) {
714733 return sdError (SD_CARD_ERROR_CMD13);
715734 }
0 commit comments