1024programmer News SD card experiment source code explanation

SD card experiment source code explanation

The connection relationship between the onboard SD card interface of the explorer STM32F4 development board and STM32F4 is shown in the figure:

Source code explanation:

SD_Init function in sdio_sdcard.c:

This function first implements SDIO clock and Initialize the relevant IO ports, and then start the initialization process of the SD card. First, through the SD_PowerON function, we will complete the power on of the SD card and obtain the type of SD card (SDHC/SDSC/SDV1.x/MMC), and then call the SD_InitializeCards function to complete the initialization of the SD card.

SD_InitializeCards function:

SD_InitializeCards function mainly sends CMD2 and CMD3 to obtain the CID register content and the relative address (RCA) of the SD card, and obtains the CSD register content through CMD9. At this point, the initialization of the SD card has actually been completed. Subsequently, the SD_Init function calls the SD_GetCardInfo function to obtain SD card related information, and then calls the SD_SelectDeselect function to select the card to be operated (CMD7+RCA), and sets the SDIO data bit width to 4 bits through the SD_EnableWideBusOperation function (but the MMC card can only 1-bit mode is supported!). Finally set the frequency of SDIO_CK clock and set the working mode (DMA/Polling).

SD card read block function: SD_ReadBlock, this function is used to read a block (sector) data from the SD card specified address:

This function first sends CMD16 for setting block size, and then configure the length of data read by the SDIO controller. Here we use the function convert_from_bytes_to_power_of_two to find the exponent of blksize with base 2, which is used to set the length of SDIO read data. Then send CMD17 (with address parameter addr) to read a block of data from the specified address. Finally, according to the mode we set (query mode/DMA mode), read data from SDIO_FIFO.

There are two points to note about this function: 1. The addr parameter type is long long to support cards larger than 4G, otherwise there may be problems when operating cards larger than 4G! ! ! 2. In polling mode, when reading and writing FIFO, any interruption is strictly prohibited, otherwise it may cause errors in reading and writing data! ! Therefore, the INTX_DISABLE function is used to turn off the general interrupt, and the general interrupt is turned on after the FIFO read and write operations are completed (INTX_ENABLE function setting).

In addition, there are three underlying read and write functions: SD_ReadMultiBlocks, for multi-block reading; SD_WriteBlock, for single-block writing; SD_WriteMultiBlocks, for multi-block writing; remind again: no matter which function, its data The address of buf must be 4-byte aligned!

Two interface functions between SDIO and file system: SD_ReadDisk and SD_WriteDisk:

//read SD card
//buf: Read data buffer area
//sector: sector address
//cnt: sector number
//return value: error status; 0, normal; other, error code;
u8 SD_ReadDisk (u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
long long lsector=sector;
u8 n;
lsector<<=9;
if((u32)buf%4!=0)
{
for(n=0;n<cnt;n++)
{
sta=SD_ReadBlock(SDIO_DATA_BUFFER,lsector+512*n, 512);//Single sector read operation
memcpy(buf,SDIO_DATA_BUFFER,512);
buf+=512;
}
}else
{
if(cnt== 1) sta=SD_ReadBlock(buf,lsector,512); //read operation of a single sector
else sta=SD_ReadMultiBlocks(buf,lsector,512,cnt);//multiple sectors
}
return sta;
}
//Write SD card
//buf: write data buffer area
//sector: sector address
//cnt: number of sectors
/ /return value: error status; 0, normal; other, error code;
u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
u8 n;
long long lsector=sector;
lsector<<=9;
if((u32)buf%4!=0)
{
for(n=0;n<cnt; n++)
{
memcpy(SDIO_DATA_BUFFER,buf,512);
sta=SD_WriteBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//single sector write
buf+=512;
}
}else
{
if(cnt==1)sta=SD_WriteBlock(buf,lsector,512); //Single sector write operation
else sta=SD_WriteMultiBlocks(buf, lsector,512,cnt); //Multiple sectors
}
return sta;
}

SD_ReadDisk is used to read data, by calling SD_ReadBlock and SD_ReadMultiBlocks implementation. SD_WriteDisk is used to write data by calling SD_WriteBlock and SD_WriteMultiBlocks. Note, because the data buffer address provided by FATFS to SD_ReadDisk or SD_WriteDisk is not necessarily 4-byte aligned, so we make a 4-byte alignment judgment in these two functions. If it is not 4-byte aligned, pass a The 4-byte alignment buffer (SDIO_DATA_BUFFER) is used as data transition to ensure that the buf passed to the underlying read and write functions is 4-byte aligned.

main.c file:

//Print SD card related information through serial port
void show_sdcard_info(void)
{
switch(SDCardInfo.CardType)
{
case SDIO_STD_CAPACITY_SD_CARD_V1_1:
printf("Card Type:SDSC V1.1\r\n");break;
case SDIO_STD_CAPACITY_SD_CARD_V2_0:
printf("Card Type:SDSC V2.0\r\n");break;
case SDIO_HIGH_CAPACITY_SD_CARD:
printf("Card Type:SDHC V2.0\r\n");break;
case SDIO_MULTIMEDIA_CARD:
printf("Card Type:MMC Card\r\n");break;
}printf("Card ManufacturerID:%d\r\n",SDCardInfo.SD_cid.ManufacturerID);// Manufacturer ID
printf("Card RCA:%d\r\n",SDCardInfo.RCA);//card relative address
printf("Card Capacity:%d MB\r\n",(u32)(SDCardInfo.CardCapacity>>20));//display capacity
printf( "Card BlockSize:%d\r\n\r\n",SDCardInfo.CardBlockSize);//display block size
}
int main(void)
{
u8 key; u8 t =0; u8 *buf;
u32 sd_size;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//Set system interrupt priority group 2
delay_init(168); //Initialize delay function
uart_init(115200 ); //Initialize serial port baud rate to 115200
LED_Init(); //Initialize LED
LCD_Init(); //Initialize LCD
KEY_Init(); //Initialize keys
my_mem_init(SRAMIN ); //Initialize the internal memory pool
my_mem_init(SRAMCCM); //Initialize the CCM memory pool
POINT_COLOR=RED;//Set the font to red
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"SD CARD TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200 ,16,16,"2014/5/15");
LCD_ShowString(30,130,200,16,16,"KEY0:Read Sector 0");
while(SD_Init())//No SD card detected
{
LCD_ShowString(30,150,200,16,16,"SD Card Error!"); delay_ms(500);
LCD_ShowString(30,150,200,16,16,"Please Check!"); delay_ms(500) ;
LED0=!LED0;//DS0 blinks
}
show_sdcard_info(); //Print SD card related information
POINT_COLOR=BLUE; //Set font to blue
// SD card detected successfully
LCD_ShowString(30,150,200,16,16,"SD Card OK ");
LCD_ShowString(30,170,200,16,16,"SD Card Size: MB");
LCD_ShowNum(30+13 *8,170,SDCardInfo.CardCapacity>>20,5,16);//Display SD card capacity
while(1)
{
key=KEY_Scan(0);
if(key== KEY0_PRES)//KEY0 is pressed
{
buf=mymalloc(0,512); //Apply for memory
if(SD_ReadDisk(buf,0,1)==0) //Read 0 sector
{
LCD_ShowString(30,190,200,16,16,"USART1 Sending Data...");
printf("SECTOR 0 DATA:\r\n");
for(sd_size =0;sd_size<512;sd_size++)printf("%x ",buf[sd_size]);//Sector data
printf("\r\nDATA ENDED\r\n");
LCD_ShowString( 30,190,200,16,16,"USART1 Send Data Over!");
}
myfree(0,buf);//Release memory
}
t++;
delay_ms(10);
if(t==20) { LED0=!LED0; t=0;}
}
}

There are 2 functions here, show_sdcard_info The function is used to output SD card related information from the serial port. The main function first initializes the SD card, and if the initialization is successful, it calls the show_sdcard_info function to output the relevant information of the SD card and display the capacity of the SD card on the LCD. Then enter an infinite loop, if the key KEY0 is pressed, read sector 0 of the SD card (physical disk, sector 0) through SD_ReadDisk, and print out the data through the serial port.

>

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/sd-card-experiment-source-code-explanation/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索