From 5a66c64a59fcf6ef7f0cd25174527afa2c8bd0d5 Mon Sep 17 00:00:00 2001 From: skythinker Date: Mon, 31 Jan 2022 00:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E5=B0=86OpenOCD?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E6=8E=A7=E5=88=B6=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E5=8F=96=E4=B8=BA=E4=B8=80=E4=B8=AA=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LinkScope.pro | 4 +++- LinkScope.pro.user | 2 +- mainwindow.cpp | 52 +++++++++------------------------------------- mainwindow.h | 6 ++++-- openocd.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ openocd.h | 26 +++++++++++++++++++++++ 6 files changed, 91 insertions(+), 46 deletions(-) create mode 100644 openocd.cpp create mode 100644 openocd.h diff --git a/LinkScope.pro b/LinkScope.pro index 3a2421c..1a358f1 100644 --- a/LinkScope.pro +++ b/LinkScope.pro @@ -24,7 +24,8 @@ SOURCES += \ helpwindow.cpp \ listwindow.cpp \ main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + openocd.cpp HEADERS += \ aboutwindow.h \ @@ -33,6 +34,7 @@ HEADERS += \ helpwindow.h \ listwindow.h \ mainwindow.h \ + openocd.h \ vartype.h FORMS += \ diff --git a/LinkScope.pro.user b/LinkScope.pro.user index 948f7e3..42c9372 100644 --- a/LinkScope.pro.user +++ b/LinkScope.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/mainwindow.cpp b/mainwindow.cpp index 47016d9..0da7d3e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -35,8 +35,11 @@ MainWindow::MainWindow(QWidget *parent) tableModel=new QStandardItemModel(this);//创建并初始化表格 initTable(); - ocdProcess=new QProcess(0);//创建openocd进程 - connect(ocdProcess,SIGNAL(readyReadStandardError()),this,SLOT(slotOCDErrorReady())); + openocd=new OpenOCD(); + connect(openocd,&OpenOCD::onErrorOccur,[=](const QString &info){//OpenOCD发生连接错误,断开连接并弹框 + setConnState(false); + QMessageBox::information(this,"连接错误",info); + }); gdb=new GDBProcess();//创建并启动GDB gdb->setTempSymbolFileName("tmp");//设定临时符号文件名 @@ -51,11 +54,12 @@ MainWindow::~MainWindow() { gdb->disconnectFromRemote(); gdb->unloadSymbolFile(); + openocd->stop(); } gdb->stop();//结束gdb进程 delete ui; delete tableModel; - delete ocdProcess; + delete openocd; delete gdb; delete stampTimer; delete watchTimer; @@ -95,17 +99,6 @@ void MainWindow::closeEvent(QCloseEvent *event) listWindow->close(); } -//ocd进程发生错误,自动断开连接 -void MainWindow::slotOCDErrorReady() -{ - QString error=ocdProcess->readAllStandardError(); - if(error.contains("Error:"))//错误信息中含有Error项,则直接断开连接并弹出提示 - { - setConnState(false); - QMessageBox::information(this,"连接错误",error); - } -} - //表格被编辑,添加变量或修改变量值 void MainWindow::slotTableEdit(QModelIndex topleft, QModelIndex bottomright) { @@ -223,9 +216,9 @@ void MainWindow::setConnState(bool connect) if(connect)//进行连接 { ui->bt_conn->setEnabled(false);//先禁用连接按钮,防止多次点击 - setOCDState(true);//运行openocd进程进行目标连接 + openocd->start(ui->cb_interface->currentText(),ui->cb_target->currentText(),3333);//运行openocd进程进行目标连接 sleep(500);//等待500ms - if(ocdProcess->state()==QProcess::Running)//若ocd成功启动 + if(openocd->isRunning())//若ocd成功启动 { gdb->loadSymbolFile(ui->txt_axf_path->text());//设置gdb符号文件 gdb->connectToRemote("localhost:3333");//连接gdb到ocd @@ -254,38 +247,13 @@ void MainWindow::setConnState(bool connect) tableTimer->stop(); gdb->disconnectFromRemote();//断开gdb gdb->unloadSymbolFile();//卸载符号文件 - setOCDState(false);//结束ocd进程 + openocd->stop(); ui->bt_conn->setText("连接目标"); ui->bt_reset->setEnabled(false);//禁用复位按钮 connected=false;//更新连接标志 } } -//参数true:运行ocd进程并连接目标;参数false:结束ocd进程 -void MainWindow::setOCDState(bool connect) -{ - if(connect) - { - ocdProcess->setWorkingDirectory(QCoreApplication::applicationDirPath()+"/openocd/bin");//设置工作路径 - ocdProcess->setProgram(QCoreApplication::applicationDirPath()+"/openocd/bin/openocd.exe");//设置程序路径 - ocdProcess->setNativeArguments( - QString("-f interface/%1 -f target/%2") - .arg(ui->cb_interface->currentText()) - .arg(ui->cb_target->currentText()));//设置参数为所选的调试器和目标芯片 - ocdProcess->start(); - } - else - { - if(ocdProcess->state()==QProcess::NotRunning) - return; - QProcess killProcess(0);//创建新进程,用taskkill强行结束ocd进程 - killProcess.setProgram("taskkill"); - killProcess.setNativeArguments(QString("/F /PID %1").arg(ocdProcess->pid()->dwProcessId)); - killProcess.start(); - killProcess.waitForFinished(); - } -} - //延时指定毫秒数,进行子任务循环 void MainWindow::sleep(uint32_t ms) { diff --git a/mainwindow.h b/mainwindow.h index beac909..ad18e43 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -25,6 +25,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -41,7 +42,7 @@ public: void closeEvent(QCloseEvent *event); private slots: - void slotOCDErrorReady(); + //void slotOCDErrorReady(); void slotTableEdit(QModelIndex topleft, QModelIndex bottomright); void slotWatchTimerTrig(); void slotTableTimerTrig(); @@ -68,6 +69,7 @@ private: Ui::MainWindow *ui; QProcess *ocdProcess; GDBProcess *gdb;//GDB进程控制 + OpenOCD *openocd;//OpenOCD进程控制 bool connected=false;//标记当前是否已连接 QStandardItemModel *tableModel;//表格数据 QList varList;//变量列表 @@ -80,7 +82,7 @@ private: void checkUpdate(); void setStylesheet(); void setConnState(bool connect); - void setOCDState(bool connect); + //void setOCDState(bool connect); void sleep(uint32_t ms); void loadConfFileList(); void initTable(); diff --git a/openocd.cpp b/openocd.cpp new file mode 100644 index 0000000..4d6e8b7 --- /dev/null +++ b/openocd.cpp @@ -0,0 +1,47 @@ +#include "openocd.h" + +OpenOCD::OpenOCD(QObject *parent) : QObject(parent) +{ + process=new QProcess(); + connect(process,&QProcess::readyReadStandardError,[=]{ + QString error=process->readAllStandardError(); + if(error.contains("Error:"))//错误信息中含有Error项,则发送信号 + { + emit onErrorOccur(error); + } + }); +} + +OpenOCD::~OpenOCD() +{ + delete process; +} + +//启动OCD进程并设定调试器类型、芯片类型、gdb端口号 +void OpenOCD::start(const QString &chosenInterface,const QString &chosenTarget,int gdbPort) +{ + process->setWorkingDirectory(QCoreApplication::applicationDirPath()+"/openocd/bin");//设置工作路径 + process->setProgram(QCoreApplication::applicationDirPath()+"/openocd/bin/openocd.exe");//设置程序路径 + process->setNativeArguments( + QString("-f interface/%1 -f target/%2 -c \"gdb_port %3\"") + .arg(chosenInterface).arg(chosenTarget).arg(gdbPort));//设置参数为所选的调试器、目标芯片和gdb端口 + process->start(); +} + +//结束OCD进程 +void OpenOCD::stop() +{ + if(process->state()==QProcess::NotRunning) + return; + QProcess killProcess(0);//创建新进程,用taskkill强行结束ocd进程 + killProcess.setProgram("taskkill"); + killProcess.setNativeArguments(QString("/F /PID %1").arg(process->pid()->dwProcessId)); + killProcess.start(); + killProcess.waitForFinished(); +} + +//返回当前OCD进程是否正在运行 +bool OpenOCD::isRunning() +{ + return process->state()==QProcess::Running; +} diff --git a/openocd.h b/openocd.h new file mode 100644 index 0000000..589c50b --- /dev/null +++ b/openocd.h @@ -0,0 +1,26 @@ +#ifndef OPENOCD_H +#define OPENOCD_H + +#include +#include +#include +#include + +class OpenOCD : public QObject +{ + Q_OBJECT +public: + explicit OpenOCD(QObject *parent = nullptr); + ~OpenOCD(); + void start(const QString &chosenInterface,const QString &chosenTarget,int gdbPort); + void stop(); + bool isRunning(); + +signals: + void onErrorOccur(const QString &info); + +private: + QProcess *process; +}; + +#endif // OPENOCD_H