diff --git a/APBUARTPLUGIN/APBUARTPLUGIN.cpp b/APBUARTPLUGIN/APBUARTPLUGIN.cpp
--- a/APBUARTPLUGIN/APBUARTPLUGIN.cpp
+++ b/APBUARTPLUGIN/APBUARTPLUGIN.cpp
@@ -24,12 +24,18 @@
ApbUartPlugin::ApbUartPlugin(QWidget *parent):socexplorerplugin(parent)
{
+ this->setBaseAddress(-1);
this->UI = new APBUART_Plugin_ui();
this->setWidget((QWidget*)this->UI);
- this->useLoopBack = false;
+ this->uartConnected = false;
+ this->UartThread = new UARTPollingThread(this);
connect(this->UI,SIGNAL(loopbackChkBxStateChanged(int)),this,SLOT(loopbackChangeState(int)));
- connect(this,SIGNAL(apbUartTextReceived(QString)),this->UI,SIGNAL(apbUartTextReceived(QString)));
- connect(&this->loopBackTimer,SIGNAL(timeout()),this,SLOT(uartReadout()));
+ connect(this->UartThread,SIGNAL(apbUartTextReceived(QString)),this->UI,SIGNAL(apbUartTextReceived(QString)));
+ connect(this->UI,SIGNAL(connectPort()),this,SLOT(toggleUartState()));
+ connect(this->UI,SIGNAL(sendChar(char)),this->UartThread,SLOT(sendChar(char)));
+ connect(this->UI,SIGNAL(PortNameChanged(QString)),this->UartThread,SLOT(setPortName(QString)));
+ connect(this->UI,SIGNAL(UartSpeedChanged(QString)),this->UartThread,SLOT(setPortSpeedStr(QString)));
+ this->UartThread->start();
}
@@ -43,137 +49,34 @@ void ApbUartPlugin::closeMe()
emit this->closePlugin(this);
}
-void ApbUartPlugin::postInstantiationTrigger()
+void ApbUartPlugin::toggleUartState()
{
- if(this->isEnabled())
+ if(!uartConnected)
{
- this->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this,this->VID(),this->PID(),0));
- loopbackChangeState(Qt::Checked);
+ uartConnected = this->UartThread->openUart();
}
+ else
+ {
+ this->UartThread->closeUart();
+ this->uartConnected = false;
+ }
+ this->UI->setUartConnected(uartConnected);
}
void ApbUartPlugin::loopbackChangeState(int state)
{
if(state==Qt::Checked)
{
- enableLoopback();
+ this->UI->setEnableForLoopBack(false);
+ this->UartThread->setFifoDebugEable(true);
}
else
{
- disableLoopback();
- }
-}
-
-void ApbUartPlugin::uartReadout()
-{
-#ifdef WIN32
- int readcnt=0;
-#endif
- if(this->isEnabled() && parent!=NULL)
- {
- if(this->useLoopBack)
- {
- if(this->baseAddress()!=-1)
- {
- this->loopBackTimer.stop();
- unsigned int status_reg,data;
- char ch;
- QString printdata="";
- parent->Read(&status_reg,1,this->baseAddress()+APB_UART_STATUS_REG);
- while ((status_reg&4)==0) {
- parent->Read(&data,1,this->baseAddress()+APB_UART_FIFO_DEBUG_REG);
- ch = (char)(0xff & data);
- printdata+=ch;
-#ifdef WIN32
- readcnt++;
- if(readcnt>=32)
- {
- qApp->processEvents();
- break;
- }
-#endif
- parent->Read(&status_reg,1,this->baseAddress()+APB_UART_STATUS_REG);
- }
- if(printdata!="")
- emit apbUartTextReceived(printdata);
- this->loopBackTimer.start(200);
- }
- else
- {
- this->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this,this->VID(),this->PID(),0));
- this->enableLoopback();
- }
- }
- }
-}
-
-void ApbUartPlugin::activate(bool flag)
-{
- this->setEnabled(flag);
- emit this->activateSig(flag);
- if(this->isEnabled())
- {this->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this,this->VID(),this->PID(),0));
-
- loopbackChangeState(Qt::Checked);
+ this->UI->setEnableForLoopBack(true);
+ this->UartThread->setFifoDebugEable(false);
}
}
-void ApbUartPlugin::activateScan(bool flag)
-{
- if(flag)
- this->loopBackTimer.start(200);
- else
- this->loopBackTimer.stop();
-}
-
-int ApbUartPlugin::enableLoopback()
-{
-
- this->useLoopBack = true;
- this->loopBackTimer.start(200);
- SocExplorerEngine::message(this,"Set FiFo debug mode mode");
- if(parent==NULL)
- {
- SocExplorerEngine::message(this,"Can't set FiFo debug mode no parent driver accessible");
- return -1;
- }
- if(this->baseAddress()==-1)
- {
- this->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this,this->VID(),this->PID(),0));
- if(this->baseAddress()==-1)
- return -1;
- }
- unsigned int ctrl_reg= 0x843;
- parent->Write(&ctrl_reg,1,this->baseAddress()+APB_UART_CONTROL_REG);
- this->UI->setEnableForLoopBack(false);
- return 0;
-}
-
-int ApbUartPlugin::disableLoopback()
-{
- SocExplorerEngine::message(this,"Disable FiFo debug mode mode");
- if(parent==NULL)
- {
- SocExplorerEngine::message(this,"Can't disable FiFo debug mode no parent driver accessible");
- return -1;
- }
- if(this->baseAddress()==-1)
- {
- this->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this,this->VID(),this->PID(),0));
- if(this->baseAddress()==-1)
- return -1;
- }
- unsigned int ctrl_reg;
- this->loopBackTimer.stop();
- /* Firts get Control reg value*/
- parent->Read(&ctrl_reg,1,this->baseAddress()+APB_UART_CONTROL_REG);
- ctrl_reg = ctrl_reg & (~(1<<11));
- parent->Write(&ctrl_reg,1,this->baseAddress()+APB_UART_CONTROL_REG);
- this->useLoopBack = false;
- this->UI->setEnableForLoopBack(true);
- return 0;
-}
-
unsigned int ApbUartPlugin::Read(unsigned int *Value,unsigned int count,unsigned int address)
{
diff --git a/APBUARTPLUGIN/APBUARTPLUGIN.h b/APBUARTPLUGIN/APBUARTPLUGIN.h
--- a/APBUARTPLUGIN/APBUARTPLUGIN.h
+++ b/APBUARTPLUGIN/APBUARTPLUGIN.h
@@ -48,18 +48,15 @@ public:
public slots:
unsigned int Write(unsigned int *Value,unsigned int count, unsigned int address=0);
unsigned int Read(unsigned int *Value,unsigned int count, unsigned int address=0);
+ void loopbackChangeState(int state);
void closeMe();
- void postInstantiationTrigger();
- void loopbackChangeState( int state );
- void uartReadout();
- void activateScan(bool flag);
- void activate(bool flag);
+ void toggleUartState();
signals:
- void apbUartTextReceived(QString text);
+
private:
APBUART_Plugin_ui* UI;
- bool useLoopBack;
- QTimer loopBackTimer;
+ bool uartConnected;
+ UARTPollingThread* UartThread;
int enableLoopback();
int disableLoopback();
};
diff --git a/APBUARTPLUGIN/apbuart_plugin_ui.cpp b/APBUARTPLUGIN/apbuart_plugin_ui.cpp
--- a/APBUARTPLUGIN/apbuart_plugin_ui.cpp
+++ b/APBUARTPLUGIN/apbuart_plugin_ui.cpp
@@ -21,6 +21,7 @@
----------------------------------------------------------------------------*/
#include "apbuart_plugin_ui.h"
#include "ui_apbuart_plugin_ui.h"
+#include "apbuartterminal.h"
APBUART_Plugin_ui::APBUART_Plugin_ui(QWidget *parent) :
QWidget(parent),
@@ -29,6 +30,10 @@ APBUART_Plugin_ui::APBUART_Plugin_ui(QWi
ui->setupUi(this);
connect(this->ui->FIFODebugChkBx,SIGNAL(stateChanged(int)),this,SIGNAL(loopbackChkBxStateChanged(int)));
connect(this,SIGNAL(apbUartTextReceived(QString)),this->ui->UART_TERM,SLOT(append(QString)));
+ connect(this->ui->ConnectQpb,SIGNAL(clicked()),this,SIGNAL(connectPort()));
+ connect(this->ui->UART_TERM,SIGNAL(sendChar(char)),this,SIGNAL(sendChar(char)));
+ connect(this->ui->PortNameLineEdit,SIGNAL(textChanged(QString)),this,SIGNAL(PortNameChanged(QString)));
+ connect(this->ui->UartSpeedLineEdit,SIGNAL(textChanged(QString)),this,SIGNAL(UartSpeedChanged(QString)));
}
APBUART_Plugin_ui::~APBUART_Plugin_ui()
@@ -42,3 +47,18 @@ void APBUART_Plugin_ui::setEnableForLoop
this->ui->ConnectQpb->setEnabled(enable);
this->ui->UartSpeedLineEdit->setEnabled(enable);
}
+
+void APBUART_Plugin_ui::setUartConnected(bool enable)
+{
+ this->ui->PortNameLineEdit->setDisabled(enable);
+ this->ui->UartSpeedLineEdit->setDisabled(enable);
+ this->ui->FIFODebugChkBx->setDisabled(enable);
+ if(enable)
+ {
+ this->ui->ConnectQpb->setText("Close Port");
+ }
+ else
+ {
+ this->ui->ConnectQpb->setText("Open Port");
+ }
+}
diff --git a/APBUARTPLUGIN/apbuart_plugin_ui.h b/APBUARTPLUGIN/apbuart_plugin_ui.h
--- a/APBUARTPLUGIN/apbuart_plugin_ui.h
+++ b/APBUARTPLUGIN/apbuart_plugin_ui.h
@@ -38,10 +38,14 @@ public:
public slots:
void setEnableForLoopBack(bool enable);
+ void setUartConnected(bool enable);
signals:
void loopbackChkBxStateChanged( int state );
void apbUartTextReceived(QString text);
-
+ void connectPort();
+ void sendChar(char c);
+ void UartSpeedChanged(QString text);
+ void PortNameChanged(QString text);
private:
Ui::APBUART_Plugin_ui *ui;
};
diff --git a/APBUARTPLUGIN/apbuart_plugin_ui.ui b/APBUARTPLUGIN/apbuart_plugin_ui.ui
--- a/APBUARTPLUGIN/apbuart_plugin_ui.ui
+++ b/APBUARTPLUGIN/apbuart_plugin_ui.ui
@@ -17,7 +17,7 @@
-
- 0
+ 1
@@ -69,7 +69,7 @@
-
-
+
@@ -77,6 +77,13 @@
+
+
+ ApbUartTerminal
+ QTextEdit
+
+
+
diff --git a/APBUARTPLUGIN/apbuartterminal.cpp b/APBUARTPLUGIN/apbuartterminal.cpp
--- a/APBUARTPLUGIN/apbuartterminal.cpp
+++ b/APBUARTPLUGIN/apbuartterminal.cpp
@@ -25,4 +25,11 @@ ApbUartTerminal::ApbUartTerminal(QWidget
QTextEdit(parent)
{
connect(this,SIGNAL(apbUartTextReceived(QString)),this,SLOT(append(QString)));
+ setReadOnly(true);
}
+
+void ApbUartTerminal::keyPressEvent(QKeyEvent *e)
+{
+ emit this->sendChar((char)e->key());
+ e->accept();
+}
diff --git a/APBUARTPLUGIN/apbuartterminal.h b/APBUARTPLUGIN/apbuartterminal.h
--- a/APBUARTPLUGIN/apbuartterminal.h
+++ b/APBUARTPLUGIN/apbuartterminal.h
@@ -24,18 +24,22 @@
#include
#include
-
+#include
class ApbUartTerminal : public QTextEdit
{
Q_OBJECT
public:
explicit ApbUartTerminal(QWidget *parent = 0);
-
+
+protected:
+ void keyPressEvent(QKeyEvent * e);
+
signals:
void apbUartTextReceived(QString text);
+ void sendChar(char c);
public slots:
-
+
};
#endif // APBUARTTERMINAL_H
diff --git a/APBUARTPLUGIN/uartpollingthread.cpp b/APBUARTPLUGIN/uartpollingthread.cpp
--- a/APBUARTPLUGIN/uartpollingthread.cpp
+++ b/APBUARTPLUGIN/uartpollingthread.cpp
@@ -20,45 +20,67 @@
-- Mail : alexis.jeandet@member.fsf.org
----------------------------------------------------------------------------*/
#include "uartpollingthread.h"
-#include
#include
UARTPollingThread::UARTPollingThread(socexplorerplugin *parent) :
QThread((QObject*)parent)
{
this->plugin = parent;
- rs232open("/dev/ttyUS0");
+ uartMutex = new QMutex();
+ uartOpened = false;
+ fifoDebugConfigured = false;
+ this->moveToThread(this);
}
void UARTPollingThread::run()
{
+
+ SocExplorerEngine::message(this->plugin,"Entering APB UART polling thread",3);
while (!this->isInterruptionRequested())
{
if(fifoDebugEnabled)
{
- if(this->plugin->baseAddress()!=-1)
+ if(fifoDebugConfigured)
{
- unsigned int status_reg,data;
- char ch;
- QString printdata="";
- plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG);
- while ((status_reg&4)==0) {
- plugin->parent->Read(&data,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG);
- ch = (char)(0xff & data);
- printdata+=ch;
+ if(this->plugin->baseAddress()!=-1)
+ {
+ unsigned int status_reg,data;
+ char ch;
+ QString printdata="";
plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG);
+ while ((status_reg&4)==0) {
+ plugin->parent->Read(&data,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG);
+ ch = (char)(0xff & data);
+ printdata+=ch;
+ plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG);
+ }
+ if(printdata!="")
+ emit apbUartTextReceived(printdata);
}
- if(printdata!="")
- emit apbUartTextReceived(printdata);
+ else
+ {
+ this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0));
+ }
}
else
{
- this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0));
+ configFifoDebug(true);
}
}
- if(uartOpened)
+ else
{
-
+ int read =0;
+ char ch;
+ uartMutex->lock();
+ if(uartOpened)
+ {
+ read =rs232read(this->uart,&ch,1);
+ }
+ uartMutex->unlock();
+ if(read==1)
+ {
+ emit this->sendChar(ch);
+ }
}
msleep(100);
}
@@ -66,5 +88,98 @@ void UARTPollingThread::run()
void UARTPollingThread::sendChar(char c)
{
+ if(fifoDebugEnabled)
+ {
+ if(this->plugin->baseAddress()!=-1)
+ {
+ unsigned int i=0x0FF & c;
+ plugin->parent->Write(&i,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG);
+ }
+ }
+ else
+ {
+ uartMutex->lock();
+ rs232write(this->uart,&c,1);
+ uartMutex->unlock();
+ }
+}
+bool UARTPollingThread::openUart()
+{
+ if(uartOpened)
+ {
+ closeUart();
+ }
+ this->uart = rs232open((char*)this->portName.toStdString().c_str());
+ if(this->uart!=badPortValue)
+ {
+ rs232setup(this->uart,8,this->uartSpeed,rs232parityNo,rs232OneStop);
+ uartOpened = true;
+ }
+ return uartOpened;
}
+
+void UARTPollingThread::closeUart()
+{
+ uartMutex->lock();
+ rs232close(this->uart);
+ uartOpened = false;
+ uartMutex->unlock();
+}
+
+void UARTPollingThread::setPortName(QString name)
+{
+ this->portName = name;
+}
+
+void UARTPollingThread::setPortSpeedStr(QString speed)
+{
+ this->uartSpeed = speed.toInt();
+}
+
+void UARTPollingThread::setPortSpeed(int speed)
+{
+ this->uartSpeed = speed;
+}
+
+void UARTPollingThread::setFifoDebugEable(bool enable)
+{
+ if(enable)
+ SocExplorerEngine::message(this->plugin,"Enabling APB UART FIFO debug mode",3);
+ else
+ SocExplorerEngine::message(this->plugin,"Disabling APB UART FIFO debug mode",3);
+ if(uartOpened && enable)
+ {
+ closeUart();
+ }
+ this->fifoDebugConfigured = false;
+ configFifoDebug(enable);
+ this->fifoDebugEnabled = enable;
+}
+
+void UARTPollingThread::configFifoDebug(bool enable)
+{
+ SocExplorerEngine::message(this->plugin,"Configuring APB UART in FIFO debug mode",3);
+ if(this->plugin->baseAddress()==-1)
+ {
+ this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0));
+ }
+ if(this->plugin->baseAddress()!=-1)
+ {
+ if(enable)
+ {
+ unsigned int ctrl_reg= 0x843;
+ this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
+ this->fifoDebugConfigured = true;
+ }
+ else
+ {
+ unsigned int ctrl_reg;
+ /* Firts get Control reg value*/
+ this->plugin->parent->Read(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
+ ctrl_reg = ctrl_reg & (~(1<<11));
+ this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
+ this->fifoDebugConfigured = true;
+ }
+ }
+}
diff --git a/APBUARTPLUGIN/uartpollingthread.h b/APBUARTPLUGIN/uartpollingthread.h
--- a/APBUARTPLUGIN/uartpollingthread.h
+++ b/APBUARTPLUGIN/uartpollingthread.h
@@ -25,6 +25,8 @@
#include
#include
#include
+#include
+#include
#define APB_UART_DATA_REG 0x0
#define APB_UART_STATUS_REG 0x4
@@ -42,12 +44,22 @@ signals:
void apbUartTextReceived(QString text);
public slots:
void sendChar(char c);
+ bool openUart();
+ void closeUart();
+ void setPortName(QString name);
+ void setPortSpeedStr(QString speed);
+ void setPortSpeed(int speed);
+ void setFifoDebugEable(bool enable);
private:
+ void configFifoDebug(bool enable);
bool fifoDebugEnabled;
+ bool fifoDebugConfigured;
QString portName;
int uartSpeed;
bool uartOpened;
socexplorerplugin* plugin;
+ rs232port_t uart;
+ QMutex* uartMutex;
};
#endif // UARTPOLLINGTHREAD_H