Apex中DMA的代碼.代碼主要來自LINUX.:)其實這些代碼已經都比較成熟了.

來源:互聯網
上載者:User

#include "dma.h"

/* 如何使用:
 *
 * 調用request_dma請求指定的DMA通道.如果返回0表現該通道可用;
 * 在使用完畢以後使用free_dma釋放該通道.
 */

 

/* dma_chan_busy[n] != 0 表示該通道不可用
 * DMA0 用作DRAM的重新整理.
 * DMA4 用作級連.
 */
static volatile unsigned int dma_chan_busy[MAX_DMA_CHANNELS] = {
 1, 0, 0, 0, 1, 0, 0, 0
};

 

/* 將*p指向的資料替換為newval.由關鍵字xchgl看,這個值是32位的值。
 * xchgl是原子操作,可以省去cli和sti,穩定和效能都有益的.
 */
static __inline__ unsigned int mutex_atomic_swap(volatile unsigned int * p, unsigned int newval)
{
 unsigned int semval = newval;

 /* If one of the operands for the XCHG instructions is a memory ref,
  * it makes the swap an uninterruptible RMW cycle.
  *
  * One operand must be in memory, the other in a register, otherwise
  * the swap may not be atomic.
  */

 asm __volatile__ ("xchgl %2, %0/n"
   : /* outputs: semval   */ "=r" (semval)
   : /* inputs: newval, p */ "0" (semval), "m" (*p)
   ); /* p is a var, containing an address */
 return semval;
} /* mutex_atomic_swap */

#define EINVAL 1
#define EBUSY 2

int request_dma(unsigned int dmanr)
{
 if (dmanr >= MAX_DMA_CHANNELS)
  return -EINVAL;

 if (mutex_atomic_swap(&dma_chan_busy[dmanr], 1) != 0)
  return -EBUSY;
 else
  /* old flag was 0, now contains 1 to indicate busy */
  return 0;
} /* request_dma */

#define printk
void free_dma(unsigned int dmanr)
{
 if (dmanr >= MAX_DMA_CHANNELS) {
  printk("Trying to free DMA%d/n", dmanr);
  return;
 }

 if (mutex_atomic_swap(&dma_chan_busy[dmanr], 0) == 0)
  printk("Trying to free free DMA%d/n", dmanr);
} /* free_dma */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.