RISC-V MCU Development Practice (Part 2): Initial Application of SD
[Copy link]
Software platform: MounRiver Studio (MRS), hardware platform: CH32V103 development board, and an SD card less than 32G.
1. Introduction to SD Card
SD memory card (Secure Digital Memory Card) is a new generation of high-speed storage device based on semiconductor flash memory. The technology of SD memory card is developed from MMC card (MultiMedia Card format. SDIO (SD Input/Output) card is developed based on the compatibility of SD memory card. This compatibility includes mechanics, electronics, electricity, signals and software. SD and SDIO cards are usually referred to as SD memory cards.
SD cards have high memory capacity, fast data transfer rate, great mobility and good security. They are widely used in portable devices such as digital cameras, tablet computers and multimedia players. According to the size of SD cards, SD cards can be divided into the following categories:
0~2G : SD card; 2~32G: SDHC card; 32~2T: SDXC card;
Among them, the SD card protocol and SDHC card version protocol are basically compatible, but the SDXC card is quite different from the SD card and SDHC card. This article mainly introduces the use of SD card and SDHC card.
There are generally two communication interfaces available for the controller to read and write SD cards, one is the SPI interface and the other is the SDIO interface. Compared with the SDIO driving SD card method, the SPI protocol driving SD card method uses fewer pin resources and the control program is relatively simple, but the transmission speed is not as fast as using the SDIO interface. Since the connection method between the SD card slot and the chip pin on the CH32V103 development board is the SPI driving method, this article uses the SPI protocol driving SD card method.
The following registers are required to drive the SD card using SPI:
① CID: Card identification number, a unique individual number used to identify the card;
② CSD: Card Specific Data, which refers to the card's operating condition information;
③ SCR: SD Configuration Register, SD card specific information;
④ OCR: Operation conditions register
For other SD card register information, please refer to the SD simplified specification document "Physical Layer Simplified Specification V2.0".
Regarding the SD command format, it is fixed at 48 bits and is transmitted continuously through the command line (CMD). The specific command format and command explanation are as follows:
Start bit and end bit: The body of the command is contained between the start bit and the end bit, and they both contain only one data bit, the start bit is 0 and the end bit is 1.
Transfer flag: used to distinguish the transfer direction. When this bit is 1, it indicates a command, and the direction is from the host to the SD card. When this bit is 0, it indicates a response, and the direction is from the SD card to the host.
The command body includes three parts: command, address information/parameters and CRC check.
1) Command number: It occupies 6 bits, so there are 64 commands in total (code: CMD0~CMD63). Each command has a specific purpose. Some commands are not applicable to the SPI bus or SD card operations, but are specifically used for MMC cards or SDI/O cards.
2) Address/parameter: Each command has 32 bits of address information/parameters for additional command content. For example, the broadcast command has no address information, and these 32 bits are used to specify parameters, while the addressing command uses these 32 bits to specify the address of the target SD card. When using the SPI bus driver, different cards are selected through the chip select pin, so when using these commands, the address can be filled with any value.
3) CRC7 check: The 7-bit check bit is used to verify the correctness of the command transmission content. If external interference causes the state of individual bits of the transmitted data to change, the calibration will fail, which also means that the command transmission fails and the SD card does not execute the command. When using the SPI driver, the CRC7 check in the command is turned off by default, that is, any value can be written to the CRC7 check bit without affecting communication. Only when sending the CMD0 command, the standard CRC7 check is required.
There are several important operation instructions in SPI mode:
The SD card R1 response format is as follows:
For specific information about SD cards, please refer to "Physical Layer Simplified Specification V2.0".
2. Development Process in MRS
- First, create a new CH32V103C8T6 project, which should correspond to the corresponding chip
The red box at the bottom of the above picture is a brief introduction to the resources of the selected chip, which is convenient for query
- After creating a new project, we open the main.c file and we can see that the main function is just some initialization and serial port printing. We can add our own main function logic below the print function.
- Create a new folder called hardware , right-click the project, select new->folder, fill in the file name, and click finish . In the same way, we can create a new SD directory and SPI directory under the hardware directory , which is clearly organized.
- In the SPI directory, New>Source File , fill in the file name spi.c , the content is the SPI initialization function and read and write functions, create a new spi.h file to declare the function, this new header file needs to be added to the header file addressing path, click the menu bar project property page button, in the pop-up page, as shown below, click the green plus sign to add the path
Some code screenshots are as follows, with detailed comments, no further elaboration
- Create sd.c and sd.h in the SD directory . The way to add the header file path is the same as the previous one. The sd.h file mainly includes various macro definitions and function declarations, including SD card type definition, SD card instruction definition, and function declaration;
The sd.c file mainly performs SD initialization and other operations. Regarding the SD card initialization function SD_Initialize , its specific operation process is as follows:
( 1 ) Call the SD_SPI_Init function to initialize the GPIO port and SPI working mode, and control the chip select pin to output a high level;
( 2 ) Use a for loop and call the SD_SPI_ReadWriteByte function to perform 10 read and write operations, generating 80 clock signals at the same time, meeting the requirement that at least 74 clocks are required for SD card initialization ;
( 3 ) Call SD_SendCmd function to send CMD0 command, reset the SD card and enter idle state;
( 4 ) Call the SD_SendCmd function to send relevant commands to determine the SD card version type and return response information .
Regarding other functions in the sd.c file, there are corresponding comments in the program, which can be understood by combining the comments and the " Physical Layer Simplified Specification V2.0 " manual.
Some of the code is as follows:
- The main.c file is mainly used to initialize related functions, detect SD card insertion, initialize detection, and read the SD card capacity.
7. Click the compile button in the toolbar to compile and generate binary files. The development phase is over.
3. Verification
Click the download button in the menu bar to download, insert the SD card, and you can see that the output is normal from the serial port assistant
|