【转载】基于Nios II的DMA传输总结(附源码)
轉(zhuǎn)載:http://blog.ednchina.com/chactor/185802/message.aspx#92932
?
最近練了一段時(shí)間的DMA傳輸,現(xiàn)做如下的總結(jié),分享自己獲得心得以及遇到的一些問(wèn)題。
??? 在系統(tǒng)運(yùn)行時(shí),當(dāng)需要傳輸大量數(shù)據(jù)時(shí),可以采用DMA的方式進(jìn)行傳輸,以解脫出CPU來(lái)處理其他命令。
?????? Nios II中的DMA傳輸有以下三種形式:
1、? 存儲(chǔ)器到存儲(chǔ)器
這種情況下需要同時(shí)打開(kāi)發(fā)送通道和接收通道,而且源地址和目標(biāo)地址都是自增的。
tx = alt_dma_txchan_open("/dev/dma_0");//打開(kāi)發(fā)送通道
dma_res = alt_dma_txchan_send(tx, tx_buf, 32, NULL, NULL);? // tx_buf是源地址
rx = alt_dma_rxchan_open("/dev/dma_0");//打開(kāi)接收通道
dma_res = alt_dma_rxchan_prepare(rx, rx_buf, 32, dma_done, NULL); // rx_buf是目標(biāo)地址,dma_done()是DMA完成后被調(diào)用的回調(diào)函數(shù)。
?
2、? 存儲(chǔ)器到外設(shè)
這種情況下只要打開(kāi)發(fā)送通道,而且源地址是自增的,目標(biāo)地址是固定的。
tx = alt_dma_txchan_open("/dev/dma_0");? // 打開(kāi)發(fā)送通道
alt_dma_txchan_ioctl(tx, ALT_DMA_TX_ONLY_ON, (void *)dst_addr); // dst_addr是目標(biāo)地址
dma_res = alt_dma_txchan_send(tx, tx_buf, 32, dma_done, NULL); // tx_buf是源地址
?
3、? 外設(shè)到存儲(chǔ)器
這種情況下只要打開(kāi)接收通道,而且源地址是固定的,目標(biāo)地址是自增的。
rx = alt_dma_rxchan_open("/dev/dma_0");? // 打開(kāi)接收通道
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是目標(biāo)地址
?
其中通過(guò)alt_dma_txchan_ioctl,alt_dma_rxchan_ioctl還可以設(shè)置每次發(fā)送和接收的字節(jié)數(shù)。
?????? 下面給出兩個(gè)實(shí)例,說(shuō)明DMA傳輸?shù)倪^(guò)程。
?
1、? 存儲(chǔ)器到存儲(chǔ)器
下面程序?yàn)?/span>SDRAM到onchip-memory的數(shù)據(jù)傳輸。
硬件連接圖示:
程序如下:
#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; /* 目標(biāo)地址,從上圖看到0x01801000為onchip-memory的地址*/
//DMA傳輸結(jié)束回調(diào)函數(shù)
static void done_t(void* handle, void* data)
{
???
??? rx_done++;
???
}
?
int main (int argc, char* argv[], char* envp[])
{
?
/* 打開(kāi)發(fā)送通道 */
? if ((txchan = alt_dma_txchan_open("/dev/dma")) == NULL)
? {
??? printf ("Failed to open transmit channel\n");
??? exit (1);
? }
/* 打開(kāi)接收通道 */
? if ((rxchan = alt_dma_rxchan_open("/dev/dma")) == NULL)
? {
??? printf ("Failed to open receive channel\n");
??? exit (1);
? }
/* 開(kāi)始發(fā)送數(shù)據(jù) */
? 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);
? }
/* 開(kāi)始接收數(shù)據(jù)*/
? 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);
? }
/* 等待傳輸結(jié)束 */
? while (!rx_done);
? printf ("Transfer successful!\n");
?
? return 0;
}
程序運(yùn)行結(jié)束后在Nios IDE的console界面中顯示:
Transfer successful!
表明傳輸成功。
?
2、? 存儲(chǔ)器到UART
下面程序?yàn)?/span>SDRAM到UART的數(shù)據(jù)傳輸
硬件連接圖:
程序如下:
#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} ;//待發(fā)送的數(shù)據(jù)
//回調(diào)函數(shù)
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); /* 目標(biāo)地址IOADDR_ALTERA_AVALON_UART_TXDATA(UART_BASE)函數(shù)讀出txdata的地址 */
?
/* 打開(kāi)發(fā)送通道 */
if ((txchan = alt_dma_txchan_open("/dev/dma")) == NULL)
{
printf ("Failed to open transmit channel\n");
exit (1);
}
/* 設(shè)置目標(biāo)地址固定 */
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);
}
//設(shè)置每次發(fā)送一個(gè)字節(jié),即8位,因?yàn)?/span>UART每次只發(fā)送8位
if((rc = alt_dma_txchan_ioctl(txchan,ALT_DMA_SET_MODE_8 ,NULL))<0)
{
??? printf("Failed to set mode 8\n");
??? exit(1);
}
/* 開(kāi)始發(fā)送 */
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);
}
/* 等待發(fā)送結(jié)束 */
while (!tx_done);
printf ("Transfer successful!\n");
return 0;
}
不過(guò)該程序執(zhí)行結(jié)束后,在串口調(diào)試器中顯示Transfer successful!,卻沒(méi)有顯示DMA發(fā)送的那20個(gè)數(shù)字。不知道哪里需要修改,放在這里和大家討論下。
轉(zhuǎn)載于:https://www.cnblogs.com/kongtiao/archive/2011/09/23/2185783.html
超強(qiáng)干貨來(lái)襲 云風(fēng)專(zhuān)訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的【转载】基于Nios II的DMA传输总结(附源码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快速附加没有日志文件的 SQL Serv
- 下一篇: Objective-C语法之static