Last active
June 26, 2019 08:45
-
-
Save eduardoaugustojulio/7794565bd45cc76662465762252b1e7b to your computer and use it in GitHub Desktop.
C++ class to he FT4222H USB interface SPI communication protocol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <string> | |
#include <vector> | |
#include "ftd2xx.h" | |
#include "libft4222.h" | |
#include "ftd4222.h" | |
namespace usb_spi | |
{ | |
ftd4222::ftd4222() | |
{ | |
std::cout << "usb contructor called" << std::endl; | |
list_usb_devices(); | |
if(this->dev_list.empty()) { | |
std::cout << "No FT4222 device is found" << std::endl; | |
exit(-1); | |
}else { | |
std::cout << "FT4222 device is found" << std::endl; | |
} | |
if(FT_OK == (this->ftd_status = FT_OpenEx(reinterpret_cast<PVOID>(this->dev_list.at(0).LocId), FT_OPEN_BY_LOCATION, &ftd_handle))) | |
std::cout << "Open a FT4222 device with success!" << std::endl; | |
if(FT_OK == (this->ftd_status = FT4222_SetClock(this->ftd_handle, SYS_CLK_80))) | |
std::cout << "Set clock of FT4222 to 80Mhz" << std::endl; | |
const unsigned char SPI_CS0 = 0x01; | |
if(FT_OK == (this->ftd_status = FT4222_SPIMaster_Init(ftd_handle, SPI_IO_SINGLE,CLK_DIV_8, CLK_IDLE_LOW,CLK_LEADING, SPI_CS0))) | |
std::cout << "Init FT4222 as SPI master device with success" << std::endl; | |
if(FT_OK == (this->ftd_status = FT4222_SPI_SetDrivingStrength(ftd_handle,DS_8MA, DS_8MA, DS_8MA))) | |
std::cout << "Set Power Strength as SPI master with success" << std::endl; | |
} | |
ftd4222::~ftd4222() | |
{ | |
std::cout << "usb descontructor called" << std::endl; | |
FT4222_UnInitialize(this->ftd_handle); | |
FT_Close(this->ftd_handle); | |
} | |
void ftd4222::exchange(std::vector<char> & w, std::vector<char> & r) | |
{ | |
uint16 read_size; | |
if(FT_OK != (this->ftd_status = FT4222_SPIMaster_SingleReadWrite(this->ftd_handle, reinterpret_cast<uint8*>(r.data()), | |
reinterpret_cast<uint8*>(w.data()), w.size(), &read_size,true))) | |
std::cout << "Failed to single write/read error " << this->ftd_status << std::endl; | |
if(read_size != r.size()) | |
std::cout << "Failed to single write/read! Bytes to read"<< w.size() << " bytes readed "<< read_size << std::endl; | |
} | |
void ftd4222::read(std::vector<char> &v) | |
{ | |
uint16 read_size; | |
if(FT_OK != (this->ftd_status = FT4222_SPIMaster_SingleRead(this->ftd_handle, reinterpret_cast<uint8*>(v.data()), v.size(), &read_size, true))) | |
std::cout << "Failed to single read error " << this->ftd_status << std::endl; | |
if(read_size != v.size()) | |
std::cout << "Failed to single read! Bytes to read"<< v.size() << " bytes readed "<< read_size << std::endl; | |
} | |
void ftd4222::write(std::vector<char> &v) | |
{ | |
uint16 write_size; | |
if( FT_OK != (this->ftd_status = FT4222_SPIMaster_SingleWrite(this->ftd_handle, reinterpret_cast<uint8*>(v.data()), v.size(), &write_size, true))) | |
std::cout << "Failed to single write error " << this->ftd_status << std::endl; | |
if(write_size != v.size()) | |
std::cout << "Failed to single write! Bytes to send"<< v.size() << " bytes sended "<< write_size << std::endl; | |
} | |
void ftd4222::list_usb_devices() | |
{ | |
DWORD num_of_devices = 0; | |
FT_STATUS status = FT_CreateDeviceInfoList(&num_of_devices); | |
for(DWORD dev=0; dev < num_of_devices; ++dev){ | |
FT_DEVICE_LIST_INFO_NODE dev_info; | |
memset(&dev_info, 0, sizeof(dev_info)); | |
status = FT_GetDeviceInfoDetail(dev,&dev_info.Flags, &dev_info.Type, &dev_info.ID, | |
&dev_info.LocId, &dev_info.SerialNumber, &dev_info.Description, &dev_info.ftHandle); | |
if (FT_OK == status){ | |
std::cout << "\t TYPE: " << dev_info.Type << std::endl; | |
std::cout << "\t ID: " << dev_info.ID << std::endl; | |
std::cout << "\t Local ID: " << dev_info.LocId << std::endl; | |
std::cout << "\t Serial number: " << (std::strlen(dev_info.SerialNumber) ? dev_info.SerialNumber : "---------") << std::endl; | |
std::cout << "\t Description: " << dev_info.Description << std::endl; | |
std::cout << "\t Device state: " << ((dev_info.Flags & 0x01) ? "DEVICE_OPEN" : "DEVICE_CLOSED") << std::endl; | |
std::cout << "\t Device speed: " << ((dev_info.Flags & 0x02) ? "High-speed USB" : "Full-speed USB") << std::endl; | |
this->dev_list.push_back(dev_info); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FTD4222_H | |
#define FTD4222_H | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include "ftd2xx.h" | |
#include "libft4222.h" | |
namespace usb_spi | |
{ | |
class ftd4222 | |
{ | |
ftd4222(); | |
~ftd4222(); | |
void exchange(std::vector<char> &write, std::vector<char> &read); | |
void read(std::vector<char> &value); | |
void write(std::vector<char> &value); | |
private: | |
void list_usb_devices(); | |
std::vector<FT_DEVICE_LIST_INFO_NODE> dev_list; | |
FT_HANDLE ftd_handle; | |
FT_STATUS ftd_status; | |
}; | |
} | |
#endif // FTD4222_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment