mirror of
https://github.com/mariusgreuel/avrdude.git
synced 2025-09-28 06:55:27 +00:00
Add support for FTDI devices via D2XX API
This commit is contained in:
320
external/libftdi1/src/ftdi.cpp
vendored
Normal file
320
external/libftdi1/src/ftdi.cpp
vendored
Normal file
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* avrdude - A Downloader/Uploader for AVR device programmers
|
||||
* Copyright (C) 2019 Marius Greuel
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "libwinftdi.h"
|
||||
#include <winsock2.h>
|
||||
#include <ftdi.h>
|
||||
|
||||
using namespace LibWinFtdi;
|
||||
|
||||
static ftdi_chip_type MapChipType(ULONG type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FT_DEVICE_BM:
|
||||
return TYPE_BM;
|
||||
case FT_DEVICE_AM:
|
||||
return TYPE_AM;
|
||||
case FT_DEVICE_100AX:
|
||||
return TYPE_AM;
|
||||
case FT_DEVICE_2232C:
|
||||
return TYPE_2232C;
|
||||
case FT_DEVICE_232R:
|
||||
return TYPE_R;
|
||||
case FT_DEVICE_2232H:
|
||||
return TYPE_2232H;
|
||||
case FT_DEVICE_4232H:
|
||||
return TYPE_4232H;
|
||||
case FT_DEVICE_232H:
|
||||
return TYPE_232H;
|
||||
default:
|
||||
return TYPE_AM;
|
||||
}
|
||||
}
|
||||
static int SetError(struct ftdi_context* ftdi, int result, const char* error_str)
|
||||
{
|
||||
if (ftdi != nullptr)
|
||||
{
|
||||
ftdi->error_str = error_str;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const char* ftdi_get_error_string(struct ftdi_context* ftdi)
|
||||
{
|
||||
return ftdi != nullptr && ftdi->error_str != nullptr ? ftdi->error_str : "unknown error";
|
||||
}
|
||||
|
||||
struct ftdi_context* ftdi_new(void)
|
||||
{
|
||||
struct ftdi_context* ftdi = new struct ftdi_context();
|
||||
|
||||
if (ftdi_init(ftdi) != 0)
|
||||
{
|
||||
delete ftdi;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ftdi;
|
||||
}
|
||||
|
||||
void ftdi_free(struct ftdi_context* ftdi)
|
||||
{
|
||||
ftdi_deinit(ftdi);
|
||||
delete ftdi;
|
||||
}
|
||||
|
||||
int ftdi_set_interface(struct ftdi_context* ftdi, enum ftdi_interface interface)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_init(struct ftdi_context* ftdi)
|
||||
{
|
||||
std::memset(ftdi, 0, sizeof(struct ftdi_context));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ftdi_deinit(struct ftdi_context* ftdi)
|
||||
{
|
||||
if (ftdi->usb_dev != nullptr)
|
||||
{
|
||||
std::unique_ptr<FtdiDevice> device(reinterpret_cast<FtdiDevice*>(ftdi->usb_dev));
|
||||
ftdi->usb_dev = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int ftdi_usb_open_desc_index(struct ftdi_context* ftdi, int vendor, int product,
|
||||
const char* description, const char* serial, unsigned int index)
|
||||
{
|
||||
if (ftdi == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -3, "invalid ftdi context");
|
||||
}
|
||||
|
||||
FtdiEnumerator enumerator;
|
||||
auto status = enumerator.EnumerateDevices();
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -3, "failed to enumerate devices");
|
||||
}
|
||||
|
||||
for (auto const& info : enumerator.GetDevices())
|
||||
{
|
||||
if ((info.Flags & FT_FLAGS_OPENED) != 0)
|
||||
continue;
|
||||
|
||||
if (vendor != static_cast<uint16_t>(info.ID >> 16))
|
||||
continue;
|
||||
|
||||
if (product != static_cast<uint16_t>(info.ID >> 0))
|
||||
continue;
|
||||
|
||||
if (description != nullptr && strcmp(description, info.Description) != 0)
|
||||
continue;
|
||||
|
||||
if (serial != nullptr && strcmp(serial, info.SerialNumber) != 0)
|
||||
continue;
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
index--;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto device = std::make_unique<FtdiDevice>();
|
||||
auto status = device->OpenBySerialNumber(info.SerialNumber);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -3, "failed to open device");
|
||||
}
|
||||
|
||||
device->SetEventNotification(FT_EVENT_RXCHAR);
|
||||
|
||||
ftdi->type = MapChipType(info.Type);
|
||||
ftdi->usb_dev = reinterpret_cast<struct libusb_device_handle*>(device.release());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SetError(ftdi, -3, "device not found");
|
||||
}
|
||||
|
||||
int ftdi_usb_close(struct ftdi_context* ftdi)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -3, "invalid device");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
auto status = device->Close();
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to close device");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_usb_purge_buffers(struct ftdi_context* ftdi)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -3, "invalid device");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
auto status = device->Purge();
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to purge buffers");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_set_baudrate(struct ftdi_context* ftdi, int baudrate)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -3, "invalid device");
|
||||
}
|
||||
|
||||
if (ftdi->bitbang_enabled)
|
||||
{
|
||||
baudrate /= 16;
|
||||
}
|
||||
|
||||
if (baudrate <= 0)
|
||||
{
|
||||
return SetError(ftdi, -1, "invalid baudrate");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
auto status = device->SetBaudRate(baudrate);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -2, "Failed to set baudrate");
|
||||
}
|
||||
|
||||
ftdi->baudrate = baudrate;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_set_bitmode(struct ftdi_context* ftdi, unsigned char bitmask, unsigned char mode)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -2, "invalid device");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
auto status = device->SetBitMode(bitmask, mode);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to set bitmode");
|
||||
}
|
||||
|
||||
ftdi->bitbang_mode = mode;
|
||||
ftdi->bitbang_enabled = mode == BITMODE_RESET ? 0 : 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_set_latency_timer(struct ftdi_context* ftdi, unsigned char latency)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -3, "invalid device");
|
||||
}
|
||||
|
||||
if (latency < 1)
|
||||
{
|
||||
return SetError(ftdi, -1, "invalid latency");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
auto status = device->SetLatencyTimer(latency);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to set latency timer");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ftdi_read_data(struct ftdi_context* ftdi, unsigned char* buf, int size)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -666, "invalid device");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
|
||||
auto handle = device->GetNotificationEvent();
|
||||
if (handle != nullptr)
|
||||
{
|
||||
DWORD dwStatus = WaitForSingleObject(handle, 1000);
|
||||
if (dwStatus != WAIT_OBJECT_0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD bytesInQueue = 0;
|
||||
auto status = device->GetQueueStatus(&bytesInQueue);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to get queue status");
|
||||
}
|
||||
|
||||
DWORD bytesRead = 0;
|
||||
if (bytesInQueue > 0)
|
||||
{
|
||||
if (size > static_cast<int>(bytesInQueue))
|
||||
size = static_cast<int>(bytesInQueue);
|
||||
|
||||
status = device->Read(buf, size, &bytesRead);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to read data");
|
||||
}
|
||||
}
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
int ftdi_write_data(struct ftdi_context* ftdi, const unsigned char* buf, int size)
|
||||
{
|
||||
if (ftdi == nullptr || ftdi->usb_dev == nullptr)
|
||||
{
|
||||
return SetError(ftdi, -666, "invalid device");
|
||||
}
|
||||
|
||||
auto device = reinterpret_cast<FtdiDevice*>(ftdi->usb_dev);
|
||||
|
||||
DWORD bytesWritten = 0;
|
||||
auto status = device->Write(const_cast<unsigned char*>(buf), size, &bytesWritten);
|
||||
if (status != FT_OK)
|
||||
{
|
||||
return SetError(ftdi, -1, "Failed to write data");
|
||||
}
|
||||
|
||||
return bytesWritten;
|
||||
}
|
380
external/libftdi1/src/libwinftdi.h
vendored
Normal file
380
external/libftdi1/src/libwinftdi.h
vendored
Normal file
@@ -0,0 +1,380 @@
|
||||
//
|
||||
// libwinftdi.h
|
||||
// Copyright (C) 2019 Marius Greuel. All rights reserved.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <windows.h>
|
||||
|
||||
namespace LibWinFtdi
|
||||
{
|
||||
enum
|
||||
{
|
||||
FT_OK,
|
||||
FT_INVALID_HANDLE,
|
||||
FT_DEVICE_NOT_FOUND,
|
||||
FT_DEVICE_NOT_OPENED,
|
||||
FT_IO_ERROR,
|
||||
FT_INSUFFICIENT_RESOURCES,
|
||||
FT_INVALID_PARAMETER,
|
||||
FT_INVALID_BAUD_RATE,
|
||||
FT_DEVICE_NOT_OPENED_FOR_ERASE,
|
||||
FT_DEVICE_NOT_OPENED_FOR_WRITE,
|
||||
FT_FAILED_TO_WRITE_DEVICE,
|
||||
FT_EEPROM_READ_FAILED,
|
||||
FT_EEPROM_WRITE_FAILED,
|
||||
FT_EEPROM_ERASE_FAILED,
|
||||
FT_EEPROM_NOT_PRESENT,
|
||||
FT_EEPROM_NOT_PROGRAMMED,
|
||||
FT_INVALID_ARGS,
|
||||
FT_NOT_SUPPORTED,
|
||||
FT_OTHER_ERROR,
|
||||
FT_DEVICE_LIST_NOT_READY,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FT_DEVICE_BM,
|
||||
FT_DEVICE_AM,
|
||||
FT_DEVICE_100AX,
|
||||
FT_DEVICE_UNKNOWN,
|
||||
FT_DEVICE_2232C,
|
||||
FT_DEVICE_232R,
|
||||
FT_DEVICE_2232H,
|
||||
FT_DEVICE_4232H,
|
||||
FT_DEVICE_232H,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FT_FLAGS_OPENED = 1,
|
||||
FT_FLAGS_HISPEED = 2,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FT_OPEN_BY_SERIAL_NUMBER = 1,
|
||||
FT_OPEN_BY_DESCRIPTION = 2,
|
||||
FT_OPEN_BY_LOCATION = 4,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FT_PURGE_RX = 1,
|
||||
FT_PURGE_TX = 2,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FT_EVENT_RXCHAR = 1,
|
||||
FT_EVENT_MODEM_STATUS = 2,
|
||||
FT_EVENT_LINE_STATUS = 4,
|
||||
};
|
||||
|
||||
typedef PVOID FT_HANDLE;
|
||||
typedef ULONG FT_STATUS;
|
||||
|
||||
struct DeviceInfo
|
||||
{
|
||||
ULONG Flags;
|
||||
ULONG Type;
|
||||
ULONG ID;
|
||||
DWORD LocId;
|
||||
char SerialNumber[16];
|
||||
char Description[64];
|
||||
FT_HANDLE ftHandle;
|
||||
};
|
||||
|
||||
class FtdiApi
|
||||
{
|
||||
public:
|
||||
FtdiApi() = default;
|
||||
FtdiApi(const FtdiApi&) = delete;
|
||||
FtdiApi& operator=(const FtdiApi&) = delete;
|
||||
FtdiApi(FtdiApi&&) = delete;
|
||||
FtdiApi& operator=(FtdiApi&&) = delete;
|
||||
|
||||
~FtdiApi()
|
||||
{
|
||||
Unload();
|
||||
}
|
||||
|
||||
HRESULT Load()
|
||||
{
|
||||
if (m_module != nullptr)
|
||||
return S_FALSE;
|
||||
|
||||
m_module = LoadLibraryExW(L"ftd2xx.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
||||
if (m_module == nullptr)
|
||||
{
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
|
||||
HRESULT hr = S_OK;
|
||||
if (FAILED(hr = LoadImport("FT_CreateDeviceInfoList", &FT_CreateDeviceInfoList)) ||
|
||||
FAILED(hr = LoadImport("FT_GetDeviceInfoList", &FT_GetDeviceInfoList)) ||
|
||||
FAILED(hr = LoadImport("FT_OpenEx", &FT_OpenEx)) ||
|
||||
FAILED(hr = LoadImport("FT_Close", &FT_Close)) ||
|
||||
FAILED(hr = LoadImport("FT_Purge", &FT_Purge)) ||
|
||||
FAILED(hr = LoadImport("FT_SetTimeouts", &FT_SetTimeouts)) ||
|
||||
FAILED(hr = LoadImport("FT_SetBaudRate", &FT_SetBaudRate)) ||
|
||||
FAILED(hr = LoadImport("FT_SetBitMode", &FT_SetBitMode)) ||
|
||||
FAILED(hr = LoadImport("FT_SetLatencyTimer", &FT_SetLatencyTimer)) ||
|
||||
FAILED(hr = LoadImport("FT_GetQueueStatus", &FT_GetQueueStatus)) ||
|
||||
FAILED(hr = LoadImport("FT_SetEventNotification", &FT_SetEventNotification)) ||
|
||||
FAILED(hr = LoadImport("FT_Read", &FT_Read)) ||
|
||||
FAILED(hr = LoadImport("FT_Write", &FT_Write)))
|
||||
{
|
||||
return hr;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT Unload()
|
||||
{
|
||||
if (m_module == nullptr)
|
||||
return S_FALSE;
|
||||
|
||||
FreeLibrary(m_module);
|
||||
m_module = nullptr;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
HRESULT LoadImport(LPCSTR lpProcName, T** ptr)
|
||||
{
|
||||
auto proc = GetProcAddress(m_module, lpProcName);
|
||||
if (proc == nullptr)
|
||||
{
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
|
||||
*ptr = reinterpret_cast<T*>(proc);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
HMODULE m_module = nullptr;
|
||||
|
||||
public:
|
||||
FT_STATUS(WINAPI* FT_CreateDeviceInfoList)(LPDWORD lpdwNumDevs) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_GetDeviceInfoList)(DeviceInfo* pDest, LPDWORD lpdwNumDevs) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_OpenEx)(PVOID pArg1, DWORD Flags, FT_HANDLE* pHandle) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_Close)(FT_HANDLE ftHandle) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_Purge)(FT_HANDLE ftHandle, ULONG Mask) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_SetTimeouts)(FT_HANDLE ftHandle, ULONG ReadTimeout, ULONG WriteTimeout) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_SetBaudRate)(FT_HANDLE ftHandle, ULONG BaudRate) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_SetBitMode)(FT_HANDLE ftHandle, UCHAR ucMask, UCHAR ucEnable) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_SetLatencyTimer)(FT_HANDLE ftHandle, UCHAR ucLatency) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_GetQueueStatus)(FT_HANDLE ftHandle, DWORD* dwRxBytes) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_SetEventNotification)(FT_HANDLE ftHandle, DWORD Mask, PVOID Param) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_Read)(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesReturned) = nullptr;
|
||||
FT_STATUS(WINAPI* FT_Write)(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpBytesWritten) = nullptr;
|
||||
};
|
||||
|
||||
class FtdiEnumerator : public FtdiApi
|
||||
{
|
||||
public:
|
||||
FtdiEnumerator() = default;
|
||||
FtdiEnumerator(const FtdiEnumerator&) = delete;
|
||||
FtdiEnumerator& operator=(const FtdiEnumerator&) = delete;
|
||||
FtdiEnumerator(FtdiEnumerator&&) = delete;
|
||||
FtdiEnumerator& operator=(FtdiEnumerator&&) = delete;
|
||||
|
||||
public:
|
||||
FT_STATUS EnumerateDevices()
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
DWORD lpdwNumDevs = 0;
|
||||
FT_STATUS status = FT_CreateDeviceInfoList(&lpdwNumDevs);
|
||||
if (status != FT_OK)
|
||||
return status;
|
||||
|
||||
m_devices.resize(lpdwNumDevs);
|
||||
|
||||
status = FT_GetDeviceInfoList(m_devices.data(), &lpdwNumDevs);
|
||||
if (status != FT_OK)
|
||||
return status;
|
||||
|
||||
return FT_OK;
|
||||
}
|
||||
|
||||
const std::vector<DeviceInfo>& GetDevices() const
|
||||
{
|
||||
return m_devices;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<DeviceInfo> m_devices;
|
||||
};
|
||||
|
||||
class FtdiDevice : public FtdiApi
|
||||
{
|
||||
public:
|
||||
FtdiDevice() = default;
|
||||
FtdiDevice(const FtdiDevice&) = delete;
|
||||
FtdiDevice& operator=(const FtdiDevice&) = delete;
|
||||
FtdiDevice(FtdiDevice&&) = delete;
|
||||
FtdiDevice& operator=(FtdiDevice&&) = delete;
|
||||
|
||||
~FtdiDevice()
|
||||
{
|
||||
if (m_event != nullptr)
|
||||
{
|
||||
CloseHandle(m_event);
|
||||
}
|
||||
}
|
||||
|
||||
HANDLE GetNotificationEvent() const
|
||||
{
|
||||
return m_event;
|
||||
}
|
||||
|
||||
public:
|
||||
FT_STATUS OpenBySerialNumber(const char* serialNumber)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_OpenEx(const_cast<char*>(serialNumber), FT_OPEN_BY_SERIAL_NUMBER, &m_handle);
|
||||
}
|
||||
|
||||
FT_STATUS OpenByLocation(uint32_t location)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_OpenEx(&location, FT_OPEN_BY_LOCATION, &m_handle);
|
||||
}
|
||||
|
||||
FT_STATUS Close()
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_Close(m_handle);
|
||||
}
|
||||
|
||||
FT_STATUS Purge(ULONG Mask = FT_PURGE_RX | FT_PURGE_TX)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_Purge(m_handle, Mask);
|
||||
}
|
||||
|
||||
FT_STATUS SetTimeouts(ULONG ReadTimeout, ULONG WriteTimeout)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_SetTimeouts(m_handle, ReadTimeout, WriteTimeout);
|
||||
}
|
||||
|
||||
FT_STATUS SetBaudRate(ULONG BaudRate)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_SetBaudRate(m_handle, BaudRate);
|
||||
}
|
||||
|
||||
FT_STATUS SetBitMode(UCHAR ucMask, UCHAR ucEnable)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_SetBitMode(m_handle, ucMask, ucEnable);
|
||||
}
|
||||
|
||||
FT_STATUS SetLatencyTimer(UCHAR ucLatency)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_SetLatencyTimer(m_handle, ucLatency);
|
||||
}
|
||||
|
||||
FT_STATUS GetQueueStatus(DWORD* dwRxBytes)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_GetQueueStatus(m_handle, dwRxBytes);
|
||||
}
|
||||
|
||||
FT_STATUS SetEventNotification(DWORD Mask)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
if (m_event == nullptr)
|
||||
{
|
||||
m_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
}
|
||||
|
||||
return FT_SetEventNotification(m_handle, Mask, m_event);
|
||||
}
|
||||
|
||||
FT_STATUS Read(LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesReturned)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_Read(m_handle, lpBuffer, dwBytesToRead, lpBytesReturned);
|
||||
}
|
||||
|
||||
FT_STATUS Write(LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpBytesWritten)
|
||||
{
|
||||
if (FAILED(Load()))
|
||||
{
|
||||
return FT_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return FT_Write(m_handle, lpBuffer, dwBytesToWrite, lpBytesWritten);
|
||||
}
|
||||
|
||||
private:
|
||||
FT_HANDLE m_handle = nullptr;
|
||||
HANDLE m_event = nullptr;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user