軟件開發
-
首先,在硬件工程文件夾里面新建一個software的文件夾用於放置軟件部分;打開tools àNios II 11.0 Software Build Tools for Eclipse,需要進行Workspace Launcher(工作空間)路徑的設置,需要注意的是路徑中不要含有空格等,然后單擊OK即可。
-
新建工程。單擊File -> New -> Nios II Application and BSP from Template,彈出Nios II Application and BSP from Template對話框。先選擇對應的SOPC系統,單擊SOPC Information File name后面的瀏覽按鈕,選擇之前硬件部分做好的軟核文件,后綴名為.sopcinfo,這里一定要注意,選擇的文件一定要對應起來,否則會因為軟硬不匹配導致系統失敗。這里選擇的 lab5_uart.sopcinfo,然后系統會自動讀取CPU name,不用再進行設置,下面填寫Project name,這里填寫為 lab5_uart,工程模板(Project template)使用默認的即可。然后單擊Finish完成即可。這時候會在左側的Project Explorer中生成兩個工程文件。
代碼設計具體看源工程
-
右擊工程,選擇Nios II -> BSP Editor,進入Nios II BSP Editor配置界面。主要在main選項卡下hall中進行配置。然后單擊Generate,生成BSP庫。生成完成后,單擊Exit退出即可。
-
編譯工程。右擊選擇Build Project。第一次編譯的話,時間也會比較常,耐心等待一下。
-
編譯完成后,先將.sof文件下載到FPGA;
-
右擊工程,選擇Run As -> Nios II Hardware,彈出Run Configurations對話框,默認Project選項卡中Project name和Project ELF file name應該都是有內容的,沒有的選一下。然后進入Target Connection選項卡,Connections中如果沒有東西的話,單擊右側的Refresh Connection來查找下載器,查找后單擊System ID Prroperties…,進行系統ID檢測,檢查是否是之前設置的ID號,無誤后點擊Apply,然后再點擊Run,這是程序會被自動下載,最終在Nios II Console選項卡中會顯示下載完成后程序運行的結果。
-
運行結果,在串口助手輸入daxigua,波特率設置為115200,然后進行發送。在調試窗口輸出daxigua。
實驗代碼
/* * "Hello World" example. * * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT * device in your system's hardware. * The memory footprint of this hosted application is ~69 kbytes by default * using the standard reference design. * * For a reduced footprint version of this template, and an explanation of how * to reduce the memory footprint for a given application, see the * "small_hello_world" template. * */ #include <stdio.h> #include <unistd.h> #include <io.h> #include <string.h> #include "system.h" #include "alt_types.h" #include "altera_avalon_uart_regs.h" #include "sys\alt_irq.h"
static alt_u8 txdata =0; static alt_u8 rxdata =0; //UART中斷服務函數 staticvoid uart_isr(void* context,alt_u32 id) { rxdata = IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE); txdata = rxdata; //查詢發送准備好信號,如果沒有准備好,則等待 while(!((IORD_ALTERA_AVALON_UART_STATUS(UART_BASE)&ALTERA_AVALON_UART_STATUS_TRDY_MSK))); //發送准備好,發送txdata IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,txdata); } void uart_init() { //清除狀態寄存器 IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE,0); //使能接受准備好中斷 IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE,0X80); } int main() { printf("Please Start!\n"); //注冊UART中斷服務函數 alt_ic_isr_register(UART_IRQ_INTERRUPT_CONTROLLER_ID, UART_IRQ,uart_isr,NULL,0x00); uart_init(); while(1){} return0; } |
大西瓜FPGA-->https://daxiguafpga.taobao.com
博客資料、代碼、圖片、文字等屬大西瓜FPGA所有,切勿用於商業! 若引用資料、代碼、圖片、文字等等請注明出處,謝謝!
每日推送不同科技解讀,原創深耕解讀當下科技,敬請關注微信公眾號“科乎”。