FT232 使用官方驱动控制GPIO很简单
- 打开 FT_OpenEx, FT_SetBitMode(FT_BITMODE_ASYNC_BITBANG)
- 写 FT_Write
- 读 FT_Read
使用libusb方式
- 打开
// libusb-win32 (v1.2.6.0)
BOOL CHubGpio::InitLibusb()
{
struct usb_interface_descriptor *setting;
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *hPortUsb;
BOOL bRetVal;
int i, nRetVal;
CHAR szPdt[256], szMft[256], szSN[256];
int nValue, nMask, nMode;
if(m_hPortUsb)
return TRUE;
bRetVal = m_libusb.Init();
if(bRetVal == FALSE)
return FALSE;
m_nIntfId = 0;
m_nEndPointOut = 0;
m_nEndPointIn = 0;
m_libusb.find_busses();
m_libusb.find_devices();
for(bus = m_libusb.get_busses();
bus;
bus = bus->next
)
{
for(dev = bus->devices;
dev;
dev = dev->next)
{
hPortUsb = m_libusb.open(dev);
if(hPortUsb == NULL)
continue;
m_libusb.get_string_simple(hPortUsb, dev->descriptor.iManufacturer, szMft, sizeof(szMft));
m_libusb.get_string_simple(hPortUsb, dev->descriptor.iProduct, szPdt, sizeof(szPdt));
m_libusb.get_string_simple(hPortUsb, dev->descriptor.iSerialNumber, szSN, sizeof(szSN));
ATLTRACE("usb_find_devices: %04X-%04X %s %s %s\r\n", dev->descriptor.idVendor, dev->descriptor.idProduct, szMft, szPdt, szSN);
if(strcmp(szMft, "PTI") != 0)
{
m_libusb.close(hPortUsb);
continue;
}
m_hPortUsb = hPortUsb;
break;
}
if(m_hPortUsb)
break;
}
if(m_hPortUsb == NULL || dev == NULL)
return FALSE;
if(dev->config == NULL || dev->config->_interface == NULL || dev->config->_interface->altsetting == NULL)
return FALSE;
setting = dev->config->_interface->altsetting;
m_nIntfId = setting->bInterfaceNumber + 1; // self._index = self._interface.bInterfaceNumber+1
m_nEndPointIn = 0;
m_nEndPointOut = 0;
for(i=0; i<setting->bNumEndpoints; i++)
{
nValue = setting->endpoint[i].bmAttributes;
nValue = nValue & ENDPOINT_TRANSFER_TYPE_MASK; // ENDPOINT_TYPE_BULK
nValue = setting->endpoint[i].bEndpointAddress;
if(nValue & ENDPOINT_DIR_MASK)
{
m_nEndPointIn = nValue & ENDPOINT_ADDR_MASK;
m_nEndPointIn = nValue;
}
else
{
m_nEndPointOut = nValue & ENDPOINT_ADDR_MASK;
m_nEndPointOut = nValue;
}
}
nMask = 0xff;
nMode = FT_BITMODE_ASYNC_BITBANG; // FT_BITMODE_SYNC_BITBANG
// nMode = FT_BITMODE_SYNC_BITBANG; // FT_BITMODE_SYNC_BITBANG
nValue = (nMode << 8) | nMask;
nRetVal = m_libusb.control_msg(m_hPortUsb, USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT, SIO_REQ_SET_BITMODE, nValue, m_nIntfId, NULL, 0, 500);
nRetVal = m_libusb.claim_interface(m_hPortUsb, 0);
m_bFind = TRUE;
m_bTypeFtdi = FALSE;
return TRUE;
}
- 写入 interrupt_write
- 读取, 读取实际不是使用read, 而是control_msg
BOOL CHubGpio::GetIo(BYTE &bValue, BOOL *pTypeFtdi)
{
FT_STATUS status;
DWORD dwCount;
int nRetVal;
int nMask, nMode, nValue;
bValue = 0;
if(pTypeFtdi)
*pTypeFtdi = FALSE;
if(m_hPort232)
{
if(pTypeFtdi)
*pTypeFtdi = TRUE;
status = m_Lib232.FT_Read(m_hPort232, &bValue, 1, &dwCount);
if(status == FT_OK)
return TRUE;
return FALSE;
}
if(m_hPortUsb)
{
if(pTypeFtdi)
*pTypeFtdi = FALSE;
nMask = 0x00;
nMode = FT_BITMODE_ASYNC_BITBANG; // FT_BITMODE_SYNC_BITBANG
nValue = (nMode << 8) | nMask;
nRetVal = m_libusb.control_msg(m_hPortUsb, USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT, SIO_REQ_SET_BITMODE, nValue, m_nIntfId, NULL, 0, 500);
nRetVal = m_libusb.control_msg(m_hPortUsb, USB_TYPE_VENDOR|USB_ENDPOINT_IN, SIO_REQ_READ_PINS, 0, m_nIntfId, (char *)&bValue, 1, 500);
if(nRetVal == 1)
return TRUE;
return FALSE;
}
return FALSE;
}
标签:FT,nValue,hPortUsb,dev,libusb,FT232,GPIO,BITMODE
From: https://www.cnblogs.com/zzz3265/p/18400004