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

📄 pcietest.cpp

📁 基于xilinx vierex5得pci express dma设计实现。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    PUCHAR WTraverse;
    PUCHAR RTraverse;

    if (ReadBufferSize <= WriteBufferSize) {
        size = ReadBufferSize;
    } else {
        size = WriteBufferSize;
    }

    WTraverse = WriteBuffer;
    RTraverse = ReadBuffer;

    for(ULONG i = 0; i < size; i++) {
        if (*WTraverse++ != *RTraverse++) {
            status = FALSE;
        }
    }

    if (status) {
        if (!Quite) {
            printf("Buffers are identical\n");
        }
    } else {
        printf("Buffers not identical\n");
        this->Status = 1;
    }

    return status;
}

void
PCIE::DisplayReadWriteBuffers()
{
    if (!Quite) {

        PUCHAR WTraverse;
        PUCHAR RTraverse;

        WTraverse = WriteBuffer;
        RTraverse = ReadBuffer;

        printf("Write: ");
        for(ULONG i = 0; i < WriteBufferSize; i++) {
            printf("%X ", *WTraverse++);
        }

        printf("\n\n\nRead: ");
        for(ULONG i = 0; i < ReadBufferSize; i++) {
            printf("%X ", *RTraverse++);
        }
        printf("\n");
    }
}

BOOL
PCIE::SetReadBufferSize(ULONG size)
{
    BOOL status = TRUE;

    if (ReadBuffer) {
        free(ReadBuffer);
    }

    ReadBufferSize = size;

    ReadBuffer = (PUCHAR)malloc(ReadBufferSize);

    if (!ReadBuffer) {
        status = FALSE;
    }

    return status;
}

BOOL
PCIE::SetWriteBufferSize(ULONG size)
{
    BOOL status = TRUE;

    if (WriteBuffer) {
        free(WriteBuffer);
    }

    WriteBufferSize = size;

    WriteBuffer = (PUCHAR)malloc(WriteBufferSize);

    if (!WriteBuffer) {
        status = FALSE;
    }

    return status;
}

BOOL
PCIE::SetBufferSizes(ULONG size)
{
    BOOL status;

    status = SetReadBufferSize(size);
    if (status) {
        status = SetWriteBufferSize(size);
    }

    return status;
}

BOOL
PCIE::GetDevicePath()
{
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
    SP_DEVINFO_DATA DeviceInfoData;

    ULONG size;
    int count, i, index;
    BOOL status = TRUE;
    TCHAR *DeviceName = NULL;
    TCHAR *DeviceLocation = NULL;

    //
    //  Retreive the device information for all PCIE devices.
    //
    hDevInfo = SetupDiGetClassDevs(&GUID_PCIE_INTERFACE,
                                   NULL,
                                   NULL,
                                   DIGCF_DEVICEINTERFACE |
                                   DIGCF_PRESENT);

    //
    //  Initialize the SP_DEVICE_INTERFACE_DATA Structure.
    //
    DeviceInterfaceData.cbSize  = sizeof(SP_DEVICE_INTERFACE_DATA);

    //
    //  Determine how many devices are present.
    //
    count = 0;
    while(SetupDiEnumDeviceInterfaces(hDevInfo,
                                      NULL,
                                      &GUID_PCIE_INTERFACE,
                                      count++,  //Cycle through the available devices.
                                      &DeviceInterfaceData)
          );

    //
    // Since the last call fails when all devices have been enumerated,
    // decrement the count to get the true device count.
    //
    count--;

    //
    //  If the count is zero then there are no devices present.
    //
    if (count == 0) {
        printf("No PCIE devices are present and enabled in the system.\n");
        this->Status = 1;
        return FALSE;
    }

    //
    //  Initialize the appropriate data structures in preparation for
    //  the SetupDi calls.
    //
    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    //
    //  Loop through the device list to allow user to choose
    //  a device.  If there is only one device, select it
    //  by default.
    //
    i = 0;
    while (SetupDiEnumDeviceInterfaces(hDevInfo,
                                       NULL,
                                       (LPGUID)&GUID_PCIE_INTERFACE,
                                       i,
                                       &DeviceInterfaceData)) {

        //
        // Determine the size required for the DeviceInterfaceData
        //
        SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                        &DeviceInterfaceData,
                                        NULL,
                                        0,
                                        &size,
                                        NULL);

        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
            printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
            this->Status = 1;
            return FALSE;
        }

        pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);

        if (!pDeviceInterfaceDetail) {
            printf("Insufficient memory.\n");
            this->Status = 1;
            return FALSE;
        }

        //
        // Initialize structure and retrieve data.
        //
        pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
        status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                                 &DeviceInterfaceData,
                                                 pDeviceInterfaceDetail,
                                                 size,
                                                 NULL,
                                                 &DeviceInfoData);

        free(pDeviceInterfaceDetail);

        if (!status) {
            printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
            this->Status = 1;
            return status;
        }

        //
        //  Get the Device Name
        //  Calls to SetupDiGetDeviceRegistryProperty require two consecutive
        //  calls, first to get required buffer size and second to get
        //  the data.
        //
        SetupDiGetDeviceRegistryProperty(hDevInfo,
                                        &DeviceInfoData,
                                        SPDRP_DEVICEDESC,
                                        NULL,
                                        (PBYTE)DeviceName,
                                        0,
                                        &size);

        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
            printf("SetupDiGetDeviceRegistryProperty failed, Error: %d", GetLastError());
            this->Status = 1;
            return FALSE;
        }

        DeviceName = (TCHAR*) malloc(size);
        if (!DeviceName) {
            printf("Insufficient memory.\n");
            this->Status = 1;
            return FALSE;
        }

        status = SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                  &DeviceInfoData,
                                                  SPDRP_DEVICEDESC,
                                                  NULL,
                                                  (PBYTE)DeviceName,
                                                  size,
                                                  NULL);
        if (!status) {
            printf("SetupDiGetDeviceRegistryProperty failed, Error: %d",
                   GetLastError());
            free(DeviceName);
            this->Status = 1;
            return status;
        }

        //
        //  Now retrieve the Device Location.
        //
        SetupDiGetDeviceRegistryProperty(hDevInfo,
                                         &DeviceInfoData,
                                         SPDRP_LOCATION_INFORMATION,
                                         NULL,
                                         (PBYTE)DeviceLocation,
                                         0,
                                         &size);

        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
            DeviceLocation = (TCHAR*) malloc(size);

            if (DeviceLocation != NULL) {
                status = SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                          &DeviceInfoData,
                                                          SPDRP_LOCATION_INFORMATION,
                                                          NULL,
                                                          (PBYTE)DeviceLocation,
                                                          size,
                                                          NULL);
                if (!status) {
                    free(DeviceLocation);
                    DeviceLocation = NULL;
                }
            }

        } else {
            DeviceLocation = NULL;
        }

        //
        // If there is more than one device print description.
        //
        if (count > 1 && console) {
            printf("%d- ", i);
        }

        printf("%s\n", DeviceName);

        if (DeviceLocation) {
            printf("        %s\n", DeviceLocation);
        }

        free(DeviceName);
        DeviceName = NULL;

        if (DeviceLocation) {
            free(DeviceLocation);
            DeviceLocation = NULL;
        }

        i++; // Cycle through the available devices.
    }

    //
    //  Select device.
    //
    index = 0;
    if (count > 1) {
        printf("\nSelect Device: ");

        if (scanf_s("%d", &index) == 0) {
            return ERROR_INVALID_DATA;
        }
    }

    //
    //  Get information for specific device.
    //
    status = SetupDiEnumDeviceInterfaces(hDevInfo,
                                    NULL,
                                    (LPGUID)&GUID_PCIE_INTERFACE,
                                    index,
                                    &DeviceInterfaceData);

    if (!status) {
        printf("SetupDiEnumDeviceInterfaces failed, Error: %d", GetLastError());
        return status;
    }

    //
    // Determine the size required for the DeviceInterfaceData
    //
    SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                    &DeviceInterfaceData,
                                    NULL,
                                    0,
                                    &size,
                                    NULL);

    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
        printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
        this->Status = 1;
        return FALSE;
    }

    pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);

    if (!pDeviceInterfaceDetail) {
        printf("Insufficient memory.\n");
        this->Status = 1;
        return FALSE;
    }

    //
    // Initialize structure and retrieve data.
    //
    pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

    status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                             &DeviceInterfaceData,
                                             pDeviceInterfaceDetail,
                                             size,
                                             NULL,
                                             &DeviceInfoData);
    if (!status) {
        printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
        this->Status = 1;
        return status;
    }

    return status;
}

BOOL
PCIE::GetDeviceHandle()
{
    BOOL status = TRUE;

    if (pDeviceInterfaceDetail == NULL) {
        status = GetDevicePath();
    }
    if (pDeviceInterfaceDetail == NULL) {
        status = FALSE;
    }

    if (status) {

        //
        //  Get handle to device.
        //
        hDevice = CreateFile(pDeviceInterfaceDetail->DevicePath,
                             GENERIC_READ|GENERIC_WRITE,
                             FILE_SHARE_READ | FILE_SHARE_WRITE,
                             NULL,
                             OPEN_EXISTING,
                             0,
                             NULL);

        if (hDevice == INVALID_HANDLE_VALUE) {
            status = FALSE;
            printf("CreateFile failed.  Error:%d", GetLastError());
            this->Status = 1;
        }
    }

    return status;
}

void
PCIE::SetThreadLifeTime(ULONG time)
{
    ThreadTimer = time;
}

⌨️ 快捷键说明

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