Skip to content

Instantly share code, notes, and snippets.

Last active August 4, 2022 17:33
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Stefan Agner
import os
import sys
import time
import usb
# FTDIs CBUS bitmode expect the following value:
# CBUS Bits
# 3210 3210
# |------ Output Control 0->LO, 1->HI
# |----------- Input/Output 0->Input, 1->Output
# PyUSB control endpoint communication, see also:
def ftdi_set_bitmode(dev, bitmask):
bmRequestType = usb.util.build_request_type(usb.util.CTRL_OUT,
wValue = bitmask | (BITMODE_CBUS << 8)
dev.ctrl_transfer(bmRequestType, SIO_SET_BITMODE_REQUEST, wValue)
def main():
"""Main program"""
dev = usb.core.find(custom_match = \
lambda d: \
d.idVendor==0x0403 and
d.idProduct==0x6001 and
# Set CBUS2/3 high...
ftdi_set_bitmode(dev, 0xCC)
# Set CBUS2/3 low...
ftdi_set_bitmode(dev, 0xC0)
# Set CBUS2/3 back to tristate
ftdi_set_bitmode(dev, 0x00)
Copy link

Nathan-L-Cook commented May 21, 2022

This was very helpful. Thank you.
On windows I needed to get the backend setup, but after that it worked great:
Could you show how to also do the "ftdi_get_bitmode()" function to read the state of the CBUS lines?

Copy link

agners commented May 23, 2022

Copy link

Perfect. Thank you. Using that example I am able to GET the values of the pin state (HI/LO).
One more question:
Is it possible to also GET the values of the pin mode (Input/Output)?

Copy link

agners commented May 24, 2022

I am not aware off. Also, from this list, it doesn't look like.

Copy link

Thanks for the link. I have a question open with the FTDI support guys. If they have an answer, I'll post it here too.

Copy link

Got a feedback direct from the FTDI guys:
"I’m afraid there is no API command to determine the CBUS mode of the FT230X.
Even the D2XX FT_GetBitMode command (in bit-bang mode) will only give you the hex value on the data bus pins, not the mode."

Copy link

agners commented May 25, 2022

Ok, oh well 🤷‍♂️ Thanks for posting it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment