mans
mans

Reputation: 18218

Error code 0xC1 when trying to create a file mapping

I am trying to work with a Xilinx-based FPGA board that has an LED at address 0x40000.

The driver for the board is installed and detected by Windows (Windows 11).

I can access the board and turn on/off the LED using a device file and seeking address 0x4000 to turn on/off the LEDs. So the hardware setup is correct and working.

I want to map the device file into memory and turn on/off the LED by accessing the mapped file in memory.

My code is as follows:

DWORD LED_Addr = 0x40000;
DWORD LED_ON = 0x3;
DWORD LED_Off = 0x0;
auto devicePaths = XdmaDevice::GetDevicePaths();
if (devicePaths.size() == 0)
{
    return false;
}
string channelPath = devicePaths[0] + "\\user";

HANDLE file_handle = CreateFile(channelPath.c_str(),
    GENERIC_READ | GENERIC_WRITE, // Access rights
    FILE_SHARE_READ | FILE_SHARE_WRITE, // Share modes
    NULL, // Security attributes
    OPEN_EXISTING, // Creation disposition
    FILE_ATTRIBUTE_NORMAL, // Flags and attributes
    NULL); // Template file
if (file_handle == INVALID_HANDLE_VALUE)
{
    printf("Error");
    return -1;
}
    
size_t mapping_size = sizeof(DWORD);
DWORD  dwMaximumSizeHigh = (LED_Addr + mapping_size) >> 32;
DWORD  dwMaximumSizeLow = (LED_Addr + mapping_size) & 0xFFFFFFFF;
HANDLE  file_mapping_handle_ = ::CreateFileMapping( file_handle, 0, PAGE_READWRITE, dwMaximumSizeHigh, dwMaximumSizeLow, 0);
if (file_mapping_handle_ == NULL)
{
    DWORD error = GetLastError();
    auto message = error_message(error);
    return -1;
}
    

DWORD  dwFileOffsetHigh = LED_Addr >> 32;
DWORD  dwFileOffsetLow = LED_Addr & 0xFFFFFFFF;
DWORD  dwNumberOfBytesToMap = mapping_size;
auto real_data = (MapViewOfFile(file_mapping_handle_, FILE_MAP_WRITE, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap));
if (!real_data)
{
    DWORD error = GetLastError();
    auto message = error_message(error);

    return -1;
}

After running this, I am getting an error on this line:

HANDLE  file_mapping_handle_ = ::CreateFileMapping( file_handle, 0, PAGE_READWRITE, dwMaximumSizeHigh, dwMaximumSizeLow, 0);

The handle is NULL and the error code is 0xC1 and the error message is:

"%1 is not a valid Win32 application.\r\n"

What is the problem and how I can fix it?

What is the meaning of the above error code?

Edit 1

This is the code with section that turn on/off the LED. Please note that turning on/off LED is only for testing, but the aim is to have access to the device (via file handle) like a memory so I can work with different part of the device:



int main() {
    DWORD LED_Addr = 0x40000;
    DWORD LED_ON = 0x3;
    DWORD LED_Off = 0x0;
    DWORD bytesWritten;
    auto devicePaths = XdmaDevice::GetDevicePaths();
    if (devicePaths.size() == 0)
    {
        return false;
    }
    string channelPath = devicePaths[0] + "\\user";

    HANDLE file_handle = CreateFile(channelPath.c_str(),
        GENERIC_READ | GENERIC_WRITE, // Access rights
        FILE_SHARE_READ | FILE_SHARE_WRITE, // Share modes
        NULL, // Security attributes
        OPEN_EXISTING, // Creation disposition
        FILE_ATTRIBUTE_NORMAL, // Flags and attributes
        NULL); // Template file
    if (file_handle == INVALID_HANDLE_VALUE)
    {
        printf("Error");
        return -1;
    }

    // direct file access
    DWORD dwPos = SetFilePointer(file_handle, LED_Addr, NULL, FILE_BEGIN);
    if (dwPos == INVALID_SET_FILE_POINTER) {
        printf("Error");
        return -1;
    }
    
    // turn on LED
    if (!WriteFile(file_handle, &LED_ON, sizeof(DWORD), &bytesWritten, NULL))
    {
        DWORD error = GetLastError();
        // Get the error message
        std::string message = error_message(error);
        // Print the error code and message
        std::cout << "Error code: " << error << std::endl;
        std::cout << "Error message: " << message << std::endl;
        return -1;

    }
    // Turn off LED
    if (!WriteFile(file_handle, &LED_Off, sizeof(DWORD), &bytesWritten, NULL))
    {
        DWORD error = GetLastError();
        // Get the error message
        std::string message = error_message(error);
        // Print the error code and message
        std::cout << "Error code: " << error << std::endl;
        std::cout << "Error message: " << message << std::endl;
        return -1;

    }

    // trying to use memory mapping of device file
    size_t mapping_size = sizeof(DWORD);
    DWORD  dwMaximumSizeHigh = (LED_Addr + mapping_size) >> 32;
    DWORD  dwMaximumSizeLow = (LED_Addr + mapping_size) & 0xFFFFFFFF;
    HANDLE  file_mapping_handle_ = ::CreateFileMapping( file_handle, 0, PAGE_READWRITE, dwMaximumSizeHigh, dwMaximumSizeLow, 0);
    if (file_mapping_handle_ == NULL)
    {
        DWORD error = GetLastError();
        auto message = error_message(error);
        return -1;
    }
    

    DWORD  dwFileOffsetHigh = LED_Addr >> 32;
    DWORD  dwFileOffsetLow = LED_Addr & 0xFFFFFFFF;
    DWORD  dwNumberOfBytesToMap = mapping_size;
    auto real_data = (MapViewOfFile(file_mapping_handle_, FILE_MAP_WRITE, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap));
    if (!real_data)
    {
        DWORD error = GetLastError();
        auto message = error_message(error);

        return -1;
    }
    return 0;
}

Upvotes: 0

Views: 94

Answers (0)

Related Questions