diff --git a/mainwindow.cpp b/mainwindow.cpp index 4e5c540..bc46921 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) @@ -177,22 +178,119 @@ 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->comboBox_2->currentIndex(), ui->comboSpiMode->currentIndex(), ui->comboSpiClock->currentIndex(), ui->comboSpiByteOrder->currentIndex(), @@ -203,31 +301,28 @@ void MainWindow::on_btnSpiSaveSetting_clicked() { } void MainWindow::onWriteRegButtonClicked() { - QByteArray ba; - ba.append(0x9F); - ba.append(0xE4); - ba.append(0x05); - ba.append(0x0B); - for (int i = 0; i < 11; ++i) { - ba.append((char)0x00); - } - ba.append(0x93); - - drvSPI->spi_Write(ba); - qDebug() << __FUNCTION__ << ba; + QByteArray tmp = fHandler->genREGWrite(0x01, 0x01, 0x09, 0x81); + ui->textBrowser->append("SPI write: " + tmp.toHex()); + drvSPI->spi_Write(tmp); } void MainWindow::onReadRegButtonClicked() { - qDebug() << __FUNCTION__; + 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); - } +// 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.ui b/mainwindow.ui index efcc3fe..68ddaea 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -1066,7 +1066,7 @@ - + Write Reg diff --git a/src/headers/drv_spi.h b/src/headers/drv_spi.h index 1934940..e6a1843 100644 --- a/src/headers/drv_spi.h +++ b/src/headers/drv_spi.h @@ -57,8 +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_Init(int devIndex, int mode, int clock, int byteOrder, int CS1Pol, int CS2Pol, int dataBits); + 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 18fdf77..46e6419 100644 --- a/src/sources/drv_spi.cpp +++ b/src/sources/drv_spi.cpp @@ -106,42 +106,46 @@ 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("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 devIndex, int mode, int clock, int byteOrder, int CS1Pol, int CS2Pol, int dataBits) { +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(devIndex, &spiCon); + rv = CH347SPI_GetCfg(m_DRV_Spi_Infors.opendDevIndex, &spiCon); DbgPrint("CH347SPI_GetCfg %s.", rv ? "succ" : "failure"); if (rv == FALSE) return false; @@ -155,11 +159,11 @@ bool DRV_Spi::spi_Init(int devIndex, int mode, int clock, int byteOrder, int CS1 spiCon.iIsAutoDeativeCS = 0; spiCon.iActiveDelay = 0; spiCon.iDelayDeactive = 0; - rv = CH347SPI_Init(devIndex, &spiCon); + rv = CH347SPI_Init(m_DRV_Spi_Infors.opendDevIndex, &spiCon); DbgPrint("CH347SPI_Init %s.", rv ? "succ" : "failure"); if (rv == FALSE) return false; - rv = CH347SPI_SetDataBits(devIndex, dataBits); + rv = CH347SPI_SetDataBits(m_DRV_Spi_Infors.opendDevIndex, dataBits); DbgPrint("CH347SPI_SetDataBits %s.", rv ? "succ" : "failure"); if (rv == FALSE) return false;