【转载】基于Nios II的DMA传输总结(附源码)
轉載:http://blog.ednchina.com/chactor/185802/message.aspx#92932
?
最近練了一段時間的DMA傳輸,現做如下的總結,分享自己獲得心得以及遇到的一些問題。
??? 在系統運行時,當需要傳輸大量數據時,可以采用DMA的方式進行傳輸,以解脫出CPU來處理其他命令。
?????? Nios II中的DMA傳輸有以下三種形式:
1、? 存儲器到存儲器
這種情況下需要同時打開發送通道和接收通道,而且源地址和目標地址都是自增的。
tx = alt_dma_txchan_open("/dev/dma_0");//打開發送通道
dma_res = alt_dma_txchan_send(tx, tx_buf, 32, NULL, NULL);? // tx_buf是源地址
rx = alt_dma_rxchan_open("/dev/dma_0");//打開接收通道
dma_res = alt_dma_rxchan_prepare(rx, rx_buf, 32, dma_done, NULL); // rx_buf是目標地址,dma_done()是DMA完成后被調用的回調函數。
?
2、? 存儲器到外設
這種情況下只要打開發送通道,而且源地址是自增的,目標地址是固定的。
tx = alt_dma_txchan_open("/dev/dma_0");? // 打開發送通道
alt_dma_txchan_ioctl(tx, ALT_DMA_TX_ONLY_ON, (void *)dst_addr); // dst_addr是目標地址
dma_res = alt_dma_txchan_send(tx, tx_buf, 32, dma_done, NULL); // tx_buf是源地址
?
3、? 外設到存儲器
這種情況下只要打開接收通道,而且源地址是固定的,目標地址是自增的。
rx = alt_dma_rxchan_open("/dev/dma_0");? // 打開接收通道
alt_dma_rxchan_ioctl(rx, ALT_DMA_RX_ONLY_ON, (void *)source_addr); // source_addr是源地址
dma_res = alt_dma_rxchan_prepare(rx, rx_buf, 32, dma_done, NULL);? // rx_buf是目標地址
?
其中通過alt_dma_txchan_ioctl,alt_dma_rxchan_ioctl還可以設置每次發送和接收的字節數。
?????? 下面給出兩個實例,說明DMA傳輸的過程。
?
1、? 存儲器到存儲器
下面程序為SDRAM到onchip-memory的數據傳輸。
硬件連接圖示:
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/alt_dma.h>
#include "system.h"
static volatile int rx_done = 0;
int rc;
alt_dma_txchan txchan;
alt_dma_rxchan rxchan;
static char buff[256];
void* tx_data = (void*) buff; /* 源地址 */
void* rx_buffer = (void*) 0x01801000; /* 目標地址,從上圖看到0x01801000為onchip-memory的地址*/
//DMA傳輸結束回調函數
static void done_t(void* handle, void* data)
{
???
??? rx_done++;
???
}
?
int main (int argc, char* argv[], char* envp[])
{
?
/* 打開發送通道 */
? if ((txchan = alt_dma_txchan_open("/dev/dma")) == NULL)
? {
??? printf ("Failed to open transmit channel\n");
??? exit (1);
? }
/* 打開接收通道 */
? if ((rxchan = alt_dma_rxchan_open("/dev/dma")) == NULL)
? {
??? printf ("Failed to open receive channel\n");
??? exit (1);
? }
/* 開始發送數據 */
? if ((rc = alt_dma_txchan_send (txchan,
???????????????????????????????? tx_data,
???????????????????????????????? 128,
???????????????????????????????? NULL,
???????????????????????????????? NULL)) < 0)
? {
??? printf ("Failed to post transmit request, reason = %i\n", rc);
??? exit (1);
? }
/* 開始接收數據*/
? if ((rc = alt_dma_rxchan_prepare (rxchan,
??????????????????????????????????? rx_buffer,
??????????????????????????????????? 128,
??????????????????????????????????? done_t,
??????????????????????????????????? NULL)) < 0)
? {
??? printf ("Failed to post read request, reason = %i\n", rc);
??? exit (1);
? }
/* 等待傳輸結束 */
? while (!rx_done);
? printf ("Transfer successful!\n");
?
? return 0;
}
程序運行結束后在Nios IDE的console界面中顯示:
Transfer successful!
表明傳輸成功。
?
2、? 存儲器到UART
下面程序為SDRAM到UART的數據傳輸
硬件連接圖:
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include "sys/alt_dma.h"
#include "altera_avalon_uart_regs.h"
#include "system.h"
#include "alt_types.h"
?
static volatile int tx_done = 0;
volatile static alt_u8 chr[20] = {1,2,3,4,6,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20} ;//待發送的數據
//回調函數
static void done (void* handle)
{
tx_done++;
}
int main()
{
int rc;
alt_dma_txchan txchan;
?
void* source_buff_ptr = (void*) chr; /* 源地址 */
void* destination_buff_ptr = (void*)IOADDR_ALTERA_AVALON_UART_TXDATA(UART_BASE); /* 目標地址IOADDR_ALTERA_AVALON_UART_TXDATA(UART_BASE)函數讀出txdata的地址 */
?
/* 打開發送通道 */
if ((txchan = alt_dma_txchan_open("/dev/dma")) == NULL)
{
printf ("Failed to open transmit channel\n");
exit (1);
}
/* 設置目標地址固定 */
if ((rc = alt_dma_txchan_ioctl(txchan, ALT_DMA_TX_ONLY_ON, destination_buff_ptr)) < 0)
{
printf ("Failed to set ioctl, reason = %i\n", rc);
exit (1);
}
//設置每次發送一個字節,即8位,因為UART每次只發送8位
if((rc = alt_dma_txchan_ioctl(txchan,ALT_DMA_SET_MODE_8 ,NULL))<0)
{
??? printf("Failed to set mode 8\n");
??? exit(1);
}
/* 開始發送 */
if ((rc = alt_dma_txchan_send(txchan, source_buff_ptr, 20, done, NULL)) < 0)
{
printf ("Failed to post transmit request, reason = %i\n", rc);
exit (1);
}
/* 等待發送結束 */
while (!tx_done);
printf ("Transfer successful!\n");
return 0;
}
不過該程序執行結束后,在串口調試器中顯示Transfer successful!,卻沒有顯示DMA發送的那20個數字。不知道哪里需要修改,放在這里和大家討論下。
轉載于:https://www.cnblogs.com/kongtiao/archive/2011/09/23/2185783.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的【转载】基于Nios II的DMA传输总结(附源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速附加没有日志文件的 SQL Serv
- 下一篇: Objective-C语法之static