nios sgdma(Scatter-Gather dma)示例

在 Quartus7.2以後的版本中,除了原有的基於avalon-mm總線的DMA以外,還增長了Scatter-Gather DMA這種基於avalon-ST流總線的DMA IP核,它更適合與大量數據流傳輸的場合,使用起來比較靈活,增長了與外設流器件配合的能力。因爲網上關於SG-DMA介紹的資料比較少,所以這裏簡單介 紹一下SG-DMA的使用,利用它能夠搭配Altera的千兆網MAC核來實現千兆網方面的應用。css

SG-DMA的 數據手冊已經介紹得很是詳細(見Scatter-Gather DMA Controller Core
),具體的相關寄存器和功能可能查閱相關手冊。Altera爲了開發的便利,已經爲各個IP核設計好了HAL軟件層的代 碼,SG-DMA也不例外,所以使用的時候咱們沒有必要逐個配置相關寄存器,直接調用HAL層代碼便可。這也是使用這類IP核簡便的地方,只是須要清楚這 類代碼如何調用。
ios

  

1. 首先咱們簡單看看SG-DMA的應用環境,從數據手冊中截下幾張圖片簡單介紹。web

SG-DMA有 三種工做方式,能夠工做在Memory-to-Stream即存儲接口到流接口,或者Stream-to-Memory即流接口到存儲接口,以及 Memory-to-Memory的存儲器到存儲器工做方式。工做在存儲器到存儲器的工做方式與普通DMA並沒有差異,沒有數據流處理的優點。另外SG- DMA增長了Descriptor Processor,能夠實現批量工做,從而進一步減輕Nios處理器的工做。只須要將Descriptor命令字寫入到相應的Descriptor memory中。咱們簡單看看以上的工做方式。緩存

 

圖1. Memory-to-Streamasync

圖2. Stream-to-Memoryide

圖3. Memory-to-Memory函數

2. 而後咱們直接進入主題,看在Altera的SOPC中如何鏈接使用SG-DMA器件。測試

M-to-M模 式就不作介紹了,這裏主要介紹M-to-S和S-to-M這兩種方式。咱們添加兩個SG-DMA器件,讓它們分別工做在這兩個工做方式下。鏈接示意以下所 示。注意到其中的descriptor memory的設置,原則上只要帶有avalon-mm接口的存儲器均可以用來作descriptor memroy,所以咱們能夠將decriptor memory與主存分離,亦能夠直接使用主存的一部分做爲descriptor memroy。但爲了避免影響主存的使用,最好將descriptor memroy分離。另外SG-DMA的有關設置,例如channel和error的位數控制能夠參考avalon-st流接口數據手冊,依照須要設置接 口。因爲在本例中只有一個通道,也不校驗錯誤,因此咱們都設置爲零。spa

3. SG-DMA HAL代碼調用。.net

要使得SG-DMA正式工做起來,咱們能夠直接調用HAL層代碼,省去不少開發時間。下面直接使用一段程序,添加部分註釋,相信SG-DMA的基本使用便可完成了,並無相信中的這麼複雜。 

  1. #include <stdio.h>
  2. #include "altera_avalon_sgdma_descriptor.h"
  3. #include "altera_avalon_sgdma_regs.h"
  4. #include "altera_avalon_sgdma.h"
  5. #include "system.h"
  6. #include "alt_types.h"
  7. //注意包含這幾個頭文件
  8. alt_sgdma_dev *sgdma_tx_dev; //sgdma_tx設備文件
  9. alt_sgdma_dev *sgdma_rx_dev; //sgdma_rx設備文件
  10. alt_sgdma_descriptor *desc; //descriptor memory指針
  11. char buf[1000]; //SG-DMA傳送緩存,暫定1000字節作測試
  12. alt_u32 rx_payload[256]; //SG-DMA接收緩存
  13. void sgdma_rx_isr(void * context, u_long intnum);
  14. //咱們的基本思路就是,先配置好sgdma_rx和sgdma_tx的基本配置,而後設置好sgdma_rx的回調函數。
  15. //即接收數據完成以後調用的函數,最後啓動sgdma_tx完成dma發送。在這個過程當中涵蓋了sgdma_tx和sgdma_rx的基本使用
  16. int main()
  17. {
  18.     int i;
  19.     int timeout = 0;
  20.     for(i=0; i<1000; i++) //填充緩存數據
  21.         buf[i] = i%256;
  22.      //重定義desc DISCRIPTOR_MEMORY_BASE定義在system.h中,即descriptor_memory的基地址
  23.     desc = (alt_sgdma_descriptor *)DESC_MEM_BASE;
  24.     //打開sgdma_tx和sgdma_rx
  25.     sgdma_tx_dev = alt_avalon_sgdma_open("/dev/tx_sgdma");
  26.     if(!sgdma_tx_dev) 
  27.     {
  28.         printf("[triple_speed_ethernet_init] Error opening TX SGDMA\n");
  29.         return -1;
  30.     }
  31.     sgdma_rx_dev = alt_avalon_sgdma_open("/dev/rx_sgdma");
  32.     if(!sgdma_rx_dev) 
  33.     {
  34.         printf("[triple_speed_ethernet_init] Error opening RX SGDMA\n");
  35.         return -1;
  36.     }
  37.     
  38.     /* Reset RX-side SGDMA */
  39.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(RX_SGDMA_BASE, ALTERA_AVALON_SGDMA_CONTROL_SOFTWARERESET_MSK);
  40.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(RX_SGDMA_BASE, 0x0);
  41.     /* Reset TX-side SGDMA */
  42.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(TX_SGDMA_BASE, 0);
  43.     IOWR_ALTERA_AVALON_SGDMA_STATUS(TX_SGDMA_BASE, 0xFF);
  44.     //註冊sgdma_rx回調函數
  45.     alt_avalon_sgdma_register_callback(
  46.                         sgdma_rx_dev,
  47.                         (alt_avalon_sgdma_callback) &sgdma_rx_isr,
  48.                         //ALTERA_AVALON_SGDMA_CONTROL_IE_DESC_COMPLETED_MSK | //每一個描述符數據傳輸完產生中斷
  49.                         ALTERA_AVALON_SGDMA_CONTROL_IE_CHAIN_COMPLETED_MSK | //全部描述符數據傳輸完時產生中斷
  50.                         ALTERA_AVALON_SGDMA_CONTROL_IE_GLOBAL_MSK //開中斷
  51.                         ,
  52.                         0);
  53.     //填充接收decriptor memory 並不須要本身填充,調用函數就行了。
  54.     alt_avalon_sgdma_construct_stream_to_mem_desc(
  55.                         &desc[0], //主描述字
  56.                         &desc[1], //次描述字
  57.                         rx_payload, //接收地址
  58.                         0, //length,爲0時當收到EOP時結束
  59.                         0); //write_fixed
  60.                         
  61.     //填充發送decriptor memory 
  62.     alt_avalon_sgdma_construct_mem_to_stream_desc(
  63.                         &desc[2], //主描述字
  64.                         &desc[3], //次描述字
  65.                         (unsigned int*)buf, //發送指針
  66.                         (256), //發送字數
  67.                         0, //read_fixed
  68.                         0, //不發送SOP
  69.                         0, //不發送EOP
  70.                         0); //暫不支持
  71.                         
  72.     alt_avalon_sgdma_construct_mem_to_stream_desc(
  73.                         &desc[3], //主描述字
  74.                         &desc[4], //次描述字
  75.                         (unsigned int*)(buf+256), //發送指針
  76.                         (256), //發送字數
  77.                         0, 
  78.                         1, //發送SOP
  79.                         1, //發送EOP
  80.                         0); 
  81.     //啓動sgdma_rx和sgdma_tx
  82.     alt_avalon_sgdma_do_async_transfer(sgdma_rx_dev, &desc[0]);
  83.     alt_avalon_sgdma_do_sync_transfer(sgdma_tx_dev, &desc[2]);
  84.     
  85.     for(i=0; i<256; i++)
  86.         printf("%x ",rx_payload[i]);
  87.     printf("\n\n\n\n");
  88. }
  89. //回調函數,負責處理接收後數據,並重置sgdma_rx,本例中並未對數據進行處理
  90. void sgdma_rx_isr(void * context, u_long intnum)
  91. {
  92.     int i;
  93.     alt_sgdma_descriptor *currdescriptor_ptr = &desc[0];
  94.     if(alt_avalon_sgdma_check_descriptor_status(&desc[0])==0)
  95.     {
  96.         printf("RX descriptor reported OK\n");
  97.     } 
  98.     else 
  99.     {
  100.         printf("RX descriptor reported error\n");
  101.     } 
  102.    
  103. }

或者也能夠這樣

  1. #include <stdio.h>
  2. #include "altera_avalon_sgdma_descriptor.h"
  3. #include "altera_avalon_sgdma_regs.h"
  4. #include "altera_avalon_sgdma.h"
  5. #include "system.h"
  6. #include "alt_types.h"
  7. //注意包含這幾個頭文件
  8. alt_sgdma_dev *sgdma_tx_dev; //sgdma_tx設備文件
  9. alt_sgdma_dev *sgdma_rx_dev; //sgdma_rx設備文件
  10. alt_sgdma_descriptor *desc; //descriptor memory指針
  11. char buf[1000]; //SG-DMA傳送緩存,暫定1000字節作測試
  12. alt_u32 rx_payload[256]; //SG-DMA接收緩存
  13. void sgdma_rx_isr(void * context, u_long intnum);
  14. //咱們的基本思路就是,先配置好sgdma_rx和sgdma_tx的基本配置,而後設置好sgdma_rx的回調函數。
  15. //即接收數據完成以後調用的函數,最後啓動sgdma_tx完成dma發送。在這個過程當中涵蓋了sgdma_tx和sgdma_rx的基本使用
  16. int main()
  17. {
  18.     int i;
  19.     int timeout = 0;
  20.     for(i=0; i<1000; i++) //填充緩存數據
  21.         buf[i] = i%256;
  22.      //重定義desc DISCRIPTOR_MEMORY_BASE定義在system.h中,即descriptor_memory的基地址
  23.     desc = (alt_sgdma_descriptor *)DESC_MEM_BASE;
  24.     //打開sgdma_tx和sgdma_rx
  25.     sgdma_tx_dev = alt_avalon_sgdma_open("/dev/tx_sgdma");
  26.     if(!sgdma_tx_dev) 
  27.     {
  28.         printf("[triple_speed_ethernet_init] Error opening TX SGDMA\n");
  29.         return -1;
  30.     }
  31.     sgdma_rx_dev = alt_avalon_sgdma_open("/dev/rx_sgdma");
  32.     if(!sgdma_rx_dev) 
  33.     {
  34.         printf("[triple_speed_ethernet_init] Error opening RX SGDMA\n");
  35.         return -1;
  36.     }
  37.     
  38.     /* Reset RX-side SGDMA */
  39.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(RX_SGDMA_BASE, ALTERA_AVALON_SGDMA_CONTROL_SOFTWARERESET_MSK);
  40.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(RX_SGDMA_BASE, 0x0);
  41.     /* Reset TX-side SGDMA */
  42.     IOWR_ALTERA_AVALON_SGDMA_CONTROL(TX_SGDMA_BASE, 0);
  43.     IOWR_ALTERA_AVALON_SGDMA_STATUS(TX_SGDMA_BASE, 0xFF);
  44.     //註冊sgdma_rx回調函數
  45.     alt_avalon_sgdma_register_callback(
  46.                         sgdma_rx_dev,
  47.                         (alt_avalon_sgdma_callback) &sgdma_rx_isr,
  48.                         //ALTERA_AVALON_SGDMA_CONTROL_IE_DESC_COMPLETED_MSK | //每一個描述符數據傳輸完產生中斷
  49.                         ALTERA_AVALON_SGDMA_CONTROL_IE_CHAIN_COMPLETED_MSK | //全部描述符數據傳輸完時產生中斷
  50.                         ALTERA_AVALON_SGDMA_CONTROL_IE_GLOBAL_MSK //開中斷
  51.                         ,
  52.                         0);
  53.     //填充接收decriptor memory 並不須要本身填充,調用函數就行了。
  54.     alt_avalon_sgdma_construct_stream_to_mem_desc(
  55.                         &desc[0], //主描述字
  56.                         &desc[1], //次描述字
  57.                         rx_payload, //接收地址
  58.                         0, //length,爲0時當收到EOP時結束
  59.                         0); //write_fixed
  60.                         
  61.     //填充發送decriptor memory 
  62.     alt_avalon_sgdma_construct_mem_to_stream_desc(
  63.                         &desc[2], //主描述字
  64.                         &desc[3], //次描述字
  65.                         (unsigned int*)buf, //發送指針
  66.                         (256), //發送字數
  67.                         0, //read_fixed
  68.                         0, //不發送SOP
  69.                         1, //發送EOP
  70.                         0); //暫不支持
  71.                         
  72.     alt_avalon_sgdma_construct_mem_to_stream_desc(
  73.                         &desc[3], //主描述字
  74.                         &desc[4], //次描述字
  75.                         (unsigned int*)(buf+256), //發送指針
  76.                         (256), //發送字數
  77.                         0, 
  78.                         1, //發送SOP
  79.                         1, //發送EOP
  80.                         0); 
  81.     //啓動sgdma_rx和sgdma_tx
  82.     alt_avalon_sgdma_do_async_transfer(sgdma_rx_dev, &desc[0]);
  83.     alt_avalon_sgdma_do_sync_transfer(sgdma_tx_dev, &desc[2]);
  84.     
  85.     for(i=0; i<256; i++)
  86.         printf("%x ",rx_payload[i]);
  87.     printf("\n\n\n\n");
  88. }
  89. //回調函數,負責處理接收後數據,並重置sgdma_rx,本例中並未對數據進行處理
  90. void sgdma_rx_isr(void * context, u_long intnum)
  91. {
  92.     int i;
  93.     alt_sgdma_descriptor *currdescriptor_ptr = &desc[0];
  94.     if(alt_avalon_sgdma_check_descriptor_status(&desc[0])==0)
  95.     {
  96.         printf("RX descriptor reported OK\n");
  97.     } 
  98.     else 
  99.     {
  100.         printf("RX descriptor reported error\n");
  101.     } 
  102.     
  103.     alt_avalon_sgdma_construct_stream_to_mem_desc(
  104.                         &desc[0], //主描述字
  105.                         &desc[1], //次描述字
  106.                         rx_payload+64, //接收地址
  107.                         0, //length,爲0時當收到EOP時結束
  108.                         0); //write_fixed 
  109.     // Re-start SGDMA (always, if we have a single descriptor)
  110.     alt_avalon_sgdma_do_async_transfer(sgdma_rx_dev, &desc[0]);
  111.     
  112. }
相關文章
相關標籤/搜索