Detailed explanation of SD card driver under STM32

Publisher:艺泉阁Latest update time:2016-08-14 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In the past few months, I put aside my study of S3C2440 for my graduation project, and started to develop modules on STM32. I used STM32 to make an MP3 with an interface, so I had to use an SD card (of course, nand flash can also be used, but the PCB is difficult to draw, and the most important thing is that it is easy to break). SD cards are convenient and fast.

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)

Detailed explanation of SD card driver under STM32 - stubbron - ChinaHEAVEN

 

Detailed explanation of SD card driver under STM32 - stubbron - ChinaHEAVEN

 

Detailed explanation of SD card driver under STM32 - stubbron - ChinaHEAVEN

 

Detailed explanation of SD card driver under STM32 - stubbron - ChinaHEAVEN
The size of each BLOCK of the SD card is determined here, usually 512.

 

Detailed explanation of SD card driver under STM32 - stubbron - ChinaHEAVEN

 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. 

Keywords:STM32 Reference address:Detailed explanation of SD card driver under STM32

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

STM32 USART DMA transmission (transfer)
Problem Description:      I have a requirement that after AD samples a certain amount of data, it is sent out by serial port DMA. Since AD ​​uses double buffering, the starting memory address and the number of transfers need to be reset each time DMA is started (these are all natural). However, when I started debugg
[Microcontroller]
stm32 SysTick timer (2) - solving the clock division problem
static __INLINE uint32_t SysTick_Config(uint32_t ticks) {    if (ticks SYSTICK_MAXCOUNT)  return (1);                                             /* Reload value impossible */     SysTick- LOAD  =  (ticks & SYSTICK_MAXCOUNT) - 1;                                      /* set reload register */   NVIC_SetPrior
[Microcontroller]
Analysis of Stm32's io port simulation spi routine
The following is the hardware circuit diagram, the main chip is stm32rbt6.   Paste the code void SPI_FLASH_Init1(void)//io初始化配置 {   GPIO_InitTypeDef GPIO_InitStructure;   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_9
[Microcontroller]
Analysis of Stm32's io port simulation spi routine
Common library functions for STM32 MCU
1.GPIO initialization function usage: voidGPIO_Configuration(void) { GPIO_InitTypeDefGPIO_InitStructure; //GPIO status restores default parameters GPIO_InitStructure.GPIO_Pin=GPIO_Pin_label|GPIO_Pin_label; //Pin position definition, the label can be NONE, ALL, 0 to 15. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
[Microcontroller]
STM32-RCC clock
Preface: I am currently debugging the STM32L152 chip. This article summarizes the RCC clock configuration method of the STM32L152. Hardware platform: STM32L152 Software platform: keil v5+cubeMX Function library: HAL library content: RCC: Reset and Clock Control, reset and clock control system. There are thre
[Microcontroller]
STM32-RCC clock
STM32 learning record 9: Selection of STM32F10X_XX macro definition
If you use stm32f10x_stdperiph_lib, you will find a section of code about macro selection in the stm32f10x.h file, such as:     view plain copy   #if !defined (STM32F10X_LD) && !defined (STM32F10X_MD) && !defined (STM32F10X_HD) && !defined (STM32F10X_CL)     /* #define STM32F10X_LD */   /*!  STM32F10
[Microcontroller]
STM32 learning record 9: Selection of STM32F10X_XX macro definition
I2C master-slave mode of stm32
1. The I2C bus of stm32 can be used as both master mode and slave mode The configuration in CUBE is the same as that in master mode and slave mode, except that the address of the i2c device is different. Timing is the timing, which is generated during configuration and does not need to be modified When the b
[Microcontroller]
I2C master-slave mode of stm32
How to use STM32's PVD to monitor the power supply voltage
When using STM32, users can use its internal PVD to monitor the voltage of VDD and set the monitored voltage value through the PLS bits in the power control register (PWR_CR).   The PLS bits are used to select the voltage threshold of the PVD monitoring power supply:   000: 2.2V 001: 2.3V 010:2.4V 011: 2.5
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号