Open
Description
Hi.
Thanks for this awesome library!
So, it would be nice to allow to receive the data D0
.. D7
from the board in asynchronous bit bang mode. For example, supposing a circuit uses a FT232R with its pins 1 (D0), 5 (D1), 3 (D2), 11 (D3), 2 (D4), 9 (D5), 10 (D6) and 6 (D7) mapped to eight switches with pull-up resistors, it would be possible to detect if the switches are ON or OFF. Below a small example showing how to do that in C code:
#include <stdio.h>
#include <stdbool.h>
#include "ftd2xx.h"
static void die(CONST PUCHAR msg) {
printf("%s\n", msg);
exit(1);
}
static bool pinStatus(UCHAR data, UCHAR pin) {
return data & (1 << pin);
}
static const char *pinStatusString(UCHAR data, UCHAR pin) {
return pinStatus(data, pin) ? "ON" : "OFF";
}
int main(void) {
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
UCHAR data;
UCHAR Mask = 0x00; // Set all ports as Input
UCHAR Mode = FT_BITMODE_SYNC_BITBANG; // Set asynchronous bit-bang mode
ftStatus = FT_Open(0, &ftHandle);
if (ftStatus != FT_OK) {
die("FT_Open failed\n");
}
ftStatus = FT_SetBitMode(ftHandle, Mask, Mode);
if (ftStatus != FT_OK) {
FT_Close(ftHandle);
die("FT_SetBitMode failed\n");
}
while (1) {
data = 0;
ftStatus = FT_GetBitMode(ftHandle, &data);
if (ftStatus != FT_OK) {
FT_Close(ftHandle);
die("FT_GetBitMode failed\n");
}
for (UCHAR i = 0; i < 7; i++) {
printf("PIN %d: %s\n", i + 1, pinStatusString(data, i));
}
printf("\n");
Sleep(500); // waits 500 ms
}
FT_Close(ftHandle);
return 0;
}
// Prints ON/OFF according to the switches states, e.g.:
// PIN 1: ON
// PIN 2: OFF
// PIN 3: OFF
// PIN 4: ON
// PIN 5: ON
// PIN 6: OFF
// PIN 7: ON
A good reference for implementation is the official app note AN_232R-01 (topic 2 Asynchronous Bit Bang Mode) available at: https://www.ftdichip.com/Support/Documents/AppNotes/AN_232R-01_Bit_Bang_Mode_Available_For_FT232R_and_Ft245R.pdf
cheers