mirror of
https://github.com/mariusgreuel/avrdude.git
synced 2025-09-27 22:45:27 +00:00
Add support for FTDI devices via D2XX API
This commit is contained in:
68
msvc/IntegerHandleMap.h
Normal file
68
msvc/IntegerHandleMap.h
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// HandleMap.h
|
||||
// Copyright (C) 2019 Marius Greuel. All rights reserved.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <limits>
|
||||
#include <unordered_map>
|
||||
#include "ReaderWriterLock.h"
|
||||
|
||||
#pragma push_macro("max")
|
||||
#undef max
|
||||
|
||||
template<typename TKey, typename TValue>
|
||||
class IntegerHandleMap
|
||||
{
|
||||
public:
|
||||
TKey Add(TValue value)
|
||||
{
|
||||
TKey key = GetNextId();
|
||||
|
||||
ReaderWriterLock::WriterLock lock(m_lock);
|
||||
m_map.emplace(key, value);
|
||||
return key;
|
||||
}
|
||||
|
||||
void Remove(TKey key) noexcept
|
||||
{
|
||||
ReaderWriterLock::WriterLock lock(m_lock);
|
||||
m_map.erase(key);
|
||||
}
|
||||
|
||||
TValue Lookup(TKey key) noexcept
|
||||
{
|
||||
ReaderWriterLock::ReaderLock lock(m_lock);
|
||||
|
||||
TValue value;
|
||||
auto result = m_map.find(key);
|
||||
return result != m_map.end() ? result->second : nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
TKey GetNextId() noexcept
|
||||
{
|
||||
ReaderWriterLock::ReaderLock lock(m_lock);
|
||||
|
||||
TKey key = m_next;
|
||||
while (m_map.find(key) != m_map.end())
|
||||
{
|
||||
key = GetNextId(key);
|
||||
}
|
||||
|
||||
m_next = GetNextId(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
static TKey GetNextId(TKey key) noexcept
|
||||
{
|
||||
return key < std::numeric_limits<TKey>::max() ? key + 1 : 1;
|
||||
}
|
||||
|
||||
private:
|
||||
ReaderWriterLock m_lock;
|
||||
std::unordered_map<TKey, TValue> m_map;
|
||||
TKey m_next = 1;
|
||||
};
|
||||
|
||||
#pragma pop_macro("max")
|
80
msvc/ReaderWriterLock.h
Normal file
80
msvc/ReaderWriterLock.h
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// ReaderWriterLock.h
|
||||
// Copyright (C) 2019 Marius Greuel. All rights reserved.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
class ReaderWriterLock
|
||||
{
|
||||
public:
|
||||
class ReaderLock
|
||||
{
|
||||
public:
|
||||
ReaderLock(ReaderWriterLock& lock) noexcept : m_lock(lock)
|
||||
{
|
||||
Acquire();
|
||||
}
|
||||
|
||||
~ReaderLock()
|
||||
{
|
||||
Release();
|
||||
}
|
||||
|
||||
private:
|
||||
void Acquire() noexcept
|
||||
{
|
||||
AcquireSRWLockShared(&m_lock.m_srwlock);
|
||||
}
|
||||
|
||||
void Release() noexcept
|
||||
{
|
||||
ReleaseSRWLockShared(&m_lock.m_srwlock);
|
||||
}
|
||||
|
||||
private:
|
||||
ReaderWriterLock& m_lock;
|
||||
};
|
||||
|
||||
class WriterLock
|
||||
{
|
||||
public:
|
||||
WriterLock(ReaderWriterLock& lock) noexcept : m_lock(lock)
|
||||
{
|
||||
Acquire();
|
||||
}
|
||||
|
||||
~WriterLock()
|
||||
{
|
||||
Release();
|
||||
}
|
||||
|
||||
private:
|
||||
void Acquire() noexcept
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_lock.m_srwlock);
|
||||
}
|
||||
|
||||
void Release() noexcept
|
||||
{
|
||||
ReleaseSRWLockExclusive(&m_lock.m_srwlock);
|
||||
}
|
||||
|
||||
private:
|
||||
ReaderWriterLock& m_lock;
|
||||
};
|
||||
|
||||
public:
|
||||
ReaderWriterLock() noexcept
|
||||
{
|
||||
InitializeSRWLock(&m_srwlock);
|
||||
}
|
||||
|
||||
ReaderWriterLock(const ReaderWriterLock&) = delete;
|
||||
ReaderWriterLock& operator=(const ReaderWriterLock&) = delete;
|
||||
ReaderWriterLock(ReaderWriterLock&&) = delete;
|
||||
ReaderWriterLock& operator=(ReaderWriterLock&&) = delete;
|
||||
|
||||
private:
|
||||
SRWLOCK m_srwlock{};
|
||||
};
|
@@ -32,13 +32,13 @@
|
||||
/* #undef HAVE_LIBELF_LIBELF_H */
|
||||
|
||||
/* Define if FTDI support is enabled via libftdi */
|
||||
/* #undef HAVE_LIBFTDI */
|
||||
#define HAVE_LIBFTDI 1
|
||||
|
||||
/* Define if FTDI support is enabled via libftdi1 */
|
||||
/* #undef HAVE_LIBFTDI1 */
|
||||
|
||||
/* Define if libftdi supports FT232H, libftdi version >= 0.20 */
|
||||
/* #undef HAVE_LIBFTDI_TYPE_232H */
|
||||
#define HAVE_LIBFTDI_TYPE_232H 1
|
||||
|
||||
/* Define if HID support is enabled via the Win32 DDK */
|
||||
#define HAVE_LIBHID 1
|
||||
@@ -92,7 +92,7 @@
|
||||
/* #undef HAVE_PARPORT */
|
||||
|
||||
/* Define to 1 if you have the <pthread.h> header file. */
|
||||
/* #undef HAVE_PTHREAD_H */
|
||||
#define HAVE_PTHREAD_H 1
|
||||
|
||||
/* Define to 1 if you have the `select' function. */
|
||||
/* #undef HAVE_SELECT */
|
||||
|
184
msvc/pthread.cpp
Normal file
184
msvc/pthread.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* avrdude - A Downloader/Uploader for AVR device programmers
|
||||
* Copyright (C) 2018 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 "pthread.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
#include <cerrno>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include "IntegerHandleMap.h"
|
||||
|
||||
struct ThreadContext
|
||||
{
|
||||
~ThreadContext()
|
||||
{
|
||||
if (hCancel != nullptr)
|
||||
{
|
||||
CloseHandle(hCancel);
|
||||
hCancel = nullptr;
|
||||
}
|
||||
|
||||
if (hThread != nullptr)
|
||||
{
|
||||
CloseHandle(hThread);
|
||||
hThread = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
using start_routine_t = void* (*)(void*);
|
||||
HANDLE hThread = nullptr;
|
||||
HANDLE hCancel = nullptr;
|
||||
start_routine_t start_routine = nullptr;
|
||||
void* arguments = nullptr;
|
||||
int canceltype = 0;
|
||||
};
|
||||
|
||||
static IntegerHandleMap<pthread_t, std::shared_ptr<ThreadContext>> contextMap;
|
||||
static thread_local pthread_t currentThread;
|
||||
|
||||
static unsigned __stdcall pthread_start_routine_wrapper(void* arguments)
|
||||
{
|
||||
pthread_t id = reinterpret_cast<pthread_t>(arguments);
|
||||
auto context = contextMap.Lookup(id);
|
||||
if (context == nullptr)
|
||||
{
|
||||
return ESRCH;
|
||||
}
|
||||
|
||||
currentThread = id;
|
||||
context->start_routine(context->arguments);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine) (void*), void* arg)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto context = std::make_shared<ThreadContext>();
|
||||
context->start_routine = start_routine;
|
||||
context->arguments = arg;
|
||||
|
||||
auto id = contextMap.Add(context);
|
||||
context->hThread = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, pthread_start_routine_wrapper, reinterpret_cast<void*>(id), 0, nullptr));
|
||||
if (context->hThread == nullptr)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
*thread = id;
|
||||
return 0;
|
||||
}
|
||||
catch (std::exception&)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
void pthread_exit(void* retval)
|
||||
{
|
||||
_endthreadex(static_cast<unsigned int>(reinterpret_cast<uintptr_t>(retval)));
|
||||
}
|
||||
|
||||
int pthread_cancel(pthread_t thread)
|
||||
{
|
||||
auto context = contextMap.Lookup(thread);
|
||||
if (context == nullptr)
|
||||
{
|
||||
return ESRCH;
|
||||
}
|
||||
|
||||
if (context->hCancel == nullptr)
|
||||
{
|
||||
context->hCancel = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (context->hCancel == nullptr)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SetEvent(context->hCancel))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_join(pthread_t thread, void** retval)
|
||||
{
|
||||
auto context = contextMap.Lookup(thread);
|
||||
if (context == nullptr)
|
||||
return ESRCH;
|
||||
|
||||
DWORD dwStatus = WaitForSingleObject(context->hThread, INFINITE);
|
||||
if (dwStatus == WAIT_OBJECT_0)
|
||||
{
|
||||
if (retval != nullptr)
|
||||
{
|
||||
DWORD dwExitCode = 0;
|
||||
if (GetExitCodeThread(context->hThread, &dwExitCode))
|
||||
{
|
||||
*retval = reinterpret_cast<void*>(static_cast<uintptr_t>(dwExitCode));
|
||||
}
|
||||
else
|
||||
{
|
||||
*retval = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
contextMap.Remove(thread);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
int pthread_setcanceltype(int type, int* oldtype)
|
||||
{
|
||||
auto context = contextMap.Lookup(currentThread);
|
||||
if (context == nullptr)
|
||||
return ESRCH;
|
||||
|
||||
if (oldtype != nullptr)
|
||||
{
|
||||
*oldtype = context->canceltype;
|
||||
}
|
||||
|
||||
context->canceltype = type;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pthread_testcancel(void)
|
||||
{
|
||||
auto context = contextMap.Lookup(currentThread);
|
||||
if (context == nullptr)
|
||||
return;
|
||||
|
||||
if (context->hCancel != nullptr)
|
||||
{
|
||||
DWORD dwStatus = WaitForSingleObject(context->hCancel, 0);
|
||||
if (dwStatus == WAIT_OBJECT_0)
|
||||
{
|
||||
pthread_exit(nullptr);
|
||||
}
|
||||
}
|
||||
}
|
43
msvc/pthread.h
Normal file
43
msvc/pthread.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* avrdude - A Downloader/Uploader for AVR device programmers
|
||||
* Copyright (C) 2018 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _PTHREAD_H
|
||||
#define _PTHREAD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
|
||||
|
||||
typedef uintptr_t pthread_t;
|
||||
typedef int pthread_attr_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine) (void*), void* arg);
|
||||
int pthread_cancel(pthread_t thread);
|
||||
int pthread_join(pthread_t thread, void** retval);
|
||||
int pthread_setcanceltype(int type, int* oldtype);
|
||||
void pthread_testcancel(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
68
msvc/semaphore.cpp
Normal file
68
msvc/semaphore.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* avrdude - A Downloader/Uploader for AVR device programmers
|
||||
* Copyright (C) 2018 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 "semaphore.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
int sem_init(sem_t* sem, int pshared, unsigned int value)
|
||||
{
|
||||
if (sem == nullptr)
|
||||
return -1;
|
||||
|
||||
sem->handle = nullptr;
|
||||
|
||||
auto handle = CreateSemaphoreW(nullptr, value, MAXLONG32, nullptr);
|
||||
if (handle == nullptr)
|
||||
return -1;
|
||||
|
||||
sem->handle = handle;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_destroy(sem_t* sem)
|
||||
{
|
||||
if (sem == nullptr)
|
||||
return -1;
|
||||
|
||||
if (sem->handle != nullptr)
|
||||
{
|
||||
CloseHandle(sem->handle);
|
||||
sem->handle = nullptr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_post(sem_t* sem)
|
||||
{
|
||||
if (sem == nullptr)
|
||||
return -1;
|
||||
|
||||
BOOL bStatus = ReleaseSemaphore(sem->handle, 1, nullptr);
|
||||
return bStatus ? 0 : -1;
|
||||
}
|
||||
|
||||
int sem_wait(sem_t* sem)
|
||||
{
|
||||
if (sem == nullptr)
|
||||
return -1;
|
||||
|
||||
DWORD dwStatus = WaitForSingleObject(sem->handle, INFINITE);
|
||||
return dwStatus == WAIT_OBJECT_0 ? 0 : -1;
|
||||
}
|
40
msvc/semaphore.h
Normal file
40
msvc/semaphore.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* avrdude - A Downloader/Uploader for AVR device programmers
|
||||
* Copyright (C) 2018 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _SEMAPHORE_H
|
||||
#define _SEMAPHORE_H
|
||||
|
||||
typedef struct sem
|
||||
{
|
||||
void* handle;
|
||||
} sem_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int sem_init(sem_t* sem, int pshared, unsigned int value);
|
||||
int sem_destroy(sem_t* sem);
|
||||
int sem_post(sem_t* sem);
|
||||
int sem_wait(sem_t* sem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user