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 sourceHttp://www.cnblogs.com/kingst/
In this section, I will explain the First hardware-relatedProgramAlthough the content is simple, it is very representative. I will adopt a register operation method to make everyone feel the simplicity of developing the nioshi as a single-chip microcomputer. I will try my best to avoid using the APIS provided by the niosii IDE, this has many advantages. First, people with experience in Single-Chip Microcomputer Development are very familiar with this operation method. Second, they can understand the nature of the Nios and actually develop it, instead of simply using its APIs to write some applications.
People who have tested the single-chip microcomputer must remember the LED experiment, because it is the first part of the hardware test. Through this simple experiment, you can have a sensory understanding of the single-chip microcomputer operation, it can be said that the meaning is unusual. In this section, I also use LED experiments to bring you into the development world of the niosii and feel the charm of the Nios. Let's get started.
Build Pio Module
Step 1: add the PIO module to the soft core. Open the Quartus project created in section 1, and double-click the kernel, as shown in red circles.
Click it to go to the on-chip system builder interface, as shown in
Click Pio (parallel I/O) in the Red Circle shown)
After clicking, as shown in, Red Circle 1 is the width of the PIO port you need, that is, you need several Io ports. Here I set it to 4, that is, I want to control four LEDs, the red circle 2 selects the output mode, And I select the output mode ).
Next, click Finish to build the PIO module and rename it pio_led, as shown in
Next, we need to allocate the address automatically, as we have already discussed in section 1, as shown in.
Next, we will start compiling. Click Generate and save. Click Save to start compiling.
After a patient wait, the compilation is successful, as shown in
After finishing the above work, click exit to display the following interface, ask whether to update the kernel, and click "Yes (y )".
Then, the following interface appears and click OK.
After you click it, the following problem will occur, indicating that the link is misplaced. We need to manually sort it out and connect the corresponding pins.
After finishing the configuration, as shown in, we can see that there is still a pio_led with no pins connected. Let's create an output pin and rename it.
As shown in, we name it led [3. 0]. This is the naming method of the bus in Quartus.
Everything is ready. We need to assign pins to him, as shown in
Click Tcl scripts, as shown in, select kingst. TCL, and click Run to complete pin allocation.
Next, we started to compile. After a long wait, we saw the red circle, indicating that the compilation was successful.
Let's take a look at the amount of resources we use, as shown in, or 66%, indicating that a PIO module occupies a small amount of resources.
So far, we have added the PIO module to the soft core. Next, we will develop the software part in the nioii IDE.
Software Programming
First, enable the nio ii 9.0 ide. Then, recompile the entire project and press the shortcut key Ctrl + B. After another long wait, we open the system. h file. Can you see any changes? Someone may have discovered it, system. the following content is added to the H file, which is the new pio_led module built in the soft kernel. We will use pio_led_base, which is the first address of all pio_led registers.
Next, we will use this first address for programming to complete the control of the four LEDs.
Step 1: Create a folder for storing header files. I name it Inc, as shown in. Right-click hello_world and click source folder in new, in this way, a folder is created,
Then it appears. Enter Inc in the red circle and click Finish to create the folder.
As you can see, an INC folder is added to the project directory on the left, as shown in
Next, I want to create a head file in Inc, as shown in, right-click on Inc, and then click header file in new, as shown in the red circle.
After you click it, it will appear. In the Red Circle, enter the file system. h and click Finish to build the header file.
After completing the above work,CodeBar, you can see, now we edit it
Shows the modified Code,
You may see that the above Code does not understand what it means. This is normal. The person who first came into contact with Nios does not know this very well. I will explain it to you in detail now.
First, let's take a look at the struct at red circle 2. Here, we define a struct and name it pio_str. The structure of this struct is well known, it is written according to the chip manual. I will cut a picture for you now. This figure is from n2cpu_embedded peripherals.pdf. The version is shown in.
The following table is displayed on pages 9-5 and 9-6, as shown in.
This table is the PIO core register ing. My struct is written based on it. The name is not important. What matters is its order, that is, the offset ). The first is data, the second is the IO port direction, the third is the interrupt control bit, and the fourth is the edge control bit. Each item has N bits. The N is the width of the width in the soft kernel construction, as shown in red box 1. We set it to 4 before, so n is equal to 4.
Next, let's talk about the code in the red box 3. In this part of the code, you must remember pio_led_base. Right, this is the base address of pio_led that I specifically emphasized in system. h. The Code intent in the Red Circle 3 is obvious. It defines a macro named led, which is a struct pointer to pio_led_base, this struct is pio_str (if you do not understand the struct pointer, you have to go back and read the C language book. I will not discuss it here ). Red Circle 1 code is also very simple, to control the definition of red circle 3, this is to enhance the code rigor and controllability. When you do not define pio_led_base, you can remove the macro definition # DEFINE _ led in red circle 1. If you don't talk about it much, you will get bricks.
After you have understood the part. H code, we can program C code. I forgot to mention it. I must remember to save it after modification in the NIO II ide. It is very naive and will not remind you to save it before compilation. If you forget to save it, it is equivalent that you have not modified it. Now let's program C code.
In order to standardize the program, I need to make some adjustments to the program for further explanation. Create two folders named driver and main respectively. As shown in,
Change hello_world.c to main. C and put it in the main folder. After modification, as shown in
Next, let's modify main. in C, I will first introduce the purpose of this Code, which is to control the four LEDs of my black gold Development Board, so that they will flash in order, that is, what we are used to talking about. It is the four LEDs we want to control. When FPGA sets the corresponding PIN to 1, the LED will be on. When it is set to 0, the LED will not be on.
As shown in the modified program, the red box 1 is first mentioned. Some people may not use this method. This is a relative path method to call the header file. The advantage is that it is portable, no matter where the project is placed, the relative location of the header file and the c file will not change, so you do not need to modify the location. Let's talk about the code in the red box 2, the key is led-> DATA = 1 <I; led, we should remember that led is in. the macro defined in H is a struct pointer, so we should be very clear about what led-> data means. It is the data content (not the address) in its struct ). LED-> DATA = 1 <I; is set to 1 for the four-digit loop of LED-> data. Therefore, the four LEDs will flash in sequence. Usleep is a subtle level of latency. Here we will delay 0.5 ms each time.
At this point, the first program is finished. What we need to do is compile and download the program to FPGA to verify whether it is correct. I will not elaborate on it.
Next, let's summarize this section. Let's compare the relationship between the Register operation method and the API. If the above program is written using the API provided by the nio ii ide, as shown in
Iowr_altera_avalon_pio is a macro. In altera_avalon_pio_regs.h, it is defined as follows:
(You can press and hold the ctrl key and click it to enter the location of the definition.) You can see that it is an IOWR macro, I will not elaborate on the specific Writing Method of IOWR here (if you are interested, you can go to the source code of the NIO). It is about controlling the hardware address. My approach is to bypass this big circle and directly control its registers.
It may be a bit confusing that we have defined four variables in our Organization, but we only use one data variable. In this case, we will explain the reason first, direction. This is the IO direction, that is, the input or output, or bidirectional. Because we have an option in the structure of the PIO module, as shown in red circle 2, we chose output ports only ), that is, we have fixed its direction at the bottom layer, so we do not need to define it in the software. There are two other variables that are used when the interrupt is involved, so they are not used in this program.
This section is the basis of future content. I hope you can understand it well, especially the struct defined in the system of File System system. In the future, UART, SPI, and so on will be involved, and struct should be created. This is all accessible, and everything else can be understood.
I would like to remind you that, although I advocate programming by using the Register operation method, I do not want all programs to be written in this way, such as flash operations, we can use APIs for writing, because Flash operations are relatively complex, and using APIs can be completed with several simple statements, and there is no need to write them on our own.
Now, let's talk about how to debug the system online and some tips in the next section. I hope you can wait patiently ......