When talking about SD card, we will think of its specific driver. There are many examples on the Internet, but none of them explain the process in such detail that it is difficult for novices to understand. Of course, this article is of no use to students who have done it. Let's analyze it in detail now.
The SD card we are talking about is just a type of flash memory. Its full name is Secure Digital, which means "secure digital". It was jointly launched by two major Japanese electrical groups, Panasonic and Toshiba, and SanDisk, and was first released in August 1999. Common flash memory includes: SD card, CF card, XD card, MMC card, SM card, Mini SD card, T-Flash card. SD cards are classified into:
SD, MiniSD, MicroSD, SDHC, MiniSD, MicroSD, SDXC, smart SD, SDI/O. They are different in appearance, speed, capacity and command. Speaking of this, we have to talk about the recent anti-Japanese movement. In fact, we really don’t need to cause trouble for the country. Many things around us are others’, but we don’t know it. So strengthening your own knowledge and doing more for the country is the real patriotism.
SD card supports two bus modes: SD mode and SPI mode. The SD mode uses 6-wire system, using CLK, CMD, DAT0~DAT3 for data communication. The SPI mode uses 4-wire system, using CS, CLK, DataIn, DataOut for data communication. The data transmission speed in SD mode is faster than that in SPI mode. When using a single-chip microcomputer to read and write SD cards, SPI mode is generally used. Using different initialization methods can make the SD card work in SD mode or SPI mode.
This means that there is a controller in the SD card, with corresponding registers. When we use the SD card, we are actually setting the corresponding registers. After setting, we can use commands to operate it. (That is why SD cards are easy to use, because SD card manufacturers have already done a lot of things).
1. How to drive SD card using SPI
The SPI communication interface of the SD card allows it to read and write data through the SPI channel. From the application point of view, the advantage of using the SPI interface is that many microcontrollers have their own SPI controller, which not only brings convenience to development, but also reduces development costs. However, it also has disadvantages, losing the fast performance advantage of the SD card. (The speed of USB download test in SPI mode is only 100K~300K, while the speed in SD mode can be as high as 3M~10M), this is a serious disadvantage of SPI, but it is the simplest for us students to use SPI. The SD mode has a very complex mode conversion, and it is difficult to switch back and forth, but it can also be used in SD mode, and many controllers have SDIO.
Second, SD card commands
The most important thing about SD card is initialization, and the important thing about initialization is the use of commands. After the SD card is successfully initialized, it can almost be used, so we should focus on the initialization. Different cards have different initialization processes (pay attention to the SPI speed not exceeding 400K during initialization)
The typical initialization process of the SD card is as follows:
1. Initialize the hardware conditions for connecting to the SD card (MCU SPI configuration, IO port configuration);
2. Power-on delay (>74 CLK) (required);
3. Reset the card (CMD0);
4. Activate the card, initialize internally and get the card type (CMD1 (for MMC card), CMD55, CMD41) (the difference is here)
5. Query OCR and obtain the power supply status (CMD58) (that is, voltage. Generally, many cards cannot be used if it is not judged);
6. Whether to use CRC (CMD59) (CRC check);
7. Set the read/write block data length (CMD16);
8. Read CSD to obtain other information of the memory card (CMD9);
9. After sending 8CLK, chip selection is disabled;
The following diagram shows: (available online)
It can be seen that the operations of the SD card are all commands. As long as you understand the functions of these commands, you can drive the SD card yourself. I will post all the commands:
If you want to use SD mode in SPI mode, STM official has very detailed library files;
Class0: (basic command set such as card identification and initialization)
CMD0: Reset SD card.
CMD1: Read OCR register.
CMD9: Read CSD register.
CMD10: Read CID register.
CMD12: Stop data transfer when reading multiple blocks
CMD13: Read Card_Status register
Class2 (card reading command set):
CMD16: Set the length of the block
CMD17: Read single block.
CMD18: Read multiple blocks until the host sends CMD12.
Class4 (card writing command set):
CMD24: Write single block.
CMD25: Write multiple blocks.
CMD27: Write CSD register.
Class5 (Erase card command set):
CMD32: Set the starting address of the erase block.
CMD33: Set the end address of the erase block.
CMD38: Erase the selected block.
Class6 (write protection command set):
CMD28: Set the address of the write protection block.
CMD29: Erase the address of the write-protected block.
CMD30: Ask the card for the status of the write protection bits
Class7: Card lock and unlock command set
class8: Apply for a specific command set.
class10-11: reserved
Among them, class1, class3, class9: SPI mode does not support
Now that we know the command, let's take a look at the format of the command:
The SD card command consists of 6 bytes, as follows:
Byte1: 0 1 xxxxxx (command number, defined by the command flag, such as CMD39 is 100111, which is hexadecimal 0x27, then the first byte of the complete CMD39 is 01100111, which is 0x27+0x40)
Byte2-5: Command Arguments, command parameters, some commands have no parameters
Byte6: The first 7 bits are CRC (Cyclic Redundacy Check) check bits, and the last bit is stop bit 1
byte1 byte2-5 byte6
01 Command number Parameter CRC check+1
Parameters are what the command requires. If there is no parameter, use 0. For example, CMD16 would have 512 written in it.
Previous article:STM32 directly drives ov7670 development notes
Next article:What do the square brackets ("[" and "]") mean in ARM assembly language?
Recommended ReadingLatest update time:2024-11-16 04:18
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The versatile NeoPixel board
- Last three days! A variety of Bluetooth, Wi-Fi, LoRa, 4G DTU modules and beta versions are available for free here!
- Are you having trouble with impedance matching? Click here
- Parsing dsp c6000 MVK instructions
- ADI's award-winning live broadcast will start at 10:00 this morning: Voltage Reference Product Technology and Application Selection
- TGF4042 Function Signal Generator Evaluation: Waveform Superposition
- CircuitPython Show
- [Home Smart Lighting Control and Indoor Environment Monitoring System]--9. Single Node Bluetooth BLE Light Switch Design
- Semiconductor Device Physics (3rd Edition)
- How Qorvo implements 5G self-shielding technology