|
|
@@ -155,6 +155,8 @@ static void dma_transfer_config(uint32_t *srcbuf, uint32_t bufsize);
|
|
|
/* configure the DMA for SDIO reveive request */
|
|
|
static void dma_receive_config(uint32_t *dstbuf, uint32_t bufsize);
|
|
|
|
|
|
+unsigned long millis(void);
|
|
|
+
|
|
|
/*!
|
|
|
\brief initialize the SD card and make it in standby state
|
|
|
\param[in] none
|
|
|
@@ -435,12 +437,11 @@ sd_error_enum sd_transfer_mode_config(uint32_t txmode)
|
|
|
\param[in] blocksize: the data block size
|
|
|
\retval sd_error_enum
|
|
|
*/
|
|
|
-sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize)
|
|
|
+sd_error_enum sd_block_read(uint32_t *preadbuffer, uint64_t readaddr, uint16_t blocksize, sdio_callback_t callback)
|
|
|
{
|
|
|
/* initialize the variables */
|
|
|
sd_error_enum status = SD_OK;
|
|
|
uint32_t count = 0, align = 0, datablksize = SDIO_DATABLOCKSIZE_1BYTE, *ptempbuff = preadbuffer;
|
|
|
- __IO uint32_t timeout = 0;
|
|
|
|
|
|
if(NULL == preadbuffer) {
|
|
|
status = SD_PARAMETER_INVALID;
|
|
|
@@ -546,12 +547,16 @@ sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t b
|
|
|
sdio_interrupt_enable(SDIO_INT_CCRCERR | SDIO_INT_DTTMOUT | SDIO_INT_RXORE | SDIO_INT_DTEND | SDIO_INT_STBITE);
|
|
|
sdio_dma_enable();
|
|
|
dma_receive_config(preadbuffer, blocksize);
|
|
|
- timeout = 100000;
|
|
|
- while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF)) && (timeout > 0)) {
|
|
|
- timeout--;
|
|
|
- if(0 == timeout) {
|
|
|
+ uint32_t start = millis();
|
|
|
+ while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF))) {
|
|
|
+ if((uint32_t)(millis() - start) > 1000) {
|
|
|
return SD_ERROR;
|
|
|
}
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ uint32_t complete = (blocksize - DMA_CHCNT(DMA1, DMA_CH3) * 4);
|
|
|
+ callback(complete);
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
status = SD_PARAMETER_INVALID;
|
|
|
@@ -567,12 +572,11 @@ sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t b
|
|
|
\param[in] blocksnumber: number of blocks that will be read
|
|
|
\retval sd_error_enum
|
|
|
*/
|
|
|
-sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize, uint32_t blocksnumber)
|
|
|
+sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint64_t readaddr, uint16_t blocksize, uint32_t blocksnumber, sdio_callback_t callback)
|
|
|
{
|
|
|
/* initialize the variables */
|
|
|
sd_error_enum status = SD_OK;
|
|
|
uint32_t count = 0, align = 0, datablksize = SDIO_DATABLOCKSIZE_1BYTE, *ptempbuff = preadbuffer;
|
|
|
- __IO uint32_t timeout = 0;
|
|
|
|
|
|
if(NULL == preadbuffer) {
|
|
|
status = SD_PARAMETER_INVALID;
|
|
|
@@ -700,14 +704,22 @@ sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint
|
|
|
sdio_dma_enable();
|
|
|
dma_receive_config(preadbuffer, totalnumber_bytes);
|
|
|
|
|
|
- timeout = 100000;
|
|
|
- while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF)) && (timeout > 0)) {
|
|
|
- timeout--;
|
|
|
- if(0 == timeout) {
|
|
|
+ uint32_t start = millis();
|
|
|
+ while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF))) {
|
|
|
+ if((uint32_t)(millis() - start) > 1000) {
|
|
|
return SD_ERROR;
|
|
|
}
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ uint32_t complete = (totalnumber_bytes - DMA_CHCNT(DMA1, DMA_CH3) * 4);
|
|
|
+ callback(complete);
|
|
|
+ }
|
|
|
}
|
|
|
while((0 == transend) && (SD_OK == transerror)) {
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(totalnumber_bytes);
|
|
|
+ }
|
|
|
}
|
|
|
if(SD_OK != transerror) {
|
|
|
return transerror;
|
|
|
@@ -727,14 +739,13 @@ sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint
|
|
|
\param[out] none
|
|
|
\retval sd_error_enum
|
|
|
*/
|
|
|
-sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize)
|
|
|
+sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint64_t writeaddr, uint16_t blocksize, sdio_callback_t callback)
|
|
|
{
|
|
|
/* initialize the variables */
|
|
|
sd_error_enum status = SD_OK;
|
|
|
uint8_t cardstate = 0;
|
|
|
uint32_t count = 0, align = 0, datablksize = SDIO_DATABLOCKSIZE_1BYTE, *ptempbuff = pwritebuffer;
|
|
|
uint32_t transbytes = 0, restwords = 0, response = 0;
|
|
|
- __IO uint32_t timeout = 0;
|
|
|
|
|
|
if(NULL == pwritebuffer) {
|
|
|
status = SD_PARAMETER_INVALID;
|
|
|
@@ -791,11 +802,18 @@ sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_
|
|
|
}
|
|
|
|
|
|
response = sdio_response_get(SDIO_RESPONSE0);
|
|
|
- timeout = 100000;
|
|
|
|
|
|
- while((0 == (response & SD_R1_READY_FOR_DATA)) && (timeout > 0)) {
|
|
|
+ uint32_t start = millis();
|
|
|
+ while((0 == (response & SD_R1_READY_FOR_DATA))) {
|
|
|
/* continue to send CMD13 to polling the state of card until buffer empty or timeout */
|
|
|
- --timeout;
|
|
|
+ if((uint32_t)(millis() - start) > 1000) {
|
|
|
+ return SD_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(0);
|
|
|
+ }
|
|
|
/* send CMD13(SEND_STATUS), addressed card sends its status registers */
|
|
|
sdio_command_response_config(SD_CMD_SEND_STATUS, (uint32_t)sd_rca << SD_RCA_SHIFT, SDIO_RESPONSETYPE_SHORT);
|
|
|
sdio_wait_type_set(SDIO_WAITTYPE_NO);
|
|
|
@@ -807,9 +825,6 @@ sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_
|
|
|
}
|
|
|
response = sdio_response_get(SDIO_RESPONSE0);
|
|
|
}
|
|
|
- if(0 == timeout) {
|
|
|
- return SD_ERROR;
|
|
|
- }
|
|
|
|
|
|
/* send CMD24(WRITE_BLOCK) to write a block */
|
|
|
sdio_command_response_config(SD_CMD_WRITE_BLOCK, writeaddr, SDIO_RESPONSETYPE_SHORT);
|
|
|
@@ -877,14 +892,22 @@ sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_
|
|
|
dma_transfer_config(pwritebuffer, blocksize);
|
|
|
sdio_dma_enable();
|
|
|
|
|
|
- timeout = 100000;
|
|
|
- while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF)) && (timeout > 0)) {
|
|
|
- timeout--;
|
|
|
- if(0 == timeout) {
|
|
|
+ uint32_t start = millis();
|
|
|
+ while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF))) {
|
|
|
+ if((uint32_t)(millis() - start) > 1000) {
|
|
|
return SD_ERROR;
|
|
|
}
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ uint32_t complete = (blocksize - DMA_CHCNT(DMA1, DMA_CH3) * 4);
|
|
|
+ callback(complete);
|
|
|
+ }
|
|
|
}
|
|
|
while((0 == transend) && (SD_OK == transerror)) {
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(blocksize);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if(SD_OK != transerror) {
|
|
|
@@ -900,6 +923,10 @@ sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_
|
|
|
/* get the card state and wait the card is out of programming and receiving state */
|
|
|
status = sd_card_state_get(&cardstate);
|
|
|
while((SD_OK == status) && ((SD_CARDSTATE_PROGRAMMING == cardstate) || (SD_CARDSTATE_RECEIVING == cardstate))) {
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(blocksize);
|
|
|
+ }
|
|
|
status = sd_card_state_get(&cardstate);
|
|
|
}
|
|
|
return status;
|
|
|
@@ -914,14 +941,13 @@ sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_
|
|
|
\param[out] none
|
|
|
\retval sd_error_enum
|
|
|
*/
|
|
|
-sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize, uint32_t blocksnumber)
|
|
|
+sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint64_t writeaddr, uint16_t blocksize, uint32_t blocksnumber, sdio_callback_t callback)
|
|
|
{
|
|
|
/* initialize the variables */
|
|
|
sd_error_enum status = SD_OK;
|
|
|
uint8_t cardstate = 0;
|
|
|
uint32_t count = 0, align = 0, datablksize = SDIO_DATABLOCKSIZE_1BYTE, *ptempbuff = pwritebuffer;
|
|
|
uint32_t transbytes = 0, restwords = 0;
|
|
|
- __IO uint32_t timeout = 0;
|
|
|
|
|
|
if(NULL == pwritebuffer) {
|
|
|
status = SD_PARAMETER_INVALID;
|
|
|
@@ -1087,14 +1113,22 @@ sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, u
|
|
|
sdio_dma_enable();
|
|
|
dma_transfer_config(pwritebuffer, totalnumber_bytes);
|
|
|
|
|
|
- timeout = 200000;
|
|
|
- while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF) && (timeout > 0))) {
|
|
|
- timeout--;
|
|
|
- if(0 == timeout) {
|
|
|
+ uint32_t start = millis();
|
|
|
+ while((RESET == dma_flag_get(DMA1, DMA_CH3, DMA_FLAG_FTF))) {
|
|
|
+ if((uint32_t)(millis() - start) > 1000) {
|
|
|
return SD_ERROR;
|
|
|
}
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ uint32_t complete = (totalnumber_bytes - DMA_CHCNT(DMA1, DMA_CH3) * 4);
|
|
|
+ callback(complete);
|
|
|
+ }
|
|
|
}
|
|
|
while((0 == transend) && (SD_OK == transerror)) {
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(totalnumber_bytes);
|
|
|
+ }
|
|
|
}
|
|
|
if(SD_OK != transerror) {
|
|
|
return transerror;
|
|
|
@@ -1110,6 +1144,10 @@ sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, u
|
|
|
/* get the card state and wait the card is out of programming and receiving state */
|
|
|
status = sd_card_state_get(&cardstate);
|
|
|
while((SD_OK == status) && ((SD_CARDSTATE_PROGRAMMING == cardstate) || (SD_CARDSTATE_RECEIVING == cardstate))) {
|
|
|
+ if (callback)
|
|
|
+ {
|
|
|
+ callback(totalnumber_bytes);
|
|
|
+ }
|
|
|
status = sd_card_state_get(&cardstate);
|
|
|
}
|
|
|
return status;
|
|
|
@@ -1122,7 +1160,7 @@ sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, u
|
|
|
\param[out] none
|
|
|
\retval sd_error_enum
|
|
|
*/
|
|
|
-sd_error_enum sd_erase(uint32_t startaddr, uint32_t endaddr)
|
|
|
+sd_error_enum sd_erase(uint64_t startaddr, uint64_t endaddr)
|
|
|
{
|
|
|
/* initialize the variables */
|
|
|
sd_error_enum status = SD_OK;
|
|
|
@@ -1211,7 +1249,7 @@ sd_error_enum sd_interrupts_process(void)
|
|
|
{
|
|
|
transerror = SD_OK;
|
|
|
if(RESET != sdio_interrupt_flag_get(SDIO_INT_FLAG_DTEND)) {
|
|
|
- /* send CMD12 to stop data transfer in multipule blocks operation */
|
|
|
+ /* send CMD12 to stop data transfer in multiple blocks operation */
|
|
|
if(1 == stopcondition) {
|
|
|
transerror = sd_transfer_stop();
|
|
|
} else {
|
|
|
@@ -2117,7 +2155,7 @@ static sd_error_enum r7_error_check(void)
|
|
|
\param[out] pcardstate: a pointer that store the card state
|
|
|
\arg SD_CARDSTATE_IDLE: card is in idle state
|
|
|
\arg SD_CARDSTATE_READY: card is in ready state
|
|
|
- \arg SD_CARDSTATE_IDENTIFICAT: card is in identificat state
|
|
|
+ \arg SD_CARDSTATE_IDENTIFICAT: card is in identification state
|
|
|
\arg SD_CARDSTATE_STANDBY: card is in standby state
|
|
|
\arg SD_CARDSTATE_TRANSFER: card is in transfer state
|
|
|
\arg SD_CARDSTATE_DATA: card is in data state
|