Beruflich Dokumente
Kultur Dokumente
Software Requirements:
Quartus II 8.1 ModelSim 6.4g Nios II 8.1 Altera university IP 8.0
Hardware Requirements:
This lab guide is set up to allow you to use the following boards: Nios Development Kits: DE2
Exercise Manual
Software and Hardware Requirements to Complete All Exercises ..............................................................1 Lab 0: Preparations for labs ..........................................................................................................................3 Lab 1: Creating a NiosII Processor System...................................................................................................5 Lab 2: Software Flow..................................................................................................................................18 Lab 3: Basic Led & Switches......................................................................................................................28 Lab 4: Basic LCD........................................................................................................................................30 Lab 5: Basic Ps2 - Keyboard.......................................................................................................................37 Lab 5: Adding Pll to Nios II Processor System...........................................................................................52
Exercise Manual
Exercise Manual
Objectives: Over the course of the lab today, you will create a hardware system and run some software code on it. As the lab progresses, you will continue to modify your system to incorporate new hardware features as you learn about them in class. This lab guide is set up to allow you to use any one of the following development kits, so, some written steps will pertain to one type of board; other steps to another type of board. The kits fall into two main categories as shown below: Nios II Processor Development Kits: DE2
Hardware and driver set up: Fallow the instructions on the flowing PDF to test your board, and making sure you have all it takes to start working.
Software set up: Fallow the instructions on the AVI movie (nios_tut\movies\1.avi) to test your board, and making sure you have all it takes to start working. Locate your project in a path witch does not have spaces in it!! o C:\altera\70\quartus\My_project OK o C:\altera\70\quartus\My project WORNG! o C:\altera\70\quartus\ WORNG!
Exercise Manual
Exercise Manual
1. Crate a QUARTUS project. 2. Add an empty BDF file. 3. Go to assignments->import assignments and select DE2_pin_assignments from the lab folder. 4. Start SOPC Builder from Tools => SOPC Builder and enter the system name, niosII, when the next window pops up. You can choose VHDL or Verilog (whichever you prefer) as the implementation language. The blank SOPC builder window will open. 1. Set the Device Family to match the FPGA you are using:
Exercise Manual
Exercise Manual
2. From the left hand window pane select Nios II Processor and click Add. Select Nios II/s for the processor core. Select DSP Block as the Hardware Multiply option for all Stratix & Stratix II boards, Embedded Multipliers for all Cyclone II boards or Logic Elements for Cyclone boards.
Exercise Manual
3. Click on the Caches and Tightly Coupled Memories tab. Set the value to be 4KB.
Exercise Manual
5. Now, click on the JTAG Debug Module tab. Select the JTAG Target Connection Download Level 1 option. This will provide us with all the debug options listed on that tab.
10
Exercise Manual
11
Exercise Manual
7. From the left hand window pane find the JTAG UART from the Interface Protocols > Serial folder and click Add. Accept the defaults. (The screen should appear as shown .) Click Finish. This will add it to the SOPC Builder system.
12
Exercise Manual
8. Locate the System ID Peripheral from the Peripherals > Debug and Performance folder in the pick list, and Add it to your system. (Note: it is recommended that every SOPC Builder design that you create has a system ID peripheral!) Click Finish. This will add it to the SOPC Builder system.
13
Exercise Manual
8. Add Sram memory: Locate the Sram from the University... folder in the pick list, and Add it to your system.
14
Exercise Manual
9. Add Ps2, LCD, PIO: Accept all defaults, Click Finish. This will add it to the SOPC Builder system. Select PIO output only for led usage only, or output and input (not bidirectional) for led and switches, Final configuration:
Toggling Avalon Bus Connections: Click your mouse on the appropriate dots with in connectivity diagram in order to toggle the connections as shown below: (Note: the connected state is solid; while the disconnected state is white.) Next step is to press SYSTEM->Auto assign base address .
15
Exercise Manual
10. Next, click Generate. SOPC Builder will now create the parameterized SOPC system. Note: If you happen to receive a warning message in SOPC Builder that prevents you from generating the system, check to make sure that all the peripherals are connected to clk_0. If setting clk_0 does not solve your problem, you may have to close SOPC Builder and then re-open it again in order to register the change you just made. 11. After SOPC Builder has finished generating your embedded sub-system: a. open the project-> topl level schematic in Quartus II using p->Open so that you can add the system to your project. 12. Rgiht click on the symble->Generate pins, change the pins name to the names shown. 13. Zoom in or out in the schematic, as needed, using the magnifying glass utility. The schematic editor should resemble the following: (led only)
16
Exercise Manual
17
Exercise Manual
18
Exercise Manual
1. You will now download the Nios II FPGA design created in the previous lab to the Nios development board. Within Quartus II, select the Programmer from the Tools menu. (If the .sof file for your project does not populate the File field then click on the Add File button select file niosII_lab.sof and click Open.) . Then
2. Tick the Program/Configure checkbox for that .sof file, and then click the Start Programming icon . If the Start Programming button is not enabled, make sure you Hardware Setup field (at the top of the Programmer window) reads USB-Blaster or Byteblaster. If not, click on the Hardware Setup button and choose USB-Blaster or Byteblaster from the drop-down menu, and click Close. 3. Launch the Nios II IDE from SOPC Builder (see Tools > Nios II IDE). Select OK if the Workspace Launcher dialog box appears. 4. Go to the Nios II IDE workbench, create a new software project by selecting New>Project from the File menu. Select Nios II C/C++ Application, and press Next.
19
Exercise Manual
5. In the second page of the wizard, type niosII_training_project as the name of the application to be created. Then verify that the .ptf file from your SOPC Builder session has automatically populated the SOPC Builder System field in the Select Target Hardware pane. 6. Now, select the Blank Project template from the Select Project Template panel on the left hand side of the window. The New Project window should now resemble the following:
20
Exercise Manual
7. Press Next, and select: Create a new system library named: niosII_training_project_syslib. Then, click Finish.
8. You have now created two new projects in the Nios II IDE - a Nios II C/C++ Application project and a system library project. However, the Nios II C/C++ Application project is blank and you need to add some source code to it. To add a C file to the project, first expand the niosII_training_project folder in the Nios II IDE. Then Drag a C file into the niosII_training_project folder in the Nios II IDE.
21
Exercise Manual
9. Now, highlight the niosII_training_project_syslib folder, right-click, and select Properties. Then, choose System Library from the left hand side of the Properties window. Ensure that the stdout, stderr and stdin devices are set to jtag_uart and that the System clock timer is set to clk_0. For all boards except the Cyclone II board, set the Read-only data, Read/write memory, and Program Memory fields to sram. Please refer to the figure on the next page.
10. Click OK. 11. Compile the program by highlighting the niosII_training_project folder in the Nios II C/C++ Projects window; then right-click and select Build Project. Note: You can choose to run this command in the background when prompted. This will free up the tool, letting you continue to use it for other tasks; whereas, running it in the foreground causes you wait for it to complete. 12. After the compiler has finished, download and run the program on the development board: Highlight the niosII_training_project folder then right-click and select Run As-> Nios II Hardware.
22
Exercise Manual
Note 1: If Run terminates before the code downloads to your board, and you get a message pertaining to the JTAG download cable. then select Run > Run and from the Target Connection tab choose the appropriate download cable that you are using and then press Apply and Run (or see instructor). Note 2: The Nios II IDE will actually Build the project automatically for you if you just click Run As -> Nios II Hardware without you having to explicitly go through the Build step. You can enable or disable this option in the Window > Preferences.
23
Exercise Manual
24
Exercise Manual
13. Now, lets run the debugger on this design and step through some code. Start the debugger by once again highlighting the niosII_training_project software project folder, then right-clicking on it, and selecting Debug As-> Nios II Hardware. The debugger will launch, connect to the target, and download the program ready for debug. Choose, Yes to switch perspectives when prompted.
14. Turn on line numbers in the C file editor. To do this, go to the Window menu, and select Preferences. Open the General folder followed by the Editors sub-folder. Select Text Editor; then check Show line numbers. Press OK. 15. Set breakpoints on at some line, as you like. To set a breakpoint, simply place the cursor on the line number or grey area next to it, and double-click. A circle should now appear next to the line number indicating that a breakpoint is set. 16. Click on the resume button .
25
Exercise Manual
17. Now, go to the Variables window, and view the contents of the variables.
Watch the variables content while the program is running! Editing the variable values in this way is useful because it gives you a way to emulate external hardware events or other conditions that may otherwise be difficult to replicate!
26
Exercise Manual
18. Now terminate and remove the program. Go to the Debug sub-menu menu; rightclick on the software project thread, and select Terminate and Remove.
END OF LAB 2
27
Exercise Manual
LEDS:
1. First open a software project (in the IDE), clear all other C files.
28
Exercise Manual
2. Include io.h. this is all the input-output commands of the cpu. 3. Look for the address of the pio devide in the SOPC builder and compare it to the one in SYSTEM.H.
4. 5. 6. 7.
Then use the IOWR_DE2_PIO_DIRECTION(PIO_BASE, DATA) to select direction of the PIO for each bit(!!). Then use the IOWR_DE2_PIO_DATA(PIO_BASE, DATA) to write to PIO. Then use the IORD_DE2_DATA(PIO_BASE) command to read from the PIO. Output ports can connect to different pins then input ports.
29
Exercise Manual
1. First open a software project (in the IDE), clear all other C files. 2. Include the:
30
Exercise Manual
io.h system.h altera_up_avalon_character_lcd.h altera_up_avalon_character_lcd_regs. 3. The LCD address is located in system.h, the name of the address (defined) is the name you had given it in the SOPC. In this case the name is LCD, there for the defined address is LCD_BASE : SYSTEM.h
SOPC:
4. All the macro commands which is given to you are located in:
31
Exercise Manual
32
Exercise Manual
How to use?
alt_up_character_lcd_dev *LCD_device;//
LCD_device=alt_up_character_lcd_open_dev("/dev/lcd");//assign
2. initialize LCD:
OR /AND
alt_up_character_lcd_init(alt_up_character_lcd_dev *lcd)
How to use?
LCD_Init();//just
write the program (all of it!) in your code and call it! use the structure you crated using
alt_up_character_lcd_init(LCD_device);//
LCD controll commands : *you must add the includes given to you above to the file...!
33
Exercise Manual
use alt_up_character_lcd_send_cmd(alt_up_character_lcd_dev
1. alt_up_character_lcd_send_cmd(LCD_device, ALT_UP_CHARACTER_LCD_CTRL_RETURN_HOME);
2. alt_up_character_lcd_send_cmd(LCD_device, ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_ON);
3. alt_up_character_lcd_send_cmd(LCD_device, ALT_UP_CHARACTER_LCD_CTRL_CURSOR_ON
);
4. alt_up_character_lcd_send_cmd(LCD_device,ALT_UP_CHARACTER_LCD_CTRL_CURSOR_BLINK_ON);
5. alt_up_character_lcd_send_cmd(LCD_device, ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_OFF);
6. alt_up_character_lcd_send_cmd(LCD_device,ALT_UP_CHARACTER_LCD_CTRL_CURSOR_BLINK_OFF);
7. alt_up_character_lcd_send_cmd(LCD_device ,ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_SHIFT_RIGHT);
8. alt_up_character_lcd_send_cmd(LCD_device, ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_SHIFT_LEFT);
9. alt_up_character_lcd_send_cmd(LCD_device ,ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_SHIFT_LEFT);
34
Exercise Manual
2.
3.
4.
(LCD_device,3);
35
Exercise Manual
36
Exercise Manual
1. First open a software project (in the IDE), clear all other C files. 2. Include the: io.h system.h altera_up_avalon_ps2.h altera_up_ps2_keyboard.h altera_up_avalon_ps2_regs.h
37
Exercise Manual
3. The Ps2 address is located in system.h, the name of the address (defined) is the name you had given it in the SOPC. In this case the name is Ps2, there for the defined address is Ps2_BASE : SYSTEM.h
SOPC:
4. All the macro commands and h files which is given to you are located in: *We will use only Keyboard... so Mouse can be emitted.
38
Exercise Manual
Ps2 initialization commands (order must be kept!): 1. Assign structure: alt_up_ps2_open_dev(device_name_as_given_in_the_SOPC) How to use?
39
Exercise Manual
Designing with the Nios II Processor and SOPC Builder declare the structure the new structure to the device.
alt_up_ps2_device *ps2;//
2. Ps2 structure can get several values: 1. PS2_KEYBOARD, PS2_KEYBOARD, PS2_UNKNOWN How to use? 2. Ps2-> device_type==?
KB commands :
40
Exercise Manual
is the pointer to the structure. get the value of the decoded mode.
decode_mode:
data:
get the value of the key pressed. get the ASCII value of the char.
ASCII:
KB_BINARY_MAKE_CODE
KB_LONG_BINARY_MAKE_CODE
KB_BREAK_CODE
KB_LONG_BREAK_CODE
Exercise Manual
How to use? Declare the variables and pointers: o char *p; P will point to a char, represent the pressed key. o For example: if TAB key is pressed then P will point to TAB string, and
printf(%s,p) will print: TAB .
42
Exercise Manual
while(1) {
if (decode_scancode(ps2_kb, &decode_mode, &data, &ascii)==0) { printf("\n"); if (ascii!=0){ printf("Character = %c\n", ascii); } if (decode_mode == KB_ASCII_MAKE_CODE || decode_mode == KB_BINARY_MAKE_CODE || decode_mode == KB_LONG_BINARY_MAKE_CODE) { translate_make_code(decode_mode, data, p); printf("Key Pressed: %s\n", p); } switch (decode_mode) { case KB_ASCII_MAKE_CODE: printf("ASCII Make code = %x\n", data); break; case KB_BINARY_MAKE_CODE: printf("BINARY Make code = %x\n", data); break; case KB_LONG_BINARY_MAKE_CODE: printf("LONG Make code = E0 %x\n", data); if (data==0x74) {x_pos++;alt_up_character_lcd_set_cursor_pos(LCD_device,x_pos,y_pos);} break; case KB_BREAK_CODE: printf("Break code = F0 %x\n", data); break; case KB_LONG_BREAK_CODE: printf("LONG Break code = E0 F0 %x\n", data); break; default: break; // do nothing } } } }
//-------------------------------------------------------------------------
43
Exercise Manual
44
Exercise Manual
In this lab you will use ModelSim-Altera to perform a hardware simulation of the processor running code out of SRAM memory. 1. 2. 3. 4. Copy your project to another folder by using the build in copy project command. Remove all bridges to non SOPC systems. Replace memory to be on-chip memory. In SOPC Builder, the simulation model of the JTAG UARTs can be configured to generate a data stream to send to the host processor. To set this up, return to the SOPC Builder System Contents page, and double click on the jtag_uart peripheral in order to re-open it. When the dialog box opens, click on the Simulation tab. Select the Create ModelSim alias to open an interactive stimulus/response radio button, and then hit Finish
45
Exercise Manual
5. Before attempting to simulate, make sure that the path to the ModelSim software is set up correctly: a. Within SOPC Builder, choose Tools > Options > HDL Simulator Options. ModelSim by ModelTech, Inc. has been selected as the HDL Simulator. Then, ensure that
b. Now, unless the following field has already been populated, browse to the directory where the ModelSim executables are located. For ModelSim-Altera, the directory is <ModelSim install directory>/win32aloem. For example: c. Click Finish. 6. Navigate to the System Generation tab in SOPC Builder, and check the Simulation box if it is not already enabled. 7. Now, Generate the system by pressing the SOPC Builder Generate button. Note: During system generation, SOPC Builder will re-create your system and produce all of the files necessary for simulation except for the memory initialization files. They need to be generated by the Nios II IDE. 8. Once the system has been generated, you will finish the simulation set up by returning to the Nios II IDE. 9. After returning to the Nios II IDE, change back to the Nios II C/C++ perspective: C:\altera\<ver>\modelsim_ae\win32aloem
46
Exercise Manual
10. Then, modify the system library parameters for your software project. You will need to specify which memory you wish to compile code into and whether you want enhance the simulation of your system by reducing code overhead. To do this, right click on the system library project, and select Properties. 11. Next, from the Properties page, select System Library. Recall that this page controls which communication devices in your design should be used for standard input / output and also which memories your code gets assigned to. o Select on_chip_memory. and keep jtag_uart as stdout, stderr and stdin. o Enable the ModelSim only, no hardware support simulation feature. o Check reduce device drivers and reduce C library. o Click OK Note: Checking the ModelSim only, no hardware support checkbox tells the compiler that the current project is being run on a simulator. The compiler will, in turn, remove sections of the start-up code to improve simulation speed. Note that the resulting software image will not run on a target board.
47
Exercise Manual
12. In the Nios II C/C++ Projects panel of the Nios II IDE, re-build by highlighting and right-clicking on it, then selecting Build Project. 13. Once build completes, use the Nios II IDE to launch ModelSim. Again, highlight the project, but this time select Run As > Nios II ModelSim.
-------------------------------------------------------------------------------------------------------------14. This should open the ModelSim project generated by SOPC Builder. Note: If you encounter an error in this step, however, and the project does not open properly, then your will simply need to manually open the Modelsim project (niosII_sim.mpf) manually and execute the set-up script (setup_sim.do), yourself. To do this, select File > Open in ModelSim, and then browse to the niosII_sim folder where your project file is located. Once inside this directory, select niosII_sim.mpf, and press Open. (Note: You will need to set Files of type to Project Files (*.mpf) in order to see this file.) Then, type source setup_sim.do at the ModelSim TCL prompt. --------------------------------------------------------------------------------------------------------------
15. You are now in a position to easily compile and simulate your Nios II design in ModelSim with the help of some macros that are generated by the Altera setup script. You can execute all of these macros at the ModelSim command line.
48
Exercise Manual
<UART name>_drive
<UART name>_log
49
Exercise Manual
17. The first step in simulating the Nios II design is to load it into ModelSim by typing s at the console prompt located in the ModelSim Transcript window. This will compile all of the relevant HDL design files into ModelSim. 18. Next, type jtag_uart_drive to launch the interactive terminal window. This console will display the output of the printf statement in the simple.c source code. (If ModelSim does not recognize this macro, then use jtag_uart_log instead as you probably forgot to set the jtag_uarts simulation model to an interactive stimulus/response window earlier in the lab.) 19. Type w at the ModelSim command prompt to execute a macro that opens the ModelSim waveform window and pre-populate it with a number of example signals from your system. These signals are delineated by function in the waveform viewer and were chosen because they are particularly useful specifically for debugging.
20. In the Objects window, select chipselect and out_port. Drag them into the waveform window, placing them at the bottom of the list of signals. (Note: Hold down Ctrl in order to grab multiple individual objects.)
50
21. In the Wave window, right click on out_port and select Insert Divider. This will insert a dividing line directly above the signals you just added. Rename the New Divider to Extra Signals by right-clicking on it, selecting properties, and then typing the new name.
22. Run the simulation for 300 us by typing run 300 us at the ModelSim console prompt in the Transcript window.
23. After the simulation has completed, the terminal window should display the printf statements from the C file.
24. From the File menu in ModelSim, select Quit to exit. 25. Also, close the jtag_uart console window.
Exercise Manual
52
Exercise Manual
1. Add the Altera PLL component to the project from the PLL folder. Then launch the ALTPLL MegaWizard and configure the PLL using the settings shown below. (Note: You navigate through the Wizard using the Next button.)
Input Clock Freq Auto PLL Selection Mode Output Tap Settings: C0: Mult Factor = Div factor = Phase shift (ns) Duty Cycle = C1: Mult Factor = Div factor = Phase shift (ns) Duty Cycle = Note: to enable PLL output tap C1, check the Use this clock check box
53
Exercise Manual
2. After you configure the PLL to match the setting shown above, the final page of the PLL Wizard should resemble the following: (Note: there will be some minor differences depending on what language you have chosen or development kit you are using for your project.)
Select Finish again and then Finish one more time to add the component to the SOPC Builder system; then re-name it, pll.
54
Exercise Manual
3. Double click on pll_c0in the Name column in the Clock Settings window in the top right corner of the SOPC Builder tool, and type over the text to re-name it as you wish... (we re-named pll_c1 to ssram_clk) The system Clock window should appear as follows: Cyclone II and Stratix II rohs Nios II Development Kits:
55