(原創) 如何讓Nios II一Power On就執行? (SOC) (Nios II) (SOPC Builder) (DE2) (DE2-70)

Abstract
目前為止,我們若要執行Nios II的C程式,都必須透過Nios II EDS的Run As Hardware執行,但一個嵌入式產品,總不能要求客戶也安裝Nios II EDS吧,所以希望能一Power On就能執行Nios II程式。

Introduction
使用環境:Quartus II 7.2 SP3 / Quartus II 8.0 + Nios II EDS 7.2 SP3 / Nios II EDS 8.0 + DE2 (Cyclone II EP2C35F672C6) or DE2-70 (Cyclone II EP2C70F896C6N)

(原創) 如何解決CONF_DONE pin failed to go high in device 1的錯誤訊息? (SOC) (Quartus II) (DE2),雖然談的是如何解決錯誤訊息,但實際上是討論如何將Quartus II的sof燒到epcs上,透過這種方式,只要一power on就可以執行FPGA的硬體程式,但若牽涉到Nios II的軟體部分,又該如何讓DE2一power on就執行Nios II呢?

要能一Power On就執行,就得靠斷電不會流失資料的flash才行。DE2有兩個flash,一個是epcs flash,一個是cfi flash,epcs有2MB,cfi有4MB。在(原創) 如何解決CONF_DONE pin failed to go high in device 1的錯誤訊息? (SOC) (Quartus II) (DE2)中,我們已經能將sof燒進epcs,若我們能將Nios II的elf燒進cfi flash,這樣就能一Power On就執行Nios II了。

Step 1:
在SOPC Builder加入cfi_flash與epcs controller

本範例直接使用DE2 CD所附的reference design,若你打算自己建立Nios II,別忘了在SOPC Builder加上cfi_flash與epcs_controller,關於cfi_flash的設定,請參考(原創) 如何解決No CFI table found at address的錯誤訊息? (SOC) (Nios II) (DE2)

nios_boot00

Step 2:
使用Nios II的Hello LED template

為了簡化Nios II的複雜度,僅使用簡單的Hello LED template測試。

nios_boot01

hello_led.c / C

1 #include "system.h"
2 #include "altera_avalon_pio_regs.h"
3 #include "alt_types.h"
4 
5 int main (void) __attribute__ ((weak, alias ("alt_main")));
6 
7 int alt_main (void) {
8   alt_u8 led = 0x2;
9   alt_u8 dir = 0;
10   volatile int i;
11 
12   while (1) {
13     if (led & 0x81)
14       dir = (dir ^ 0x1);
15 
16     if (dir)
17       led = led >> 1;
18     else 
19       led = led << 1;
20      
21     IOWR_ALTERA_AVALON_PIO_DATA(LED_GREEN_BASE, led);
22 
23     i = 0;
24     while (i<200000)
25       i++;
26   }
27 
28   return 0;
29 }
30 


由於這個template是配合Altera原廠的版子,所以在SOPC Builder內的命名不太一樣,要將21行的LED_PIO_BASE

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);


改成DE2 reference design的LED_GREEN_BASE

IOWR_ALTERA_AVALON_PIO_DATA(LED_GREEN_BASE, led);


Step 3:
使用Nios II EDS的Flash Programmer

Tools -> Flash Programmer

將elf燒進cfi flash。(無法選擇,Flash Programmer預設就是將elf燒進cfi flash)

nios_boot02

將sof燒進epcs flash。(可以選擇燒進epcs或者cfi flash)

nios_boot03

最後按下[Program Flash],這樣會開始將sof燒進epcs flash,elf燒進cfi flash,若成功會出現以下訊息:

#!/bin/sh
#
# This file was automatically generated by the Nios II IDE Flash Programmer.
#
# It will be overwritten when the flash programmer options change.
#

cd D:/0Clare/DE2/DE2_NIOS_epcs_boot/software/hello_led_0/Debug

# Creating .flash file for the FPGA configuration
"$SOPC_KIT_NIOS2/bin/sof2flash" --epcs --input="D:/0Clare/DE2/DE2_NIOS_epcs_boot
/DE2_NIOS.sof
" --output="DE2_NIOS.flash"
Info: *******************************************************************
Info: Running Quartus II Convert_programming_file
Info: Command: quartus_cpf --no_banner --convert --device
=EPCS64 --option=DE2_NI
OS.opt D:/0Clare/DE2/DE2_NIOS_epcs_boot/DE2_NIOS.sof DE2_NIOS.pof
Info: Quartus II Convert_programming_file was successful.
0 errors, 0 warnings
    Info: Allocated
57 megabytes of memory during processing
    Info: Processing ended: Mon Aug
18 22:27:18 2008
    Info: Elapsed time:
00:00:01
Info: *******************************************************************
Info: Running Quartus II Convert_programming_file
Info: Command: quartus_cpf --no_banner --convert DE2_NIOS.pof DE2_NIOS.rpd
Info: Quartus II Convert_programming_file was successful.
0 errors, 0 warnings
    Info: Allocated
53 megabytes of memory during processing
    Info: Processing ended: Mon Aug
18 22:27:20 2008
    Info: Elapsed time:
00:00:01

# Programming flash with the FPGA configuration
"$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --epcs --base=0x01b01800 --instance
=0 "DE2_NIOS.flash"
Using cable
"USB-Blaster [USB-0]", device 1, instance 0x00
Resetting and pausing target processor: OK

              : Checksumming existing contents         

00000000      : Verifying existing contents            

00010000      : Verifying existing contents            

00020000      : Verifying existing contents            

00030000      : Verifying existing contents            

00040000      : Verifying existing contents            

00050000      : Verifying existing contents            

00000000      : Reading existing contents              

00010000      : Reading existing contents              

00020000      : Reading existing contents              

00030000      : Reading existing contents              

00040000      : Reading existing contents              

00050000      : Reading existing contents              

Checksummed/read 384kB in
9.2s                                       

00000000 ( 0%): Erasing                                

00010000 (16%): Erasing                                

00020000 (33%): Erasing                                

00030000 (50%): Erasing                                

00040000 (66%): Erasing                                

00050000 (83%): Erasing                                

Erased 384kB in
5.9s (65.0kB/s)                       

00000000 ( 0%): Programming                            

00010000 (16%): Programming                            

00020000 (33%): Programming                            

00030000 (50%): Programming                            

00040000 (66%): Programming                            

00050000 (83%): Programming                            

Programmed 341KB +43KB in
7.6s (50.5KB/s)                 
Did not attempt to verify device contents
Leaving target processor paused

# Creating .flash file for the project
"$SOPC_KIT_NIOS2/bin/elf2flash" --base=0x01400000 --end=0x17fffff --reset=0x1400
000 --input="hello_led_0.elf" --output="cfi_flash_0.flash" --boot="C:/altera/72/
ip/nios2_ip/altera_nios2/boot_loader_cfi.srec
"

# Programming flash with the project
"$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --base=0x01400000 --instance=0 "cfi
_flash_0.flash
"
Using cable
"USB-Blaster [USB-0]", device 1, instance 0x00
Resetting and pausing target processor: OK

              : Checksumming existing contents         

00000000      : Verifying existing contents            

Checksummed/read 1kB in
0.0s                                       
Erase not required

00000000 ( 0%): Programming                            

Programmed 1KB in
0.0s                                    
No change to device contents
Leaving target processor paused


重新power on後,你就可以看到Nios II程式執行了。

完整程式碼下載
DE2_NIOS_epcs_boot.7z (for DE2)
DE2_70_NIOS_epcs_boot.7z (for DE2-70 with Quartus II 7.2 SP3)
DE2_70_NIOS_epcs_boot_q80.7z (for DE2-70 with Quartus II 8.0)

Remark
Q1.『為什麼nios2-flash-programmer要加上--instance=0參數呢?』

根據Nios II Flash Programmer User Guide的p.2-3的解釋,在多Nios II CPU的系統,會有各自的JTAG debug module,加上此參數指定要使用哪一個JTAG來燒進flash,若只有一個Nios II CPU,怎可忽略此參數。以下摘自Nios II Flash Programmer User Guide

nios_boot04

nios_boot05 


Q2.『你確定是將sof燒進epcs flash嗎?而elf燒進cfi flash嗎?』

所謂『有code有真相,沒code是假象』,我們在Flash Programmer所做的任何設定,最後都會產生script(放在\DE2_NIOS_epcs_boot\software\hello_led_0\Debug\hello_led_0_programmer.sh)

hello_led_0_programmer.sh

1 #!/bin/sh
2 #
3 # This file was automatically generated by the Nios II IDE Flash Programmer.
4 #
5 # It will be overwritten when the flash programmer options change.
6 #
7 
8 cd D:/0Clare/DE2/DE2_NIOS_epcs_boot/software/hello_led_0/Debug
9 
10 # Creating .flash file for the FPGA configuration
11 "$SOPC_KIT_NIOS2/bin/sof2flash" --epcs --input="D:/0Clare/DE2/DE2_NIOS_epcs_boot/DE2_NIOS.sof" --output="DE2_NIOS.flash"
12 
13 # Programming flash with the FPGA configuration
14 "$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --epcs --base=0x01b01800 --instance=0 "DE2_NIOS.flash"
15 
16 # Creating .flash file for the project
17 "$SOPC_KIT_NIOS2/bin/elf2flash" --base=0x01400000 --end=0x17fffff --reset=0x1400000 --input="hello_led_0.elf" --output="cfi_flash_0.flash" --boot="C:/altera/72/ip/nios2_ip/altera_nios2/boot_loader_cfi.srec"
18 
19 # Programming flash with the project
20 "$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --base=0x01400000 --instance=0 "cfi_flash_0.flash"


13行

# Programming flash with the FPGA configuration
"$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --epcs --base=0x01b01800 --instance=0 "DE2_NIOS.flash"


使用nios2-flash-programmer將sof燒進epcs,注意多了--epcs的參數。

19行

# Programming flash with the project
"$SOPC_KIT_NIOS2/bin/nios2-flash-programmer" --base=0x01400000 --instance=0 "cfi_flash_0.flash"


使用nios2-flash-programmer將elf燒進cfi flash,注意沒有--epcs參數,預設就是燒進cfi flash,且--base的位址就是SOPC Builder分配給cfi flash的位址。

Conclsion
至於DE2-70,原理和操作類似,就不在多言,僅附上source code供下載研究。

See Also
(原創) 如何解決CONF_DONE pin failed to go high in device 1的錯誤訊息? (SOC) (Quartus II) (DE2)
(原創) 如何解決No CFI table found at address的錯誤訊息? (SOC) (Nios II) (DE2)
(原創) 如何在Nios II對Flash進行讀寫? (SOC) (Nios II) (DE2) (DE2-70)

Reference
Nios II Flash Programmer User Guide

posted on 2008-08-18 20:34  真 OO无双  阅读(15985)  评论(67编辑  收藏  举报

导航