Disclaimer: This article is an original work and copyright belongs to the author of this blog.
All. If you need to repost, please indicate the source
Http://www.cnblogs.com/kingst/
I. Introduction
In this section, let's talk about SDRAM. As the most important external device in the system, the system plays an important role and everyone should be familiar with it. Each time you power on, FPGAProgramThe reason for this is that it is fast, but it will lose data when power loss occurs. Therefore, you need to store the data in flash.
I will not talk about the theoretical knowledge of SDRAM here. I don't know Baidu or Google. In fact, even if you do not understand the theoretical knowledge of SDRAM during the development of the niosii, it will not delay your use of it. The system has driven the system perfectly. We only need to know how to use it. Next, let's talk about his usage, which is actually very simple.
When we talk about Section 1, we have already talked about how to build the SDRAM Controller. I will not repeat it here. If you have already built the controller, I will mainly talk about the software.
2. Software Development
First, enable the niosii 9.0 ide software. After opening the software, let's take a look at the system. h file and check whether the SDRAM controller module has been added. If the following content is added
# Define sdram_name "/dev/SDRAM" # define sdram_type "altera_avalon_new_sdram_controller" # define sdram_base 0x01000000 ......
Next, we will start to compile the software related to SDRAM.CodeThe code is very simple, as shown below:
/** =================================================== ======================== * Filename: main. C * Description: SDRAM read/write test * version: 1.0.0 * created: 2010.4.16 * revision: none * Compiler: NiO II 9.0 ide * Author: Mary (AVIC) * Email: avic633@gmail.com * =================================================== ======================= * // * ------------------------------------------------------------ * include * Others */# include <stdio. h> # include ".. /INC/systems. H "# include" system. H "# include" string. H "/* --------------------------------------------------------------- * variable */unsigned short * Ram = (unsigned short *) (sdram_base + 0x10000 ); // SDRAM address/** === function ================================================== =======================================* Name: main * description: function main program * ================================================= ============================= */INT main (void) {int I; memset (RAM, 0,100); // write data to ram. After Ram is written, the RAM address is changed to (sdram_base + 0x10100) for (I = 0; I <100; I ++) {* (RAM ++) = I ;}// reads data from Ram in reverse order (I = 0; I <100; I ++) {printf ("% d \ n", * (-- Ram);} return 0 ;}
The program is simple, that is, assigning values to the specified SDRAM. There are several points to explain in this program. First, I defined an unsigned short type pointer Variable RAM in front of the program and pointed it to the location of SDRAM + 0x10000. The reason why we set it to the unsigned short data type is that the SDRAM we use is a 16-bit data bus. The reason for pointing to the SDRAM + 0x10000 is that part of the space of the SDRAM will be used during the running of the nio ii. We must avoid this part of space to avoid running errors. The 0x10000 value is not fixed. You only need to avoid the part of the space of the SDRAM. Note that when we assign values to SDRAM, the pointer moves backward and points to the next address space. The address moves 16 bits to the next address space after each addition. Assume that we are at the position of SDRAM + 0x10000. When the pointer moves backward once, the address is changed to SDRAM + 0x10002, and the address is changed to SDRAM + 0x10004 again, and so on. These are all internal automatic processing, and we don't need to participate, we just need to know.
In fact, I want to tell you that once the SDRAM controller is built, we can assign values and read values to and from the internal address. A very small part of the 64 Mbit SDRAM on the Development Board is actually used by the NIO system, and the rest are idle. Do you think it is a waste. In fact, in some cases, we can use this part of resources. For example, in a system, we need to cache the data received by peripherals, we can use this part of idle SDRAM space for processing.
in C, if we want to receive relatively large data, another method is to use heap ). Some people may not be clear about the heap and stack. I will explain it here. Stacks are automatically allocated by the system. For example, to declare a local variable int B in a function, the system automatically opens up space for B in the stack. Heap requires the programmer to apply and specify the size. Someone uses this metaphor to explain the difference between stack and stack. It is very well-formed: using Stack is like eating at a restaurant, just ordering food (issuing an application) pay, and eat (use). When you are full, you don't have to worry about cooking, washing, and other preparation work and cleaning, such as washing dishes, flushing, etc. His advantage is fast, but the degree of freedom is small. Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom. This person is really talented. Next I will write a heap of code. This part of the code is excerpted, not completely. The function is to receive data through the XMODEM protocol and put its content in the heap. As shown below
/** = function ============================================== ============================ * Name: main * description: main function * = ========================================= */INT main () {char * Ram = (char *) malloc (sizeof (Char) * 500000); If (RAM = NULL) {fprintf (stderr, "\ ndynamic memory allocation failed \ n"); exit (exit_failure);} UART. init ();/* ------------------- flash ----------------------------- */while (1) {If (UART. mode_flag) {XMODEM. xmodem_rx (); printf ("ram_cnt: % d \ n", ram_cnt); parse_srecord_buf (RAM, ram_cnt); printf ("successful! "); Ram_cnt = 0; UART. mode_flag = 0 ;}} free (RAM); Return 0 ;}
After reading the above Code, you should have understood the usage of the heap. It applies for space from the system through malloc. After the application is successful, the first address of the application address is returned. If the application fails, null is returned. We can know exactly where the application is located through the print pointer, but it is not necessary because these are all handled by the system. We can get the first address and use it. Note that the address we have applied for must be released after it is used up. Use free to release the address. If it is not released, memory leakage may occur, and the trouble will be high. I don't know how big it is, huh, huh.
Some may ask, why don't we use stacks to solve this problem? This has something to do with compilation. During the compilation process, the system will add the space required by the stack to the code. That is to say, if you use the stack in the code to handle big data, the compiled code will be very large. After you download the code to flash, the loading time will be very long. The heap does not. The system will handle the heap in a way that is suitable for allocation and does not occupy the code space.
Speaking of this, the content of the SDRAM section is finished. To sum up, there are two ways to use SDRAM. The first is to directly process the SDRAM address, and the second is to use the heap for processing. Well, this part of the content to talk about this, if you have any questions about this, or found that I am talking about the content of the problem can directly contact me, mail: avic633@gmail.com; QQ: 984597569.