vxworks c语言xdma中断代码
时间: 2023-11-18 22:05:35 浏览: 192
以下是一个基本的 VxWorks C 语言 XDMA 中断处理程序的代码示例:
```c
#include <vxWorks.h>
#include <vxLib.h>
#include <vxInterruptLib.h>
#include <vxMemLib.h>
#include <vxStdio.h>
#include <vxIpiLib.h>
#include <vmLib.h>
#include <vxbFdtDev.h>
#define DMA_IRQ_NUM 5
LOCAL int dmaCh = 0; // DMA channel
LOCAL BOOL dmaDone = FALSE; // DMA done flag
LOCAL VOID dmaHandler(int irqNum)
{
if (dmaCh == irqNum) {
dmaDone = TRUE; // Set DMA done flag
}
}
int dmaSetup(UINT32 *srcAddr, UINT32 *dstAddr, UINT32 len)
{
int status;
// Map source and destination addresses
char *mappedSrc = (char *)vxMemProbe((char *)srcAddr, VX_READ, sizeof(UINT32));
char *mappedDst = (char *)vxMemProbe((char *)dstAddr, VX_WRITE, sizeof(UINT32));
if (mappedSrc == NULL || mappedDst == NULL) {
return ERROR;
}
// Set up DMA transfer
status = dmaSetupTransfer(dmaCh, srcAddr, dstAddr, len, DMA_MODE);
if (status != OK) {
return status;
}
// Enable DMA channel
status = dmaEnable(dmaCh);
if (status != OK) {
return status;
}
// Wait for DMA completion
while (!dmaDone) {
taskDelay(1);
}
// Disable DMA channel
status = dmaDisable(dmaCh);
if (status != OK) {
return status;
}
return OK;
}
void dmaInit(void)
{
// Install DMA interrupt handler
if (intConnect(INUM_TO_IVEC(DMA_IRQ_NUM), (VOIDFUNCPTR)dmaHandler, dmaCh) == ERROR) {
printf("Failed to install DMA interrupt handler\n");
return;
}
// Enable DMA interrupt
if (intEnable(DMA_IRQ_NUM) == ERROR) {
printf("Failed to enable DMA interrupt\n");
return;
}
// Initialize DMA controller
if (dmaInitController() == ERROR) {
printf("Failed to initialize DMA controller\n");
return;
}
// Set up DMA channel
dmaCh = dmaAllocChannel(DMA_CONTROLLER, DMA_DIRECTION);
if (dmaCh == ERROR) {
printf("Failed to allocate DMA channel\n");
return;
}
}
```
此代码示例中,`dmaInit()` 函数初始化 DMA 控制器并分配 DMA 通道。在 `dmaSetup()` 函数中,使用 `dmaSetupTransfer()` 函数设置 DMA 传输并启用 DMA 通道。在 DMA 传输完成后,`dmaHandler()` 函数被调用,将 `dmaDone` 标志设置为 TRUE。在主程序中,可以使用 `dmaSetup()` 函数来启动 DMA 传输,并等待传输完成。
阅读全文