From ab1dd41525ce8bf97769e5c5a6767bba4371501e Mon Sep 17 00:00:00 2001 From: h-4nd-h Date: Sun, 20 Sep 2020 09:54:29 +0200 Subject: [PATCH] First commit --- .gitignore | 2 + HarwareTest.pro | 35 ++++ main.cpp | 11 ++ mainwindow.cpp | 437 ++++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 79 +++++++++ mainwindow.ui | 432 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 996 insertions(+) create mode 100644 .gitignore create mode 100644 HarwareTest.pro create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0161bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.user + diff --git a/HarwareTest.pro b/HarwareTest.pro new file mode 100644 index 0000000..06f436e --- /dev/null +++ b/HarwareTest.pro @@ -0,0 +1,35 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + /home/hasis/Desktop/pcie/pcie_driver/api/api.cpp + +HEADERS += \ + mainwindow.h \ + /home/hasis/Desktop/pcie/pcie_driver/api/api.h + +FORMS += \ + mainwindow.ui + +INCLUDEPATH += /home/hasis/Desktop/pcie/pcie_driver/api/ + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..7c9faf5 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,437 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include +#include +#include +#include + +#define MESSAGE_BOX(M) \ + emit showMessage(M) + + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + _settings = new QSettings("Hasis", "HwTester"); + + _usd = new UltraSoundDevice(); + + this->setFixedSize(this->width(),this->height()); + + ui->prg_binUpload->setVisible(false); + ui->prg_fpgaProgram->setVisible(false); + + ui->tb_binFile->setReadOnly(true); + ui->tb_fpgaBit->setReadOnly(true); + + ui->tb_binFile->setText(_settings->value(BIN_FILE_PATH).value()); + ui->tb_binOffset->setText(_settings->value(BIN_OFFSET).value()); + ui->cb_binBarNum->setCurrentIndex(_settings->value(BIN_BAR_INDEX).value()); + ui->rbtn_reg->setChecked(_settings->value(REG_ACCESS_SEL).value()); + ui->rbtn_offset->setChecked(!_settings->value(REG_ACCESS_SEL).value()); + ui->tb_fpgaBit->setText(_settings->value(FPGA_FILE_PATH).value()); + + connect(this, &MainWindow::updateBlockProgressValue, this, &MainWindow::newBlockProgressValue); + connect(this, &MainWindow::updateFpgaProgressValue, this, &MainWindow::newFpgaProgressValue); + connect(this, &MainWindow::updateBlockProgressVisibility, this, &MainWindow::newBlockProgressVisibility); + connect(this, &MainWindow::updateFpgaProgressVisibility, this, &MainWindow::newFpgaProgressVisibility); + connect(this, &MainWindow::showMessage, this, &MainWindow::newMessage); +} + +/*************************************************************************************************/ +MainWindow::~MainWindow() +{ + delete ui; + + delete _settings; + + delete _usd; +} + +/*************************************************************************************************/ +void MainWindow::on_btn_browse_clicked() +{ + QFileDialog fileDialog; + fileDialog.setNameFilters({"SRAM binary file (*.bin)"}); + auto result = fileDialog.exec(); + + if(result) + { + auto selectedPath = fileDialog.selectedFiles()[0]; + ui->tb_binFile->setText(selectedPath); + _settings->setValue(BIN_FILE_PATH, selectedPath); + } +} + +/*************************************************************************************************/ +void MainWindow::on_btn_binUpload_clicked() +{ + auto bar = ui->cb_binBarNum->currentText().toUInt(); + + auto offset = ui->tb_binOffset->text().toUInt(Q_NULLPTR, 16); + if(offset == 0 && ui->tb_binOffset->text() != "0") + { + MESSAGE_BOX("Invalid input format for offset"); + return; + } + _settings->setValue(BIN_OFFSET, offset); + _settings->setValue(BIN_BAR_INDEX, ui->cb_binBarNum->currentIndex()); + + auto path = ui->tb_binFile->text(); + + QtConcurrent::run(this, &MainWindow::binaryFileUploader, bar, offset, path); +} + +/*************************************************************************************************/ +void MainWindow::on_btn_binVerify_clicked() +{ + auto bar = ui->cb_binBarNum->currentText().toUInt(); + + auto offset = ui->tb_binOffset->text().toUInt(Q_NULLPTR, 16); + if(offset == 0 && ui->tb_binOffset->text() != "0") + { + MESSAGE_BOX("Invalid input format for offset"); + return; + } + _settings->setValue(BIN_OFFSET, offset); + _settings->setValue(BIN_BAR_INDEX, ui->cb_binBarNum->currentIndex()); + + auto path = ui->tb_binFile->text(); + + QtConcurrent::run(this, &MainWindow::binaryFileVerifier, bar, offset, path); +} + +/*************************************************************************************************/ +void MainWindow::binaryFileUploader(quint32 bar, quint32 offset, QString path) +{ + QFile file(path); + + if (!file.open(QFile::ReadOnly)) + { + MESSAGE_BOX("Could not open binary file, aborting operation"); + return; + } + + emit updateBlockProgressVisibility(true); + emit updateBlockProgressValue(0); + + const auto actualSize = file.size(); + auto readSize = 0; + while(readSize < actualSize) + { + QByteArray chunk = file.read(4); + auto value = byteArrayTo32LittleEndian(chunk); + try + { + _usd->writeWord(offset + readSize, bar, value); + } catch (myexception e) + { + MESSAGE_BOX(e.what()); + + emit updateBlockProgressVisibility(false); + + file.close(); + + return; + } + readSize += 4; + + auto percentage = (readSize * 100 / actualSize); + emit updateBlockProgressValue(percentage); + } + + MESSAGE_BOX("Binary file upload finished with success"); + + emit updateBlockProgressVisibility(false); + + file.close(); +} + +/*************************************************************************************************/ +void MainWindow::binaryFileVerifier(quint32 bar, quint32 offset, QString path) +{ + QFile file(path); + + if (!file.open(QFile::ReadOnly)) + { + MESSAGE_BOX("Could not open binary file, aborting operation"); + return; + } + + emit updateBlockProgressVisibility(true); + emit updateBlockProgressValue(0); + + const auto actualSize = file.size(); + auto readSize = 0; + while(readSize < actualSize) + { + QByteArray chunk = file.read(4); + auto value = byteArrayTo32LittleEndian(chunk); + try + { + auto data = _usd->readWord(offset + readSize, bar); + if(data != value) + { + auto message = QString("Error in data @ offset 0x%1, expected 0x%2 saw 0x%3") + .arg(QString::number(offset + readSize)) + .arg(QString::number(value, 16)) + .arg(QString::number(data, 16)); + MESSAGE_BOX(message); + + emit updateBlockProgressVisibility(false); + + file.close(); + + return; + } + } + catch (myexception e) + { + MESSAGE_BOX(e.what()); + + emit updateBlockProgressVisibility(false); + + file.close(); + + return; + } + readSize += 4; + + auto percentage = (readSize * 100 / actualSize); + emit updateBlockProgressValue(percentage); + } + + MESSAGE_BOX("Binary verified with success"); + + emit updateBlockProgressVisibility(false); + + file.close(); +} + +/*************************************************************************************************/ +void MainWindow::fpgaProgrammer(quint32 bar, quint32 offset, QString path) +{ + QFile file(path); + + if (!file.open(QFile::ReadOnly)) + { + MESSAGE_BOX("Could not open bit file, aborting operation"); + return; + } + + emit updateFpgaProgressValue(0); + emit updateFpgaProgressVisibility(true); + + const auto actualSize = file.size(); + auto readSize = 0; + while(readSize < actualSize) + { + QByteArray chunk = file.read(4); + auto value = byteArrayTo32BigEndian(chunk); + try + { + _usd->writeWord(offset, bar, value); + auto temp = _usd->readWord(offset + 4, bar); + while ((temp & 0x01) == 0x01) + { + temp = _usd->readWord(offset + 4, bar); + } + + if((temp & 0x08) == 0x08) + { + throw(myexception("init_fail flag raised")); + } + + if((temp & 0x10) == 0x10) + { + throw(myexception("time_out flag raised")); + } + } + catch (myexception e) + { + MESSAGE_BOX(e.what()); + + emit updateFpgaProgressVisibility(false); + + file.close(); + + return; + } + readSize += 4; + + auto percentage = (readSize * 100 / actualSize); + emit updateFpgaProgressValue(percentage); + } + + auto temp = _usd->readWord(offset + 4, bar); + if((temp & 0x02) == 0x02) + { + MESSAGE_BOX("FPGA programming finished with success"); + } + else if((temp & 0x04) == 0x04) + { + MESSAGE_BOX("FPGA programming failed"); + } + + emit updateFpgaProgressVisibility(false); + + file.close(); +} + +/*************************************************************************************************/ +quint32 MainWindow::byteArrayTo32LittleEndian(QByteArray data) +{ + quint32 temp = 0; + + temp = (data[0] & 0x000000FF) | + ((data[1] << 8) & 0x0000FF00) | + ((data[2] << 16) & 0x00FF0000) | + ((data[3] << 24) & 0xFF000000); + + return temp; +} + +/*************************************************************************************************/ +quint32 MainWindow::byteArrayTo32BigEndian(QByteArray data) +{ + quint32 temp = 0; + + temp = (data[3] & 0x000000FF) | + ((data[2] << 8) & 0x0000FF00) | + ((data[1] << 16) & 0x00FF0000) | + ((data[0] << 24) & 0xFF000000); + + return temp; +} + +/*************************************************************************************************/ +void MainWindow::on_rbtn_reg_toggled(bool checked) +{ + if(checked) + { + _settings->setValue(REG_ACCESS_SEL, true); + ui->l_regIndicator->setText("Register number: (Dec)"); + } +} + +/*************************************************************************************************/ +void MainWindow::on_rbtn_offset_toggled(bool checked) +{ + if(checked) + { + _settings->setValue(REG_ACCESS_SEL, false); + ui->l_regIndicator->setText("Register offset: (Hex)"); + } +} + +/*************************************************************************************************/ +void MainWindow::on_btn_readReg_clicked() +{ + auto bar = ui->cb_regBarNum->currentText().toUInt(); + + quint32 offset = 0; + if(ui->rbtn_reg->isChecked()) + offset = ui->tb_regIndicator->text().toUInt(Q_NULLPTR, 10) * 4; + else + offset = ui->tb_regIndicator->text().toUInt(Q_NULLPTR, 16); + + if(offset == 0 && ui->tb_regIndicator->text() != "0") + { + MESSAGE_BOX("Invalid input format for offset"); + return; + } + + auto value = _usd->readWord(offset, bar); + + ui->lcd_regvalue->setDigitCount(8); + ui->lcd_regvalue->display(QString::number(value, 16)); +} + +/*************************************************************************************************/ +void MainWindow::on_btn_writeReg_clicked() +{ + auto bar = ui->cb_regBarNum->currentText().toUInt(); + + quint32 offset = 0; + if(ui->rbtn_reg->isChecked()) + offset = ui->tb_regIndicator->text().toUInt(Q_NULLPTR, 10) * 4; + else + offset = ui->tb_regIndicator->text().toUInt(Q_NULLPTR, 16); + + if(offset == 0 && ui->tb_regIndicator->text() != "0") + { + MESSAGE_BOX("Invalid input format for offset"); + return; + } + + auto value = ui->tb_regValue->text().toUInt(Q_NULLPTR, 16); + if(value == 0 && ui->tb_regValue->text() != "0") + { + MESSAGE_BOX("Invalid input format for write value"); + return; + } + + _usd->writeWord(offset, bar, value); +} + +/*************************************************************************************************/ +void MainWindow::newBlockProgressValue(int percentage) +{ + ui->prg_binUpload->setValue(percentage); +} + +/*************************************************************************************************/ +void MainWindow::newFpgaProgressValue(int percentage) +{ + ui->prg_fpgaProgram->setValue(percentage); +} + +/*************************************************************************************************/ +void MainWindow::newBlockProgressVisibility(bool show) +{ + ui->prg_binUpload->setVisible(show); +} + +/*************************************************************************************************/ +void MainWindow::newFpgaProgressVisibility(bool show) +{ + ui->prg_fpgaProgram->setVisible(show); +} + +/*************************************************************************************************/ +void MainWindow::on_btn_fpgaBrowse_clicked() +{ + QFileDialog fileDialog; + fileDialog.setNameFilters({"FPGA program file (*.bit)"}); + auto result = fileDialog.exec(); + + if(result) + { + auto selectedPath = fileDialog.selectedFiles()[0]; + ui->tb_fpgaBit->setText(selectedPath); + _settings->setValue(FPGA_FILE_PATH, selectedPath); + } +} + +/*************************************************************************************************/ +void MainWindow::on_btn_fpgaProgram_clicked() +{ + auto bar = 1; + auto offset = 1000; + + auto path = ui->tb_fpgaBit->text(); + + QtConcurrent::run(this, &MainWindow::fpgaProgrammer, bar, offset, path); +} + +/*************************************************************************************************/ +void MainWindow::newMessage(QString message) +{ + QMessageBox msgBox; + msgBox.setText(message); + msgBox.exec(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..b224861 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,79 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +#include "api.h" + +#define BIN_FILE_PATH "binFilePath" +#define FPGA_FILE_PATH "fpgaFilePath" +#define BIN_OFFSET "binOffset" +#define BIN_BAR_INDEX "binBarIndex" +#define REG_ACCESS_SEL "regAccessType" + + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void on_btn_browse_clicked(); + + void on_btn_binUpload_clicked(); + + void on_btn_binVerify_clicked(); + + void on_rbtn_reg_toggled(bool checked); + + void on_rbtn_offset_toggled(bool checked); + + void on_btn_readReg_clicked(); + + void on_btn_writeReg_clicked(); + + void newBlockProgressValue(int percentage); + + void newFpgaProgressValue(int percentage); + + void newBlockProgressVisibility(bool show); + + void newFpgaProgressVisibility(bool show); + + void on_btn_fpgaBrowse_clicked(); + + void on_btn_fpgaProgram_clicked(); + + void newMessage(QString message); + + +private: + Ui::MainWindow *ui; + + QSettings* _settings; + + UltraSoundDevice* _usd; + + void binaryFileUploader(quint32 bar, quint32 offset, QString path); + void binaryFileVerifier(quint32 bar, quint32 offset, QString path); + void fpgaProgrammer(quint32 bar, quint32 offset, QString path); + quint32 byteArrayTo32LittleEndian(QByteArray data); + quint32 byteArrayTo32BigEndian(QByteArray data); + +signals: + void updateBlockProgressValue(int percentage); + void updateFpgaProgressValue(int percentage); + void updateBlockProgressVisibility(bool show); + void updateFpgaProgressVisibility(bool show); + void showMessage(QString message); + +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..fbd116b --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,432 @@ + + + MainWindow + + + + 0 + 0 + 800 + 487 + + + + + 0 + 0 + + + + HW TESTER + + + + + + 10 + 0 + 781 + 461 + + + + + + + Block operation + + + + + 10 + 30 + 761 + 27 + + + + + + + File to Upload: + + + + + + + + + + Browse... + + + + + + + + + 17 + 70 + 27 + 25 + + + + Bar: + + + + + + 70 + 70 + 41 + 25 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + + + 90 + 100 + 91 + 25 + + + + + + + 20 + 100 + 71 + 25 + + + + Offset: 0x + + + + + + 250 + 100 + 361 + 25 + + + + + 1 + 0 + + + + 24 + + + + + + 690 + 70 + 82 + 58 + + + + + + + Verify + + + + + + + Upload + + + + + + + + + + + Qt::Horizontal + + + + + + + Register operation + + + + + 610 + 30 + 161 + 80 + + + + Access registers by: + + + + + 10 + 50 + 131 + 23 + + + + Register offset + + + + + + 10 + 30 + 141 + 23 + + + + Register number + + + + + + + 12 + 32 + 181 + 91 + + + + + + + : + + + + + + + Value: (Hex) + + + + + + + + 0 + 0 + + + + Read result: (Hex) + + + + + + + + + 200 + 30 + 232 + 95 + + + + + + + + + + + + Bar: + + + + + + + + 0 + + + + + 1 + + + + + 2 + + + + + + + + + + + + + + + Write + + + + + + + + + + + + + + + 0 + 0 + + + + Read + + + + + + + + + + + + + Qt::Horizontal + + + + + + + FPGA Program: + + + + + 10 + 30 + 761 + 27 + + + + + + + Bit file: + + + + + + + + + + Browse... + + + + + + + + + 690 + 71 + 80 + 25 + + + + Program + + + + + + 11 + 71 + 671 + 25 + + + + 24 + + + + + + + + + + + 0 + 0 + 800 + 22 + + + + + + + +