##// END OF EJS Templates
SpwPlugin:...
Jeandet Alexis -
r91:857534592b43 default draft
parent child
Show More
@@ -1,1014 +1,1015
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 "fakestardundeespwusb_lib.h"
25 25 #include <socexplorerengine.h>
26 26 #include <qhexedit.h>
27 27
28 28 const QString dwLinkStatusQString[6] = {
29 29 "CFG_SPACEWIRE_ERROR_RESET",
30 30 "CFG_SPACEWIRE_ERROR_WAIT",
31 31 "CFG_SPACEWIRE_READY",
32 32 "CFG_SPACEWIRE_STARTED",
33 33 "CFG_SPACEWIRE_CONNECTING",
34 34 "CFG_SPACEWIRE_RUN"
35 35 };
36 36
37 37 stardundeeSPW_USB::stardundeeSPW_USB(socexplorerplugin *parent) :
38 38 abstractSpwBridge(parent)
39 39 {
40 40 Q_UNUSED(parent)
41 41 this->manager = new stardundeeSPW_USB_Manager(parent,this);
42 42 makeGUI(parent);
43 43 connect(this->manager,SIGNAL(emitPacket(char*,int)),this,SIGNAL(pushPacketOverTCP(char*,int)));
44 44 connect(this->manager, SIGNAL(bytesReceivedFromSpw(uint)), this, SIGNAL(BytesReceivedFromSpw(uint)));
45 45 connect(this->manager, SIGNAL(bytesTransmittedToSpw(uint)), this, SIGNAL(BytesTransmittedToSpw(uint)));
46 46 connect(this->manager, SIGNAL(ccsdsPacketTransmittedToSpw()), this, SIGNAL(CCSDSPacketTransmittedToSpw()));
47 47 this->manager->start();
48 48 }
49 49
50 50 stardundeeSPW_USB::~stardundeeSPW_USB()
51 51 {
52 52 SocExplorerEngine::message(this,"Deleting stardundeeSPW_USB",0);
53 delete this->manager;
53 54 }
54 55
55 56 void stardundeeSPW_USB::toggleBridgeConnection()
56 57 {
57 58 if(this->plugin->isConnected())
58 59 {
59 60 this->disconnectBridge();
60 61 }
61 62 else
62 63 {
63 64 this->connectBridge();
64 65 }
65 66 }
66 67
67 68 bool stardundeeSPW_USB::connectBridge()
68 69 {
69 70 if(this->manager->connectBridge())
70 71 {
71 72 this->timecodeFrequencyChanged( ((StarDundeeGUI*)this->p_GUI)->getTimecodeFrequency());
72 73 this->startSendingTimecodes( ((StarDundeeGUI*)this->p_GUI)->getStartSendingTimecodes());
73 74 ((StarDundeeGUI*)this->p_GUI)->lock(true);
74 75 emit setConnected(true);
75 76 return true;
76 77 }
77 78 return false;
78 79 }
79 80
80 81 bool stardundeeSPW_USB::disconnectBridge()
81 82 {
82 83 if(this->manager->disconnectBridge())
83 84 {
84 85 ((StarDundeeGUI*)this->p_GUI)->lock(false);
85 86 emit setConnected(false);
86 87 return true;
87 88 }
88 89 return false;
89 90 }
90 91
91 92 int stardundeeSPW_USB::pushRMAPPacket(char *packet, int size)
92 93 {
93 94 return this->manager->sendPacket(packet,size);
94 95 }
95 96
96 97 unsigned int stardundeeSPW_USB::Write(unsigned int *Value, unsigned int count, unsigned int address)
97 98 {
98 99 char writeBuffer[RMAP_WRITE_PACKET_MIN_SZ((RMAP_MAX_XFER_SIZE*4))+1];
99 100 char *RMAPAckBuff;
100 101 writeBuffer[0]=this->manager->linkNumber;//Link number
101 102 int transactionID = 0;
102 103 int written=0;
103 104 SocExplorerEngine::message(this->plugin,"Enter Write function",2);
104 105 QProgressBar* progress=NULL;
105 106 SocExplorerAutoProgressBar autopb;
106 107 if(count>RMAP_MAX_XFER_SIZE)
107 108 {
108 109 progress= SocExplorerEngine::getProgressBar("Writing on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
109 110 autopb.setProgressBar(progress);
110 111 }
111 112 //Quite stupide loop, I guess that I always get the number of byte I asked for!
112 113 while(count>=RMAP_MAX_XFER_SIZE)
113 114 {
114 115 for(int i=0;i<(RMAP_MAX_XFER_SIZE);i++)
115 116 {
116 117 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
117 118 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
118 119 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
119 120 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
120 121 }
121 122 transactionID=manager->getRMAPtransactionID();
122 123 SocExplorerEngine::message(this->plugin,QString("Sending Write request with ID=%1").arg(transactionID),2);
123 124 RMAP_build_tx_request_header(
124 125 this->manager->destinationLogicalAddress,
125 126 this->manager->destinationKey,
126 127 this->manager->sourceLogicalAddress,
127 128 transactionID,
128 129 address+(written*4),
129 130 RMAP_MAX_XFER_SIZE*4,
130 131 writeBuffer+1);
131 132 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(RMAP_MAX_XFER_SIZE*4)+1);
132 133 manager->getRMAPanswer(transactionID,&RMAPAckBuff);
133 134 free(RMAPAckBuff);
134 135 written+=RMAP_MAX_XFER_SIZE;
135 136 count-=RMAP_MAX_XFER_SIZE;
136 137 progress->setValue(written);
137 138 qApp->processEvents();
138 139 }
139 140 if(count>0)
140 141 {
141 142 for(int i=0;i<((int)count);i++)
142 143 {
143 144 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
144 145 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
145 146 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
146 147 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
147 148 }
148 149 transactionID=manager->getRMAPtransactionID();
149 150 SocExplorerEngine::message(this->plugin,QString("Sending Write request with ID=%1").arg(transactionID),2);
150 151 RMAP_build_tx_request_header(
151 152 this->manager->destinationLogicalAddress,
152 153 this->manager->destinationKey,
153 154 this->manager->sourceLogicalAddress,
154 155 transactionID,
155 156 address+(written*4),
156 157 count*4,
157 158 writeBuffer+1);
158 159 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(count*4) +1);
159 160 manager->getRMAPanswer(transactionID,&RMAPAckBuff);
160 161 free(RMAPAckBuff);
161 162 written+=count;
162 163 if(progress!=NULL)
163 164 {
164 165 progress->setValue(written);
165 166 qApp->processEvents();
166 167 }
167 168 }
168 169 return written;
169 170 }
170 171
171 172 unsigned int stardundeeSPW_USB::Read(unsigned int *Value, unsigned int count, unsigned int address)
172 173 {
173 174 char requestBuffer[RMAP_READ_HEADER_MIN_SZ+1];
174 175 char* RMAP_AnswerBuffer;
175 176 requestBuffer[0]=this->manager->linkNumber;//Link number
176 177 int transactionID = 0;
177 178 int read=0;
178 179 QProgressBar* progress=NULL;
179 180 SocExplorerAutoProgressBar autopb;
180 181 if(count>RMAP_MAX_XFER_SIZE)
181 182 {
182 183 progress= SocExplorerEngine::getProgressBar("Reading on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
183 184 autopb.setProgressBar(progress);
184 185 }
185 186 SocExplorerEngine::message(this->plugin,QString("Enter read function, count=%1, RMAP_MAX_XFER_SIZE=%2").arg(count).arg(RMAP_MAX_XFER_SIZE),2);
186 187
187 188 //Quite stupide loop, I guess that I always get the number of byte I asked for!
188 189 while((int)count>=(int)RMAP_MAX_XFER_SIZE)
189 190 {
190 191 transactionID = manager->getRMAPtransactionID();
191 192 SocExplorerEngine::message(this->plugin,QString("New transactionID:%1").arg(transactionID),2);
192 193 RMAP_build_rx_request_header(
193 194 this->manager->destinationLogicalAddress,
194 195 this->manager->destinationKey,
195 196 this->manager->sourceLogicalAddress,
196 197 transactionID,
197 198 address+(read*4),
198 199 RMAP_MAX_XFER_SIZE*4,
199 200 requestBuffer+1);
200 201 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
201 202 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
202 203 if(len==-1)
203 204 {
204 205 this->toggleBridgeConnection();
205 206 return 0;
206 207 }
207 208 for(int i=0;i<((len-13)/4);i++)
208 209 {
209 210 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
210 211 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
211 212 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
212 213 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
213 214 }
214 215 free(RMAP_AnswerBuffer);
215 216 read+=RMAP_MAX_XFER_SIZE;
216 217 count-=RMAP_MAX_XFER_SIZE;
217 218 if(progress!=NULL)
218 219 {
219 220 progress->setValue(read);
220 221 qApp->processEvents();
221 222 }
222 223 }
223 224 if((int)count>0)
224 225 {
225 226 transactionID = manager->getRMAPtransactionID();
226 227 SocExplorerEngine::message(this->plugin,QString("New transactionID: %1").arg(transactionID),2);
227 228 SocExplorerEngine::message(this->plugin,QString("Building request with:"),2);
228 229 SocExplorerEngine::message(this->plugin,QString("Address = %1").arg(address+(read*4),8,16),2);
229 230 SocExplorerEngine::message(this->plugin,QString("Size = %1").arg(count*4),2);
230 231 SocExplorerEngine::message(this->plugin,QString("Size + 13 = %1").arg((count*4)+13),2);
231 232 RMAP_build_rx_request_header(
232 233 this->manager->destinationLogicalAddress,
233 234 this->manager->destinationKey,
234 235 this->manager->sourceLogicalAddress,
235 236 transactionID,
236 237 address+(read*4),
237 238 count*4,
238 239 requestBuffer+1);
239 240 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
240 241 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
241 242 if(len==-1)
242 243 {
243 244 this->toggleBridgeConnection();
244 245 return 0;
245 246 }
246 247 for(int i=0;i<((len-13)/4);i++)
247 248 {
248 249 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
249 250 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
250 251 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
251 252 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
252 253 }
253 254 free(RMAP_AnswerBuffer);
254 255 read+=count;
255 256 if(progress!=NULL)
256 257 {
257 258 progress->setValue(read);
258 259 qApp->processEvents();
259 260 }
260 261 }
261 262 return read;
262 263 }
263 264
264 265 void stardundeeSPW_USB::brickSelectionChanged(int brickIndex)
265 266 {
266 267 this->manager->selectedBrick = brickIndex-1;
267 268 SocExplorerEngine::message(plugin,QString("Changing brick index: %1").arg(manager->selectedBrick),1);
268 269 }
269 270
270 271 void stardundeeSPW_USB::linkNumberSelectionChanged(int linkIndex)
271 272 {
272 273 this->manager->linkNumber = linkIndex + 1;
273 274 SocExplorerEngine::message(plugin,QString("Changing Link Number: %1").arg(manager->linkNumber),1);
274 275 }
275 276
276 277 void stardundeeSPW_USB::linkSpeedSelectionChanged(const QString &linkSpeed)
277 278 {
278 279 this->manager->linkSpeed = linkSpeed.toInt();
279 280
280 281 SocExplorerEngine::message(plugin,QString("Changing Link Speed: %1").arg(manager->linkSpeed),1);
281 282 }
282 283
283 284 void stardundeeSPW_USB::sourceLogicalAddressChanged(const QString &sourceAddress)
284 285 {
285 286 this->manager->sourceLogicalAddress = sourceAddress.toInt();
286 287 SocExplorerEngine::message(plugin,QString("Changing Destination Key: %1").arg(manager->sourceLogicalAddress),1);
287 288 }
288 289
289 290 void stardundeeSPW_USB::destinationAddressChanged(const QString &rmapaddress)
290 291 {
291 292 this->manager->destinationLogicalAddress = rmapaddress.toInt();
292 293 SocExplorerEngine::message(plugin,QString("Changing RMAP address: %1").arg(manager->destinationLogicalAddress),1);
293 294 }
294 295
295 296 void stardundeeSPW_USB::destinationKeyChanged(const QString &key)
296 297 {
297 298 this->manager->destinationKey = key.toInt();
298 299 SocExplorerEngine::message(plugin,QString("Changing RMAP Key: %1").arg(manager->destinationKey),1);
299 300 }
300 301
301 302 void stardundeeSPW_USB::brickModeChanged( bool interfaceMode )
302 303 {
303 304 this->manager->interfaceMode = interfaceMode;
304 305 }
305 306
306 307 void stardundeeSPW_USB::timecodeFrequencyChanged(const QString &frequency)
307 308 {
308 309 this->manager->timecodeFrequency = frequency.toDouble();
309 310 this->manager->setTimecodeFrequency( this->manager->timecodeFrequency);
310 311 SocExplorerEngine::message(plugin,QString("Changing timecode frequency: %1").arg(manager->timecodeFrequency),1);
311 312 }
312 313
313 314 void stardundeeSPW_USB::startSendingTimecodes(bool onOff )
314 315 {
315 316 this->manager->sendTimecodePeriodically( onOff );
316 317 }
317 318
318 319 void stardundeeSPW_USB::rmapTimeoutChanged(const QString &timeout)
319 320 {
320 321 int tim=timeout.toInt();
321 322 if(tim<50)
322 323 {
323 324 tim = 50;
324 325 ((StarDundeeGUI*)this->p_GUI)->setRmapTimeout(QString("%1").arg(tim));
325 326 }
326 327 this->manager->RMAPtimeout = tim;
327 328 SocExplorerEngine::message(plugin,QString("Changing RMAP Timeout: %1").arg(manager->RMAPtimeout),1);
328 329 }
329 330
330 331 void stardundeeSPW_USB::makeGUI(socexplorerplugin *parent)
331 332 {
332 333 Q_UNUSED(parent)
333 334 this->p_GUI = new StarDundeeGUI();
334 335 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(connectClicked()),this,SLOT(toggleBridgeConnection()));
335 336 connect(this->manager,SIGNAL(updateAvailableBrickCount(int)),((StarDundeeGUI*)this->p_GUI),SLOT(updateAvailableBrickCount(int)));
336 337 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(brickSelectionChanged(int)),this,SLOT(brickSelectionChanged(int)));
337 338 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(linkNumberSelectionChanged(int)),this,SLOT(linkNumberSelectionChanged(int)));
338 339 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(linkSpeedSelectionChanged(QString)),this,SLOT(linkSpeedSelectionChanged(QString)));
339 340 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(sourceLogicalAddressChanged(QString)),this,SLOT(sourceLogicalAddressChanged(QString)));
340 341 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(rmapAddressChanged(QString)),this,SLOT(destinationAddressChanged(QString)));
341 342 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(destinationKeyChanged(QString)),this,SLOT(destinationKeyChanged(QString)));
342 343 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(rmapTimeoutChanged(QString)),this,SLOT(rmapTimeoutChanged(QString)));
343 344 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(brickModeChanged(bool)), this, SLOT(brickModeChanged(bool)));
344 345 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(timecodeFrequencyChange(QString)), this, SLOT(timecodeFrequencyChanged(QString)));
345 346 connect(((StarDundeeGUI*)this->p_GUI),SIGNAL(startSendingTimecode(bool)), this, SLOT(startSendingTimecodes(bool)));
346 347
347 348 this->brickSelectionChanged( ((StarDundeeGUI*)this->p_GUI)->getBrickSelection());
348 349 this->linkNumberSelectionChanged( ((StarDundeeGUI*)this->p_GUI)->getLinkNumberSelection());
349 350 this->linkSpeedSelectionChanged( ((StarDundeeGUI*)this->p_GUI)->getLinkSpeedSelection());
350 351 this->sourceLogicalAddressChanged(((StarDundeeGUI*)this->p_GUI)->getSourceAddress());
351 352 this->destinationAddressChanged( ((StarDundeeGUI*)this->p_GUI)->getDestinationAddress());
352 353 this->destinationKeyChanged( ((StarDundeeGUI*)this->p_GUI)->getDestinationKey());
353 354 this->rmapTimeoutChanged( ((StarDundeeGUI*)this->p_GUI)->getRmapTimeout());
354 355 this->brickModeChanged( ((StarDundeeGUI*)this->p_GUI)->isBrickSetAsAnInterface());
355 356
356 357 connect(this,SIGNAL(SelectBrick(int)), ((StarDundeeGUI*)this->p_GUI),SLOT(selectBrick(int)));
357 358 connect(this,SIGNAL(SelectLinkNumber(int)), ((StarDundeeGUI*)this->p_GUI),SLOT(selectLinkNumber(int)));
358 359 connect(this,SIGNAL(SelectLinkSpeed(int)), ((StarDundeeGUI*)this->p_GUI),SLOT(selectLinkSpeed(int)));
359 360 connect(this,SIGNAL(SetDestinationKey(QString)), ((StarDundeeGUI*)this->p_GUI),SLOT(setDestinationKey(QString)));
360 361 connect(this,SIGNAL(SetDestinationAddress(QString)),((StarDundeeGUI*)this->p_GUI),SLOT(setDestinationAddress(QString)));
361 362 connect(this,SIGNAL(SetSourceAddress(QString)), ((StarDundeeGUI*)this->p_GUI),SLOT(setSourceAddress(QString)));
362 363 connect(this,SIGNAL(SetRmapTimeout(QString)), ((StarDundeeGUI*)this->p_GUI),SLOT(setRmapTimeout(QString)));
363 364 connect(this,SIGNAL(GetAvailableBrickCount()), ((StarDundeeGUI*)this->p_GUI),SLOT(getAvailableBrickCount()));
364 365 connect(this,SIGNAL(GetNbPacketsTransmittedToSpw()),((StarDundeeGUI*)this->p_GUI),SLOT(getNbPacketsTransmittedToSpw()));
365 366 connect(this,SIGNAL(GetNbCCSDSPacketsTransmittedToSpw()),
366 367 ((StarDundeeGUI*)this->p_GUI),SLOT(getNbCCSDSPacketsTransmittedToSpw()));
367 368 connect(this,SIGNAL(SetBrickAsAnInterface(bool)), ((StarDundeeGUI*)this->p_GUI),SLOT(setBrickAsAnInterface(bool)));
368 369 connect(this,SIGNAL(SetBrickAsARouter(bool)), ((StarDundeeGUI*)this->p_GUI),SLOT(setBrickAsARouter(bool)));
369 370 connect(this,SIGNAL(BytesReceivedFromSpw(uint)), ((StarDundeeGUI*)this->p_GUI),SLOT(updateNbReceivedBytesFromSpw(uint)));
370 371 connect(this,SIGNAL(BytesTransmittedToSpw(uint)), ((StarDundeeGUI*)this->p_GUI),SLOT(updateNbTransmittedBytesToSpw(uint)));
371 372 connect(this,SIGNAL(CCSDSPacketTransmittedToSpw()), ((StarDundeeGUI*)this->p_GUI),SLOT(updateCCSDSPacketTransmittedToSpw()));
372 373 connect(this,SIGNAL(SetTimecodeFrequency(double)), ((StarDundeeGUI*)this->p_GUI),SLOT(setTimecodeFrequency(double)));
373 374 connect(this,SIGNAL(StartSendingTimecodes(bool)), ((StarDundeeGUI*)this->p_GUI),SLOT(setStartSendingTimecodes(bool)));
374 375
375 376 connect(this,SIGNAL(SendOneTimecode(unsigned char)),this->manager, SLOT(sendOneTimecode(unsigned char)));
376 377 connect(this,SIGNAL(GetLinkNumber()), this->manager, SLOT(getLinkNumber()));
377 378 }
378 379
379 380 void stardundeeSPW_USB::sendPacketComingFromTCPServer(char *packet, int size)
380 381 {
381 382 char* data;
382 383 int i;
383 384
384 385 data = (char *) malloc( size + 5 );
385 386
386 387 data[0] = this->manager->linkNumber;
387 388 data[1] = this->manager->destinationLogicalAddress; // target logical address
388 389 data[2] = SPW_PROTO_ID_CCSDS; // protocol identifier
389 390 data[3] = 0x00; // reserved
390 391 data[4] = 0x00; // user application
391 392
392 393 for ( i=0; i<size; i++ )
393 394 {
394 395 data[i+5] = packet[i];
395 396 }
396 397
397 398 this->manager->sendPacket( data, size + 5);
398 399
399 400 free(data);
400 401 free(packet);
401 402 }
402 403
403 404 stardundeeSPW_USB_Manager::stardundeeSPW_USB_Manager(socexplorerplugin *plugin, QObject *parent)
404 405 :abstractSpwManager(plugin, parent)
405 406 {
406 407 // TODO remove this crap!
407 408 this->initDialog();
408 409 // this->moveToThread(this);
409 410 }
410 411
411 412 stardundeeSPW_USB_Manager::~stardundeeSPW_USB_Manager()
412 413 {
413 414 if(this->connected)disconnectBridge();
414 415 this->requestInterruption();
415 416 while(!this->isFinished());
416 417 }
417 418
418 419 void stardundeeSPW_USB_Manager::run()
419 420 {
420 421 USB_SPACEWIRE_PACKET_PROPERTIES properties;
421 422 USB_SPACEWIRE_ID pIdentifier=NULL;
422 423 USB_SPACEWIRE_STATUS stat;
423 424 SocExplorerEngine::message(this->plugin,"Starting Startdundee USB pooling thread",1);
424 425 char buffer[(RMAP_MAX_XFER_SIZE*4)+50];
425 426 while (!QThread::currentThread()->isInterruptionRequested())
426 427 {
427 428 if(this->connected)
428 429 {
429 430 this->handleMutex->lock();
430 431 SocExplorerEngine::message(this->plugin,"Looking for new RMAP packets",4);
431 432 if(USBSpaceWire_WaitOnReadPacketAvailable(hDevice,0.01))
432 433 {
433 434 SocExplorerEngine::message(this->plugin,"Got packet",2);
434 435 stat = USBSpaceWire_ReadPackets(hDevice, buffer, (RMAP_MAX_XFER_SIZE*4)+50,1, 1, &properties, &pIdentifier);
435 436 if (stat == TRANSFER_SUCCESS)
436 437 {
437 438 if(USBSpaceWire_GetReadTrafficType(&properties, 0) ==SPACEWIRE_TRAFFIC_PACKET)
438 439 {
439 440 SocExplorerEngine::message(this->plugin,"It's a SPW packet",2);
440 441 if(USBSpaceWire_GetReadEOPStatus(&properties, 0)== SPACEWIRE_USB_EOP)
441 442 {
442 443 SocExplorerEngine::message(this->plugin,"Got end of packet",2);
443 444 emit bytesReceivedFromSpw( properties.len );
444 445 if(buffer[1]==(char)SPW_PROTO_ID_RMAP) //RMAP packet
445 446 {
446 447 RMAP_Answer* packet;
447 448 SocExplorerEngine::message(this->plugin,"Got RMAP packet",2);
448 449 SocExplorerEngine::message(this->plugin,QString("Rmap packet size %1").arg(properties.len),2);
449 450 char* packetbuffer = (char*)malloc(properties.len);
450 451 memcpy(packetbuffer,buffer,properties.len);
451 452 USBSpaceWire_FreeRead(hDevice, pIdentifier);
452 453 pIdentifier = NULL;
453 454 this->handleMutex->unlock();
454 455 if(properties.len==8)
455 456 {
456 457 packet=new RMAP_Answer(RMAP_get_transactionID(buffer),packetbuffer,properties.len);
457 458 }
458 459 else
459 460 {
460 461 packet=new RMAP_Answer(RMAP_get_transactionID(buffer+1),packetbuffer,properties.len);
461 462 }
462 463 RMAP_AnswersMtx->lock();
463 464 RMAP_Answers.append(packet);
464 465 RMAP_AnswersMtx->unlock();
465 466 RMAP_AnswersSem->release();
466 467 }
467 468 else //any non-rmap packet will be pushed to the network
468 469 {
469 470 char* packetbuffer = (char*)malloc(properties.len);
470 471 memcpy(packetbuffer,buffer,properties.len);
471 472 emit emitPacket(packetbuffer,properties.len);
472 473 USBSpaceWire_FreeRead(hDevice, pIdentifier);
473 474 this->handleMutex->unlock();
474 475 SocExplorerEngine::message(this->plugin,"Got SPW packet",2);
475 476 }
476 477 }
477 478 else
478 479 {
479 480 SocExplorerEngine::message(this->plugin,"No EOP received",2);
480 481 this->handleMutex->unlock();
481 482 }
482 483 }
483 484
484 485 }
485 486 else
486 487 {
487 488 USBSpaceWire_FreeRead(hDevice, pIdentifier);
488 489 this->handleMutex->unlock();
489 490 }
490 491 }
491 492 else
492 493 {
493 494 USBSpaceWire_FreeRead(hDevice, pIdentifier);
494 495 this->handleMutex->unlock();
495 496 }
496 497 }
497 498 else
498 499 {
499 500 //do some sanity checks!
500 501 int list = USBSpaceWire_ListDevices();
501 502 if(this->brickList!=list)
502 503 {
503 504 this->brickList = list;
504 505 emit updateAvailableBrickCount(this->brickList);
505 506 }
506 507 usleep(RMAPtimeout/2);
507 508 }
508 509 usleep(1000);
509 510 }
510 511 SocExplorerEngine::message(this->plugin,"Exiting Startdundee USB pooling thread",1);
511 512 }
512 513
513 514 bool stardundeeSPW_USB_Manager::connectBridge()
514 515 {
515 516 bool ret;
516 517
517 518 if (this->interfaceMode == BRICK_IS_SET_AS_AN_INTERFACE)
518 519 {
519 520 ret = connectBridgeAsInterface();
520 521 }
521 522 else if (this->interfaceMode == BRICK_IS_SET_AS_A_ROUTER)
522 523 {
523 524 ret = connectBridgeAsRouter();
524 525 }
525 526 else
526 527 {
527 528 ret = false;
528 529 }
529 530
530 531 return ret;
531 532 }
532 533
533 534 bool stardundeeSPW_USB_Manager::connectBridgeAsInterface()
534 535 {
535 536 this->handleMutex->lock();
536 537 int status;
537 538 U32 statusControl;
538 539 this->connected = false;
539 540 if (!USBSpaceWire_Open(&hDevice, this->selectedBrick)) // Open the USB device
540 541 {
541 542 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** ERROR: USBSpaceWire_Open(&hDevice, 0))",0);
542 543 this->handleMutex->unlock();
543 544 return false;
544 545 }
545 546 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** USBSpaceWire_Open successful",0);
546 547
547 548 USBSpaceWire_EnableNetworkMode(hDevice, 0); // deactivate the network mode
548 549 CFGSpaceWire_EnableRMAP(1); // Enable the use of RMAP for the StarDundee brick configuration
549 550 CFGSpaceWire_SetRMAPDestinationKey(0x20); // Set the destination key expected by STAR-Dundee devices
550 551
551 552 // Set the path and return path to the device
552 553 CFGSpaceWire_StackClear();
553 554 CFGSpaceWire_AddrStackPush(0);
554 555 CFGSpaceWire_AddrStackPush(254);
555 556 CFGSpaceWire_RetAddrStackPush(254);
556 557 // set the base transmit rate to 100 MHz
557 558 status = CFGSpaceWire_SetBrickBaseTransmitRate( hDevice, CFG_BRK_CLK_100_MHZ, CFG_BRK_DVDR_1, 0xff);
558 559 if (status != CFG_TRANSFER_SUCCESS)
559 560 {
560 561 SocExplorerEngine::message(this->plugin,"ERROR CFGSpaceWire_SetBrickBaseTransmitRate",1);
561 562 this->handleMutex->unlock();
562 563 return false;
563 564 }
564 565 else
565 566 {
566 567 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_SetBrickBaseTransmitRate, base rate = 100 MHz",1);
567 568 }
568 569
569 570 // read the link status
570 571 if (CFGSpaceWire_GetLinkStatusControl(hDevice, this->linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
571 572 {
572 573 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(this->linkNumber),1);
573 574 this->handleMutex->unlock();
574 575 return false;
575 576 }
576 577 else
577 578 {
578 579 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(this->linkNumber),1);
579 580
580 581 // Set the link status control register properties
581 582 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
582 583 CFGSpaceWire_LSEnableStart(&statusControl, 1);
583 584 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
584 585 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
585 586 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
586 587
587 588 // Set the link status control register
588 589 if (CFGSpaceWire_SetLinkStatusControl(hDevice, this->linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
589 590 {
590 591 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(this->linkNumber),1);
591 592 this->handleMutex->unlock();
592 593 return false;
593 594 }
594 595 else
595 596 {
596 597 SocExplorerEngine::message(this->plugin,"Set the link status control for link " + QString::number(this->linkNumber),1);
597 598 }
598 599 }
599 600
600 601 if (CFGSpaceWire_SetAsInterface(hDevice, 1, 0) != CFG_TRANSFER_SUCCESS)
601 602 {
602 603 SocExplorerEngine::message(this->plugin,"Could not set the device to be an interface",1);
603 604 this->handleMutex->unlock();
604 605 return false;
605 606 }
606 607 else
607 608 {
608 609 SocExplorerEngine::message(this->plugin,"Device set to be an interface",1);
609 610 }
610 611
611 612 USBSpaceWire_RegisterReceiveOnAllPorts(hDevice); // Register to receive on all ports
612 613 USBSpaceWire_ClearEndpoints(hDevice); // clear the USB endpoints
613 614 USBSpaceWire_SetTimeout(hDevice,1.0);
614 615 this->handleMutex->unlock();
615 616 SocExplorerEngine::message(this->plugin,"The driver's current send buffer size is " + QString::number(USBSpaceWire_GetDriverSendBufferSize(hDevice)) + " bytes",1);
616 617 SocExplorerEngine::message(this->plugin,"The driver's current read buffer size is " + QString::number(USBSpaceWire_GetDriverReadBufferSize(hDevice)) + " bytes",1);
617 618 SocExplorerEngine::message(this->plugin,"USBSpaceWire_IsReadThrottling is " + QString::number(USBSpaceWire_IsReadThrottling(hDevice)),1);
618 619 this->connected = true;
619 620 return true;
620 621 }
621 622
622 623 bool stardundeeSPW_USB_Manager::connectBridgeAsRouter()
623 624 {
624 625 // QMutexLocker mlock(&this->handleMutex);
625 626 this->handleMutex->lock();
626 627 int status;
627 628 U32 statusControl;
628 629 unsigned int linkStatus1;
629 630 unsigned int linkStatus2;
630 631 unsigned char linkNumber;
631 632 unsigned char deviceIsAnInterface;
632 633
633 634 if (!USBSpaceWire_Open(&hDevice, this->selectedBrick)) // Open the USB device
634 635 {
635 636 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** ERROR: USBSpaceWire_Open(&hDevice, 0))");
636 637 this->handleMutex->unlock();
637 638 return false;
638 639 }
639 640 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** USBSpaceWire_Open successful, device number: "
640 641 + QString::number(this->selectedBrick));
641 642
642 643 USBSpaceWire_EnableNetworkMode(hDevice, 0); // deactivate the network mode
643 644 CFGSpaceWire_EnableRMAP(1); // Enable the use of RMAP for the StarDundee brick configuration
644 645 CFGSpaceWire_SetRMAPDestinationKey(0x20); // Set the destination key expected by STAR-Dundee devices
645 646
646 647 // Set the path and return path to the device
647 648 // This affects just the operations performed by the Configuration Library and does not affect the packets
648 649 // sent and received using the driver API.
649 650 CFGSpaceWire_StackClear();
650 651 CFGSpaceWire_AddrStackPush(0);
651 652 CFGSpaceWire_AddrStackPush(254);
652 653 CFGSpaceWire_RetAddrStackPush(254);
653 654
654 655 // set the base transmit rate to 100 MHz
655 656 status = CFGSpaceWire_SetBrickBaseTransmitRate( hDevice, CFG_BRK_CLK_100_MHZ, CFG_BRK_DVDR_1, 0xff);
656 657 if (status != CFG_TRANSFER_SUCCESS)
657 658 {
658 659 SocExplorerEngine::message(this->plugin,"ERROR CFGSpaceWire_SetBrickBaseTransmitRate");
659 660 }
660 661 else SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_SetBrickBaseTransmitRate, base rate = 100 MHz");
661 662
662 663 //*********************
663 664 // LINK 1 CONFIGURATION
664 665 linkNumber = 1;
665 666 if (CFGSpaceWire_GetLinkStatusControl(hDevice, linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
666 667 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(linkNumber));
667 668 else
668 669 {
669 670 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(linkNumber));
670 671
671 672 // Set the link status control register properties
672 673 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
673 674 CFGSpaceWire_LSEnableStart(&statusControl, 1);
674 675 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
675 676 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
676 677 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
677 678
678 679 // Set the link status control register
679 680 if (CFGSpaceWire_SetLinkStatusControl(hDevice, linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
680 681 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(linkNumber));
681 682 else
682 683 SocExplorerEngine::message(this->plugin,"link status control for link " + QString::number(0x01) + " is set");
683 684 }
684 685
685 686 //*********************
686 687 // LINK 2 CONFIGURATION
687 688 linkNumber = 2;
688 689 if (CFGSpaceWire_GetLinkStatusControl(hDevice, linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
689 690 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(linkNumber));
690 691 else
691 692 {
692 693 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(linkNumber));
693 694
694 695 // Set the link status control register properties
695 696 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
696 697 CFGSpaceWire_LSEnableStart(&statusControl, 1);
697 698 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
698 699 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
699 700 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
700 701
701 702 // Set the link status control register
702 703 if (CFGSpaceWire_SetLinkStatusControl(hDevice, linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
703 704 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(linkNumber));
704 705 else
705 706 SocExplorerEngine::message(this->plugin,"link status control for link " + QString::number(linkNumber) + " is set");
706 707 }
707 708
708 709 //***************************
709 710 // SET THE DEVICE AS A ROUTER
710 711 deviceIsAnInterface = 0; // 0 = router, 1 = interface
711 712 if (CFGSpaceWire_SetAsInterface(hDevice, deviceIsAnInterface, 0) != CFG_TRANSFER_SUCCESS)
712 713 SocExplorerEngine::message(this->plugin,"Could not set the device to be an interface");
713 714 else
714 715 SocExplorerEngine::message(this->plugin,"Device is an interface: " + QString::number(deviceIsAnInterface) + " (1 => true, 0 => false)");
715 716
716 717 setRoutingTableEntry(0xfe, 0x02, 0); // [0010] => route 0xfe on port 1
717 718 setRoutingTableEntry(32 , 0x08, 0); // [1000] => route 32 on port 3
718 719
719 720 USBSpaceWire_RegisterReceiveOnAllPorts(hDevice); // Register to receive on port 1 only
720 721 USBSpaceWire_ClearEndpoints(hDevice); // clear the USB endpoints
721 722
722 723 SocExplorerEngine::message(this->plugin,"The driver's current send buffer size is " + QString::number(USBSpaceWire_GetDriverSendBufferSize(hDevice)) + " bytes");
723 724 SocExplorerEngine::message(this->plugin,"The driver's current read buffer size is " + QString::number(USBSpaceWire_GetDriverReadBufferSize(hDevice)) + " bytes");
724 725 SocExplorerEngine::message(this->plugin,"USBSpaceWire_IsReadThrottling is " + QString::number(USBSpaceWire_IsReadThrottling(hDevice)));
725 726
726 727 //************
727 728 // test Link 1 and Link 2
728 729 linkStatus1 = getLinkStatus(0x01);
729 730 linkStatus2 = getLinkStatus(0x02);
730 731 this->handleMutex->unlock();
731 732
732 733 if ((linkStatus1==1) || (linkStatus2==1))
733 734 {
734 735 initializeTimecodeGeneration();
735 736 this->connected=true;
736 737 return true;
737 738 }
738 739 else
739 740 {
740 741 statusLink1->setText("Link 1 status code: " + QString::number(linkStatus1));
741 742 statusLink2->setText("Link 2 status code: " + QString::number(linkStatus2));
742 743 starDundeeStatusQueryDialog->exec();
743 744 this->connected = false;
744 745 return false;
745 746 }
746 747 }
747 748
748 749 void stardundeeSPW_USB_Manager::initDialog( void )
749 750 {
750 751 // STAR DUNDEE STATUS QUERY DIALOG
751 752 starDundeeStatusQueryDialog = new QDialog;
752 753 starDundeeStatusQueryDialogLayout = new QGridLayout;
753 754 starDundeeStatusQueryDialogLabel = new QLabel(tr("SpaceWire links state"));
754 755 starDundeeStatusQueryContinueButton = new QPushButton(tr("Continue"));
755 756 starDundeeStatusQueryRetryButton = new QPushButton(tr("Retry"));
756 757 starDundeeStatusQueryAbortButton = new QPushButton(tr("Abort"));
757 758 statusLink1 = new QLabel(tr("Link 1 status code: -"));
758 759 statusLink2 = new QLabel(tr("Link 2 status code: -"));
759 760
760 761 starDundeeStatusQueryDialogLayout->addWidget(starDundeeStatusQueryDialogLabel, 0, 0, 1, 2);
761 762 starDundeeStatusQueryDialogLayout->addWidget(starDundeeStatusQueryContinueButton, 1, 0, 0);
762 763 starDundeeStatusQueryDialogLayout->addWidget(starDundeeStatusQueryRetryButton, 1, 1, 0);
763 764 starDundeeStatusQueryDialogLayout->addWidget(starDundeeStatusQueryAbortButton, 1, 2, 0);
764 765 starDundeeStatusQueryDialogLayout->addWidget(statusLink1, 2, 0, 0);
765 766 starDundeeStatusQueryDialogLayout->addWidget(statusLink2, 3, 0, 0);
766 767 starDundeeStatusQueryDialog->setLayout(starDundeeStatusQueryDialogLayout);
767 768 }
768 769
769 770 unsigned char stardundeeSPW_USB_Manager::setRoutingTableEntry(int tableEntry, U32 dwOutputPorts, char bDelHead)
770 771 {
771 772 U32 routingTableEntry;
772 773 // SET THE ROUTING TABLE ENTRY FOR LOGICAL ADDRESSING, TARGET entryNumber
773 774 if (CFGSpaceWire_ClearRoutingTableEntry(hDevice, tableEntry) != CFG_TRANSFER_SUCCESS)
774 775 {
775 776 SocExplorerEngine::message(this->plugin,"Could not clear routing table entry " + QString::number(tableEntry));
776 777 }
777 778 // Build the routing table entry
778 779 CFGSpaceWire_RTBuildRoutingTableEntry(&routingTableEntry,
779 780 dwOutputPorts, // route out of port dwOutputPorts
780 781 bDelHead, // header deletion is enabled [1] or disabled [0]
781 782 0); // priority normal
782 783 // Set the routing table entry for logical address tableEntry
783 784 if (CFGSpaceWire_SetRoutingTableEntry(hDevice, tableEntry, routingTableEntry) != CFG_TRANSFER_SUCCESS)
784 785 {
785 786 SocExplorerEngine::message(this->plugin,"Could not set routing table entry [" + QString::number(tableEntry) + "]");
786 787 }
787 788 else SocExplorerEngine::message(this->plugin,"Routing table entry [" + QString::number(tableEntry) + "] set" );
788 789 return 1;
789 790 }
790 791
791 792 unsigned int stardundeeSPW_USB_Manager::getRoutingTableEntry(int tableEntry)
792 793 {
793 794 U32 routingTableEntry, outputPorts;
794 795 char enabled, delHead, priority;
795 796 int portNum;
796 797
797 798 SocExplorerEngine::message(this->plugin,"GetRoutingTableEntry [" + QString::number(tableEntry) + "]");
798 799 // Read the routing table entry
799 800 if (CFGSpaceWire_GetRoutingTableEntry(hDevice, tableEntry, &routingTableEntry) != CFG_TRANSFER_SUCCESS)
800 801 {
801 802 SocExplorerEngine::message(this->plugin,"Could not read routing table entry [" + QString::number(tableEntry) + "]");
802 803 }
803 804 else
804 805 {
805 806 // Display the routing table entry properties
806 807 CFGSpaceWire_RTIsEnabled(routingTableEntry, &enabled);
807 808 CFGSpaceWire_RTIsDelHead(routingTableEntry, &delHead);
808 809 CFGSpaceWire_RTIsPriority(routingTableEntry, &priority);
809 810 CFGSpaceWire_RTGetOutputPorts(routingTableEntry, &outputPorts);
810 811 SocExplorerEngine::message(this->plugin,"CFGSpaceWire_RTIsEnabled : " + QString::number(enabled));
811 812 SocExplorerEngine::message(this->plugin,"CFGSpaceWire_RTIsDelHead : " + QString::number(delHead));
812 813 SocExplorerEngine::message(this->plugin,"CFGSpaceWire_RTIsPriority : " + QString::number(priority));
813 814 SocExplorerEngine::message(this->plugin,"CFGSpaceWire_RTGetOutputPorts : ");
814 815 for (portNum = 0; portNum < 32; portNum++)
815 816 {
816 817 if (outputPorts & (1 << portNum))
817 818 {
818 819 SocExplorerEngine::message(this->plugin,QString::number(portNum));
819 820 }
820 821 }
821 822 }
822 823
823 824 return 1;
824 825 }
825 826
826 827 void stardundeeSPW_USB_Manager::initializeTimecodeGeneration()
827 828 {
828 829 U32 dwTickEnableStatus;
829 830 U32 rtr_clk_freq;
830 831
831 832 // (1) RESET
832 833 if (!USBSpaceWire_TC_Reset(hDevice))
833 834 SocExplorerEngine::message(this->plugin,"ERR *** in Open *** Could not reset timecodes\n");
834 835
835 836 // (2) Clear the tick enable register
836 837 if (CFGSpaceWire_SetTickEnableStatus(hDevice, 6) != CFG_TRANSFER_SUCCESS)
837 838 SocExplorerEngine::message(this->plugin,"Could not clear the tick enable register");
838 839 else
839 840 SocExplorerEngine::message(this->plugin,"Cleared the tick enable register");
840 841
841 842 // (3) get the tick status
842 843 CFGSpaceWire_GetTickEnableStatus(hDevice, &dwTickEnableStatus);
843 844 SocExplorerEngine::message(this->plugin,"OK *** in Open *** CFGSpaceWire_GetTickEnableStatus, code is " + QString::number(dwTickEnableStatus, 2));
844 845
845 846 // (4) enable external timecode selection
846 847 if(!USBSpaceWire_TC_EnableExternalTimecodeSelection(hDevice,0))
847 848 SocExplorerEngine::message(this->plugin,"ERR *** disable external timecode selection");
848 849
849 850 rtr_clk_freq = USBSpaceWire_TC_GetClockFrequency(hDevice);
850 851
851 852 SocExplorerEngine::message(this->plugin,"clock frequency = " + QString::number(rtr_clk_freq) );
852 853
853 854 //**************************************************
854 855 // auto _ tick _ freq = rtr _ clk _ freq / freqCount
855 856 if (!USBSpaceWire_TC_SetAutoTickInFrequency(hDevice, rtr_clk_freq) )
856 857 SocExplorerEngine::message(this->plugin,"Could not set the tick-in frequency");
857 858 }
858 859
859 860 unsigned int stardundeeSPW_USB_Manager::getLinkStatus(unsigned char link)
860 861 {
861 862 U32 statusControl, errorStatus, portType;
862 863 U32 linkStatus=0, operatingSpeed, outputPortConnection;
863 864 char isLinkRunning, isAutoStart, isStart, isDisabled, isTristate;
864 865
865 866 // Read the link status control register
866 867 if (CFGSpaceWire_GetLinkStatusControl(hDevice, link, &statusControl) != CFG_TRANSFER_SUCCESS)
867 868 {
868 869 SocExplorerEngine::message(this->plugin,"Could not read link status control for link" + QString::number(link));
869 870 }
870 871 else
871 872 {
872 873 // Display the link status control register properties
873 874 CFGSpaceWire_LSPortType(statusControl, &portType);
874 875 if (portType == CFG_CONFIGURATION_PORT)
875 876 {
876 877 CFGSpaceWire_LSConfigErrorStatus(statusControl, &errorStatus);
877 878 }
878 879 else if (portType == CFG_SPACEWIRE_EXTERNAL_PORT)
879 880 {
880 881 CFGSpaceWire_LSExternalErrorStatus(statusControl, &errorStatus);
881 882 }
882 883 else
883 884 {
884 885 CFGSpaceWire_LSErrorStatus(statusControl, &errorStatus);
885 886 }
886 887 CFGSpaceWire_LSLinkState(statusControl, &linkStatus);
887 888 CFGSpaceWire_LSIsLinkRunning(statusControl, &isLinkRunning);
888 889 CFGSpaceWire_LSIsAutoStart(statusControl, &isAutoStart);
889 890 CFGSpaceWire_LSIsStart(statusControl, &isStart);
890 891 CFGSpaceWire_LSIsDisabled(statusControl, &isDisabled);
891 892 CFGSpaceWire_LSIsTristate(statusControl, &isTristate);
892 893 CFGSpaceWire_LSOperatingSpeed(statusControl, &operatingSpeed);
893 894 CFGSpaceWire_LSOutputPortConnection(statusControl, &outputPortConnection);
894 895 }
895 896 SocExplorerEngine::message(this->plugin,"status of link " + QString::number(link)
896 897 +" is " + dwLinkStatusQString[linkStatus]);
897 898 if (linkStatus == 5)
898 899 {
899 900 return 1;
900 901 }
901 902 else return 0;
902 903 }
903 904
904 905 bool stardundeeSPW_USB_Manager::disconnectBridge()
905 906 {
906 907 this->handleMutex->lock();
907 908 USBSpaceWire_UnregisterReceiveOnAllPorts(hDevice); // Stop receiving on all ports
908 909 USBSpaceWire_Close(hDevice); // Close the device
909 910 SocExplorerEngine::message(this->plugin,"stardundee *** Close *** USBSpaceWire_Close, device: " + QString::number(0),0);
910 911 this->handleMutex->unlock();
911 912 this->RMAP_pending_transaction_IDsMtx->lock();
912 913 this->RMAP_pending_transaction_IDs.clear();
913 914 this->RMAP_pending_transaction_IDsMtx->unlock();
914 915 this->RMAP_AnswersMtx->lock();
915 916 this->RMAP_Answers.clear();
916 917 this->RMAP_AnswersMtx->unlock();
917 918 this->RMAP_AnswersSem->acquire(this->RMAP_AnswersSem->available());
918 919 this->connected=false;
919 920 return true;
920 921 }
921 922
922 923 bool stardundeeSPW_USB_Manager::sendPacket(char *packet, int size)
923 924 {
924 925 char protocoleIdentifier;
925 926 USB_SPACEWIRE_STATUS result=TRANSFER_ERROR_NOT_FOUND;
926 927 USB_SPACEWIRE_ID pIdentifier;
927 928 SocExplorerEngine::message(this->plugin,"Sending SPW packet",2);
928 929 this->handleMutex->lock();
929 930 result = USBSpaceWire_SendPacket(hDevice,packet,size,1, &pIdentifier);
930 931 USBSpaceWire_FreeSend(hDevice, pIdentifier);
931 932 this->handleMutex->unlock();
932 933 if (result != TRANSFER_SUCCESS)
933 934 {
934 935 SocExplorerEngine::message(this->plugin,"ERR sending the READ command ",2);
935 936 return false;
936 937 }
937 938 else
938 939 {
939 940 emit bytesTransmittedToSpw( size-1 ); // -1 is for removing the first bytes added to the packet to route to the right link
940 941 // read the protocole identifier
941 942 protocoleIdentifier = packet[2];
942 943 if (protocoleIdentifier == SPW_PROTO_ID_CCSDS)
943 944 emit ccsdsPacketTransmittedToSpw();
944 945
945 946 SocExplorerEngine::message(this->plugin,"Packet sent",2);
946 947 }
947 948 return true;
948 949 }
949 950
950 951
951 952 void stardundeeSPW_USB_Manager::sendTimecodePeriodically( bool onOff )
952 953 {
953 954 this->handleMutex->lock();
954 955 if (onOff == true)
955 956 {
956 957 if (!USBSpaceWire_TC_EnableAutoTickIn(hDevice, 1, 1))
957 958 SocExplorerEngine::message(this->plugin,"Could not enable auto tick-in");
958 959 }
959 960 else
960 961 {
961 962 if (!USBSpaceWire_TC_EnableAutoTickIn(hDevice, 0, 0))
962 963 SocExplorerEngine::message(this->plugin,"Could not disable auto tick-in");
963 964 }
964 965 this->handleMutex->unlock();
965 966 }
966 967
967 968 int stardundeeSPW_USB_Manager::getLinkNumber( void )
968 969 {
969 970 return this->linkNumber;
970 971 }
971 972
972 973 void stardundeeSPW_USB_Manager::setTimecodeFrequency(double requestedFrequency)
973 974 {
974 975 U32 rtr_clk_freq=0;
975 976 U32 freqCount=0;
976 977 double freqCountInDouble=0.0;
977 978 double currentFrequency=0.0;
978 979
979 980 this->handleMutex->lock();
980 981 rtr_clk_freq = USBSpaceWire_TC_GetClockFrequency(hDevice);
981 982 freqCountInDouble = ((double) rtr_clk_freq) / requestedFrequency;
982 983 freqCount = (unsigned int) freqCountInDouble;
983 984
984 985 currentFrequency = ((double) rtr_clk_freq) / ((double) freqCount);
985 986
986 987 //**************************************************
987 988 // auto _ tick _ freq = rtr _ clk _ freq / freqCount
988 989 if (!USBSpaceWire_TC_SetAutoTickInFrequency(hDevice, freqCount) )
989 990 SocExplorerEngine::message(this->plugin,"Could not set the tick-in frequency");
990 991 else
991 992 SocExplorerEngine::message(this->plugin,"tick frequency set to " + QString::number(currentFrequency) +" Hz"
992 993 + " (freqCount set to " + QString::number(freqCount) + ")" );
993 994 this->handleMutex->unlock();
994 995 }
995 996
996 997 void stardundeeSPW_USB_Manager::sendOneTimecode( unsigned char nTimein )
997 998 {
998 999 this->handleMutex->lock();
999 1000 // enable external timecode selection
1000 1001 if(!USBSpaceWire_TC_EnableExternalTimecodeSelection(hDevice,1))
1001 1002 SocExplorerEngine::message(this->plugin,"sendOneTimecode *** ERR *** enable external timecode selection");
1002 1003
1003 1004 if (!USBSpaceWire_TC_PerformTickIn( hDevice, nTimein) )
1004 1005 SocExplorerEngine::message( this->plugin,"sendOneTimecode *** ERR *** Could not send the requested timecode: " + QString::number(nTimein) );
1005 1006 else
1006 1007 SocExplorerEngine::message( this->plugin,"sendOneTimecode *** OK *** timecode sent " + QString::number(nTimein) );
1007 1008
1008 1009 // disable external timecode selection
1009 1010 if(!USBSpaceWire_TC_EnableExternalTimecodeSelection(hDevice,0))
1010 1011 SocExplorerEngine::message(this->plugin,"sendOneTimecode *** ERR *** disable external timecode selection");
1011 1012 this->handleMutex->unlock();
1012 1013 }
1013 1014
1014 1015
@@ -1,187 +1,186
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 "spwplugin.h"
24 24 #include "stardundeespw_usb.h"
25 25 #include "gr_esb_bridge.h"
26 26 #include <socexplorerproxy.h>
27 27 #include "fakestardundeespwusb_lib.h"
28 28
29 29
30 30 spwplugin::spwplugin(QWidget *parent):socexplorerplugin(parent,false)
31 31 {
32 32 Q_UNUSED(parent)
33 33 FakeStarDundeeSpwUSB_Lib::resolve();
34 34 this->bridge = Q_NULLPTR;
35 35 this->scanDone = false;
36 // this->pyObject = new spwPyWrapper(this);
37 36 this->tcpServer = new SpwTcpPacketServer(this);
38 37 this->mainGroupBox = new QGroupBox("SpaceWire Plugin Configuration",this);
39 38 this->bridgeSelector = new QComboBox(this);
40 39 this->mainTabWidgt = new QTabWidget(this);
41 40 this->mainTabWidgt->addTab(this->mainGroupBox,"Bridge Configuration");
42 41 this->mainTabWidgt->addTab(this->tcpServer,"TCP Server");
43 42 this->mainLayout = new QGridLayout(this->mainGroupBox);
44 43 this->mainLayout->addWidget(new QLabel("Select SpaceWire bridge",this),0,0,1,1,Qt::AlignCenter);
45 44 this->mainLayout->addWidget(this->bridgeSelector,0,1,1,1);
46 45 this->setWidget(this->mainTabWidgt);
47 46 this->bridgeSelector->addItem("none");
48 47 this->bridgeSelector->addItem("STAR-Dundee Spw USB Brick");
49 48 this->bridgeSelector->addItem("GR-ESB");
50 49 connect(this->bridgeSelector,SIGNAL(currentIndexChanged(QString)),this,SLOT(bridgeSelectionChanged(QString)));
51 50 connect(this,SIGNAL(selectBridge(QString)),this,SLOT(selectBridge(QString)));
52 51 connect(this,SIGNAL(TCPServerConnect()),this->tcpServer,SLOT(connectServer()));
53 52 connect(this,SIGNAL(TCPServerDisconnect()),this->tcpServer,SLOT(disconnectServer()));
54 53 connect(this,SIGNAL(TCPServerSetPort(qint32)),this->tcpServer,SLOT(setServerPort(qint32)));
55 54 connect(this,SIGNAL(TCPServerSetIP(QString)),this->tcpServer,SLOT(setServerSetIP(QString)));
56 55 }
57 56
58 57
59 58 spwplugin::~spwplugin()
60 59 {
61 60 if(this->bridge!=Q_NULLPTR)
62 61 delete this->bridge;
63 62 }
64 63
65 64
66 65
67 66 unsigned int spwplugin::Read(unsigned int *Value,unsigned int count,unsigned int address)
68 67 {
69 68 if(Connected)
70 69 {
71 70 return bridge->Read(Value,count,address);
72 71 }
73 72 return 0;
74 73 }
75 74
76 75 void spwplugin::bridgeSelectionChanged(const QString &text)
77 76 {
78 77 printf("test");
79 78 if(text=="none")
80 79 {
81 80 if(this->bridge!=NULL)
82 81 {
83 82 this->mainLayout->removeWidget(this->bridge->getGUI());
84 83 this->bridge->getGUI()->hide();
85 84 this->disconnect(this,SLOT(setConnected(bool)));
86 85 delete this->bridge;
87 86 this->bridge= NULL;
88 87 }
89 88 }
90 89 if(text=="STAR-Dundee Spw USB Brick")
91 90 {
92 91 if(this->bridge!=NULL)
93 92 {
94 93 this->mainLayout->removeWidget(this->bridge->getGUI());
95 94 this->bridge->getGUI()->hide();
96 95 this->disconnect(this,SLOT(setConnected(bool)));
97 96 delete this->bridge;
98 97 }
99 98 this->bridge = new stardundeeSPW_USB(this);
100 99 this->mainLayout->addWidget(this->bridge->getGUI(),1,0,1,2);
101 100 connect(this->bridge,SIGNAL(setConnected(bool)),this,SLOT(setConnected(bool)));
102 101 connect(this,SIGNAL(StarDundeeSelectBrick(int)),((stardundeeSPW_USB*)bridge),SIGNAL(SelectBrick(int)));
103 102 connect(this,SIGNAL(StarDundeeSelectLinkNumber(int)),((stardundeeSPW_USB*)bridge),SIGNAL(SelectLinkNumber(int)));
104 103 connect(this,SIGNAL(StarDundeeSelectLinkSpeed(int)),((stardundeeSPW_USB*)bridge),SIGNAL(SelectLinkSpeed(int)));
105 104 connect(this,SIGNAL(StarDundeeSetSourceAddress(QString)),((stardundeeSPW_USB*)bridge),SIGNAL(SetSourceAddress(QString)));
106 105 connect(this,SIGNAL(StarDundeeSetDestinationAddress(QString)),((stardundeeSPW_USB*)bridge),SIGNAL(SetDestinationAddress(QString)));
107 106 connect(this,SIGNAL(StarDundeeSetDestinationKey(QString)),((stardundeeSPW_USB*)bridge),SIGNAL(SetDestinationKey(QString)));
108 107 connect(this,SIGNAL(StarDundeeSetRmapTimeout(QString)),((stardundeeSPW_USB*)bridge),SIGNAL(SetRmapTimeout(QString)));
109 108 connect(this,SIGNAL(connectBridge()),((stardundeeSPW_USB*)bridge),SLOT(connectBridge()));
110 109 connect(this,SIGNAL(disconnectBridge()),((stardundeeSPW_USB*)bridge),SLOT(disconnectBridge()));
111 110 connect(this,SIGNAL(StarDundeeGetAvailableBrickCount()),
112 111 ((stardundeeSPW_USB*)bridge),SIGNAL(GetAvailableBrickCount()));
113 112 connect(this,SIGNAL(StarDundeeGetNbPacketsTransmittedToSpw()),
114 113 ((stardundeeSPW_USB*)bridge),SIGNAL(GetNbPacketsTransmittedToSpw()));
115 114 connect(this,SIGNAL(StarDundeeGetNbCCSDSPacketsTransmittedToSpw()),
116 115 ((stardundeeSPW_USB*)bridge),SIGNAL(GetNbCCSDSPacketsTransmittedToSpw()));
117 116 connect(this,SIGNAL(StarDundeeGetLinkNumber()),((stardundeeSPW_USB*)bridge),SIGNAL(GetLinkNumber()));
118 117 connect(this,SIGNAL(StarDundeeSetBrickAsAninterface(bool)),
119 118 ((stardundeeSPW_USB*)bridge),SIGNAL(SetBrickAsAnInterface(bool)));
120 119 connect(this,SIGNAL(StarDundeeSetBrickAsARouter(bool)),
121 120 ((stardundeeSPW_USB*)bridge),SIGNAL(SetBrickAsARouter(bool)));
122 121 connect(this,SIGNAL(StarDundeeStartTimecodes(bool)),
123 122 ((stardundeeSPW_USB*)bridge),SIGNAL(StartSendingTimecodes(bool)));
124 123 connect(this,SIGNAL(StarDundeeSetTimecodeFrequency(double)),
125 124 ((stardundeeSPW_USB*)bridge),SIGNAL(SetTimecodeFrequency(double)));
126 125 connect(this,SIGNAL(StarDundeeSendOneTimecode(unsigned char)),
127 126 ((stardundeeSPW_USB*)bridge),SIGNAL(SendOneTimecode(unsigned char)));
128 127 connect(this->bridge,SIGNAL(pushPacketOverTCP(char*,int)),this->tcpServer,SLOT(pushPacket(char*,int)));
129 128 connect(this->tcpServer, SIGNAL(sendSPWPacket(char*,int)), ((stardundeeSPW_USB*)bridge), SLOT(sendPacketComingFromTCPServer(char*,int)));
130 129 }
131 130 if(text=="GR-ESB")
132 131 {
133 132 if(this->bridge!=NULL)
134 133 {
135 134 this->mainLayout->removeWidget(this->bridge->getGUI());
136 135 this->bridge->getGUI()->hide();
137 136 this->disconnect(this,SLOT(setConnected(bool)));
138 137 delete this->bridge;
139 138 }
140 139 this->bridge = new GR_ESB_bridge(this);
141 140 this->mainLayout->addWidget(this->bridge->getGUI(),1,0,1,2);
142 141 connect(this->bridge,SIGNAL(setConnected(bool)),this,SLOT(setConnected(bool)));
143 142 }
144 143 }
145 144
146 145
147 146 void spwplugin::selectBridge(const QString &text)
148 147 {
149 148
150 149 if(text=="none")
151 150 {
152 151 this->bridgeSelector->setCurrentIndex(0);
153 152 }
154 153 if(text=="STAR-Dundee Spw USB Brick")
155 154 {
156 155 this->bridgeSelector->setCurrentIndex(1);
157 156 }
158 157 }
159 158
160 159 void spwplugin::setConnected(bool connected)
161 160 {
162 161 this->bridgeSelector->setDisabled(connected);
163 162 this->Connected = connected;
164 163 emit activateSig(connected);
165 164 if(!this->scanDone)
166 165 {
167 166 socexplorerproxy::loadChildSysDriver(this,"AMBA_PLUGIN");
168 167 this->scanDone=true;
169 168 }
170 169 }
171 170
172 171 unsigned int spwplugin::Write(unsigned int *Value,unsigned int count, unsigned int address)
173 172 {
174 173 if(Connected)
175 174 {
176 175 return bridge->Write(Value,count,address);
177 176 }
178 177 return 0;
179 178 }
180 179
181 180
182 181
183 182
184 183
185 184
186 185
187 186
General Comments 0
You need to be logged in to leave comments. Login now