From a91958a068c32688142ab5fc1ace51f2ca5bb2f7 Mon Sep 17 00:00:00 2001 From: Ivan Olenichev Date: Mon, 12 Nov 2018 18:14:30 +0600 Subject: [PATCH] Implementation of I2C-master on an FT2232 for debugging FPGAs --- ft232_qt/ft232_qt.pro | 36 ++++ ft232_qt/ft232_qt.pro.user | 336 +++++++++++++++++++++++++++++++++++++ ft232_qt/ft232_reign.cpp | 216 ++++++++++++++++++++++++ ft232_qt/ft232_reign.h | 60 +++++++ ft232_qt/ft232_reign.ui | 276 ++++++++++++++++++++++++++++++ ft232_qt/ftdi_common.cpp | 59 +++++++ ft232_qt/i2c.c | 144 ++++++++++++++++ ft232_qt/i2c.h | 19 +++ ft232_qt/main.cpp | 11 ++ 9 files changed, 1157 insertions(+) create mode 100644 ft232_qt/ft232_qt.pro create mode 100644 ft232_qt/ft232_qt.pro.user create mode 100644 ft232_qt/ft232_reign.cpp create mode 100644 ft232_qt/ft232_reign.h create mode 100644 ft232_qt/ft232_reign.ui create mode 100644 ft232_qt/ftdi_common.cpp create mode 100644 ft232_qt/i2c.c create mode 100644 ft232_qt/i2c.h create mode 100644 ft232_qt/main.cpp diff --git a/ft232_qt/ft232_qt.pro b/ft232_qt/ft232_qt.pro new file mode 100644 index 0000000..8a74c63 --- /dev/null +++ b/ft232_qt/ft232_qt.pro @@ -0,0 +1,36 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-09-26T00:17:40 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = ft232_qt +TEMPLATE = app + + +SOURCES += main.cpp\ + ft232_reign.cpp \ + i2c.c + +HEADERS += ft232_reign.h \ + i2c.h + +FORMS += ft232_reign.ui + +unix:!macx: LIBS += -L$$PWD/../libmpsse-1.3/src/ -lmpsse + +INCLUDEPATH += $$PWD/../libmpsse-1.3/src +DEPENDPATH += $$PWD/../libmpsse-1.3/src + +unix:!macx: PRE_TARGETDEPS += $$PWD/../libmpsse-1.3/src/libmpsse.a + +unix:!macx: LIBS += -L$$PWD/../libftdi1/src/ -lftdi + +INCLUDEPATH += $$PWD/../libftdi1/src +DEPENDPATH += $$PWD/../libftdi1/src + +unix:!macx: PRE_TARGETDEPS += $$PWD/../libftdi1/src/libftdi.so diff --git a/ft232_qt/ft232_qt.pro.user b/ft232_qt/ft232_qt.pro.user new file mode 100644 index 0000000..44c218a --- /dev/null +++ b/ft232_qt/ft232_qt.pro.user @@ -0,0 +1,336 @@ + + + + + + EnvironmentId + {9f5795d5-b622-4dd0-a24e-5a8fef00d441} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {25311bd8-1f8c-4ae8-a25e-4d0e50893f82} + 0 + 0 + 0 + + /home/t-bird/i2c_keyboard/ft232_qt/build-ft232_qt-Desktop-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Отладка + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/t-bird/i2c_keyboard/ft232_qt/build-ft232_qt-Desktop-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Выпуск + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/t-bird/i2c_keyboard/ft232_qt/build-ft232_qt-Desktop-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Профилирование + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Установка + + ProjectExplorer.BuildSteps.Deploy + + 1 + Локальная установка + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + ft232_qt + + Qt4ProjectManager.Qt4RunConfiguration:/home/t-bird/i2c_keyboard/ft232_qt/ft232_qt/ft232_qt.pro + true + + ft232_qt.pro + false + + /home/t-bird/i2c_keyboard/ft232_qt/build-ft232_qt-Desktop-Debug + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/ft232_qt/ft232_reign.cpp b/ft232_qt/ft232_reign.cpp new file mode 100644 index 0000000..304eaf7 --- /dev/null +++ b/ft232_qt/ft232_reign.cpp @@ -0,0 +1,216 @@ +#include "ft232_reign.h" +#include "ui_ft232_reign.h" + +#include + +#include +#include + +#define FTDI_ERROR(info) { toLog(info); return; } + +QString last_write_data = "00"; + +QString hex_values[16] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; + +int tmr_on = 0; + +ft232_reign::ft232_reign(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ft232_reign) +{ + ui->setupUi(this); + log = ""; + tmr = new QTimer(); + tmr->setInterval(500); + tmr->setSingleShot(false); + tmr->start(); + QObject::connect(tmr, SIGNAL(timeout ()), this, SLOT(tmr_fxn())); +} + +ft232_reign::~ft232_reign() +{ + i2c_close(); + delete ui; +} + +void ft232_reign::on_pushButton_clicked() +{ + //i2c_example(); + //char desc[1024], serial[1024]; + //i2c_open_index_port (0, desc, serial); + + //if (bitbang_open (0, 1, 0) == 0) + // tmr_on = 1; +} + +void ft232_reign::on_pushButton_2_clicked() +{ + //i2c_example(); + //bitbang_close(); + //tmr_on = 0; +} + +void ft232_reign::tmr_fxn() +{ + //if (tmr_on == 0) + // return; + //char rd[10]; + //bitbang_read(rd); + //qDebug ("Reading from FTDI bitbang: %x, %x, %x, %x", rd[0], rd[1], rd[2], rd[3]); +} + +void ft232_reign::toLog(QString info) +{ + log = QTime::currentTime().toString("HH:mm:ss") + "\t" + info + "\n" + log; + ui->log->setText("LOG\n\n" + log); +} + +QString ft232_reign::char_to_str(char *data, int len) +{ + QString str = ""; + for (int i = 0; i < len; i++) { + if (data[i] == 0x00) + break; + str = str + data[i]; + } + return str; +} + +void ft232_reign::on_clr_log_clicked() +{ + log = ""; + ui->log->setText("LOG\n\n"); +} + +void ft232_reign::on_connect_clicked() +{ + int result = 0; + if (ui->connect->text() == "Connect") { + res = i2c_open(ui->i2c_clk->value(), ui->i2c_index->value(), (ui->i2c_port->currentIndex() + 1)); + qDebug ("Connecting to FTDI device, clk %d, index %d, port %d. RETURN %d", ui->i2c_clk->value(), ui->i2c_index->value(), (ui->i2c_port->currentIndex() + 1), res); + if (res == 0) { + ui->connect->setText("Disconnect"); + toLog ("CONNECTING TO DEVICE OK, " + ui->i2c_port->currentText() + ", CLK " + QString::number(ui->i2c_clk->value()) + ", INDEX " + QString::number(ui->i2c_index->value())); + } + else { + FTDI_ERROR("ABORT_OPEN_CHANNEL, ERROR " + QString::number(res)); + } + } + else { + i2c_close(); + ui->connect->setText("Connect"); + toLog ("DEVICE DISCONNECTED"); + } +} + +void ft232_reign::on_i2c_length_valueChanged(int arg1) +{ + QString mask = "HH"; int last_mask_len; QString last_data; + for (int i = 1; i < arg1; i++) + mask = mask + " HH"; + last_mask_len = ui->i2c_write_data->inputMask().length(); + last_data = ui->i2c_write_data->text(); + ui->i2c_write_data->setInputMask(mask); + if (last_mask_len < mask.length()) { + for (int i = 0; i < ((mask.length() - last_mask_len) / 3); i++) + last_data = last_data + " 00"; + ui->i2c_write_data->setText(last_data); + } +} + +void ft232_reign::on_i2c_write_data_textChanged(const QString &arg1) +{ + if (arg1.length() != (3 * ui->i2c_length->value() - 1)) + ui->i2c_write_data->setText(last_write_data); + else + last_write_data = ui->i2c_write_data->text(); +} + +void ft232_reign::on_i2c_write_clicked() +{ + QString str = ui->i2c_write_data->text(); + for (int i = 0; i < ui->i2c_length->value(); i++) + data[i] = str.mid(i*3, 2).toInt(NULL, 16); + res = i2c_tx (ui->i2c_adress->value(), (char*)data, ui->i2c_length->value(), 1, 1); //res = I2C_DeviceWrite(reign, ui->i2c_adress->value(), ui->i2c_length->value(), data, &transfer_len, 0x07); + if (res == 10/*FT_DEVICE_NOT_FOUND*/) { + toLog ("I2C DEVICE WITH ADRESS 0x" + QString::number(ui->i2c_adress->value(), 16) + " NOT ASK (TX)"); + return; + } + if (res != 0/*FT_OK*/) + FTDI_ERROR("ABORT_TRANSMIT, ERROR " + QString::number(res)); + str = "I2C TRANSMIT TO 0x" + QString::number(ui->i2c_adress->value(), 16) + " " + QString::number(ui->i2c_length->value()) + " BYTES:\t"; + for (int i = 0; i < ui->i2c_length->value(); i++) + str = str + " " + QString::number(data[i], 16); + toLog(str); +} + +void ft232_reign::on_i2c_read_clicked() +{ + QString str; + res = i2c_rx (ui->i2c_adress->value(), (char*)data, ui->i2c_length->value(), 1, 1); //res = I2C_DeviceRead(reign, ui->i2c_adress->value(), ui->i2c_length->value(), data, &transfer_len, 0x0B); + if (res == 10/*FT_DEVICE_NOT_FOUND*/) { + toLog ("I2C DEVICE WITH ADRESS 0x" + QString::number(ui->i2c_adress->value(), 16) + " NOT ASK (RX)"); + return; + } + if (res != 0/*FT_OK*/) + FTDI_ERROR("ABORT_RECEIVE, ERROR " + QString::number(res)); + str = "I2C RECEIVE FROM 0x" + QString::number(ui->i2c_adress->value(), 16) + " " + QString::number(ui->i2c_length->value()) + " BYTES:\t"; + for (int i = 0; i < ui->i2c_length->value(); i++) + str = str + " " + QString::number(data[i], 16); + toLog(str); +} + +void ft232_reign::on_read_i2c_hid_desc_clicked() +{ + toLog ("READING I2C HID DESCRIPTOR"); + ui->i2c_length->setValue(2); + ui->i2c_write_data->setText("01 00"); + on_i2c_write_clicked(); + QThread::msleep(100); + ui->i2c_length->setValue(30); + on_i2c_read_clicked(); + for (int i = 0; i < 30; i++) { + i2c_hid_descr[i] = data[i]; + } + toLog ("------------------------------------------"); +} + +void ft232_reign::on_read_hid_report_desc_clicked() +{ + toLog ("READING HID REPORT DESCRIPTOR"); + ui->i2c_length->setValue(2); + QString reg = hex_values[(i2c_hid_descr[6]>>4) & 0xF] + hex_values[(i2c_hid_descr[6]>>0) & 0xF] + " " + \ + hex_values[(i2c_hid_descr[7]>>4) & 0xF] + hex_values[(i2c_hid_descr[7]>>0) & 0xF]; + ui->i2c_write_data->setText(reg); + on_i2c_write_clicked(); + QThread::msleep(100); + ui->i2c_length->setValue((i2c_hid_descr[4] & 0xFF) + (i2c_hid_descr[5] & 0xFF) * 256); + on_i2c_read_clicked(); + toLog ("------------------------------------------"); +} + +void ft232_reign::on_read_input_report_int_clicked() +{ + toLog ("READING INPUT REPORT (INTERRUPT MODE)"); + ui->i2c_length->setValue((i2c_hid_descr[10] & 0xFF) + (i2c_hid_descr[11] & 0xFF) * 256); + on_i2c_read_clicked(); + toLog ("------------------------------------------"); +} + +void ft232_reign::on_read_input_report_cmd_clicked() +{ + toLog ("READING INPUT REPORT (COMMAND MODE)"); + ui->i2c_length->setValue(6); + QString reg = hex_values[(i2c_hid_descr[16]>>4) & 0xF] + hex_values[(i2c_hid_descr[16]>>0) & 0xF] + " " + \ + hex_values[(i2c_hid_descr[17]>>4) & 0xF] + hex_values[(i2c_hid_descr[17]>>0) & 0xF]; + reg = reg + " 10 02 "; // COMMAND OPCODE - 2 (INPUT REPORT REQUEST), TYPE 01 - INPUT + reg = reg + hex_values[(i2c_hid_descr[18]>>4) & 0xF] + hex_values[(i2c_hid_descr[18]>>0) & 0xF] + " " + \ + hex_values[(i2c_hid_descr[19]>>4) & 0xF] + hex_values[(i2c_hid_descr[19]>>0) & 0xF]; + ui->i2c_write_data->setText(reg); + on_i2c_write_clicked(); + QThread::msleep(100); + ui->i2c_length->setValue((i2c_hid_descr[10] & 0xFF) + (i2c_hid_descr[11] & 0xFF) * 256); + on_i2c_read_clicked(); + toLog ("------------------------------------------"); +} + diff --git a/ft232_qt/ft232_reign.h b/ft232_qt/ft232_reign.h new file mode 100644 index 0000000..297cf0b --- /dev/null +++ b/ft232_qt/ft232_reign.h @@ -0,0 +1,60 @@ +#ifndef FT232_REIGN_H +#define FT232_REIGN_H + +#include +#include + +namespace Ui { +class ft232_reign; +} + +class ft232_reign : public QMainWindow +{ + Q_OBJECT + +public: + explicit ft232_reign(QWidget *parent = 0); + ~ft232_reign(); + +public slots: + void tmr_fxn(); + +private slots: + void toLog(QString info); + QString char_to_str(char *data, int len); + void on_clr_log_clicked(); + + void on_pushButton_clicked(); + + void on_pushButton_2_clicked(); + + void on_connect_clicked(); + + void on_i2c_length_valueChanged(int arg1); + + void on_i2c_write_data_textChanged(const QString &arg1); + + void on_i2c_write_clicked(); + + void on_i2c_read_clicked(); + + void on_read_i2c_hid_desc_clicked(); + + void on_read_hid_report_desc_clicked(); + + void on_read_input_report_int_clicked(); + + void on_read_input_report_cmd_clicked(); + +private: + Ui::ft232_reign *ui; + + QString log; + int res; + unsigned char data[1001]; + unsigned char i2c_hid_descr[30]; + + QTimer *tmr; +}; + +#endif // FT232_REIGN_H diff --git a/ft232_qt/ft232_reign.ui b/ft232_qt/ft232_reign.ui new file mode 100644 index 0000000..a885576 --- /dev/null +++ b/ft232_qt/ft232_reign.ui @@ -0,0 +1,276 @@ + + + ft232_reign + + + + 0 + 0 + 627 + 671 + + + + ft232_reign + + + + + + 280 + 0 + 81 + 27 + + + + <html><head/><body><p>Device index.</p><p>If only one device is connected, set zero.</p><p>If two or more devices are connected, index can be zero or non zero.</p></body></html> + + + Index + + + 10 + + + + + + 90 + 0 + 181 + 27 + + + + Hz + + + Clockrate + + + 1000000 + + + 100000 + + + 10 + + + + + + 10 + 0 + 71 + 27 + + + + 0 + + + true + + + + Port A + + + + + Port B + + + + + Port C + + + + + Port D + + + + + + + 370 + 0 + 241 + 27 + + + + Connect + + + + + + 10 + 210 + 601 + 381 + + + + + + + 10 + 50 + 601 + 27 + + + + + + + 140 + 80 + 141 + 27 + + + + Length + + + 0 + + + 1000 + + + 1000 + + + + + + 10 + 80 + 121 + 27 + + + + Adress 0x + + + 127 + + + 52 + + + 16 + + + + + + 290 + 80 + 99 + 27 + + + + TX + + + + + + 400 + 80 + 99 + 27 + + + + RX + + + + + + 10 + 130 + 151 + 27 + + + + Read I2C HID Desc + + + + + + 180 + 130 + 181 + 27 + + + + Read HID Report Desc + + + + + + 380 + 130 + 231 + 27 + + + + Read Input Report (Interrupt) + + + + + + 380 + 160 + 231 + 27 + + + + Read Input Report (Command) + + + + + + + 0 + 0 + 627 + 25 + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/ft232_qt/ftdi_common.cpp b/ft232_qt/ftdi_common.cpp new file mode 100644 index 0000000..3a80454 --- /dev/null +++ b/ft232_qt/ftdi_common.cpp @@ -0,0 +1,59 @@ +/*! + * \file ftdi_common.c + * + * \author FTDI + * \date 20110321 + * + * Copyright ? 2000-2014 Future Technology Devices International Limited + * + * + * THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * + * Project: libMPSSE + * Module: Common + * + * Rivision History: + * 0.1 - initial version + */ + + +/******************************************************************************/ +/* Include files */ +/******************************************************************************/ +#include "ftdi_infra.h" /*Common prrtable infrastructure(datatypes, libraries, etc)*/ +#include "ftdi_common.h" /*Common across I2C, SPI, JTAG modules*/ + + +/******************************************************************************/ +/* Macro defines */ +/******************************************************************************/ + + + + +/******************************************************************************/ +/* Local function declarations */ +/******************************************************************************/ + + + + +/******************************************************************************/ +/* Global variables */ +/******************************************************************************/ + + + +/******************************************************************************/ +/* Public function definations */ +/******************************************************************************/ + + diff --git a/ft232_qt/i2c.c b/ft232_qt/i2c.c new file mode 100644 index 0000000..2ff43e0 --- /dev/null +++ b/ft232_qt/i2c.c @@ -0,0 +1,144 @@ + + +#include +#include +#include +#include <../libftdi1/src/ftdi.h> + +struct vid_pid supported_devices[] = { + { 0x0403, 0x6010, "FT2232 Future Technology Devices International, Ltd" }, + { 0x0403, 0x6011, "FT4232 Future Technology Devices International, Ltd" }, + { 0x0403, 0x6014, "FT232H Future Technology Devices International, Ltd" }, + + /* These devices are based on FT2232 chips, but have not been tested. */ + { 0x0403, 0x8878, "Bus Blaster v2 (channel A)" }, + { 0x0403, 0x8879, "Bus Blaster v2 (channel B)" }, + { 0x0403, 0xBDC8, "Turtelizer JTAG/RS232 Adapter A" }, + { 0x0403, 0xCFF8, "Amontec JTAGkey" }, + { 0x0403, 0x8A98, "TIAO Multi Protocol Adapter"}, + { 0x15BA, 0x0003, "Olimex Ltd. OpenOCD JTAG" }, + { 0x15BA, 0x0004, "Olimex Ltd. OpenOCD JTAG TINY" }, + + { 0, 0, NULL } +}; + +struct mpsse_context *i2c_dev = NULL; +char i2c_adress; +int i; + +struct ftdi_context *ftdi; +int f;//,i; + +int i2c_open (int clockrate, int index, int port) +// PORT A - 1, PORT B - 2, ANY PORT - 0 +// CLOCKRATE IN HERZ +// INDEX IS FOR SITUATION WHEN NO ONE FTDI MPSSE DEVICE IS CONNECTED TO PC (IF ONE DEVICE IS CONNECTED PLEASE SET INDEX TO ZERO, IF MULTIPLE DEVICES - INDEX MAY BE NOT ZERO) +{ + i2c_dev = OpenIndex(0x0403, 0x6010, I2C, clockrate, MSB, port, NULL, NULL, index); + if (i2c_dev->open) + return 0; // ALL OK, RETURN ZERO + // NOT OK, CLOSING AND RETURNING NON-ZERO + Close(i2c_dev); + i2c_dev = NULL; + return 1; +} + +void i2c_close () +{ + Close(i2c_dev); + i2c_dev = NULL; +} + +int i2c_tx (char adress, char* data, int len, int stt, int stp) +{ + if ((i2c_dev == NULL) || (i2c_dev->open == 0)) + return 1; // NO OPENED DEVICE + if (stt) + Start (i2c_dev); + i2c_adress = (adress << 1) & (~0x01) ; + Write (i2c_dev, &i2c_adress, 1); + if (GetAck (i2c_dev) != ACK) { + if (stp) + Stop (i2c_dev); + return 10; // NO ACK ADRESS + } + Write (i2c_dev, data, len); + i = GetAck(i2c_dev); + if (stp) + Stop (i2c_dev); + if (i != ACK) + return 11; // NO ACK DATA + return 0; +} + +int i2c_rx (char adress, char* data, int len, int stt, int stp) +{ + char* temp_data; + if ((i2c_dev == NULL) || (i2c_dev->open == 0)) + return 1; + if (stt) + Start (i2c_dev); + i2c_adress = (adress << 1) | (0x01) ; + Write (i2c_dev, &i2c_adress, 1); + if (GetAck (i2c_dev) != ACK) { + if (stp) + Stop (i2c_dev); + return 10; // NO ACK ADRESS + } + SendAcks (i2c_dev); // SEND ACKS WHEN DATA PROCESSING + temp_data = Read (i2c_dev, len); + + /* Tell libmpsse to send NACKs after reading data */ + SendNacks(i2c_dev); + /* Read in one dummy byte, with a NACK */ + Read(i2c_dev, 1); + + if (stp) + Stop (i2c_dev); + if (data) { + for (int i = 0; i < len; i++) + data[i] = temp_data[i]; + free(temp_data); + return 0; // ALL GOOD + } + return 12; // UNKNOWN ERROR, NO DATA +} + +int bitbang_open (int index, int port, int mask) +{ + if ((ftdi = ftdi_new()) == 0) + { + fprintf(stderr, "ftdi_new failed\n"); + return EXIT_FAILURE; + } + f = ftdi_usb_open_desc_index (ftdi, 0x0403, 0x6010, NULL, NULL, index); + if (f < 0 && f != -5) + { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi)); + ftdi_free(ftdi); + return 2; + } + f = ftdi_set_interface(ftdi, port); + if (f < 0 && f != -5) + { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi)); + ftdi_free(ftdi); + return 3; + } + ftdi_set_bitmode(ftdi, mask, BITMODE_BITBANG); + return 0; +} + +void bitbang_close () +{ + ftdi_disable_bitbang(ftdi); + + ftdi_usb_close(ftdi); + + ftdi_free(ftdi); +} + +int bitbang_read (char* pins) +{ + return ftdi_read_pins(ftdi, pins); +} diff --git a/ft232_qt/i2c.h b/ft232_qt/i2c.h new file mode 100644 index 0000000..48089ac --- /dev/null +++ b/ft232_qt/i2c.h @@ -0,0 +1,19 @@ +#ifdef __cplusplus + +extern "C" { + +int i2c_open (int clockrate, int index, int port); +// PORT A - 1, PORT B - 2, ANY PORT - 0 +// CLOCKRATE IN HERZ +// INDEX IS FOR SITUATION WHEN NO ONE FTDI MPSSE DEVICE IS CONNECTED TO PC (IF ONE DEVICE IS CONNECTED PLEASE SET INDEX TO ZERO, IF MULTIPLE DEVICES - INDEX MAY BE NOT ZERO) +void i2c_close (); +int i2c_tx (char adress, char* data, int len, int stt, int stp); +int i2c_rx (char adress, char* data, int len, int stt, int stp); + +int bitbang_open (int index, int port, int mask); +void bitbang_close (); +int bitbang_read (char* pins); + +} + +#endif diff --git a/ft232_qt/main.cpp b/ft232_qt/main.cpp new file mode 100644 index 0000000..27878d4 --- /dev/null +++ b/ft232_qt/main.cpp @@ -0,0 +1,11 @@ +#include "ft232_reign.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ft232_reign w; + w.show(); + + return a.exec(); +}