⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sgl.c

📁 pci卡驱动开发程序.该程序采用DMA方式传输
💻 C
📖 第 1 页 / 共 2 页
字号:
    desc.EnableBTERMInput = 0;
    desc.EnableIopBurst = 0;
    desc.EnableWriteInvalidMode = 0;
    desc.EnableDmaEOTPin = 0;
    desc.DmaStopTransferMode = AssertBLAST;
    desc.HoldIopAddrConst = 0;
    desc.HoldIopSourceAddrConst = 0;
    desc.HoldIopDestAddrConst = 0;
    desc.DemandMode = 0;
    desc.EnableTransferCountClear = 0;
    desc.DmaChannelPriority = Rotational; /* rotational priority */
    desc.WaitStates = 0;
    desc.IopBusWidth = 3; /* 32 bit bus */
    desc.Reserved1 = 0;
    desc.TholdForIopWrites = 0;
    desc.TholdForIopReads = 0;
    desc.TholdForPciWrites = 0;
    desc.TholdForPciReads = 0;
    desc.Reserved2 = 0;

    printf ("Opening channel...\n");

    if ((rc=PlxDmaSglChannelOpen(drvHandle, 
                               dmaChannel, 
                               &desc))
         != ApiSuccess)
    {
        printf ("\n\nError in opening channel 0.\n");
        DescribeErrorCode(rc, stdout);
        printf ("Press any key to exit.....");
        getch();
        return;
    }


    /*
     *  WARNING: A wrong IOP local offset may hang the system.  
     */
    printf("Please enter the address of the local buffer (in hex):");
    scanf( "%x", &localStartOffset );

    /*
     *  WARNING: A wrong IOP local size may also hang the system 
     */
    printf("Now enter the size in bytes of the buffers (in hex):");
    scanf( "%x", &totalSize );

    totalSize &= 0xFFFFFFFC;

    /* Allocate buffers */
    bufFrom = (PU32)calloc( totalSize, sizeof( U8 ) );
    if( bufFrom == NULL )
    {
        printf( "Can't allocate memory\n" );
        PlxDmaSglChannelClose(drvHandle, dmaChannel);
 		return;
    }
    bufTo = (PU32)calloc( totalSize, sizeof( U8 ) );
    if( bufTo == NULL )
    {
        printf( "Can't allocate memory\n" );
        free( bufFrom );    
        PlxDmaSglChannelClose(drvHandle, dmaChannel);
        return;
    }
    
    /* Initialize from buffer with a count */
    count = totalSize;
    buf = bufFrom;
    value = 0;
    while(count != 0)
    {
        count -= sizeof(U32);
        *buf++ = value++;
    }
    
    printf("Enter the number of transfer cycles (in dec):");
    scanf( "%d", &count );

    printf("\n Performing transfer continuously... \n");

    /* These parameters will never change */
    dmaData.Pci9080Dma.IopAddr           = localStartOffset;
    dmaData.Pci9080Dma.TransferCount     = totalSize;
    dmaData.Pci9080Dma.TerminalCountIntr = 0;

    /* Get the timer */
    startTime = GetTickCount();

    /* 
        Perform transfers until the number of cycles has been reached 
        or any key was pressed.
    */
    while(!kbhit() && (count-- != 0))
    {
        /* Increment each value of the from buffer */
        for(value = 0; value < (totalSize / 4); value ++)
        {
            bufFrom[value]++;
        }

        /* Set the variables members of the data structure */
        dmaData.Pci9080Dma.UserAddr        = (U32)bufFrom;
        dmaData.Pci9080Dma.IopToPciDma       = 0;

        /* Transfer from PCI to IOP */
        if( (rc=PlxDmaSglTransfer(drvHandle, 
                                    dmaChannel,
                                    DmaStart,
                                    &dmaData,
                                    FALSE ))
        != ApiSuccess)
        {
            printf ("\n\nError in PCI to IOP Sgl Dma.\n");
            DescribeErrorCode(rc, stdout);
            break;
        }

        /* Just print a back slash */
        printf("\\");

        /* Setup the transfer structure for the data return */
        dmaData.Pci9080Dma.UserAddr        = (U32)bufTo;
        dmaData.Pci9080Dma.IopToPciDma       = 1;

        /* Transfer from IOP back to PCI */
        if( (rc=PlxDmaSglTransfer(drvHandle, 
                                    dmaChannel,
                                    DmaStart,
                                    &dmaData,
                                    FALSE ))
        != ApiSuccess)
        {
            printf ("\n\nError in IOP to PCI Sgl Dma.\n");
            DescribeErrorCode(rc, stdout);
            break;
        }

        /* Compare the 2 buffers */
        for(value = 0; value < (totalSize / 4); value ++)
        {
            if (bufFrom[value] != bufTo[value])
            {
                printf("mismatch at 0x%08x\n", value * 4);
                break;
            }
        }

        /* Print a slash */
        printf("/");

    }

    /* Display some stats */
    transferTime = GetTickCount() - startTime;
    printf("\nNumber of cycles: %d\n", bufFrom[0]);
    printf("Transfer time (ms): %d\n", transferTime);
    printf("Total number of bytes transfered: 0x%08x\n", 2 * totalSize * bufFrom[0]);

    /* In the case the while was stopped by a keyboard hit, get the hit */
    if (kbhit())
        getch();

    free(bufFrom);
    free(bufTo);

    PlxDmaSglChannelClose(drvHandle, dmaChannel);
}


/******************************************************************************
 * Function: DescribeErrorCode
 *
 * Description: Print a message to the output file specified about the meaning
 *              of an error message.
 *
 ******************************************************************************/
VOID
DescribeErrorCode(RETURN_CODE rc, FILE *fp)
{
    typedef struct _SOMETHING
    {
        RETURN_CODE rc;
        char *String;
    }SOMETHING;

    SOMETHING errorStruct[] = 
    {
        ApiSuccess, "ApiSuccess",
        ApiFailed, "ApiFailed",
        ApiAccessDenied, "ApiAccessDenied",
        ApiDmaChannelUnavailable,"ApiDmaChannelUnavailable",
        ApiDmaChannelInvalid,"ApiDmaChannelInvalid",
        ApiDmaChannelTypeError,"ApiDmaChannelTypeError",
        ApiDmaInProgress,"ApiDmaInProgress",
        ApiDmaDone,"ApiDmaDone",
        ApiDmaPaused,"ApiDmaPaused",
        ApiDmaNotPaused,"ApiDmaNotPaused",
        ApiDmaCommandInvalid,"ApiDmaCommandInvalid",
        ApiDmaManReady,"ApiDmaManReady",
        ApiDmaManNotReady,"ApiDmaManNotReady",
        ApiDmaInvalidChannelPriority,"ApiDmaInvalidChannelPriority",
        ApiDmaManCorrupted,"ApiDmaManCorrupted",
        ApiDmaInvalidElementIndex,"ApiDmaInvalidElementIndex",
        ApiDmaNoMoreElements,"ApiDmaNoMoreElements",
        ApiDmaSglInvalid,"ApiDmaSglInvalid",
        ApiDmaSglQueueFull,"ApiDmaSglQueueFull",
        ApiNullParam,"ApiNullParam",
        ApiInvalidBusIndex,"ApiInvalidBusIndex",
        ApiUnsupportedFunction,"ApiUnsupportedFunction",
        ApiInvalidPciSpace,"ApiInvalidPciSpace",
        ApiInvalidIopSpace,"ApiInvalidIopSpace",
        ApiInvalidSize,"ApiInvalidSize",
        ApiInvalidAddress,"ApiInvalidAddress",
        ApiInvalidAccessType,"ApiInvalidAccessType",
        ApiMuNotReady,"ApiMuNotReady",
        ApiMuFifoEmpty,"ApiMuFifoEmpty",
        ApiMuFifoFull,"ApiMuFifoFull",
        ApiInvalidRegister,"ApiInvalidRegister",
        ApiDoorbellClearFailed,"ApiDoorbellClearFailed",
        ApiInvalidUserPin,"ApiInvalidUserPin",
        ApiInvalidUserState,"ApiInvalidUserState",
        ApiEepromNotPresent,"ApiEepromNotPresent",
        ApiEepromTypeNotSupported,"ApiEepromTypeNotSupported",
        ApiConfigAccessFailed,"ApiConfigAccessFailed",
        ApiInvalidDeviceInfo,"ApiInvalidDeviceInfo",
        ApiNoActiveDriver,"ApiNoActiveDriver",
        ApiInsufficientResources,"ApiInsufficientResources",
        ApiObjectAlreadyAllocated,"ApiObjectAlreadyAllocated",
        ApiAlreadyInitialized,"ApiAlreadyInitialized",
        ApiNotInitialized,"ApiNotInitialized",
        ApiBadConfigRegEndianMode,"ApiBadConfigRegEndianMode",
        ApiInvalidPowerState,"ApiInvalidPowerState",
        ApiPowerDown , "ApiPowerDown",
        ApiLastError , "ApiLastError"
    };
    SOMETHING *errorParser;

    errorParser = errorStruct;
    
    do
    {
        if (rc == errorParser->rc)
        {
            fprintf(fp, "Error code is %s\n", errorParser->String);
            break;
        }
        errorParser++;
    }while(errorParser->rc != ApiLastError);

    if ((errorParser->rc == ApiLastError) && (rc != ApiLastError))
        fprintf(fp, "Unknown error code\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -