Like most of my friends, I used to only use the SD card SPI mode. This mode is simple and can be implemented on most single-chip computers without the need for CRC verification. The SD mode can achieve simultaneous transmission of four data lines, which has a great advantage over the SPI mode in terms of speed. Therefore, this project selects the SD mode and uses the sdhc controller that comes with the processor.
In fact, the hardware development should basically be clear at three points to avoid detours. First, we should be clear about the driving process of the controlled hardware, and second, we should be familiar with the specific meaning and operation methods of each register of the processor controller, third, there are some debugging methods. The introduction of the specific processor controller and the knowledge of the SD card will not be described in detail. This article mainly records the problems encountered during debugging and solutions.
1. Initialization fails to enter SD mode and always enters SPI mode. This problem has plagued me for several days. Obviously, SD card power-on enters SD mode by default, and data3 is obviously pulled up in settings. However, no response is given to any commands sent online in cmd.
Solution: use an oscilloscope to observe the waveform. After power-on, data3 (CS) is pulled down, the SD card enters the SPI status, and data0 (Miso) responds, after checking the N encoding program, I found that the operation was not low and it was very frustrating. Later I found that it was a hardware problem. In order to enable the SD card to adapt to the two modes, the hardware engineer, the pull-up and pull-down resistors are set for data3, and are displayed as pull-down! Do not carefully look at the schematic diagram, remove the drop-down resistance is OK.
2. the initialization sequence is unclear. Because the SD mode initialization sequence is different from that of SPI, it is always performed in the initial stage according to the SPI initialization sequence. The result is a hit and has not been fixed for a long time. So Google downloaded a comprehensive instruction document on csdn, which helped a lot to find the following initialization process.
3. How to Use the sdhc controller for reset. This controller can enable hardware power-on Reset or software reset through a certain time sequence, and software reset is required when developing the driver, because the driver can achieve dynamic loading. However, this sdhc controller has a very strange reset order, and now it has been on the cloud, but since people say this, we cannot compete with him, simply follow it.
4. How to send a command. The biggest difference between SD mode and SPI mode is how to send a command, except for the initialization process. SPI is relatively simple. You can directly use SPI to send commands, while the SD mode is sent by setting the sdhc controller. Every bit must be carefully checked, otherwise, it will be very strange. For example, when sending the limit 0 message, you need to send 80 clocks first, so that the SD card can be initialized on its own; there is a dedicated control on read/write and whether there are data transmission bits; and data width control. Different parameters need to be set according to different situations of sending each cmd, which is indeed much more complicated than SPI mode.
5. determine the status of the card. After sending different commands and parameters, the SD card will give different responses. The next instruction can be sent only when the SD card responds to ready. Next we will follow the instructions to explain the problems encountered.
1) 0. 0. this is the first command to be sent. The purpose is to initialize the SD card state machine and enable the SD card to switch to idle. This command will make me crazy. According to the SPI method, after sending limit 0, there is a response! However, I couldn't understand why I didn't. I found that there was little information on this part on Google, but the final answer was that SD mode success 0 didn't respond! It's really a pitfall.
2) cmd8. this command is a command to negotiate the working voltage of the SD card and determine whether the SD card is V1.0 or V2.0. If there is a response, it is the card of V2.0. But why is the parameter 0x1aa? The answer is that this is an initialization mode. Ask if the SD card can work in 2.7 V-3.3 V. If the SD card can work in this voltage range, it also returns 0x1aa, no problem. You can work. However, I have not found any more information for a long time. I strongly recommend that you add this information to the manual.
3) acmd41. this is a special command, which is actually running 55 + 12741. It is used to query whether the SD card supports sdhc and enables the card to return the operating voltage parameters. Most of my friends cannot get the correct response because they wrote the parameter wrong! Acmd41 is mainly used to write the OCR register of the SD card. When setting the HCs bit, do not forget to write the supported voltage parameters, otherwise, the response OCR register parameter cannot make it in ready state! In addition, you must read the CCS bit to determine whether this is an sdhccard.
4) 2. 2. this step is used to obtain the CID register information, but it does not seem to be very useful. It is indeed an indispensable part of initialization. Then, send cmd3 to ask the SD card to establish a relative address (RCA, in this way, when multiple SD card cmd lines are connected in parallel, you can know that the command is sent to that card. This relative address will be used in many subsequent commands.
5) 9. obtain the CSD information. In fact, this step is mainly to obtain the capacity information of the SD card, determine whether it is sdhc Based on the returned CSD register value, and calculate the capacity based on the corresponding calculation method. If sdhc is used, most of the parameters are fixed and the capacity is also relatively calculated. If not, it will be much more complicated. Generally, cards above 4G are sdhccards.
6). Connect the card to bring the card into the transfer state. The details should also be determined based on the SD card state machine transition diagram, as shown below:
It can be seen that its main function is to switch between the stand by state and the transfer state. In one aspect, the SD card is not connected. If the SD card is connected, it enters the programming state, otherwise, enter stand.
By status. Once you enter this status, subsequent read/write commands cannot respond. Be sure to pay attention!
6. How to change the bus width of the SD card. The changeable data line width is the biggest advantage of the SD mode. It can achieve 4-bit data transmission at the same time, which is also the main consideration of this design change. This operation involves three steps: 1. Send the dig 42 command to unlock the SD card; 2. Send acmd6 to change the SD card from 1bit to 4bit; 3. Set the sdhc controller, change the transmission data control mode. It is worth noting that when sending each command, you must check whether the status returned by the card is in ready status; otherwise, you need to resend the command, wait until the SD card is in the ready state and then send the next command. Otherwise, the sending will fail.
7. Read and Write a data block. The ultimate goal of SD card development is to store data. Therefore, reading and writing a data block is the most critical step. This step is a success. Read/write operations are implemented by sending messages 17 and 24, but before that, you 'd better use period 13 to query the status of the card. Ensure that the card is in ready status and the read/write name is valid. After receiving a response, the data in the SD card is directly stored in the sdhc data buffer. You must determine the buffer read/write flag to effectively read and write the buffer.
In this way, I found a lot of problems, and it took three weeks to solve them. There were many challenges and a lot of fun.