RM新时代网站-首页

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

【GD32F303紅楓派開發(fā)板使用手冊】第二十三講 SDIO-SD卡讀寫實(shí)驗(yàn)

聚沃科技 ? 2024-06-23 10:49 ? 次閱讀

23.1實(shí)驗(yàn)內(nèi)容

通過本實(shí)驗(yàn)主要學(xué)習(xí)以下內(nèi)容:

  • SDIO操作原理
  • SD卡讀寫實(shí)現(xiàn)

23.2實(shí)驗(yàn)原理

SD卡是一種主要以Nand Flash作為存儲介質(zhì),具有體積小、數(shù)據(jù)傳輸速度快以及支持熱插拔的優(yōu)點(diǎn)。如今,已被廣泛應(yīng)用于數(shù)碼相機(jī)、便攜式移動設(shè)備以及手機(jī)等多種設(shè)備中。SD卡的驅(qū)動一般有SPI接口或SDIO接口,本例程介紹使用GD32F303的SDIO接口驅(qū)動SD卡的實(shí)現(xiàn)。

23.2.1SD卡基礎(chǔ)知識

SD卡:secure digital memory card是一種安全存儲器件。屬性是快閃存儲器(flash eeprom),功能用來存儲數(shù)據(jù)。

wKgaomZIBOOAW0SvAAdIqgQyiAM171.png

SD卡雖然是薄薄的一片,但是它并不是一個整體,而是由大量的集成電路組成。SD卡的內(nèi)部結(jié)構(gòu)如下圖所示,主要由信號端子,接口控制器和存儲區(qū)組成。

wKgZomZIBO-Af-2eAANX9e0f8TQ689.png

SD卡主要有兩種模式,SD模式和SPI模式。不同模式下,接口定義不同。下面是SD卡的引腳。

wKgZomZIBPqAUdemAAEl4DiwNvo259.png

兩種模式的接口定義如下

wKgaomZIBQ2ANbo3AABUvvoNqj0000.png

SD模式中,主要由VCC(電源),VSS(GND),CLK(時鐘,由主控提供),CMD(命令),DAT0-3(數(shù)據(jù)輸入輸出),由6線制組成進(jìn)行通信。SPI模式,主要采用4線制通信,除了電源地外,由MISO,MOSI,CLK,CS組成。下面簡單介紹SD模式的操作。

要驅(qū)動SD卡工作,主要涉及兩個步驟。第一個步驟是SD卡的識別過程。第二個步驟是對SD卡進(jìn)行讀寫過程,即主機(jī)控制器和SD卡之間進(jìn)行數(shù)據(jù)傳輸?shù)倪^程。
要使SD卡能正常工作,一是要給SD卡供給穩(wěn)定的電壓,二是要SD卡按用戶規(guī)定的方式工作。這兩項(xiàng)工作的實(shí)現(xiàn),都是主機(jī)控制器通過給SD卡發(fā)送控制命令來實(shí)現(xiàn)的。
主機(jī)(SDIO控制器)要驅(qū)動SD卡工作,要使用許多的命令,包括應(yīng)用層命令ACMD和 通用命令CMD.主機(jī)(SDIO控制器)把命令發(fā)送給SD卡,SD卡會作出回應(yīng),這里的回應(yīng)叫做響應(yīng),響應(yīng)命令分為6類,分別是R1、R1b、R2、R3、R6、R7。主機(jī)(SDIO控制器)給SD卡發(fā)送命令之后,SD卡會作出響應(yīng),響應(yīng)中包含主機(jī)(SDIO控制器)需要的數(shù)據(jù),這些數(shù)據(jù)有SD的信息,容量,和存儲數(shù)據(jù)等等。上面已經(jīng)提到了,SD卡工作,主要是識別和數(shù)據(jù)傳輸,它的識別過程有些復(fù)雜,寫代碼的時候,可以參考協(xié)議給的初始化流程圖。數(shù)據(jù)傳輸包括讀和寫,單字節(jié)和多字節(jié)讀寫。下兩節(jié)描述識別初始化流程圖和數(shù)據(jù)讀寫時序圖。

1、讀寫數(shù)據(jù)的時序圖

SDIO與SD卡通信一般以數(shù)據(jù)塊的形式進(jìn)行傳輸,SDIO(多)數(shù)據(jù)塊讀操作,如下圖所示。

wKgZomZIBROAPfxhAADJJJ3oGpU355.png

SDIO(多)數(shù)據(jù)塊寫操作,如下圖所示。

wKgZomZIBR6AYuTMAAD10Do1lh0487.png

2、命令格式
SDIO所有的命令和響應(yīng)都是在SDIO_CMD引腳上面?zhèn)鬏數(shù)模铋L度固定為48位,SDIO命令格式如下表所示。

wKgaomZIBSqAJfA6AAF9fE5Zsr8990.png

3、寄存器
SDIO控制器的寄存器,主要設(shè)置SDIO控制器和命令的索引參數(shù)。SD卡有5個寄存器CID,RCA,CSD,SCR.OCR。SD卡的信息從SD卡寄存器中獲取。

SD卡正常工作,就是根據(jù)SD卡初始化流程圖,發(fā)送命令,收到回復(fù),直到流程結(jié)束。傳輸數(shù)據(jù),也是根據(jù)讀寫時序圖,將要發(fā)送的數(shù)據(jù)放進(jìn)命令中發(fā)送出去。

23.2.2SDIO模塊原理

SDIO為安全的數(shù)字輸入輸出接口,可以用于驅(qū)動SD卡、EMMC等,主要特征如下:

? MMC: 與多媒體卡系統(tǒng)規(guī)格書V4.2及之前的版本全兼容。有三種不同的數(shù)據(jù)總線模式:1位(默認(rèn))、4位和8位;
? SD卡: 與SD存儲卡規(guī)格版本2.0全兼容;
? SD I/O: 與SD I/O卡規(guī)格版本2.0全兼容,有兩種不同的數(shù)據(jù)總線模式:1位(默認(rèn))和4位;
? CE-ATA: 與CE-ATA數(shù)字協(xié)議版本1.1全兼容;
? 48MHz數(shù)據(jù)傳輸頻率和8位數(shù)據(jù)傳輸模式;
?中斷和DMA請求;
?完成信號使能和失能(CE-ATA)。

SDIO模塊結(jié)構(gòu)框圖如下所示。主要包含兩大部分:SDIO適配器:由控制單元、命令單元和數(shù)據(jù)單元組成,控制單元管理時鐘信號,命令單元管理命令的傳輸,數(shù)據(jù)單元管理數(shù)據(jù)的傳輸;AHB接口:包括通過AHB總線訪問的寄存器、用于數(shù)據(jù)傳輸?shù)腇IFO單元以及產(chǎn)生中斷和DMA請求信號。

wKgaomZIBTaABdY_AAESRS7LgzA785.png

SDIO模塊可以實(shí)現(xiàn)對SD卡的完全驅(qū)動以及協(xié)議的實(shí)現(xiàn),包括命令、響應(yīng)等相關(guān)操作,本例程實(shí)現(xiàn)使用SDIO驅(qū)動SD卡初始化以及讀寫測試等相關(guān)操作,具體實(shí)現(xiàn)可以參考GD32F303用戶手冊以及代碼解析等。

23.3硬件設(shè)計(jì)

SD卡相關(guān)硬件電路如下圖所示,實(shí)驗(yàn)板上具有SD卡卡座,信號線上有四根數(shù)據(jù)線,一根CMD命令線以及一根CLK時鐘線,所有信號線通過10K電阻進(jìn)行上拉,電源地信號線具有10uf以及100nf電容,SD卡插入時,金屬接觸點(diǎn)朝下插入。

wKgZomZ3jEOAN55jAAHxfCvJUGI690.png

23.4代碼解析

23.4.1SDIO初始化配置函數(shù)

SDIO初始化配置在sd_io_init()函數(shù)中,其中包括sd_init()初始化、sd_card_information_get()SD卡信息獲取、sd_card_select_deselect()SD卡選擇、sd_cardstatus_get()SD卡狀態(tài)獲取、sd_bus_mode_config()SD卡總線寬度配置以及sd_transfer_mode_config()SD卡通信模式配置,歷程中選擇了4線查詢模式。

C
sd_error_enum sd_io_init(void)
{
sd_error_enum status = SD_OK;
uint32_t cardstate = 0;
status = sd_init();
if(SD_OK == status){
status = sd_card_information_get(&sd_cardinfo);
}
if(SD_OK == status){
status = sd_card_select_deselect(sd_cardinfo.card_rca);
}
status = sd_cardstatus_get(&cardstate);
if(cardstate & 0x02000000){
// printf("\r\n the card is locked!");
while (1){
}
}
if ((SD_OK == status) && (!(cardstate & 0x02000000)))
{
/* set bus mode */
status = sd_bus_mode_config(SDIO_BUSMODE_4BIT);
// status = sd_bus_mode_config( SDIO_BUSMODE_1BIT );
}
if (SD_OK == status)
{
/* set data transfer mode */
// status = sd_transfer_mode_config( SD_DMA_MODE );
status = sd_transfer_mode_config( SD_POLLING_MODE );
}
return status;
}

23.4.2獲取SD卡信息函數(shù)

獲取SD卡信息的函數(shù)如下所示,card_info_get()。

C
void card_info_get(void)
{
uint8_t sd_spec, sd_spec3, sd_spec4, sd_security;
uint32_t block_count, block_size;
uint16_t temp_ccc;
//printf("\r\n Card information:");
sd_spec = (sd_scr[1] & 0x0F000000) >> 24;
sd_spec3 = (sd_scr[1] & 0x00008000) >> 15;
sd_spec4 = (sd_scr[1] & 0x00000400) >> 10;
if(2 == sd_spec)
{
if(1 == sd_spec3)
{
if(1 == sd_spec4)
{
// printf("\r\n## Card version 4.xx ##");
}
else
{
// printf("\r\n## Card version 3.0x ##");
}
}
else
{
// printf("\r\n## Card version 2.00 ##");
}
}
else if(1 == sd_spec)
{
// printf("\r\n## Card version 1.10 ##");
}
else if(0 == sd_spec)
{
// printf("\r\n## Card version 1.0x ##");
}

sd_security = (sd_scr[1] & 0x00700000) >> 20;
if(2 == sd_security)
{
// printf("\r\n## SDSC card ##");
}
else if(3 == sd_security)
{
// printf("\r\n## SDHC card ##");
}
else if(4 == sd_security)
{
// printf("\r\n## SDXC card ##");
}

block_count = (sd_cardinfo.card_csd.c_size + 1)*1024;
block_size = 512;
// printf("\r\n## Device size is %dKB ##", sd_card_capacity_get());
// printf("\r\n## Block size is %dB ##", block_size);
// printf("\r\n## Block count is %d ##", block_count);

if(sd_cardinfo.card_csd.read_bl_partial){
// printf("\r\n## Partial blocks for read allowed ##" );
}
if(sd_cardinfo.card_csd.write_bl_partial){
// printf("\r\n## Partial blocks for write allowed ##" );
}
temp_ccc = sd_cardinfo.card_csd.ccc;
//printf("\r\n## CardCommandClasses is: %x ##", temp_ccc);
if((SD_CCC_BLOCK_READ & temp_ccc) && (SD_CCC_BLOCK_WRITE & temp_ccc)){
// printf("\r\n## Block operation supported ##");
}
if(SD_CCC_ERASE & temp_ccc){
// printf("\r\n## Erase supported ##");
}
if(SD_CCC_WRITE_PROTECTION & temp_ccc){
// printf("\r\n## Write protection supported ##");
}
if(SD_CCC_LOCK_CARD & temp_ccc){
// printf("\r\n## Lock unlock supported ##");
}
if(SD_CCC_APPLICATION_SPECIFIC & temp_ccc){
// printf("\r\n## Application specific supported ##");
}
if(SD_CCC_IO_MODE & temp_ccc){
// printf("\r\n## I/O mode supported ##");
}
if(SD_CCC_SWITCH & temp_ccc){
// printf("\r\n## Switch function supported ##");
}
}

23.4.3SD卡數(shù)據(jù)塊寫入函數(shù)

SD卡數(shù)據(jù)塊寫入函數(shù)如下所示,通過該函數(shù)可實(shí)現(xiàn)SD卡數(shù)據(jù)塊的數(shù)據(jù)寫入。

C
sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize)
{
/* 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;
return status;
}

transerror = SD_OK;
transend = 0;
totalnumber_bytes = 0;
/* clear all DSM configuration */
sdio_data_config(0, 0, SDIO_DATABLOCKSIZE_1BYTE);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOCARD);
sdio_dsm_disable();
sdio_dma_disable();

/* check whether the card is locked */
if(sdio_response_get(SDIO_RESPONSE0) & SD_CARDSTATE_LOCKED){
status = SD_LOCK_UNLOCK_FAILED;
return status;
}

/* blocksize is fixed in 512B for SDHC card */
if(SDIO_HIGH_CAPACITY_SD_CARD == cardtype){
blocksize = 512;
writeaddr /= 512;
}

align = blocksize & (blocksize - 1);
if((blocksize > 0) && (blocksize <= 2048) && (0 == align)){
datablksize = sd_datablocksize_get(blocksize);
/* send CMD16(SET_BLOCKLEN) to set the block length */
sdio_command_response_config(SD_CMD_SET_BLOCKLEN, (uint32_t)blocksize, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();

/* check if some error occurs */
status = r1_error_check(SD_CMD_SET_BLOCKLEN);
if(SD_OK != status){
return status;
}
}else{
status = SD_PARAMETER_INVALID;
return status;
}

/* 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);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_SEND_STATUS);
if(SD_OK != status){
return status;
}

response = sdio_response_get(SDIO_RESPONSE0);
timeout = 100000;

while((0 == (response & SD_R1_READY_FOR_DATA)) && (timeout > 0)){
/* continue to send CMD13 to polling the state of card until buffer empty or timeout */
--timeout;
/* 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);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_SEND_STATUS);
if(SD_OK != status){
return status;
}
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);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_WRITE_BLOCK);
if(SD_OK != status){
return status;
}

stopcondition = 0;
totalnumber_bytes = blocksize;

/* configure the SDIO data transmission */
sdio_data_config(SD_DATATIMEOUT, totalnumber_bytes, datablksize);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOCARD);
sdio_dsm_enable();

if(SD_POLLING_MODE == transmode){
/* polling mode */
while(!sdio_flag_get(SDIO_FLAG_DTCRCERR | SDIO_FLAG_DTTMOUT | SDIO_FLAG_TXURE | SDIO_FLAG_DTBLKEND | SDIO_FLAG_STBITE)){
if(RESET != sdio_flag_get(SDIO_FLAG_TFH)){
/* at least 8 words can be written into the FIFO */
if((totalnumber_bytes - transbytes) < SD_FIFOHALF_BYTES){
restwords = (totalnumber_bytes - transbytes)/4 + (((totalnumber_bytes - transbytes)%4 == 0) ? 0 : 1);
for(count = 0; count < restwords; count++){
sdio_data_write(*ptempbuff);
++ptempbuff;
transbytes += 4;
}
}else{
for(count = 0; count < SD_FIFOHALF_WORDS; count++){
sdio_data_write(*(ptempbuff + count));
}
/* 8 words(32 bytes) has been transferred */
ptempbuff += SD_FIFOHALF_WORDS;
transbytes += SD_FIFOHALF_BYTES;
}
}
}

/* whether some error occurs and return it */
if(RESET != sdio_flag_get(SDIO_FLAG_DTCRCERR)){
status = SD_DATA_CRC_ERROR;
sdio_flag_clear(SDIO_FLAG_DTCRCERR);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_DTTMOUT)){
status = SD_DATA_TIMEOUT;
sdio_flag_clear(SDIO_FLAG_DTTMOUT);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_TXURE)){
status = SD_TX_UNDERRUN_ERROR;
sdio_flag_clear(SDIO_FLAG_TXURE);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_STBITE)){
status = SD_START_BIT_ERROR;
sdio_flag_clear(SDIO_FLAG_STBITE);
return status;
}
}else if(SD_DMA_MODE == transmode){
/* DMA mode */
/* enable the SDIO corresponding interrupts and DMA */
sdio_interrupt_enable(SDIO_INT_DTCRCERR | SDIO_INT_DTTMOUT | SDIO_INT_TXURE | SDIO_INT_DTEND | SDIO_INT_STBITE);
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){
return SD_ERROR;
}
}
while ((0 == transend) && (SD_OK == transerror)){
}

if(SD_OK != transerror){
return transerror;
}
}else{
status = SD_PARAMETER_INVALID;
return status;
}

/* clear the SDIO_INTC flags */
sdio_flag_clear(SDIO_MASK_INTC_FLAGS);
/* 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))){
status = sd_card_state_get(&cardstate);
}
return status;
}

23.4.4SD卡數(shù)據(jù)塊讀取函數(shù)

SD卡數(shù)據(jù)塊讀取函數(shù)如下所示。

C
sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize)
{
/* 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;
return status;
}

transerror = SD_OK;
transend = 0;
totalnumber_bytes = 0;
/* clear all DSM configuration */
sdio_data_config(0, 0, SDIO_DATABLOCKSIZE_1BYTE);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOCARD);
sdio_dsm_disable();
sdio_dma_disable();

/* check whether the card is locked */
if(sdio_response_get(SDIO_RESPONSE0) & SD_CARDSTATE_LOCKED){
status = SD_LOCK_UNLOCK_FAILED;
return status;
}

/* blocksize is fixed in 512B for SDHC card */
if(SDIO_HIGH_CAPACITY_SD_CARD == cardtype){
blocksize = 512;
readaddr /= 512;
}

align = blocksize & (blocksize - 1);
if((blocksize > 0) && (blocksize <= 2048) && (0 == align)){
datablksize = sd_datablocksize_get(blocksize);
/* send CMD16(SET_BLOCKLEN) to set the block length */
sdio_command_response_config(SD_CMD_SET_BLOCKLEN, (uint32_t)blocksize, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();

/* check if some error occurs */
status = r1_error_check(SD_CMD_SET_BLOCKLEN);
if(SD_OK != status){
return status;
}
}else{
status = SD_PARAMETER_INVALID;
return status;
}

stopcondition = 0;
totalnumber_bytes = blocksize;

/* configure SDIO data transmission */
sdio_data_config(SD_DATATIMEOUT, totalnumber_bytes, datablksize);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOSDIO);
sdio_dsm_enable();

/* send CMD17(READ_SINGLE_BLOCK) to read a block */
sdio_command_response_config(SD_CMD_READ_SINGLE_BLOCK, (uint32_t)readaddr, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_READ_SINGLE_BLOCK);
if(SD_OK != status){
return status;
}

if(SD_POLLING_MODE == transmode){
/* polling mode */
while(!sdio_flag_get(SDIO_FLAG_DTCRCERR | SDIO_FLAG_DTTMOUT | SDIO_FLAG_RXORE | SDIO_FLAG_DTBLKEND | SDIO_FLAG_STBITE)){
if(RESET != sdio_flag_get(SDIO_FLAG_RFH)){
/* at least 8 words can be read in the FIFO */
for(count = 0; count < SD_FIFOHALF_WORDS; count++){
*(ptempbuff + count) = sdio_data_read();
}
ptempbuff += SD_FIFOHALF_WORDS;
}
}

/* whether some error occurs and return it */
if(RESET != sdio_flag_get(SDIO_FLAG_DTCRCERR)){
status = SD_DATA_CRC_ERROR;
sdio_flag_clear(SDIO_FLAG_DTCRCERR);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_DTTMOUT)){
status = SD_DATA_TIMEOUT;
sdio_flag_clear(SDIO_FLAG_DTTMOUT);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_RXORE)){
status = SD_RX_OVERRUN_ERROR;
sdio_flag_clear(SDIO_FLAG_RXORE);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_STBITE)){
status = SD_START_BIT_ERROR;
sdio_flag_clear(SDIO_FLAG_STBITE);
return status;
}
while(RESET != sdio_flag_get(SDIO_FLAG_RXDTVAL)){
*ptempbuff = sdio_data_read();
++ptempbuff;
}
/* clear the SDIO_INTC flags */
sdio_flag_clear(SDIO_MASK_INTC_FLAGS);
}else if(SD_DMA_MODE == transmode){
/* DMA mode */
/* enable the SDIO corresponding interrupts and DMA function */
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){
return SD_ERROR;
}
}
}else{
status = SD_PARAMETER_INVALID;
}
return status;
}

23.4.5SD卡lock和unlock配置函數(shù)

SD卡lock和unlock配置函數(shù)如下所示。通過形參實(shí)現(xiàn)對SD卡的lock和unlock,若希望lock SD卡,lcokstate配置為SD_LOCK;若希望unlock SD卡,lockstate配置為SD_UNLOCK.

C
sd_error_enum sd_lock_unlock(uint8_t lockstate)
{
sd_error_enum status = SD_OK;
uint8_t cardstate = 0, tempbyte = 0;
uint32_t pwd1 = 0, pwd2 = 0, response = 0;
__IO uint32_t timeout = 0;
uint16_t tempccc = 0;

/* get the card command classes from CSD */
tempbyte = (uint8_t)((sd_csd[1] & SD_MASK_24_31BITS) >> 24);
tempccc = (uint16_t)((uint16_t)tempbyte << 4);
tempbyte = (uint8_t)((sd_csd[1] & SD_MASK_16_23BITS) >> 16);
tempccc |= (uint16_t)((uint16_t)(tempbyte & 0xF0) >> 4);

if(0 == (tempccc & SD_CCC_LOCK_CARD)){
/* don't support the lock command */
status = SD_FUNCTION_UNSUPPORTED;
return status;
}
/* password pattern */
pwd1 = (0x01020600|lockstate);
pwd2 = 0x03040506;

/* clear all DSM configuration */
sdio_data_config(0, 0, SDIO_DATABLOCKSIZE_1BYTE);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOCARD);
sdio_dsm_disable();
sdio_dma_disable();

/* send CMD16(SET_BLOCKLEN) to set the block length */
sdio_command_response_config(SD_CMD_SET_BLOCKLEN, (uint32_t)8, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_SET_BLOCKLEN);
if(SD_OK != status){
return status;
}

/* send CMD13(SEND_STATUS), addressed card sends its status register */
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);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_SEND_STATUS);
if(SD_OK != status){
return status;
}

response = sdio_response_get(SDIO_RESPONSE0);
timeout = 100000;
while((0 == (response & SD_R1_READY_FOR_DATA)) && (timeout > 0)){
/* continue to send CMD13 to polling the state of card until buffer empty or timeout */
--timeout;
/* 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);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_SEND_STATUS);
if(SD_OK != status){
return status;
}
response = sdio_response_get(SDIO_RESPONSE0);
}
if(0 == timeout){
return SD_ERROR;
}

/* send CMD42(LOCK_UNLOCK) to set/reset the password or lock/unlock the card */
sdio_command_response_config(SD_CMD_LOCK_UNLOCK, (uint32_t)0x0, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_LOCK_UNLOCK);
if(SD_OK != status){
return status;
}

response = sdio_response_get(SDIO_RESPONSE0);

/* configure the SDIO data transmission */
sdio_data_config(SD_DATATIMEOUT, (uint32_t)8, SDIO_DATABLOCKSIZE_8BYTES);
sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK, SDIO_TRANSDIRECTION_TOCARD);
sdio_dsm_enable();

/* write password pattern */
sdio_data_write(pwd1);
sdio_data_write(pwd2);

/* whether some error occurs and return it */
if(RESET != sdio_flag_get(SDIO_FLAG_DTCRCERR)){
status = SD_DATA_CRC_ERROR;
sdio_flag_clear(SDIO_FLAG_DTCRCERR);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_DTTMOUT)){
status = SD_DATA_TIMEOUT;
sdio_flag_clear(SDIO_FLAG_DTTMOUT);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_TXURE)){
status = SD_TX_UNDERRUN_ERROR;
sdio_flag_clear(SDIO_FLAG_TXURE);
return status;
}else if(RESET != sdio_flag_get(SDIO_FLAG_STBITE)){
status = SD_START_BIT_ERROR;
sdio_flag_clear(SDIO_FLAG_STBITE);
return status;
}

/* clear the SDIO_INTC flags */
sdio_flag_clear(SDIO_MASK_INTC_FLAGS);
/* 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))){
status = sd_card_state_get(&cardstate);
}
return status;
}

23.4.6SD卡erase擦除操作函數(shù)

SD卡擦除操作函數(shù)如下,其形參為擦除起始地址以及結(jié)束地址。

C
sd_error_enum sd_erase(uint32_t startaddr, uint32_t endaddr)
{
/* initialize the variables */
sd_error_enum status = SD_OK;
uint32_t count = 0, clkdiv = 0;
__IO uint32_t delay = 0;
uint8_t cardstate = 0, tempbyte = 0;
uint16_t tempccc = 0;

/* get the card command classes from CSD */
tempbyte = (uint8_t)((sd_csd[1] & SD_MASK_24_31BITS) >> 24);
tempccc = (uint16_t)((uint16_t)tempbyte << 4);
tempbyte = (uint8_t)((sd_csd[1] & SD_MASK_16_23BITS) >> 16);
tempccc |= (uint16_t)((uint16_t)(tempbyte & 0xF0) >> 4);
if(0 == (tempccc & SD_CCC_ERASE)){
/* don't support the erase command */
status = SD_FUNCTION_UNSUPPORTED;
return status;
}
clkdiv = (SDIO_CLKCTL & SDIO_CLKCTL_DIV);
clkdiv += ((SDIO_CLKCTL & SDIO_CLKCTL_DIV8)>>31)*256;
clkdiv += 2;
delay = 120000 / clkdiv;

/* check whether the card is locked */
if (sdio_response_get(SDIO_RESPONSE0) & SD_CARDSTATE_LOCKED){
status = SD_LOCK_UNLOCK_FAILED;
return(status);
}

/* blocksize is fixed in 512B for SDHC card */
if (SDIO_HIGH_CAPACITY_SD_CARD == cardtype){
startaddr /= 512;
endaddr /= 512;
}

if((SDIO_STD_CAPACITY_SD_CARD_V1_1 == cardtype) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == cardtype) ||
(SDIO_HIGH_CAPACITY_SD_CARD == cardtype)){
/* send CMD32(ERASE_WR_BLK_START) to set the address of the first write block to be erased */
sdio_command_response_config(SD_CMD_ERASE_WR_BLK_START, startaddr, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_ERASE_WR_BLK_START);
if(SD_OK != status){
return status;
}

/* send CMD33(ERASE_WR_BLK_END) to set the address of the last write block of the continuous range to be erased */
sdio_command_response_config(SD_CMD_ERASE_WR_BLK_END, endaddr, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_ERASE_WR_BLK_END);
if(SD_OK != status){
return status;
}
}

/* send CMD38(ERASE) to set the address of the first write block to be erased */
sdio_command_response_config(SD_CMD_ERASE, (uint32_t)0x0, SDIO_RESPONSETYPE_SHORT);
sdio_wait_type_set(SDIO_WAITTYPE_NO);
sdio_csm_enable();
/* check if some error occurs */
status = r1_error_check(SD_CMD_ERASE);
if(SD_OK != status){
return status;
}
/* loop until the counter is reach to the calculated time */
for(count = 0; count < delay; count++){
}
/* 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))){
status = sd_card_state_get(&cardstate);
}
return status;
}

23.4.7主函數(shù)

SD卡主函數(shù)如下,可實(shí)現(xiàn)對SD卡的擦寫讀以及加鎖解鎖操作。

C
int main(void)
{
sd_error_enum sd_error;
uint16_t i = 5;
#ifdef DATA_PRINT
uint8_t *pdata;
#endif /* DATA_PRINT */

/* configure the NVIC, USART and LED */
nvic_config();

bsp_uart_init(&BOARD_UART);

/* initialize the card */
do{
sd_error = sd_io_init();
}while((SD_OK != sd_error) && (--i));

if(i){
printf("\r\n Card init success!\r\n");
}else{
printf("\r\n Card init failed!\r\n");
while (1){
}
}

/* get the information of the card and print it out by USART */
card_info_get();

/* init the write buffer */
for(i=0; i<512; i++){
buf_write[i] = i;
}

printf("\r\n\r\n Card test:");

/* single block operation test */
sd_error = sd_block_write(buf_write, 100*512, 512);
if(SD_OK != sd_error){

printf("\r\n Block write fail!");
while (1){
}
}else{
printf("\r\n Block write success!");
}
sd_error = sd_block_read(buf_read, 100*512, 512);
if(SD_OK != sd_error){
printf("\r\n Block read fail!");

while (1){
}
}else{
//printf("\r\n Block read success!");
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf("\r\n");
for(i = 0; i < 128; i++){
printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf("\r\n");
}
}
#endif /* DATA_PRINT */
}

/* lock and unlock operation test */
if(SD_CCC_LOCK_CARD & sd_cardinfo.card_csd.ccc){
/* lock the card */
sd_error = sd_lock_unlock(SD_LOCK);
if(SD_OK != sd_error){
printf("\r\n Lock failed!");
while (1){
}
}else{
printf("\r\n The card is locked!");
}
sd_error = sd_erase(100*512, 101*512);
if(SD_OK != sd_error){
printf("\r\n Erase failed!");
}else{
__NOP();
printf("\r\n Erase success!");
}

/* unlock the card */
sd_error = sd_lock_unlock(SD_UNLOCK);
if(SD_OK != sd_error){
printf("\r\n Unlock failed!");
while (1){
}
}else{
printf("\r\n The card is unlocked!");
}
sd_error = sd_erase(100*512, 101*512);
if(SD_OK != sd_error){
printf("\r\n Erase failed!");
}else{
printf("\r\n Erase success!");
}

sd_error = sd_block_read(buf_read, 100*512, 512);
if(SD_OK != sd_error){
printf("\r\n Block read fail!");
while (1){
}
}else{
printf("\r\n Block read success!");
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf("\r\n");
for(i = 0; i < 128; i++){
printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf("\r\n");
}
}
#endif /* DATA_PRINT */
}
}

/* multiple blocks operation test */
sd_error = sd_multiblocks_write(buf_write, 200*512, 512, 3);
if(SD_OK != sd_error){
printf("\r\n Multiple block write fail!");
while (1){
}
}else{
printf("\r\n Multiple block write success!");
}
sd_error = sd_multiblocks_read(buf_read, 200*512, 512, 3);
if(SD_OK != sd_error){
printf("\r\n Multiple block read fail!");

while (1){
}
}else{
printf("\r\n Multiple block read success!");
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf("\r\n");
for(i = 0; i < 512; i++){
printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf("\r\n");
}
}
#endif /* DATA_PRINT */
}

while (1){
}
}

23.5實(shí)驗(yàn)結(jié)果

將SD卡讀寫實(shí)驗(yàn)例程燒錄到紅楓派開發(fā)板中,并在卡槽中插入SD卡,打開對應(yīng)串口復(fù)位芯片,將會觀察到SD卡相關(guān)操作結(jié)果。

wKgZomZ3jNmAanvFAACpvj8BAmQ928.png

教程GD32 MCU方案商聚沃科技原創(chuàng)發(fā)布,了解更多GD32 MCU教程,關(guān)注聚沃科技官網(wǎng)

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 單片機(jī)
    +關(guān)注

    關(guān)注

    6034

    文章

    44545

    瀏覽量

    634357
  • mcu
    mcu
    +關(guān)注

    關(guān)注

    146

    文章

    17108

    瀏覽量

    350885
  • SD卡
    +關(guān)注

    關(guān)注

    2

    文章

    562

    瀏覽量

    63870
  • 開發(fā)板
    +關(guān)注

    關(guān)注

    25

    文章

    5024

    瀏覽量

    97348
收藏 人收藏

    評論

    相關(guān)推薦

    GD32F470紫藤開發(fā)板使用手冊】第十二講 SDIO-SD讀寫實(shí)驗(yàn)

    通過本實(shí)驗(yàn)主要學(xué)習(xí)以下內(nèi)容: ?SDIO操作原理 ?SD讀寫實(shí)現(xiàn)
    的頭像 發(fā)表于 05-18 09:36 ?1348次閱讀
    【<b class='flag-5'>GD32F</b>470紫藤<b class='flag-5'>派</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>使用手冊</b>】第十二講 <b class='flag-5'>SDIO-SD</b><b class='flag-5'>卡</b><b class='flag-5'>讀寫實(shí)驗(yàn)</b>

    眾想科技-劉洋邊講邊寫STM32視頻教程 15.RS232串口通訊實(shí)驗(yàn)

    彩色液晶屏顯示漢字、英文、數(shù)字 買免費(fèi) 第二十一講 SD存儲工作原理 買免費(fèi) 第二十二講
    發(fā)表于 10-12 11:16

    GD32F303】星空介紹

    一、開發(fā)板介紹星空GD開發(fā)板是由旗點(diǎn)科技推出的一款GD32開發(fā)板,板載
    發(fā)表于 09-11 17:55

    【星空GD32F303開發(fā)板試用體驗(yàn)】開箱+環(huán)境搭建

    本帖最后由 lustao 于 2021-10-19 09:29 編輯 感謝 發(fā)燒友學(xué)院以及廣州旗點(diǎn)智能科技有限公司為我和孩子提供此產(chǎn)品星空GD32F303開發(fā)板。收到了星空
    發(fā)表于 10-18 14:15

    【星空GD32F303開發(fā)板試用體驗(yàn)】開箱+環(huán)境搭建

    https://bbs.elecfans.com/jishu_2179209_1_1.html感謝 發(fā)燒友學(xué)院以及廣州旗點(diǎn)智能科技有限公司為我和孩子提供此產(chǎn)品星空gd32F303開發(fā)板。收到了星空
    發(fā)表于 11-02 15:36

    【星空GD32F303開發(fā)板試用體驗(yàn)】+板卡概覽

    本帖最后由 cooldog123pp 于 2021-11-6 21:07 編輯 星空GD開發(fā)板是由旗點(diǎn)科技推出的一款GD32開發(fā)板
    發(fā)表于 11-06 21:05

    【星空GD32F303開發(fā)板試用體驗(yàn)】文件讀寫與數(shù)碼相框的實(shí)現(xiàn) (兼結(jié)題報(bào)告)

    本帖最后由 jinglixixi 于 2021-11-27 01:23 編輯 星空開發(fā)板是旗點(diǎn)科技推出的一款以GD32F303ZET6為核心的開發(fā)板,該
    發(fā)表于 11-26 12:05

    星空GD32F303開發(fā)板的相關(guān)資料下載

    一、開發(fā)板介紹星空GD開發(fā)板是由旗點(diǎn)科技推出的一款GD32開發(fā)板,板載
    發(fā)表于 12-10 08:27

    第二十三講 異步計(jì)數(shù)器

    第二十三講 異步計(jì)數(shù)器 概述一、計(jì)數(shù)器:用以統(tǒng)計(jì)輸入計(jì)數(shù)脈沖CP個數(shù)的電路。計(jì)數(shù)器的“?!保ㄓ肕表示): 二、 計(jì)數(shù)器的分類1.按計(jì)數(shù)進(jìn)制分2.按計(jì)數(shù)
    發(fā)表于 03-30 16:27 ?1.2w次閱讀
    <b class='flag-5'>第二十三講</b> 異步計(jì)數(shù)器

    模擬電路網(wǎng)絡(luò)課件 第二十三節(jié):乙類雙電源互補(bǔ)對稱功率放大電路

    模擬電路網(wǎng)絡(luò)課件 第二十三節(jié):乙類雙電源互補(bǔ)對稱功率放大電路 5.2.1 電路的組成
    發(fā)表于 09-17 08:56 ?2044次閱讀

    2017中國西部微波射頻技術(shù)研討會暨第二十三屆國際電子測試測量研討會

    2017中國西部微波射頻技術(shù)研討會暨第二十三屆國際電子測試測量研討會
    發(fā)表于 07-05 15:12 ?1016次閱讀

    【正點(diǎn)原子FPGA連載】第二十三章RGB-LCD字符和圖片顯示實(shí)驗(yàn) -摘自【正點(diǎn)原子】新起點(diǎn)之FPGA開發(fā)指南_V2.1

    【正點(diǎn)原子FPGA連載】第二十三章RGB-LCD字符和圖片顯示實(shí)驗(yàn) -摘自【正點(diǎn)原子】新起點(diǎn)之FPGA開發(fā)指南_V2.1
    發(fā)表于 11-24 14:36 ?13次下載
    【正點(diǎn)原子FPGA連載】<b class='flag-5'>第二十三</b>章RGB-LCD字符和圖片顯示<b class='flag-5'>實(shí)驗(yàn)</b> -摘自【正點(diǎn)原子】新起點(diǎn)之FPGA<b class='flag-5'>開發(fā)</b>指南_V2.1

    GD32F303紅楓開發(fā)板使用手冊第二講 GPIO-流水燈實(shí)驗(yàn)

    GD32F303系列MCU最多可支持?112?個通用I/O?引腳(GPIO),分別為?PA0 ~ PA15,?PB0 ~ PB15,?PC0 ~ PC15,PD0 ~ PD15,?PE0
    的頭像 發(fā)表于 05-29 10:02 ?1613次閱讀
    【<b class='flag-5'>GD32F303</b><b class='flag-5'>紅楓</b><b class='flag-5'>派</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>使用手冊</b>】<b class='flag-5'>第二</b>講 GPIO-流水燈<b class='flag-5'>實(shí)驗(yàn)</b>

    GD32H757Z海棠開發(fā)板使用手冊】第十二講 SDIO-SD讀寫實(shí)驗(yàn)

    SD是一種主要以Nand Flash作為存儲介質(zhì),具有體積小、數(shù)據(jù)傳輸速度快以及支持熱插拔的優(yōu)點(diǎn)。如今,已被廣泛應(yīng)用于數(shù)碼相機(jī)、便攜式移動設(shè)備以及手機(jī)等多種設(shè)備中。SD的驅(qū)動一般有
    的頭像 發(fā)表于 06-05 10:35 ?678次閱讀
    【<b class='flag-5'>GD</b>32H757Z海棠<b class='flag-5'>派</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>使用手冊</b>】第十二講 <b class='flag-5'>SDIO-SD</b><b class='flag-5'>卡</b><b class='flag-5'>讀寫實(shí)驗(yàn)</b>

    GD32F303紅楓開發(fā)板使用手冊第二十講 SPI-SPI NAND FLASH讀寫實(shí)驗(yàn)

    通過本實(shí)驗(yàn)主要學(xué)習(xí)以下內(nèi)容: ?SPI通信協(xié)議,參考19.2.1東方紅開發(fā)板使用手冊 ?GD32F303 SPI操作方式,參考19.2.2東方紅
    的頭像 發(fā)表于 06-20 09:50 ?871次閱讀
    【<b class='flag-5'>GD32F303</b><b class='flag-5'>紅楓</b><b class='flag-5'>派</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>使用手冊</b>】<b class='flag-5'>第二十</b>講 SPI-SPI NAND FLASH<b class='flag-5'>讀寫實(shí)驗(yàn)</b>
    RM新时代网站-首页