diff --git a/mainwindow.cpp b/mainwindow.cpp index 556078e..1990074 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,5 +1,6 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -9,6 +10,7 @@ MainWindow::MainWindow(QWidget *parent) drvSPI = new DRV_Spi(this); drvUart = new DRV_Uart(this); fHandler = new FrameHandler(this); + fHandler = new FrameHandler(this); } MainWindow::~MainWindow() @@ -107,6 +109,7 @@ void MainWindow::on_pushButton_5_clicked() ui->lineEdit->setEnabled(false); ui->pushButton_5->setEnabled(false); ui->pushButton_7->setEnabled(true); + ui->btnSpiSaveSetting->setEnabled(false); onLogPrint("Close device success"); }else{ onLogPrint("Close device failed"); @@ -135,15 +138,17 @@ void MainWindow::on_pushButton_2_clicked() // 判断打开串口设备是否成功 if(status == TRUE){ - ui->comboBox_3->setEnabled(true); - ui->comboBox_4->setEnabled(true); - ui->comboBox_5->setEnabled(true); - ui->comboBox_6->setEnabled(true); - ui->comboBox_16->setEnabled(true); - ui->comboBox_17->setEnabled(true); + ui->comboSpiMode->setEnabled(true); + ui->comboSpiClock->setEnabled(true); + ui->comboSpiByteOrder->setEnabled(true); + ui->comboSpiDataBits->setEnabled(true); + ui->comboSpiCS1Pol->setEnabled(true); + ui->comboSpiCS2Pol->setEnabled(true); ui->pushButton_3->setEnabled(true); ui->pushButton_2->setEnabled(false); + ui->btnSpiSaveSetting->setEnabled(true); onLogPrint("Open device success"); + on_btnSpiSaveSetting_clicked(); // 初始化SPI参数 }else{ onLogPrint("Open device failed"); } @@ -158,12 +163,12 @@ void MainWindow::on_pushButton_3_clicked() // 判断打开串口设备是否成功 if(status == TRUE){ - ui->comboBox_3->setEnabled(false); - ui->comboBox_4->setEnabled(false); - ui->comboBox_5->setEnabled(false); - ui->comboBox_6->setEnabled(false); - ui->comboBox_16->setEnabled(false); - ui->comboBox_17->setEnabled(false); + ui->comboSpiMode->setEnabled(false); + ui->comboSpiClock->setEnabled(false); + ui->comboSpiByteOrder->setEnabled(false); + ui->comboSpiDataBits->setEnabled(false); + ui->comboSpiCS1Pol->setEnabled(false); + ui->comboSpiCS2Pol->setEnabled(false); ui->pushButton_3->setEnabled(false); ui->pushButton_2->setEnabled(true); @@ -174,16 +179,151 @@ void MainWindow::on_pushButton_3_clicked() } -void MainWindow::on_pushButton_9_clicked() -{ - QByteArray tmp = fHandler->genREGWrite(0x01,0x01,0x09,0x81); - qDebug()<comboBox_15->currentText().toUInt(NULL, 10); + unsigned char ucChannel = 0; + QString tmp = ui->lineEdit_2->text(); + if (tmp.toInt() < 0 || tmp.toInt() > 256) { + onLogPrint("Wrong channel #"); + return; + } + switch (tmp.toUInt()) { + case 256: + ucChannel = 0; + ucDAC = ucDAC | 0x80; + REG[1] = REG[1] | 0x02; + break; + default: + ucChannel = tmp.toUInt(); + REG[3] = ucChannel; + break; + } + + unsigned char ucFun1 = 0; + //EN_TADC + if (ui->checkBox->isChecked()) { + ucFun1 = ucFun1 | 0x01; + REG[1] = REG[1] | 0x80; + } + //FBK_EN + if (ui->checkBox_2->isChecked()) { + ucFun1 = ucFun1 | 0x02; + REG[1] = REG[1] | 0x01; + + } + //TEMPTEST_EN + if (ui->checkBox_4->isChecked()) { + ucFun1 = ucFun1 | 0x04; + REG[2] = REG[2] | 0x80; + qDebug() << REG[2]; + } + //TEST_EN + if (ui->checkBox_3->isChecked()) { + ucFun1 = ucFun1 | 0x08; + REG[2] = REG[2] | 0x20; + } + //TRIG_TADC + if (ui->checkBox_5->isChecked()) { + ucFun1 = ucFun1 | 0x10; + REG[2] = REG[2] | 0x40; + } + + unsigned char ucFun2 = 0; + ucFun2 = ucFun2 | ui->comboBox_12->currentText().toUInt(NULL, 10); //N_CLKDIV18 + ucFun2 = ucFun2 | (ui->comboBox_14->currentText().toUInt(NULL, 2) << 4); //ICON8 + REG[1] = REG[1] | (ui->comboBox_12->currentText().toUInt(NULL, 10) << 5); + REG[2] = REG[2] | (ui->comboBox_14->currentText().toUInt(NULL, 2)); + + QString sendRegData; + for (auto i : REG) { + sendRegData += "0x" + QString::number(i, 16) + " "; + } + onLogPrint("Ready to write REG[1] - REG[4] : " + sendRegData); + + QByteArray tmp1 = fHandler->genREGWrite(ucDAC, ucChannel, ucFun1, ucFun2); + switch (ui->comboBox->currentIndex()) { + case 0: + if (drvUart->Uart_Write(tmp1)) { + onLogPrint("Send data success"); + } else { + onLogPrint("Send data fail"); + } + break; + case 1: + onLogPrint("write data is " + tmp1.toHex()); + if (drvSPI->spi_Write(tmp1)) { + onLogPrint("Send data success"); + } else { + onLogPrint("Send data failed"); + } + break; + } + + } - -void MainWindow::on_pushButton_10_clicked() -{ - QByteArray tmp = fHandler->genREGRead(0x01); - qDebug()<comboBox_15->currentText().toUInt(NULL, 10); + QByteArray tmp = fHandler->genREGRead(ucDAC); + switch (ui->comboBox->currentIndex()) { + case 0: + if (drvUart->Uart_Write(tmp)) { + onLogPrint("Send data success"); + } else { + onLogPrint("Send data fail"); + } + break; + case 1: + onLogPrint("write data is " + tmp.toHex()); + if (drvSPI->spi_Write(tmp)) { + onLogPrint("Send data success"); + QTimer::singleShot(1000, [this]() { + QByteArray ba; + drvSPI->spi_Read(ba); + ui->textBrowser->append("SPI read: " + ba.toHex()); + }); + } else { + onLogPrint("Send data fail"); + } + break; + } +} + +void MainWindow::on_btnSpiSaveSetting_clicked() { + drvSPI->spi_Init( + ui->comboSpiMode->currentIndex(), + ui->comboSpiClock->currentIndex(), + ui->comboSpiByteOrder->currentIndex(), + ui->comboSpiDataBits->currentIndex(), + ui->comboSpiCS1Pol->currentIndex(), + ui->comboSpiCS2Pol->currentIndex() + ); +} + +void MainWindow::onWriteRegButtonClicked() { + QByteArray tmp = fHandler->genREGWrite(0x01, 0x01, 0x09, 0x81); + ui->textBrowser->append("SPI write: " + tmp.toHex()); + drvSPI->spi_Write(tmp); +} + +void MainWindow::onReadRegButtonClicked() { + QByteArray tmp = fHandler->genREGRead(0x01); + ui->textBrowser->append("SPI write: " + tmp.toHex()); + QTimer::singleShot(2000, [this]() { + QByteArray ba; + drvSPI->spi_Read(ba); + ui->textBrowser->append("SPI read: " + ba.toHex()); + }); +} + +void MainWindow::on_comboBox_currentTextChanged(const QString& text) { +// if (text.toLower() == "spi") { +// connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked); +// connect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked); +// } else { +// disconnect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked); +// disconnect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked); +// } } diff --git a/mainwindow.h b/mainwindow.h index c3ab6f7..14081e1 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,7 +6,7 @@ #include "src/headers/drv_uart.h" #include "src/headers/drv_spi.h" #include "QDateTime" -#include "src/headers/framehandler.h" +#include "src/headers/framehandler.h"#include "src/headers/framehandler.h" @@ -53,6 +53,12 @@ private slots: void on_pushButton_10_clicked(); + void on_btnSpiSaveSetting_clicked(); + void onWriteRegButtonClicked(); + void onReadRegButtonClicked(); + + void on_comboBox_currentTextChanged(const QString& text); + private: Ui::MainWindow *ui; DialogCalibrate *dc; @@ -60,6 +66,8 @@ private: DRV_Spi *drvSPI; FrameHandler *fHandler; + FrameHandler *fHandler; + }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 0a2762c..68ddaea 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -90,7 +90,7 @@ Timeout(ms): - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter @@ -149,7 +149,7 @@ Data Bits: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter @@ -159,7 +159,7 @@ Stop Bits: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter @@ -258,7 +258,7 @@ Baud Rate: - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter @@ -268,7 +268,7 @@ Parity Bits: - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter @@ -288,7 +288,7 @@ UART Device List: - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter @@ -311,15 +311,18 @@ SPI Device List: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter - + false + + 1 + LSB @@ -333,7 +336,7 @@ - + false @@ -355,12 +358,15 @@ SPI Mode: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter - + + + false + 0 @@ -383,10 +389,13 @@ - + false + + 1 + 60MHz @@ -435,7 +444,7 @@ Clock: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter @@ -445,12 +454,12 @@ LSB/MSB - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter - + false @@ -482,12 +491,12 @@ DataBits: - Qt::AlignCenter + Qt::AlignmentFlag::AlignCenter - + false @@ -504,7 +513,7 @@ - + false @@ -632,10 +641,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::MinimumExpanding + QSizePolicy::Policy::MinimumExpanding @@ -1091,7 +1100,7 @@ 0 0 839 - 21 + 33 diff --git a/src/headers/drv_spi.h b/src/headers/drv_spi.h index b130ca1..e6a1843 100644 --- a/src/headers/drv_spi.h +++ b/src/headers/drv_spi.h @@ -57,7 +57,8 @@ public: bool spi_openDevice(ULONG SpiI2cGpioDevIndex); // 打开设备 bool spi_closeDevice(); // 关闭设备 bool spi_Write(QByteArray &sendData); // SPI写数据 - BOOL spi_Read(QByteArray &revData); // SPI读数据 + bool spi_Read(QByteArray &revData); // SPI读数据 + bool spi_Init(int mode, int clock, int byteOrder, int CS1Pol, int CS2Pol, int dataBits); /*************变量定义*************/ mDRV_Spi_Infors m_DRV_Spi_Infors; //Spi驱动相关信息 diff --git a/src/sources/drv_spi.cpp b/src/sources/drv_spi.cpp index 4fe6cdb..46e6419 100644 --- a/src/sources/drv_spi.cpp +++ b/src/sources/drv_spi.cpp @@ -79,7 +79,6 @@ bool DRV_Spi::spi_openDevice(ULONG SpiI2cGpioDevIndex) // 设置当前打开设备的索引号 this->m_DRV_Spi_Infors.opendDevIndex = SpiI2cGpioDevIndex; return true; -// CH347InitSpi(); } //关闭设备 @@ -107,35 +106,67 @@ bool DRV_Spi::spi_Write(QByteArray &sendData) // 获取数据包的长度 ULONG OutLen = sendData.length(); // 将QByteArray转化为UCHAR - const UCHAR *OutBuf = reinterpret_cast(sendData.constData()); + UCHAR *OutBuf = reinterpret_cast(sendData.data()); BOOL RetVal = FALSE; // 调用底层串口发送函数发送数据 - RetVal = CH347SPI_Write(this->m_DRV_Spi_Infors.opendDevIndex, SPI_WRITE_CS, OutLen, SPI_WRITE_BLOCK_SIZE, (UCHAR *)OutBuf); + //RetVal = CH347SPI_Write(this->m_DRV_Spi_Infors.opendDevIndex, SPI_WRITE_CS, OutLen, SPI_WRITE_BLOCK_SIZE, (UCHAR *)OutBuf); + RetVal = CH347SPI_WriteRead(this->m_DRV_Spi_Infors.opendDevIndex, 0x80, sendData.count(), OutBuf); // 日志窗口输出调试信息 - DbgPrint("frame:%d,Spi_Write %dBytes %s.",sendData.at(14), OutLen,RetVal?"succ":"failure"); + DbgPrint("Spi_Write %dBytes %s.", OutLen,RetVal?"succ":"failure"); - return RetVal; + return RetVal ? true : false; } -BOOL DRV_Spi::spi_Read(QByteArray &revData) +bool DRV_Spi::spi_Read(QByteArray &revData) { - ULONG InLen = 512; // 设置读取的长度,读取完后会将该值赋值为真实的长度 - ULONG OutLen = 0; // 设置读取的长度,读取完后会将该值赋值为真实的长度 - UCHAR InBuf[512] = {0x00}; + ULONG InLen = 7; +// ULONG OutLen = 0; +// UCHAR InBuf[512] = {0x00}; + revData.clear(); + revData.append(InLen, 0x00); BOOL RetVal = FALSE; // 读取串口数据 - RetVal = CH347SPI_Read(this->m_DRV_Spi_Infors.opendDevIndex, SPI_READ_CS, OutLen, &InLen, InBuf); + //RetVal = CH347SPI_Read(this->m_DRV_Spi_Infors.opendDevIndex, SPI_READ_CS, OutLen, &InLen, InBuf); + RetVal = CH347SPI_WriteRead(this->m_DRV_Spi_Infors.opendDevIndex, 0x80, InLen, revData.data()); - DbgPrint("CH347Spi_Read %dB %s.",InLen,RetVal?"succ":"failure"); + DbgPrint("CH347Spi_Read %dB %s.", InLen, RetVal ? "succ" : "failure"); - if(RetVal) - { - //将数据存入revData变量中,返回给调用的函数 - revData = QByteArray::fromRawData(reinterpret_cast(InBuf), InLen); - } - return RetVal; +// if(RetVal) +// { +// //将数据存入revData变量中,返回给调用的函数 +// revData = QByteArray::fromRawData(static_cast(InBuf), InLen); +// } + return RetVal ? true : false; +} + +bool DRV_Spi::spi_Init(int mode, int clock, int byteOrder, int CS1Pol, int CS2Pol, int dataBits) { + mSpiCfgS spiCon; + BOOL rv = FALSE; + rv = CH347SPI_GetCfg(m_DRV_Spi_Infors.opendDevIndex, &spiCon); + DbgPrint("CH347SPI_GetCfg %s.", rv ? "succ" : "failure"); + if (rv == FALSE) + return false; + spiCon.iMode = mode; + spiCon.iClock = clock; + spiCon.iByteOrder = byteOrder; + spiCon.iSpiOutDefaultData = 0xFF; + spiCon.iChipSelect = 0x80; + spiCon.CS1Polarity = CS1Pol; + spiCon.CS2Polarity = CS2Pol; + spiCon.iIsAutoDeativeCS = 0; + spiCon.iActiveDelay = 0; + spiCon.iDelayDeactive = 0; + rv = CH347SPI_Init(m_DRV_Spi_Infors.opendDevIndex, &spiCon); + DbgPrint("CH347SPI_Init %s.", rv ? "succ" : "failure"); + if (rv == FALSE) + return false; + rv = CH347SPI_SetDataBits(m_DRV_Spi_Infors.opendDevIndex, dataBits); + DbgPrint("CH347SPI_SetDataBits %s.", rv ? "succ" : "failure"); + if (rv == FALSE) + return false; + return true; } diff --git a/voltageTmp/voltagetmp_0.csv b/voltageTmp/voltagetmp_0.csv new file mode 100644 index 0000000..9eeccff --- /dev/null +++ b/voltageTmp/voltagetmp_0.csv @@ -0,0 +1,16 @@ +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 diff --git a/voltageTmp/voltagetmp_random.csv b/voltageTmp/voltagetmp_random.csv new file mode 100644 index 0000000..f28b470 --- /dev/null +++ b/voltageTmp/voltagetmp_random.csv @@ -0,0 +1,16 @@ +291,147,51,117,295,144,54,7,270,81,106,163,18,79,45,244 +170,20,292,109,222,298,184,276,249,163,35,141,99,301,148,80 +106,215,5,77,264,239,111,242,145,50,316,118,2,134,204,228 +133,33,240,317,85,32,90,318,275,247,0,234,155,153,216,145 +235,257,73,162,249,42,249,41,23,49,199,104,259,55,103,240 +163,58,212,255,306,186,192,168,317,199,196,213,192,131,178,310 +231,3,262,95,146,14,154,212,251,76,38,147,19,186,142,170 +56,67,296,21,313,93,48,272,302,28,176,27,145,46,296,272 +77,94,311,50,74,275,78,267,151,226,264,191,1,27,141,233 +253,114,2,170,149,46,109,154,212,150,307,65,194,281,134,0 +237,240,185,65,160,287,275,204,91,107,164,300,187,287,107,213 +160,136,114,175,93,77,255,69,217,9,310,59,85,106,275,173 +66,19,126,263,284,72,61,234,225,273,286,34,300,268,177,205 +24,52,165,242,95,79,72,288,224,198,149,313,12,266,149,126 +273,34,55,189,292,139,136,98,288,160,29,232,225,164,197,107 +146,310,19,24,11,118,92,181,283,107,52,122,253,8,87,286