##// END OF EJS Templates
APBUART Working, need some cosmetic now.
Jeandet Alexis -
r29:8466dbc97576 default
parent child
Show More
@@ -1,64 +1,64
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 2 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #include "apbuart_plugin_ui.h"
23 23 #include "ui_apbuart_plugin_ui.h"
24 24 #include "apbuartterminal.h"
25 25
26 26 APBUART_Plugin_ui::APBUART_Plugin_ui(QWidget *parent) :
27 27 QWidget(parent),
28 28 ui(new Ui::APBUART_Plugin_ui)
29 29 {
30 30 ui->setupUi(this);
31 31 connect(this->ui->FIFODebugChkBx,SIGNAL(stateChanged(int)),this,SIGNAL(loopbackChkBxStateChanged(int)));
32 connect(this,SIGNAL(apbUartTextReceived(QString)),this->ui->UART_TERM,SLOT(append(QString)));
32 connect(this,SIGNAL(apbUartTextReceived(QString)),this->ui->UART_TERM,SLOT(apbUartTextReceived(QString)));
33 33 connect(this->ui->ConnectQpb,SIGNAL(clicked()),this,SIGNAL(connectPort()));
34 34 connect(this->ui->UART_TERM,SIGNAL(sendChar(char)),this,SIGNAL(sendChar(char)));
35 35 connect(this->ui->PortNameLineEdit,SIGNAL(textChanged(QString)),this,SIGNAL(PortNameChanged(QString)));
36 36 connect(this->ui->UartSpeedLineEdit,SIGNAL(textChanged(QString)),this,SIGNAL(UartSpeedChanged(QString)));
37 37 }
38 38
39 39 APBUART_Plugin_ui::~APBUART_Plugin_ui()
40 40 {
41 41 delete ui;
42 42 }
43 43
44 44 void APBUART_Plugin_ui::setEnableForLoopBack(bool enable)
45 45 {
46 46 this->ui->PortNameLineEdit->setEnabled(enable);
47 47 this->ui->ConnectQpb->setEnabled(enable);
48 48 this->ui->UartSpeedLineEdit->setEnabled(enable);
49 49 }
50 50
51 51 void APBUART_Plugin_ui::setUartConnected(bool enable)
52 52 {
53 53 this->ui->PortNameLineEdit->setDisabled(enable);
54 54 this->ui->UartSpeedLineEdit->setDisabled(enable);
55 55 this->ui->FIFODebugChkBx->setDisabled(enable);
56 56 if(enable)
57 57 {
58 58 this->ui->ConnectQpb->setText("Close Port");
59 59 }
60 60 else
61 61 {
62 62 this->ui->ConnectQpb->setText("Open Port");
63 63 }
64 64 }
@@ -1,89 +1,89
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>APBUART_Plugin_ui</class>
4 4 <widget class="QWidget" name="APBUART_Plugin_ui">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>400</width>
10 10 <height>300</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Form</string>
15 15 </property>
16 16 <layout class="QVBoxLayout" name="verticalLayout">
17 17 <item>
18 18 <widget class="QTabWidget" name="TabWidget">
19 19 <property name="currentIndex">
20 <number>1</number>
20 <number>0</number>
21 21 </property>
22 22 <widget class="QWidget" name="Config">
23 23 <attribute name="title">
24 24 <string>Configuration</string>
25 25 </attribute>
26 26 <layout class="QGridLayout" name="gridLayout">
27 27 <item row="1" column="1">
28 28 <widget class="QLineEdit" name="PortNameLineEdit"/>
29 29 </item>
30 30 <item row="0" column="0" colspan="2">
31 31 <widget class="QCheckBox" name="FIFODebugChkBx">
32 32 <property name="text">
33 33 <string>Enable FIFO debug mode</string>
34 34 </property>
35 35 <property name="checked">
36 <bool>true</bool>
36 <bool>false</bool>
37 37 </property>
38 38 </widget>
39 39 </item>
40 40 <item row="1" column="0">
41 41 <widget class="QLabel" name="PortNameLbl">
42 42 <property name="text">
43 43 <string>Port Name</string>
44 44 </property>
45 45 </widget>
46 46 </item>
47 47 <item row="3" column="0">
48 48 <widget class="QLabel" name="UartSpeedLbl">
49 49 <property name="text">
50 50 <string>Uart Speed</string>
51 51 </property>
52 52 </widget>
53 53 </item>
54 54 <item row="4" column="1">
55 55 <widget class="QPushButton" name="ConnectQpb">
56 56 <property name="text">
57 57 <string>Open Port</string>
58 58 </property>
59 59 </widget>
60 60 </item>
61 61 <item row="3" column="1">
62 62 <widget class="QLineEdit" name="UartSpeedLineEdit"/>
63 63 </item>
64 64 </layout>
65 65 </widget>
66 66 <widget class="QWidget" name="Terminal">
67 67 <attribute name="title">
68 68 <string>Terminal</string>
69 69 </attribute>
70 70 <layout class="QVBoxLayout" name="verticalLayout_2">
71 71 <item>
72 72 <widget class="ApbUartTerminal" name="UART_TERM"/>
73 73 </item>
74 74 </layout>
75 75 </widget>
76 76 </widget>
77 77 </item>
78 78 </layout>
79 79 </widget>
80 80 <customwidgets>
81 81 <customwidget>
82 82 <class>ApbUartTerminal</class>
83 83 <extends>QTextEdit</extends>
84 84 <header>apbuartterminal.h</header>
85 85 </customwidget>
86 86 </customwidgets>
87 87 <resources/>
88 88 <connections/>
89 89 </ui>
@@ -1,35 +1,45
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2013, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #include "apbuartterminal.h"
23 23
24 24 ApbUartTerminal::ApbUartTerminal(QWidget *parent) :
25 25 QTextEdit(parent)
26 26 {
27 connect(this,SIGNAL(apbUartTextReceived(QString)),this,SLOT(append(QString)));
27 // connect(this,SIGNAL(apbUartTextReceived(QString)),this,SLOT(append(QString)));
28 28 setReadOnly(true);
29 29 }
30 30
31 31 void ApbUartTerminal::keyPressEvent(QKeyEvent *e)
32 32 {
33 emit this->sendChar((char)e->key());
33 if((e->modifiers()==(Qt::ShiftModifier | Qt::ControlModifier)) && (e->key()==Qt::Key_L))
34 this->clear();
35 else
36 emit this->sendChar((char)e->key());
34 37 e->accept();
35 38 }
39
40 void ApbUartTerminal::apbUartTextReceived(QString text)
41 {
42 moveCursor (QTextCursor::End);
43 insertPlainText (text);
44 moveCursor (QTextCursor::End);
45 }
@@ -1,45 +1,45
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2013, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #ifndef APBUARTTERMINAL_H
23 23 #define APBUARTTERMINAL_H
24 24
25 25 #include <QWidget>
26 26 #include <QTextEdit>
27 27 #include <QKeyEvent>
28 28
29 29 class ApbUartTerminal : public QTextEdit
30 30 {
31 31 Q_OBJECT
32 32 public:
33 33 explicit ApbUartTerminal(QWidget *parent = 0);
34 34
35 35 protected:
36 36 void keyPressEvent(QKeyEvent * e);
37
37 public slots:
38 void apbUartTextReceived(QString text);
38 39 signals:
39 void apbUartTextReceived(QString text);
40 40 void sendChar(char c);
41 41 public slots:
42 42
43 43 };
44 44
45 45 #endif // APBUARTTERMINAL_H
@@ -1,185 +1,201
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 2 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #include "uartpollingthread.h"
23 23 #include <socexplorerengine.h>
24 24
25 25 UARTPollingThread::UARTPollingThread(socexplorerplugin *parent) :
26 26 QThread((QObject*)parent)
27 27 {
28 28 this->plugin = parent;
29 29 uartMutex = new QMutex();
30 30 uartOpened = false;
31 31 fifoDebugConfigured = false;
32 fifoDebugEnabled = false;
32 33 this->moveToThread(this);
33 34 }
34 35
36 UARTPollingThread::~UARTPollingThread()
37 {
38 this->requestInterruption();
39 while(isRunning());
40 }
41
35 42 void UARTPollingThread::run()
36 43 {
37 44
38 45 SocExplorerEngine::message(this->plugin,"Entering APB UART polling thread",3);
39 46 while (!this->isInterruptionRequested())
40 47 {
41 48 if(fifoDebugEnabled)
42 49 {
43 50 if(fifoDebugConfigured)
44 51 {
45 52 if(this->plugin->baseAddress()!=-1)
46 53 {
47 54 unsigned int status_reg,data;
48 55 char ch;
49 56 QString printdata="";
50 57 plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG);
51 58 while ((status_reg&4)==0) {
52 59 plugin->parent->Read(&data,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG);
53 60 ch = (char)(0xff & data);
54 61 printdata+=ch;
55 62 plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG);
56 63 }
57 64 if(printdata!="")
58 65 emit apbUartTextReceived(printdata);
59 66 }
60 67 else
61 68 {
62 69 this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0));
63 70 }
64 71 }
65 72 else
66 73 {
67 74 configFifoDebug(true);
68 75 }
69 76 }
70 77 else
71 78 {
72 79 int read =0;
73 char ch;
80 char ch[1];
74 81 uartMutex->lock();
75 82 if(uartOpened)
76 83 {
77 read =rs232read(this->uart,&ch,1);
84 read =rs232read(this->uart,ch,1);
85 SocExplorerEngine::message(this->plugin,QString("Read %1 bytes on uart").arg(read),3);
78 86 }
79 87 uartMutex->unlock();
80 if(read==1)
88 if(read>=1)
81 89 {
82 emit this->sendChar(ch);
90 SocExplorerEngine::message(this->plugin,QString("Received one char from APBUART"),3);
91 emit this->apbUartTextReceived(QString(ch[0]));
83 92 }
93 msleep(1);
84 94 }
85 msleep(100);
86 95 }
87 96 }
88 97
89 98 void UARTPollingThread::sendChar(char c)
90 99 {
91 100 if(fifoDebugEnabled)
92 101 {
93 102 if(this->plugin->baseAddress()!=-1)
94 103 {
95 104 unsigned int i=0x0FF & c;
96 105 plugin->parent->Write(&i,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG);
97 106 }
98 107 }
99 108 else
100 109 {
101 110 uartMutex->lock();
102 111 rs232write(this->uart,&c,1);
103 112 uartMutex->unlock();
104 113 }
105 114 }
106 115
107 116 bool UARTPollingThread::openUart()
108 117 {
118 uartMutex->lock();
109 119 if(uartOpened)
110 120 {
111 121 closeUart();
112 122 }
123 SocExplorerEngine::message(this->plugin,"Opening UART "+this->portName,3);
113 124 this->uart = rs232open((char*)this->portName.toStdString().c_str());
114 125 if(this->uart!=badPortValue)
115 126 {
127 SocExplorerEngine::message(this->plugin,QString("Configuring UART, speed =%1").arg(this->uartSpeed),3);
116 128 rs232setup(this->uart,8,this->uartSpeed,rs232parityNo,rs232OneStop);
117 129 uartOpened = true;
118 130 }
131 uartMutex->unlock();
119 132 return uartOpened;
120 133 }
121 134
122 135 void UARTPollingThread::closeUart()
123 136 {
124 137 uartMutex->lock();
125 138 rs232close(this->uart);
126 139 uartOpened = false;
127 140 uartMutex->unlock();
128 141 }
129 142
130 143 void UARTPollingThread::setPortName(QString name)
131 144 {
145 SocExplorerEngine::message(this->plugin,"Changing UART port Name: "+name,3);
132 146 this->portName = name;
133 147 }
134 148
135 149 void UARTPollingThread::setPortSpeedStr(QString speed)
136 150 {
151 SocExplorerEngine::message(this->plugin,"Changing UART speed: "+speed,3);
137 152 this->uartSpeed = speed.toInt();
138 153 }
139 154
140 155 void UARTPollingThread::setPortSpeed(int speed)
141 156 {
157 SocExplorerEngine::message(this->plugin,QString("Changing UART speed: %1").arg(speed),3);
142 158 this->uartSpeed = speed;
143 159 }
144 160
145 161 void UARTPollingThread::setFifoDebugEable(bool enable)
146 162 {
147 163 if(enable)
148 164 SocExplorerEngine::message(this->plugin,"Enabling APB UART FIFO debug mode",3);
149 165 else
150 166 SocExplorerEngine::message(this->plugin,"Disabling APB UART FIFO debug mode",3);
151 167 if(uartOpened && enable)
152 168 {
153 169 closeUart();
154 170 }
155 171 this->fifoDebugConfigured = false;
156 172 configFifoDebug(enable);
157 173 this->fifoDebugEnabled = enable;
158 174 }
159 175
160 176 void UARTPollingThread::configFifoDebug(bool enable)
161 177 {
162 178 SocExplorerEngine::message(this->plugin,"Configuring APB UART in FIFO debug mode",3);
163 179 if(this->plugin->baseAddress()==-1)
164 180 {
165 181 this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0));
166 182 }
167 183 if(this->plugin->baseAddress()!=-1)
168 184 {
169 185 if(enable)
170 186 {
171 187 unsigned int ctrl_reg= 0x843;
172 188 this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
173 189 this->fifoDebugConfigured = true;
174 190 }
175 191 else
176 192 {
177 193 unsigned int ctrl_reg;
178 194 /* Firts get Control reg value*/
179 195 this->plugin->parent->Read(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
180 196 ctrl_reg = ctrl_reg & (~(1<<11));
181 197 this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG);
182 198 this->fifoDebugConfigured = true;
183 199 }
184 200 }
185 201 }
@@ -1,65 +1,66
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 2 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #ifndef UARTPOLLINGTHREAD_H
23 23 #define UARTPOLLINGTHREAD_H
24 24
25 25 #include <QObject>
26 26 #include <QThread>
27 27 #include <socexplorerplugin.h>
28 28 #include <RS232.h>
29 29 #include <QMutex>
30 30
31 31 #define APB_UART_DATA_REG 0x0
32 32 #define APB_UART_STATUS_REG 0x4
33 33 #define APB_UART_CONTROL_REG 0x8
34 34 #define APB_UART_SCALE_REG 0xC
35 35 #define APB_UART_FIFO_DEBUG_REG 0x10
36 36
37 37 class UARTPollingThread : public QThread
38 38 {
39 39 Q_OBJECT
40 40 public:
41 41 explicit UARTPollingThread(socexplorerplugin *parent = 0);
42 ~UARTPollingThread();
42 43 void run();
43 44 signals:
44 45 void apbUartTextReceived(QString text);
45 46 public slots:
46 47 void sendChar(char c);
47 48 bool openUart();
48 49 void closeUart();
49 50 void setPortName(QString name);
50 51 void setPortSpeedStr(QString speed);
51 52 void setPortSpeed(int speed);
52 53 void setFifoDebugEable(bool enable);
53 54 private:
54 55 void configFifoDebug(bool enable);
55 56 bool fifoDebugEnabled;
56 57 bool fifoDebugConfigured;
57 58 QString portName;
58 59 int uartSpeed;
59 60 bool uartOpened;
60 61 socexplorerplugin* plugin;
61 62 rs232port_t uart;
62 63 QMutex* uartMutex;
63 64 };
64 65
65 66 #endif // UARTPOLLINGTHREAD_H
@@ -1,223 +1,225
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #include "gr_esb_bridge.h"
23 23 #include "gr_esb_ui.h"
24 24 #include <unistd.h>
25 25 #include "spw.h"
26 26 #include <socexplorerengine.h>
27 27
28 28 GR_ESB_bridge::GR_ESB_bridge(socexplorerplugin *parent) :
29 29 abstractSpwBridge(parent)
30 30 {
31 31 this->p_GUI = new GR_ESB_ui();
32 32 this->manager = new GR_ESB_Manager(parent,this);
33 33
34 34 connect((GR_ESB_ui*)(this->p_GUI),SIGNAL(ipchanged(QString)),this,SLOT(setIP(QString)));
35 35 connect((GR_ESB_ui*)(this->p_GUI),SIGNAL(vlinkchanged(QString)),this,SLOT(setVirtualLink(QString)));
36 36 connect((GR_ESB_ui*)(this->p_GUI),SIGNAL(connectClicked()),this,SLOT(toggleBridgeConnection()));
37 37 this->manager->virtualLinkIndex = 0;
38 38 this->manager->start();
39 39 }
40 40
41 41 GR_ESB_bridge::~GR_ESB_bridge()
42 42 {
43 this->manager->requestInterruption();
44 while(this->manager->isRunning());
43 45 }
44 46
45 47 void GR_ESB_bridge::toggleBridgeConnection()
46 48 {
47 49 if(this->plugin->isConnected())
48 50 {
49 51 this->disconnectBridge();
50 52 }
51 53 else
52 54 {
53 55 this->connectBridge();
54 56 }
55 57 }
56 58
57 59 bool GR_ESB_bridge::connectBridge()
58 60 {
59 61 if(this->manager->connectBridge())
60 62 {
61 63 ((GR_ESB_ui*)this->p_GUI)->lock(true);
62 64 emit setConnected(true);
63 65 return true;
64 66 }
65 67 return false;
66 68 }
67 69
68 70 bool GR_ESB_bridge::disconnectBridge()
69 71 {
70 72 if(this->manager->disconnectBridge())
71 73 {
72 74 ((GR_ESB_ui*)this->p_GUI)->lock(false);
73 75 emit setConnected(false);
74 76 return true;
75 77 }
76 78 return false;
77 79 }
78 80
79 81 void GR_ESB_bridge::setIP(QString ip)
80 82 {
81 83 this->manager->IP = ip;
82 84 }
83 85
84 86 void GR_ESB_bridge::setVirtualLink(QString vlink)
85 87 {
86 88 vlink = vlink.section("Virtual link",0,0);
87 89 bool success;
88 90 int vlinkTmp = vlink.toInt(&success);
89 91 if(success)
90 92 {
91 93 setVirtualLink(vlinkTmp);
92 94 }
93 95 }
94 96
95 97 void GR_ESB_bridge::setVirtualLink(qint32 vlink)
96 98 {
97 99 if(vlink<6 && vlink>=0)
98 100 {
99 101 this->manager->virtualLinkIndex = vlink;
100 102 }
101 103 }
102 104
103 105
104 106 unsigned int GR_ESB_bridge::Write(unsigned int *Value, unsigned int count, unsigned int address)
105 107 {
106 108
107 109 }
108 110
109 111 unsigned int GR_ESB_bridge::Read(unsigned int *Value, unsigned int count, unsigned int address)
110 112 {
111 113
112 114 }
113 115
114 116 int GR_ESB_bridge::pushRMAPPacket(char *packet, int size)
115 117 {
116 118 return this->manager->sendPacket(packet,size);
117 119 }
118 120
119 121
120 122 GR_ESB_Manager::GR_ESB_Manager(socexplorerplugin *plugin, QObject *parent)
121 123 :QThread((QObject*)parent)
122 124 {
123 125 this->Read_soc = new QTcpSocket(this);
124 126 this->Write_soc = new QTcpSocket(this);
125 127 this->RMAPtimeout = 2000;
126 128 this->handleMutex = new QMutex(QMutex::NonRecursive);
127 129 this->RMAP_AnswersSem = new QSemaphore(0);
128 130 this->RMAP_AnswersMtx=new QMutex(QMutex::Recursive);
129 131 this->RMAP_pending_transaction_IDsMtx=new QMutex(QMutex::Recursive);
130 132 this->plugin = plugin;
131 133 connected = false;
132 134 this->moveToThread(this);
133 135 }
134 136
135 137 GR_ESB_Manager::~GR_ESB_Manager()
136 138 {
137 139
138 140 }
139 141
140 142 void GR_ESB_Manager::run()
141 143 {
142 144 SocExplorerEngine::message(this->plugin,"Starting GRESB pooling thread",1);
143 145 while (!this->isInterruptionRequested())
144 146 {
145 147 if(this->connected)
146 148 {
147 149 handleMutex->lock();
148 150 SocExplorerEngine::message(this->plugin,"Looking for new RMAP packets",4);
149 151
150 152 }
151 153 else
152 154 {
153 155 //do some sanity checks!
154 156
155 157 usleep(RMAPtimeout/2);
156 158 }
157 159 usleep(1000);
158 160 }
159 161 SocExplorerEngine::message(this->plugin,"Exiting Startdundee USB pooling thread",1);
160 162 }
161 163
162 164 bool GR_ESB_Manager::connectBridge()
163 165 {
164 166 int timeout=60;
165 167 if(this->Read_soc->state()==QTcpSocket::UnconnectedState)
166 168 {
167 169 this->Read_soc->connectToHost(IP,gresb_Conf[virtualLinkIndex].Read_port);
168 170 this->Read_soc->waitForConnected(30000);
169 171 }
170 172 if(this->Write_soc->state()==QTcpSocket::UnconnectedState)
171 173 {
172 174 this->Write_soc->connectToHost(IP,gresb_Conf[virtualLinkIndex].Write_port);
173 175 this->Write_soc->waitForConnected(30000);
174 176 }
175 177 while((this->Read_soc->state()!=QTcpSocket::ConnectedState) && (this->Write_soc->state()!=QTcpSocket::ConnectedState))
176 178 {
177 179 usleep(100000);
178 180 if(timeout--==0)return false;
179 181 }
180 182 return true;
181 183
182 184 }
183 185
184 186 bool GR_ESB_Manager::disconnectBridge()
185 187 {
186 188 int timeout=60;
187 189 if(this->Read_soc->state()!=QTcpSocket::UnconnectedState)
188 190 {
189 191 this->Read_soc->disconnectFromHost();
190 192 this->Read_soc->waitForDisconnected(30000);
191 193 }
192 194 if(this->Write_soc->state()!=QTcpSocket::UnconnectedState)
193 195 {
194 196 this->Write_soc->disconnectFromHost();
195 197 this->Write_soc->waitForDisconnected(30000);
196 198 }
197 199 while((this->Read_soc->state()!=QTcpSocket::UnconnectedState) && (this->Write_soc->state()!=QTcpSocket::UnconnectedState))
198 200 {
199 201 usleep(100000);
200 202 if(timeout--==0)return false;
201 203 }
202 204 return true;
203 205 }
204 206
205 207 int GR_ESB_Manager::getRMAPtransactionID()
206 208 {
207 209
208 210 }
209 211
210 212 int GR_ESB_Manager::getRMAPanswer(int transactionID, char **buffer)
211 213 {
212 214
213 215 }
214 216
215 217 bool GR_ESB_Manager::sendPacket(char *packet, int size)
216 218 {
217 219
218 220 }
219 221
220 222 void GR_ESB_Manager::pushRmapPacket(char *packet, int len)
221 223 {
222 224
223 225 }
@@ -1,646 +1,647
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22
23 23 #include "stardundeespw_usb.h"
24 24 #include <socexplorerengine.h>
25 25 #include <qhexedit.h>
26 26
27 27 stardundeeSPW_USB::stardundeeSPW_USB(socexplorerplugin *parent) :
28 28 abstractSpwBridge(parent)
29 29 {
30 30 Q_UNUSED(parent)
31 31 this->manager = new stardundeeSPW_USB_Manager(parent,this);
32 32 makeGUI(parent);
33 33 this->manager->start();
34 34 connect(this->manager,SIGNAL(emitPacket(char*,int)),this,SIGNAL(pushPacketOverTCP(char*,int)));
35 35 }
36 36
37 37 stardundeeSPW_USB::~stardundeeSPW_USB()
38 38 {
39 39 this->manager->requestInterruption();
40 while(this->manager->isRunning());
40 41 }
41 42
42 43 void stardundeeSPW_USB::toggleBridgeConnection()
43 44 {
44 45 if(this->plugin->isConnected())
45 46 {
46 47 this->disconnectBridge();
47 48 }
48 49 else
49 50 {
50 51 this->connectBridge();
51 52 }
52 53 }
53 54
54 55 bool stardundeeSPW_USB::connectBridge()
55 56 {
56 57 if(this->manager->connectBridge())
57 58 {
58 59 ((StarDundeeGUI*)this->p_GUI)->lock(true);
59 60 emit setConnected(true);
60 61 return true;
61 62 }
62 63 return false;
63 64 }
64 65
65 66 bool stardundeeSPW_USB::disconnectBridge()
66 67 {
67 68 if(this->manager->disconnectBridge())
68 69 {
69 70 ((StarDundeeGUI*)this->p_GUI)->lock(false);
70 71 emit setConnected(false);
71 72 return true;
72 73 }
73 74 return false;
74 75 }
75 76
76 77
77 78 int stardundeeSPW_USB::pushRMAPPacket(char *packet, int size)
78 79 {
79 80 return this->manager->sendPacket(packet,size);
80 81 }
81 82
82 83 unsigned int stardundeeSPW_USB::Write(unsigned int *Value, unsigned int count, unsigned int address)
83 84 {
84 85 char writeBuffer[RMAP_WRITE_PACKET_MIN_SZ((RMAP_MAX_XFER_SIZE*4))+1];
85 86 char *RMAPAckBuff;
86 87 writeBuffer[0]=this->manager->linkNumber;//Link number
87 88 int transactionID = 0;
88 89 int written=0;
89 90 SocExplorerEngine::message(this->plugin,"Enter Write function",2);
90 91 QProgressBar* progress=NULL;
91 92 SocExplorerAutoProgressBar autopb;
92 93 if(count>RMAP_MAX_XFER_SIZE)
93 94 {
94 95 progress= SocExplorerEngine::getProgressBar("Writing on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
95 96 autopb.setProgressBar(progress);
96 97 }
97 98 //Quite stupide loop, I guess that I always get the number of byte I asked for!
98 99 while(count>=RMAP_MAX_XFER_SIZE)
99 100 {
100 101 for(int i=0;i<(RMAP_MAX_XFER_SIZE);i++)
101 102 {
102 103 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
103 104 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
104 105 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
105 106 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
106 107 }
107 108 transactionID=manager->getRMAPtransactionID();
108 109 SocExplorerEngine::message(this->plugin,QString("Sending Write request with ID=%1").arg(transactionID),2);
109 110 RMAP_build_tx_request_header(this->manager->rmapAddress,this->manager->rmapKey,1,transactionID,address+(written*4),RMAP_MAX_XFER_SIZE*4,writeBuffer+1);
110 111 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(RMAP_MAX_XFER_SIZE*4)+1);
111 112 manager->getRMAPanswer(transactionID,&RMAPAckBuff);
112 113 free(RMAPAckBuff);
113 114 written+=RMAP_MAX_XFER_SIZE;
114 115 count-=RMAP_MAX_XFER_SIZE;
115 116 progress->setValue(written);
116 117 qApp->processEvents();
117 118 }
118 119 if(count>0)
119 120 {
120 121 for(int i=0;i<((int)count);i++)
121 122 {
122 123 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
123 124 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
124 125 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
125 126 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
126 127 }
127 128 transactionID=manager->getRMAPtransactionID();
128 129 SocExplorerEngine::message(this->plugin,QString("Sending Write request with ID=%1").arg(transactionID),2);
129 130 RMAP_build_tx_request_header(this->manager->rmapAddress,this->manager->rmapKey,1,transactionID,address+(written*4),count*4,writeBuffer+1);
130 131 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(count*4) +1);
131 132 manager->getRMAPanswer(transactionID,&RMAPAckBuff);
132 133 free(RMAPAckBuff);
133 134 written+=count;
134 135 if(progress!=NULL)
135 136 {
136 137 progress->setValue(written);
137 138 qApp->processEvents();
138 139 }
139 140 }
140 141 return written;
141 142 }
142 143
143 144 unsigned int stardundeeSPW_USB::Read(unsigned int *Value, unsigned int count, unsigned int address)
144 145 {
145 146 char requestBuffer[RMAP_READ_HEADER_MIN_SZ+1];
146 147 char* RMAP_AnswerBuffer;
147 148 requestBuffer[0]=this->manager->linkNumber;//Link number
148 149 int transactionID = 0;
149 150 int read=0;
150 151 QProgressBar* progress=NULL;
151 152 SocExplorerAutoProgressBar autopb;
152 153 if(count>RMAP_MAX_XFER_SIZE)
153 154 {
154 155 progress= SocExplorerEngine::getProgressBar("Reading on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
155 156 autopb.setProgressBar(progress);
156 157 }
157 158 SocExplorerEngine::message(this->plugin,QString("Enter read function, count=%1, RMAP_MAX_XFER_SIZE=%2").arg(count).arg(RMAP_MAX_XFER_SIZE),2);
158 159
159 160 //Quite stupide loop, I guess that I always get the number of byte I asked for!
160 161 while((int)count>=(int)RMAP_MAX_XFER_SIZE)
161 162 {
162 163 transactionID = manager->getRMAPtransactionID();
163 164 SocExplorerEngine::message(this->plugin,QString("New transactionID:%1").arg(transactionID),2);
164 165 RMAP_build_rx_request_header(this->manager->rmapAddress,this->manager->rmapKey,1,transactionID,address+(read*4),RMAP_MAX_XFER_SIZE*4,requestBuffer+1);
165 166 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
166 167 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
167 168 if(len==-1)
168 169 {
169 170 this->toggleBridgeConnection();
170 171 return 0;
171 172 }
172 173 for(int i=0;i<((len-13)/4);i++)
173 174 {
174 175 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
175 176 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
176 177 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
177 178 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
178 179 }
179 180 free(RMAP_AnswerBuffer);
180 181 read+=RMAP_MAX_XFER_SIZE;
181 182 count-=RMAP_MAX_XFER_SIZE;
182 183 progress->setValue(read);
183 184 qApp->processEvents();
184 185 }
185 186 if((int)count>0)
186 187 {
187 188 transactionID = manager->getRMAPtransactionID();
188 189 SocExplorerEngine::message(this->plugin,QString("New transactionID: %1").arg(transactionID),2);
189 190 SocExplorerEngine::message(this->plugin,QString("Building request with:"),2);
190 191 SocExplorerEngine::message(this->plugin,QString("Address = %1").arg(address+(read*4),8,16),2);
191 192 SocExplorerEngine::message(this->plugin,QString("Size = %1").arg(count*4),2);
192 193 SocExplorerEngine::message(this->plugin,QString("Size + 13 = %1").arg((count*4)+13),2);
193 194 RMAP_build_rx_request_header(this->manager->rmapAddress,this->manager->rmapKey,1,transactionID,address+(read*4),count*4,requestBuffer+1);
194 195 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
195 196 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
196 197 if(len==-1)
197 198 {
198 199 this->toggleBridgeConnection();
199 200 return 0;
200 201 }
201 202 for(int i=0;i<((len-13)/4);i++)
202 203 {
203 204 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
204 205 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
205 206 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
206 207 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
207 208 }
208 209 free(RMAP_AnswerBuffer);
209 210 read+=count;
210 211 if(progress!=NULL)
211 212 {
212 213 progress->setValue(read);
213 214 qApp->processEvents();
214 215 }
215 216 }
216 217 return read;
217 218 }
218 219
219 220 void stardundeeSPW_USB::brickSelectionChanged(int brickIndex)
220 221 {
221 222 this->manager->selectedBrick = brickIndex-1;
222 223 SocExplorerEngine::message(plugin,QString("Changing brick index: %1").arg(manager->selectedBrick),1);
223 224 }
224 225
225 226 void stardundeeSPW_USB::linkNumberSelectionChanged(int linkIndex)
226 227 {
227 228 this->manager->linkNumber = linkIndex + 1;
228 229 SocExplorerEngine::message(plugin,QString("Changing Link Number: %1").arg(manager->linkNumber),1);
229 230 }
230 231
231 232 void stardundeeSPW_USB::linkSpeedSelectionChanged(const QString &linkSpeed)
232 233 {
233 234 this->manager->linkSpeed = linkSpeed.toInt();
234 235
235 236 SocExplorerEngine::message(plugin,QString("Changing Link Speed: %1").arg(manager->linkSpeed),1);
236 237 }
237 238
238 239 void stardundeeSPW_USB::destinationKeyChanged(const QString &destKey)
239 240 {
240 241 this->manager->destinationKey = destKey.toInt();
241 242 SocExplorerEngine::message(plugin,QString("Changing Destination Key: %1").arg(manager->destinationKey),1);
242 243 }
243 244
244 245 void stardundeeSPW_USB::rmapAddressChanged(const QString &rmapaddress)
245 246 {
246 247 this->manager->rmapAddress = rmapaddress.toInt();
247 248 SocExplorerEngine::message(plugin,QString("Changing RMAP address: %1").arg(manager->rmapAddress),1);
248 249 }
249 250
250 251 void stardundeeSPW_USB::rmapKeyChanged(const QString &key)
251 252 {
252 253 this->manager->rmapKey = key.toInt();
253 254 SocExplorerEngine::message(plugin,QString("Changing RMAP Key: %1").arg(manager->rmapKey),1);
254 255 }
255 256
256 257 void stardundeeSPW_USB::rmapTimeoutChanged(const QString &timeout)
257 258 {
258 259 int tim=timeout.toInt();
259 260 if(tim<50)
260 261 {
261 262 tim = 50;
262 263 ((StarDundeeGUI*)this->p_GUI)->setRmapTimeout(QString("%1").arg(tim));
263 264 }
264 265 this->manager->RMAPtimeout = tim;
265 266 SocExplorerEngine::message(plugin,QString("Changing RMAP Timeout: %1").arg(manager->RMAPtimeout),1);
266 267 }
267 268
268 269
269 270 void stardundeeSPW_USB::makeGUI(socexplorerplugin *parent)
270 271 {
271 272 this->p_GUI = new StarDundeeGUI();
272 273 // this->mainLayout = new QGridLayout(this->p_GUI);
273 274 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(connectClicked()),this,SLOT(toggleBridgeConnection()));
274 275 connect(this->manager,SIGNAL(updateAvailableBrickCount(int)),((StarDundeeGUI*)this->p_GUI),SLOT(updateAvailableBrickCount(int)));
275 276 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(brickSelectionChanged(int)),this,SLOT(brickSelectionChanged(int)));
276 277 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(linkNumberSelectionChanged(int)),this,SLOT(linkNumberSelectionChanged(int)));
277 278 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(linkSpeedSelectionChanged(QString)),this,SLOT(linkSpeedSelectionChanged(QString)));
278 279 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(destinationKeyChanged(QString)),this,SLOT(destinationKeyChanged(QString)));
279 280 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(rmapAddressChanged(QString)),this,SLOT(rmapAddressChanged(QString)));
280 281 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(rmapKeyChanged(QString)),this,SLOT(rmapKeyChanged(QString)));
281 282 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(rmapTimeoutChanged(QString)),this,SLOT(rmapTimeoutChanged(QString)));
282 283
283 284 this->brickSelectionChanged(((StarDundeeGUI*)this->p_GUI)->getBrickSelection());
284 285 this->linkNumberSelectionChanged(((StarDundeeGUI*)this->p_GUI)->getLinkNumberSelection());
285 286 this->linkSpeedSelectionChanged(((StarDundeeGUI*)this->p_GUI)->getLinkSpeedSelection());
286 287 this->destinationKeyChanged(((StarDundeeGUI*)this->p_GUI)->getDestinationKey());
287 288 this->rmapAddressChanged(((StarDundeeGUI*)this->p_GUI)->getRmapAddress());
288 289 this->rmapKeyChanged(((StarDundeeGUI*)this->p_GUI)->getRmapKey());
289 290 this->rmapTimeoutChanged(((StarDundeeGUI*)this->p_GUI)->getRmapTimeout());
290 291
291 292 connect(this,SIGNAL(SelectBrick(int)),((StarDundeeGUI*)this->p_GUI),SLOT(selectBrick(int)));
292 293 connect(this,SIGNAL(SelectLinkNumber(int)),((StarDundeeGUI*)this->p_GUI),SLOT(selectLinkNumber(int)));
293 294 connect(this,SIGNAL(SelectLinkSpeed(int)),((StarDundeeGUI*)this->p_GUI),SLOT(selectLinkSpeed(int)));
294 295 connect(this,SIGNAL(SetDestinationKey(QString)),((StarDundeeGUI*)this->p_GUI),SLOT(setDestinationKey(QString)));
295 296 connect(this,SIGNAL(SetRmapAddress(QString)),((StarDundeeGUI*)this->p_GUI),SLOT(setRmapAddress(QString)));
296 297 connect(this,SIGNAL(SetRmapKey(QString)),((StarDundeeGUI*)this->p_GUI),SLOT(setRmapKey(QString)));
297 298 connect(this,SIGNAL(SetRmapTimeout(QString)),((StarDundeeGUI*)this->p_GUI),SLOT(setRmapTimeout(QString)));
298 299
299 300 }
300 301
301 302 stardundeeSPW_USB_Manager::stardundeeSPW_USB_Manager(socexplorerplugin *plugin, QObject *parent)
302 303 :QThread((QObject*)parent)
303 304 {
304 305 this->RMAPtimeout = 2000;
305 306 this->handleMutex = new QMutex(QMutex::NonRecursive);
306 307 this->RMAP_AnswersSem = new QSemaphore(0);
307 308 this->RMAP_AnswersMtx=new QMutex(QMutex::Recursive);
308 309 this->RMAP_pending_transaction_IDsMtx=new QMutex(QMutex::Recursive);
309 310 this->plugin = plugin;
310 311 connected = false;
311 312 this->moveToThread(this);
312 313 }
313 314
314 315 stardundeeSPW_USB_Manager::~stardundeeSPW_USB_Manager()
315 316 {
316 317 this->terminate();
317 318 while (!this->isFinished()) {
318 319 this->usleep(1000);
319 320 }
320 321 }
321 322
322 323
323 324 void stardundeeSPW_USB_Manager::run()
324 325 {
325 326 USB_SPACEWIRE_PACKET_PROPERTIES properties;
326 327 USB_SPACEWIRE_ID pIdentifier=NULL;
327 328 USB_SPACEWIRE_STATUS stat;
328 329 SocExplorerEngine::message(this->plugin,"Starting Startdundee USB pooling thread",1);
329 330 char buffer[(RMAP_MAX_XFER_SIZE*4)+50];
330 331 while (!this->isInterruptionRequested())
331 332 {
332 333 if(this->connected)
333 334 {
334 335 handleMutex->lock();
335 336 SocExplorerEngine::message(this->plugin,"Looking for new RMAP packets",4);
336 337 if(USBSpaceWire_WaitOnReadPacketAvailable(hDevice,0.01))
337 338 {
338 339 SocExplorerEngine::message(this->plugin,"Got packet",2);
339 340 stat = USBSpaceWire_ReadPackets(hDevice, buffer, (RMAP_MAX_XFER_SIZE*4)+50,1, 1, &properties, &pIdentifier);
340 341 if (stat == TRANSFER_SUCCESS)
341 342 {
342 343 if(USBSpaceWire_GetReadTrafficType(&properties, 0) ==SPACEWIRE_TRAFFIC_PACKET)
343 344 {
344 345 SocExplorerEngine::message(this->plugin,"It's a SPW packet",2);
345 346 if(USBSpaceWire_GetReadEOPStatus(&properties, 0)== SPACEWIRE_USB_EOP)
346 347 {
347 348 SocExplorerEngine::message(this->plugin,"Got end of packet",2);
348 349 if(buffer[1]==(char)SPW_PROTO_ID_RMAP) //RMAP packet
349 350 {
350 351 RMAP_Answer* packet;
351 352 SocExplorerEngine::message(this->plugin,"Got RMAP packet",2);
352 353 SocExplorerEngine::message(this->plugin,QString("Rmap packet size %1").arg(properties.len),2);
353 354 char* packetbuffer = (char*)malloc(properties.len);
354 355 memcpy(packetbuffer,buffer,properties.len);
355 356 USBSpaceWire_FreeRead(hDevice, pIdentifier);
356 357 pIdentifier = NULL;
357 358 handleMutex->unlock();
358 359 if(properties.len==8)
359 360 {
360 361 packet=new RMAP_Answer(RMAP_get_transactionID(buffer),packetbuffer,properties.len);
361 362 }
362 363 else
363 364 {
364 365 packet=new RMAP_Answer(RMAP_get_transactionID(buffer+1),packetbuffer,properties.len);
365 366 }
366 367 RMAP_AnswersMtx->lock();
367 368 RMAP_Answers.append(packet);
368 369 RMAP_AnswersMtx->unlock();
369 370 RMAP_AnswersSem->release();
370 371 }
371 372 else //any non-rmap packet will be pushed to the network
372 373 {
373 374 char* packetbuffer = (char*)malloc(properties.len);
374 375 memcpy(packetbuffer,buffer,properties.len);
375 376 emit emitPacket(packetbuffer,properties.len);
376 377 USBSpaceWire_FreeRead(hDevice, pIdentifier);
377 378 handleMutex->unlock();
378 379 SocExplorerEngine::message(this->plugin,"Got SPW packet",2);
379 380 }
380 381 }
381 382 else
382 383 {
383 384 SocExplorerEngine::message(this->plugin,"No EOP received",2);
384 385 }
385 386 }
386 387
387 388 }
388 389 else
389 390 {
390 391 USBSpaceWire_FreeRead(hDevice, pIdentifier);
391 392 handleMutex->unlock();
392 393 }
393 394 }
394 395 else
395 396 {
396 397 USBSpaceWire_FreeRead(hDevice, pIdentifier);
397 398 handleMutex->unlock();
398 399 }
399 400 }
400 401 else
401 402 {
402 403 //do some sanity checks!
403 404 int list = USBSpaceWire_ListDevices();
404 405 if(this->brickList!=list)
405 406 {
406 407 this->brickList = list;
407 408 emit updateAvailableBrickCount(this->brickList);
408 409 }
409 410 usleep(RMAPtimeout/2);
410 411 }
411 412 usleep(1000);
412 413 }
413 414 SocExplorerEngine::message(this->plugin,"Exiting Startdundee USB pooling thread",1);
414 415 }
415 416
416 417 bool stardundeeSPW_USB_Manager::connectBridge()
417 418 {
418 419 QMutexLocker mlock(this->handleMutex);
419 420 int status;
420 421 U32 statusControl;
421 422 this->connected = false;
422 423 if (!USBSpaceWire_Open(&hDevice, this->selectedBrick)) // Open the USB device
423 424 {
424 425 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** ERROR: USBSpaceWire_Open(&hDevice, 0))",0);
425 426 return false;
426 427 }
427 428 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** USBSpaceWire_Open successful",0);
428 429
429 430 USBSpaceWire_EnableNetworkMode(hDevice, 0); // deactivate the network mode
430 431 CFGSpaceWire_EnableRMAP(1); // Enable the use of RMAP for the StarDundee brick configuration
431 432 CFGSpaceWire_SetRMAPDestinationKey(0x20); // Set the destination key expected by STAR-Dundee devices
432 433
433 434 // Set the path and return path to the device
434 435 CFGSpaceWire_StackClear();
435 436 CFGSpaceWire_AddrStackPush(0);
436 437 CFGSpaceWire_AddrStackPush(254);
437 438 CFGSpaceWire_RetAddrStackPush(254);
438 439 // set the base transmit rate to 100 MHz
439 440 status = CFGSpaceWire_SetBrickBaseTransmitRate( hDevice, CFG_BRK_CLK_100_MHZ, CFG_BRK_DVDR_1, 0xff);
440 441 if (status != CFG_TRANSFER_SUCCESS)
441 442 {
442 443 SocExplorerEngine::message(this->plugin,"ERROR CFGSpaceWire_SetBrickBaseTransmitRate",1);
443 444 return false;
444 445 }
445 446 else
446 447 {
447 448 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_SetBrickBaseTransmitRate, base rate = 100 MHz",1);
448 449 }
449 450
450 451 // read the link status
451 452 if (CFGSpaceWire_GetLinkStatusControl(hDevice, this->linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
452 453 {
453 454 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(this->linkNumber),1);
454 455 return false;
455 456 }
456 457 else
457 458 {
458 459 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(this->linkNumber),1);
459 460
460 461 // Set the link status control register properties
461 462 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
462 463 CFGSpaceWire_LSEnableStart(&statusControl, 1);
463 464 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
464 465 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
465 466 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
466 467
467 468 // Set the link status control register
468 469 if (CFGSpaceWire_SetLinkStatusControl(hDevice, this->linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
469 470 {
470 471 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(this->linkNumber),1);
471 472 return false;
472 473 }
473 474 else
474 475 {
475 476 SocExplorerEngine::message(this->plugin,"Set the link status control for link " + QString::number(this->linkNumber),1);
476 477 }
477 478 }
478 479
479 480 if (CFGSpaceWire_SetAsInterface(hDevice, 1, 0) != CFG_TRANSFER_SUCCESS)
480 481 {
481 482 SocExplorerEngine::message(this->plugin,"Could not set the device to be an interface",1);
482 483 return false;
483 484 }
484 485 else
485 486 {
486 487 SocExplorerEngine::message(this->plugin,"Device set to be an interface",1);
487 488 }
488 489
489 490 USBSpaceWire_RegisterReceiveOnAllPorts(hDevice); // Register to receive on all ports
490 491 USBSpaceWire_ClearEndpoints(hDevice); // clear the USB endpoints
491 492 USBSpaceWire_SetTimeout(hDevice,1.0);
492 493 SocExplorerEngine::message(this->plugin,"The driver's current send buffer size is " + QString::number(USBSpaceWire_GetDriverSendBufferSize(hDevice)) + " bytes",1);
493 494 SocExplorerEngine::message(this->plugin,"The driver's current read buffer size is " + QString::number(USBSpaceWire_GetDriverReadBufferSize(hDevice)) + " bytes",1);
494 495 SocExplorerEngine::message(this->plugin,"USBSpaceWire_IsReadThrottling is " + QString::number(USBSpaceWire_IsReadThrottling(hDevice)),1);
495 496 this->connected = true;
496 497 return true;
497 498 }
498 499
499 500 bool stardundeeSPW_USB_Manager::disconnectBridge()
500 501 {
501 502 this->handleMutex->lock();
502 503 USBSpaceWire_Close(hDevice); // Close the device
503 504 SocExplorerEngine::message(this->plugin,"stardundee *** Close *** USBSpaceWire_Close, device: " + QString::number(0),0);
504 505 USBSpaceWire_UnregisterReceiveOnAllPorts(hDevice); // Stop receiving on all ports
505 506 this->handleMutex->unlock();
506 507 this->RMAP_pending_transaction_IDsMtx->lock();
507 508 this->RMAP_pending_transaction_IDs.clear();
508 509 this->RMAP_pending_transaction_IDsMtx->unlock();
509 510 this->RMAP_AnswersMtx->lock();
510 511 this->RMAP_Answers.clear();
511 512 this->RMAP_AnswersMtx->unlock();
512 513 this->RMAP_AnswersSem->acquire(this->RMAP_AnswersSem->available());
513 514 return true;
514 515 }
515 516
516 517 int stardundeeSPW_USB_Manager::getRMAPtransactionID()
517 518 {
518 519 this->RMAP_pending_transaction_IDsMtx->lock();
519 520 int ID=0;
520 521 bool found=true;
521 522 while(ID<511)
522 523 {
523 524 for(int i=0;i<RMAP_pending_transaction_IDs.count();i++)
524 525 {
525 526 if(RMAP_pending_transaction_IDs[i]==ID)found=false;
526 527 }
527 528 if(found==true)break;
528 529 ID++;
529 530 found = true;
530 531 }
531 532 if(found)
532 533 {
533 534 RMAP_pending_transaction_IDs.append(ID);
534 535 }
535 536 this->RMAP_pending_transaction_IDsMtx->unlock();
536 537 return ID;
537 538 }
538 539
539 540 int stardundeeSPW_USB_Manager::getRMAPanswer(int transactionID, char **buffer)
540 541 {
541 542 QTime timeout;
542 543 *buffer=NULL;
543 544 int count=0;
544 545 SocExplorerEngine::message(this->plugin,"Looking for RMAP answer",2);
545 546 timeout.start();
546 547 while (*buffer==NULL)
547 548 {
548 549 this->RMAP_AnswersMtx->lock();
549 550 SocExplorerEngine::message(this->plugin,"Got exclusive access on RMAP_Answers stack",2);
550 551 SocExplorerEngine::message(this->plugin,QString("%1 packet(s) available in RMAP_Answers stack").arg(RMAP_Answers.count()),2);
551 552 for(int i=0;i<RMAP_Answers.count();i++)
552 553 {
553 554 SocExplorerEngine::message(this->plugin,QString("Packet %1 ID=%2").arg(i).arg(RMAP_Answers[i]->transactionID),2);
554 555 if(RMAP_Answers[i]->transactionID==transactionID)
555 556 {
556 557 this->RMAP_pending_transaction_IDsMtx->lock();
557 558 SocExplorerEngine::message(this->plugin,"Got exclusive access on RMAP_pending_transaction_ID stack",2);
558 559 for(int j=0;j<RMAP_pending_transaction_IDs.count();j++)
559 560 {
560 561 if(RMAP_pending_transaction_IDs[j]==transactionID)
561 562 {
562 563 RMAP_pending_transaction_IDs.removeAt(j);
563 564 }
564 565 }
565 566 this->RMAP_pending_transaction_IDsMtx->unlock();
566 567 *buffer = RMAP_Answers[i]->data;
567 568 count = RMAP_Answers[i]->len;
568 569 RMAP_Answer* tmp=RMAP_Answers[i];
569 570 RMAP_Answers.removeAt(i);
570 571 delete tmp;
571 572 }
572 573 }
573 574 this->RMAP_AnswersMtx->unlock();
574 575 //if no answer found in the stack wait until a new packet is pushed
575 576 SocExplorerEngine::message(this->plugin,"waiting until a new packet is pushed",2);
576 577 if(*buffer==NULL)
577 578 {
578 579 while (0==this->RMAP_AnswersSem->available())
579 580 {
580 581 SocExplorerEngine::message(this->plugin,QString("this->RMAP_AnswersSem->available() = %1").arg(this->RMAP_AnswersSem->available()),2);
581 582 if(timeout.elapsed()>=RMAPtimeout)
582 583 {
583 584 SocExplorerEngine::message(this->plugin,"Timeout reached giving up!",2);
584 585 return -1;
585 586 }
586 587 usleep(1000);
587 588 }
588 589 this->RMAP_AnswersSem->acquire();
589 590 }
590 591 }
591 592 return count;
592 593 }
593 594
594 595 bool stardundeeSPW_USB_Manager::sendPacket(char *packet, int size)
595 596 {
596 597 USB_SPACEWIRE_STATUS result;
597 598 USB_SPACEWIRE_ID pIdentifier;
598 599 SocExplorerEngine::message(this->plugin,"Sending SPW packet",2);
599 600 this->handleMutex->lock();
600 601 result = USBSpaceWire_SendPacket(hDevice,packet,size,1, &pIdentifier);
601 602 if (result != TRANSFER_SUCCESS)
602 603 {
603 604 SocExplorerEngine::message(this->plugin,"ERR sending the READ command ",2);
604 605 this->handleMutex->unlock();
605 606 return false;
606 607 }
607 608 else
608 609 {
609 610 SocExplorerEngine::message(this->plugin,"Packet sent",2);
610 611 USBSpaceWire_FreeSend(hDevice, pIdentifier);
611 612 }
612 613 this->handleMutex->unlock();
613 614 return true;
614 615 }
615 616
616 617 void stardundeeSPW_USB_Manager::pushRmapPacket(char *packet, int len)
617 618 {
618 619 char* packetbuffer = (char*)malloc(len);
619 620 memcpy(packetbuffer,packet,len);
620 621 RMAP_Answer* RMPAPpacket=new RMAP_Answer(RMAP_get_transactionID(packetbuffer+1),packetbuffer,len);
621 622 RMAP_AnswersMtx->lock();
622 623 RMAP_Answers.append(RMPAPpacket);
623 624 RMAP_AnswersMtx->unlock();
624 625 }
625 626
626 627
627 628
628 629
629 630
630 631
631 632
632 633
633 634
634 635
635 636
636 637
637 638
638 639
639 640
640 641
641 642
642 643
643 644
644 645
645 646
646 647
General Comments 0
You need to be logged in to leave comments. Login now