##// END OF EJS Templates
Working Snapshot
Jeandet Alexis -
r1:ca5cf5cd2804 Working Snapshot default
parent child
Show More
@@ -1,29 +1,29
1 #ifndef ABSTRACTSPWBRIDGE_H
1 #ifndef ABSTRACTSPWBRIDGE_H
2 #define ABSTRACTSPWBRIDGE_H
2 #define ABSTRACTSPWBRIDGE_H
3
3
4 #include <QObject>
4 #include <QObject>
5 #include <socexplorerplugin.h>
5 #include <socexplorerplugin.h>
6 #define RMAP_MAX_XFER_SIZE 3000 //slightly less than 16kBytes
6 #define RMAP_MAX_XFER_SIZE 4000 //slightly less than 16kBytes
7 #include <spw.h>
7 #include <spw.h>
8
8
9 class abstractSpwBridge : public QObject
9 class abstractSpwBridge : public QObject
10 {
10 {
11 Q_OBJECT
11 Q_OBJECT
12 public:
12 public:
13 explicit abstractSpwBridge(socexplorerplugin *parent);
13 explicit abstractSpwBridge(socexplorerplugin *parent);
14 QWidget *getGUI();
14 QWidget *getGUI();
15
15
16 public slots:
16 public slots:
17 virtual bool connectBridge();
17 virtual bool connectBridge();
18 virtual bool disconnectBridge();
18 virtual bool disconnectBridge();
19 virtual unsigned int Write(unsigned int *Value,unsigned int count, unsigned int address=0)=0;
19 virtual unsigned int Write(unsigned int *Value,unsigned int count, unsigned int address=0)=0;
20 virtual unsigned int Read(unsigned int *Value,unsigned int count, unsigned int address=0)=0;
20 virtual unsigned int Read(unsigned int *Value,unsigned int count, unsigned int address=0)=0;
21 virtual int pushRMAPPacket(char* packet,int size)=0;
21 virtual int pushRMAPPacket(char* packet,int size)=0;
22 protected:
22 protected:
23 socexplorerplugin* plugin;
23 socexplorerplugin* plugin;
24 QWidget* p_GUI;
24 QWidget* p_GUI;
25 private:
25 private:
26
26
27 };
27 };
28
28
29 #endif // ABSTRACTSPWBRIDGE_H
29 #endif // ABSTRACTSPWBRIDGE_H
@@ -1,424 +1,464
1 #include "stardundeespw_usb.h"
1 #include "stardundeespw_usb.h"
2 #include <socexplorerengine.h>
2 #include <socexplorerengine.h>
3 #include <qhexedit.h>
3 #include <qhexedit.h>
4
4
5 stardundeeSPW_USB::stardundeeSPW_USB(socexplorerplugin *parent) :
5 stardundeeSPW_USB::stardundeeSPW_USB(socexplorerplugin *parent) :
6 abstractSpwBridge(parent)
6 abstractSpwBridge(parent)
7 {
7 {
8 Q_UNUSED(parent)
8 Q_UNUSED(parent)
9 this->manager = new stardundeeSPW_USB_Manager(parent);
9 this->manager = new stardundeeSPW_USB_Manager(parent);
10 this->manager->start();
10 this->manager->start();
11 }
11 }
12
12
13 bool stardundeeSPW_USB::connectBridge()
13 bool stardundeeSPW_USB::connectBridge()
14 {
14 {
15 return this->manager->connectBridge();
15 return this->manager->connectBridge();
16 }
16 }
17
17
18 bool stardundeeSPW_USB::disconnectBridge()
18 bool stardundeeSPW_USB::disconnectBridge()
19 {
19 {
20 return this->manager->disconnectBridge();
20 return this->manager->disconnectBridge();
21 }
21 }
22
22
23 int stardundeeSPW_USB::pushRMAPPacket(char *packet, int size)
23 int stardundeeSPW_USB::pushRMAPPacket(char *packet, int size)
24 {
24 {
25 return this->manager->sendPacket(packet,size);
25 return this->manager->sendPacket(packet,size);
26 }
26 }
27
27
28 unsigned int stardundeeSPW_USB::Write(unsigned int *Value, unsigned int count, unsigned int address)
28 unsigned int stardundeeSPW_USB::Write(unsigned int *Value, unsigned int count, unsigned int address)
29 {
29 {
30 //Add transactionID!
30 char writeBuffer[RMAP_WRITE_PACKET_MIN_SZ((RMAP_MAX_XFER_SIZE*4))+1];
31 char writeBuffer[RMAP_WRITE_PACKET_MIN_SZ(RMAP_MAX_XFER_SIZE)+1];
32 writeBuffer[0]=1;//Link number
31 writeBuffer[0]=1;//Link number
33 int transactionID = 0;
32 int transactionID = 0;
34 int written=0;
33 int written=0;
35 SocExplorerEngine::message(this->plugin,"Enter Write function");
34 SocExplorerEngine::message(this->plugin,"Enter Write function");
35 QProgressBar* progress=NULL;
36 if(count>RMAP_MAX_XFER_SIZE)
37 progress= SocExplorerEngine::getProgressBar("Writing on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
36 //Quite stupide loop, I guess that I always get the number of byte I asked for!
38 //Quite stupide loop, I guess that I always get the number of byte I asked for!
37 for(;count>=RMAP_MAX_XFER_SIZE;count-=RMAP_MAX_XFER_SIZE)
39 while(count>=RMAP_MAX_XFER_SIZE)
38 {
40 {
39 for(int i=0;i<(RMAP_MAX_XFER_SIZE);i++)
41 for(int i=0;i<(RMAP_MAX_XFER_SIZE);i++)
40 {
42 {
41 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char) ((unsigned int) Value[written+i]>>24);
43 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
42 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char) ((unsigned int) Value[written+i]>>16);
44 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
43 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char) ((unsigned int) Value[written+i]>>8);
45 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
44 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char) ((unsigned int) Value[written+i]);
46 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
45 }
47 }
46 RMAP_build_tx_request_header(254,2,1,transactionID,address+written,RMAP_MAX_XFER_SIZE*4,writeBuffer+1);
48 RMAP_build_tx_request_header(254,2,1,transactionID,address+(written*4),RMAP_MAX_XFER_SIZE*4,writeBuffer+1);
47 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(RMAP_MAX_XFER_SIZE*4)+1);
49 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(RMAP_MAX_XFER_SIZE*4)+1);
48 written+=RMAP_MAX_XFER_SIZE;
50 written+=RMAP_MAX_XFER_SIZE;
51 count-=RMAP_MAX_XFER_SIZE;
52 progress->setValue(written);
53 qApp->processEvents();
49 }
54 }
50 if(count>0)
55 if(count>0)
51 {
56 {
52 for(int i=0;i<((int)count);i++)
57 for(int i=0;i<((int)count);i++)
53 {
58 {
54 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char) ((unsigned int) Value[written+i]>>24);
59 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+1] = (char)(((unsigned int)Value[i+written]>>24)&0xFF);
55 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char) ((unsigned int) Value[written+i]>>16);
60 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+2] = (char)(((unsigned int)Value[i+written]>>16)&0xFF);
56 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char) ((unsigned int) Value[written+i]>>8);
61 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+3] = (char)(((unsigned int)Value[i+written]>>8)&0xFF);
57 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char) ((unsigned int) Value[written+i]);
62 writeBuffer[RMAP_WRITE_HEADER_MIN_SZ+(i*4)+4] = (char)(((unsigned int)Value[i+written])&0xFF);
58 }
63 }
59 RMAP_build_tx_request_header(254,2,1,transactionID,address+written,count*4,writeBuffer+1);
64 RMAP_build_tx_request_header(254,2,1,transactionID,address+(written*4),count*4,writeBuffer+1);
60 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(count*4) +1);
65 manager->sendPacket(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(count*4) +1);
61 // QHexEdit* viewer = new QHexEdit();
62 // viewer->setData(QByteArray(writeBuffer,RMAP_WRITE_PACKET_MIN_SZ(count*4)+1));
63 // viewer->show();
64 written+=count;
66 written+=count;
67 if(progress!=NULL)
68 {
69 progress->setValue(written);
70 qApp->processEvents();
71 }
72 }
73 if(progress!=NULL)
74 {
75 SocExplorerEngine::deleteProgressBar(progress);
65 }
76 }
66 return written;
77 return written;
67 }
78 }
68
79
69 unsigned int stardundeeSPW_USB::Read(unsigned int *Value, unsigned int count, unsigned int address)
80 unsigned int stardundeeSPW_USB::Read(unsigned int *Value, unsigned int count, unsigned int address)
70 {
81 {
71 //Add transactionID!
72 char requestBuffer[RMAP_READ_HEADER_MIN_SZ+1];
82 char requestBuffer[RMAP_READ_HEADER_MIN_SZ+1];
73 char* RMAP_AnswerBuffer;
83 char* RMAP_AnswerBuffer;
74 requestBuffer[0]=1;//Link number
84 requestBuffer[0]=1;//Link number
75 int transactionID = 0;
85 int transactionID = 0;
76 int read=0;
86 int read=0;
77 // SocExplorerEngine::message(this->plugin,"Enter read function");
87 QProgressBar* progress=NULL;
88 if(count>RMAP_MAX_XFER_SIZE)
89 progress= SocExplorerEngine::getProgressBar("Reading on SPW @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
78 SocExplorerEngine::message(this->plugin,QString("Enter read function, count=%1, RMAP_MAX_XFER_SIZE=%2").arg(count).arg(RMAP_MAX_XFER_SIZE));
90 SocExplorerEngine::message(this->plugin,QString("Enter read function, count=%1, RMAP_MAX_XFER_SIZE=%2").arg(count).arg(RMAP_MAX_XFER_SIZE));
79
91
80 //Quite stupide loop, I guess that I always get the number of byte I asked for!
92 //Quite stupide loop, I guess that I always get the number of byte I asked for!
81 while((int)count>=(int)RMAP_MAX_XFER_SIZE)
93 while((int)count>=(int)RMAP_MAX_XFER_SIZE)
82 {
94 {
83 transactionID = manager->getRMAPtransactionID();
95 transactionID = manager->getRMAPtransactionID();
84 SocExplorerEngine::message(this->plugin,QString("New transactionID:%1").arg(transactionID));
96 SocExplorerEngine::message(this->plugin,QString("New transactionID:%1").arg(transactionID));
85 RMAP_build_rx_request_header(254,2,1,transactionID,address+(read*4),RMAP_MAX_XFER_SIZE*4,requestBuffer+1);
97 RMAP_build_rx_request_header(254,2,1,transactionID,address+(read*4),RMAP_MAX_XFER_SIZE*4,requestBuffer+1);
86 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
98 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
87 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
99 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
88 for(int i=0;i<(len/4);i++)
100 for(int i=0;i<((len-13)/4);i++)
89 {
101 {
90 Value[read+i]=((unsigned int)(RMAP_AnswerBuffer[i])<<24) + ((unsigned int)(RMAP_AnswerBuffer[i+1])<<16) + ((unsigned int)(RMAP_AnswerBuffer[i+2])<<8) + ((unsigned int)(RMAP_AnswerBuffer[i+3]));
102 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
103 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
104 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
105 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
91 }
106 }
92 free(RMAP_AnswerBuffer);
107 free(RMAP_AnswerBuffer);
93 read+=RMAP_MAX_XFER_SIZE;
108 read+=RMAP_MAX_XFER_SIZE;
94 count-=RMAP_MAX_XFER_SIZE;
109 count-=RMAP_MAX_XFER_SIZE;
110 progress->setValue(read);
111 qApp->processEvents();
95 }
112 }
96 if((int)count>0)
113 if((int)count>0)
97 {
114 {
98 transactionID = manager->getRMAPtransactionID();
115 transactionID = manager->getRMAPtransactionID();
99 SocExplorerEngine::message(this->plugin,QString("New transactionID: %1").arg(transactionID));
116 SocExplorerEngine::message(this->plugin,QString("New transactionID: %1").arg(transactionID));
100 SocExplorerEngine::message(this->plugin,QString("Building request with:"));
117 SocExplorerEngine::message(this->plugin,QString("Building request with:"));
101 SocExplorerEngine::message(this->plugin,QString("Address = %1").arg(address+(read*4),8,16));
118 SocExplorerEngine::message(this->plugin,QString("Address = %1").arg(address+(read*4),8,16));
102 SocExplorerEngine::message(this->plugin,QString("Size = %1").arg(count*4));
119 SocExplorerEngine::message(this->plugin,QString("Size = %1").arg(count*4));
103 SocExplorerEngine::message(this->plugin,QString("Size + 13 = %1").arg((count*4)+13));
120 SocExplorerEngine::message(this->plugin,QString("Size + 13 = %1").arg((count*4)+13));
104 RMAP_build_rx_request_header(254,2,1,transactionID,address+(read*4),count*4,requestBuffer+1);
121 RMAP_build_rx_request_header(254,2,1,transactionID,address+(read*4),count*4,requestBuffer+1);
105 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
122 manager->sendPacket(requestBuffer,RMAP_READ_HEADER_MIN_SZ+1);
106 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
123 int len=manager->getRMAPanswer(transactionID,&RMAP_AnswerBuffer);
107 for(int i=0;i<(len/4);i++)
124 for(int i=0;i<((len-13)/4);i++)
108 {
125 {
109 Value[read+i]=((unsigned int)(RMAP_AnswerBuffer[i])<<24) + ((unsigned int)(RMAP_AnswerBuffer[i+1])<<16) + ((unsigned int)(RMAP_AnswerBuffer[i+2])<<8) + ((unsigned int)(RMAP_AnswerBuffer[i+3]));
126 Value[read+i] = 0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+12]);
127 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+13]));
128 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+14]));
129 Value[read+i] = (Value[read+i]<<8) + (0x0FF & ((unsigned int)RMAP_AnswerBuffer[(4*i)+15]));
110 }
130 }
111 free(RMAP_AnswerBuffer);
131 free(RMAP_AnswerBuffer);
112 read+=count;
132 read+=count;
133 if(progress!=NULL)
134 {
135 progress->setValue(read);
136 qApp->processEvents();
137 }
138 }
139 if(progress!=NULL)
140 {
141 SocExplorerEngine::deleteProgressBar(progress);
113 }
142 }
114 return read;
143 return read;
115 }
144 }
116
145
117 stardundeeSPW_USB_Manager::stardundeeSPW_USB_Manager(socexplorerplugin *parent)
146 stardundeeSPW_USB_Manager::stardundeeSPW_USB_Manager(socexplorerplugin *parent)
118 :QThread((QObject*)parent)
147 :QThread((QObject*)parent)
119 {
148 {
120 this->handleMutex = new QMutex(QMutex::NonRecursive);
149 this->handleMutex = new QMutex(QMutex::NonRecursive);
121 this->RMAP_AnswersSem = new QSemaphore(0);
150 this->RMAP_AnswersSem = new QSemaphore(0);
122 this->RMAP_AnswersMtx=new QMutex(QMutex::Recursive);
151 this->RMAP_AnswersMtx=new QMutex(QMutex::Recursive);
123 this->RMAP_pending_transaction_IDsMtx=new QMutex(QMutex::Recursive);
152 this->RMAP_pending_transaction_IDsMtx=new QMutex(QMutex::Recursive);
124 this->plugin = parent;
153 this->plugin = parent;
125 connected = false;
154 connected = false;
126 }
155 }
127
156
128 stardundeeSPW_USB_Manager::~stardundeeSPW_USB_Manager()
157 stardundeeSPW_USB_Manager::~stardundeeSPW_USB_Manager()
129 {
158 {
130 this->terminate();
159 this->terminate();
131 while (!this->isFinished()) {
160 while (!this->isFinished()) {
132 this->usleep(1000);
161 this->usleep(1000);
133 }
162 }
134 }
163 }
135
164
136
165
137 void stardundeeSPW_USB_Manager::run()
166 void stardundeeSPW_USB_Manager::run()
138 {
167 {
139 USB_SPACEWIRE_PACKET_PROPERTIES properties;
168 USB_SPACEWIRE_PACKET_PROPERTIES properties;
140 USB_SPACEWIRE_ID pIdentifier=NULL;
169 USB_SPACEWIRE_ID pIdentifier=NULL;
141 USB_SPACEWIRE_STATUS stat;
170 USB_SPACEWIRE_STATUS stat;
142 SocExplorerEngine::message(this->plugin,"Starting Startdundee USB pooling thread");
171 SocExplorerEngine::message(this->plugin,"Starting Startdundee USB pooling thread");
143 char buffer[RMAP_MAX_XFER_SIZE*4];
172 char buffer[(RMAP_MAX_XFER_SIZE*4)+50];
144 while (!this->isInterruptionRequested())
173 while (!this->isInterruptionRequested())
145 {
174 {
146 if(this->connected)
175 if(this->connected)
147 {
176 {
148 handleMutex->lock();
177 handleMutex->lock();
149 //SocExplorerEngine::message(this->plugin,"Looking for new RMAP packets");
178 //SocExplorerEngine::message(this->plugin,"Looking for new RMAP packets");
150 if(USBSpaceWire_WaitOnReadPacketAvailable(hDevice,0.01))
179 if(USBSpaceWire_WaitOnReadPacketAvailable(hDevice,0.01))
151 {
180 {
152 SocExplorerEngine::message(this->plugin,"Got packet");
181 SocExplorerEngine::message(this->plugin,"Got packet");
153 stat = USBSpaceWire_ReadPackets(hDevice, buffer, RMAP_MAX_XFER_SIZE*4,1, 1, &properties, &pIdentifier);
182 stat = USBSpaceWire_ReadPackets(hDevice, buffer, (RMAP_MAX_XFER_SIZE*4)+50,1, 1, &properties, &pIdentifier);
154 if (stat == TRANSFER_SUCCESS)
183 if (stat == TRANSFER_SUCCESS)
155 {
184 {
156 if(USBSpaceWire_GetReadTrafficType(&properties, 0) ==SPACEWIRE_TRAFFIC_PACKET)
185 if(USBSpaceWire_GetReadTrafficType(&properties, 0) ==SPACEWIRE_TRAFFIC_PACKET)
157 {
186 {
158 SocExplorerEngine::message(this->plugin,"It's a SPW packet");
187 SocExplorerEngine::message(this->plugin,"It's a SPW packet");
159 if(USBSpaceWire_GetReadEOPStatus(&properties, 0)== SPACEWIRE_USB_EOP)
188 if(USBSpaceWire_GetReadEOPStatus(&properties, 0)== SPACEWIRE_USB_EOP)
160 {
189 {
161 SocExplorerEngine::message(this->plugin,"Got end of packet");
190 SocExplorerEngine::message(this->plugin,"Got end of packet");
162 if(buffer[1]==(char)SPW_PROTO_ID_RMAP) //RMAP packet
191 if(buffer[1]==(char)SPW_PROTO_ID_RMAP) //RMAP packet
163 {
192 {
164 SocExplorerEngine::message(this->plugin,"Got RMAP packet");
193 SocExplorerEngine::message(this->plugin,"Got RMAP packet");
165 SocExplorerEngine::message(this->plugin,QString("Rmap packet size %1").arg(properties.len));
194 SocExplorerEngine::message(this->plugin,QString("Rmap packet size %1").arg(properties.len));
166 char* packetbuffer = (char*)malloc(properties.len-13);
195 if(properties.len>8)
167 memcpy(packetbuffer,buffer+12,properties.len-13);
196 {
168 USBSpaceWire_FreeRead(hDevice, pIdentifier);
197 char* packetbuffer = (char*)malloc(properties.len);
169 pIdentifier = NULL;
198 memcpy(packetbuffer,buffer,properties.len);
170 handleMutex->unlock();
199 USBSpaceWire_FreeRead(hDevice, pIdentifier);
171 RMAP_Answer* packet=new RMAP_Answer(RMAP_get_transactionID(buffer+1),packetbuffer,properties.len);
200 pIdentifier = NULL;
172 RMAP_AnswersMtx->lock();
201 handleMutex->unlock();
173 RMAP_Answers.append(packet);
202 RMAP_Answer* packet=new RMAP_Answer(RMAP_get_transactionID(buffer+1),packetbuffer,properties.len);
174 RMAP_AnswersMtx->unlock();
203 RMAP_AnswersMtx->lock();
175 RMAP_AnswersSem->release();
204 RMAP_Answers.append(packet);
205 RMAP_AnswersMtx->unlock();
206 RMAP_AnswersSem->release();
207
208 }
209 else //it's a RMAP write response
210 {
211 USBSpaceWire_FreeRead(hDevice, pIdentifier);
212 pIdentifier = NULL;
213 handleMutex->unlock();
214 }
215
176 }
216 }
177 else //any non-rmap packet will be pushed to the network
217 else //any non-rmap packet will be pushed to the network
178 {
218 {
179 USBSpaceWire_FreeRead(hDevice, pIdentifier);
219 USBSpaceWire_FreeRead(hDevice, pIdentifier);
180 handleMutex->unlock();
220 handleMutex->unlock();
181 SocExplorerEngine::message(this->plugin,"Got SPW packet");
221 SocExplorerEngine::message(this->plugin,"Got SPW packet");
182 }
222 }
183 }
223 }
184 else
224 else
185 {
225 {
186 SocExplorerEngine::message(this->plugin,"No EOP received");
226 SocExplorerEngine::message(this->plugin,"No EOP received");
187 }
227 }
188 }
228 }
189
229
190 }
230 }
191 else
231 else
192 {
232 {
193 USBSpaceWire_FreeRead(hDevice, pIdentifier);
233 USBSpaceWire_FreeRead(hDevice, pIdentifier);
194 handleMutex->unlock();
234 handleMutex->unlock();
195 }
235 }
196 }
236 }
197 else
237 else
198 {
238 {
199 USBSpaceWire_FreeRead(hDevice, pIdentifier);
239 USBSpaceWire_FreeRead(hDevice, pIdentifier);
200 handleMutex->unlock();
240 handleMutex->unlock();
201 }
241 }
202 }
242 }
203 else
243 else
204 {
244 {
205 sleep(1);
245 sleep(1);
206 SocExplorerEngine::message(this->plugin,"Bridge not connected");
246 SocExplorerEngine::message(this->plugin,"Bridge not connected");
207 }
247 }
208 usleep(1000);
248 usleep(1000);
209 // sleep(2);
249 // sleep(2);
210 }
250 }
211 SocExplorerEngine::message(this->plugin,"Exiting Startdundee USB pooling thread");
251 SocExplorerEngine::message(this->plugin,"Exiting Startdundee USB pooling thread");
212 }
252 }
213
253
214 bool stardundeeSPW_USB_Manager::connectBridge()
254 bool stardundeeSPW_USB_Manager::connectBridge()
215 {
255 {
216 this->handleMutex->lock();
256 this->handleMutex->lock();
217 int status;
257 int status;
218 U32 statusControl;
258 U32 statusControl;
219 int brickNumber=0;
259 int brickNumber=0;
220 int linkNumber=1;
260 int linkNumber=1;
221 this->connected = false;
261 this->connected = false;
222 if (!USBSpaceWire_Open(&hDevice, brickNumber)) // Open the USB device
262 if (!USBSpaceWire_Open(&hDevice, brickNumber)) // Open the USB device
223 {
263 {
224 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** ERROR: USBSpaceWire_Open(&hDevice, 0))");
264 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** ERROR: USBSpaceWire_Open(&hDevice, 0))");
225 this->handleMutex->unlock();
265 this->handleMutex->unlock();
226 return false;
266 return false;
227 }
267 }
228 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** USBSpaceWire_Open successful");
268 SocExplorerEngine::message(this->plugin,"stardundee *** Open *** USBSpaceWire_Open successful");
229
269
230 USBSpaceWire_EnableNetworkMode(hDevice, 0); // deactivate the network mode
270 USBSpaceWire_EnableNetworkMode(hDevice, 0); // deactivate the network mode
231 CFGSpaceWire_EnableRMAP(1); // Enable the use of RMAP for the StarDundee brick configuration
271 CFGSpaceWire_EnableRMAP(1); // Enable the use of RMAP for the StarDundee brick configuration
232 CFGSpaceWire_SetRMAPDestinationKey(0x20); // Set the destination key expected by STAR-Dundee devices
272 CFGSpaceWire_SetRMAPDestinationKey(0x20); // Set the destination key expected by STAR-Dundee devices
233
273
234 // Set the path and return path to the device
274 // Set the path and return path to the device
235 CFGSpaceWire_StackClear();
275 CFGSpaceWire_StackClear();
236 CFGSpaceWire_AddrStackPush(0);
276 CFGSpaceWire_AddrStackPush(0);
237 CFGSpaceWire_AddrStackPush(254);
277 CFGSpaceWire_AddrStackPush(254);
238 CFGSpaceWire_RetAddrStackPush(254);
278 CFGSpaceWire_RetAddrStackPush(254);
239 // set the base transmit rate to 100 MHz
279 // set the base transmit rate to 100 MHz
240 status = CFGSpaceWire_SetBrickBaseTransmitRate( hDevice, CFG_BRK_CLK_100_MHZ, CFG_BRK_DVDR_1, 0xff);
280 status = CFGSpaceWire_SetBrickBaseTransmitRate( hDevice, CFG_BRK_CLK_100_MHZ, CFG_BRK_DVDR_1, 0xff);
241 if (status != CFG_TRANSFER_SUCCESS)
281 if (status != CFG_TRANSFER_SUCCESS)
242 {
282 {
243 SocExplorerEngine::message(this->plugin,"ERROR CFGSpaceWire_SetBrickBaseTransmitRate");
283 SocExplorerEngine::message(this->plugin,"ERROR CFGSpaceWire_SetBrickBaseTransmitRate");
244 return false;
284 return false;
245 }
285 }
246 else
286 else
247 {
287 {
248 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_SetBrickBaseTransmitRate, base rate = 100 MHz");
288 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_SetBrickBaseTransmitRate, base rate = 100 MHz");
249 }
289 }
250
290
251 // read the link status
291 // read the link status
252 if (CFGSpaceWire_GetLinkStatusControl(hDevice, linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
292 if (CFGSpaceWire_GetLinkStatusControl(hDevice, linkNumber, &statusControl) != CFG_TRANSFER_SUCCESS)
253 {
293 {
254 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(linkNumber));
294 SocExplorerEngine::message(this->plugin,"Could not read link status control for link " + QString::number(linkNumber));
255 return false;
295 return false;
256 }
296 }
257 else
297 else
258 {
298 {
259 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(linkNumber));
299 SocExplorerEngine::message(this->plugin,"OK CFGSpaceWire_GetLinkStatusControl of link " + QString::number(linkNumber));
260
300
261 // Set the link status control register properties
301 // Set the link status control register properties
262 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
302 CFGSpaceWire_LSEnableAutoStart(&statusControl, 1);
263 CFGSpaceWire_LSEnableStart(&statusControl, 1);
303 CFGSpaceWire_LSEnableStart(&statusControl, 1);
264 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
304 CFGSpaceWire_LSEnableDisabled(&statusControl, 0);
265 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
305 CFGSpaceWire_LSEnableTristate(&statusControl, 0);
266 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
306 CFGSpaceWire_LSSetOperatingSpeed(&statusControl, 9); // sets the link speed to ( 100 MHz / (9+1) ) = 10 MHz
267
307
268 // Set the link status control register
308 // Set the link status control register
269 if (CFGSpaceWire_SetLinkStatusControl(hDevice, linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
309 if (CFGSpaceWire_SetLinkStatusControl(hDevice, linkNumber, statusControl) != CFG_TRANSFER_SUCCESS)
270 {
310 {
271 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(linkNumber));
311 SocExplorerEngine::message(this->plugin,"Could not set the link status control for link " + QString::number(linkNumber));
272 return false;
312 return false;
273 }
313 }
274 else
314 else
275 {
315 {
276 SocExplorerEngine::message(this->plugin,"Set the link status control for link " + QString::number(linkNumber));
316 SocExplorerEngine::message(this->plugin,"Set the link status control for link " + QString::number(linkNumber));
277 }
317 }
278 }
318 }
279
319
280 if (CFGSpaceWire_SetAsInterface(hDevice, 1, 0) != CFG_TRANSFER_SUCCESS)
320 if (CFGSpaceWire_SetAsInterface(hDevice, 1, 0) != CFG_TRANSFER_SUCCESS)
281 {
321 {
282 SocExplorerEngine::message(this->plugin,"Could not set the device to be an interface");
322 SocExplorerEngine::message(this->plugin,"Could not set the device to be an interface");
283 return false;
323 return false;
284 }
324 }
285 else
325 else
286 {
326 {
287 SocExplorerEngine::message(this->plugin,"Device set to be an interface");
327 SocExplorerEngine::message(this->plugin,"Device set to be an interface");
288 }
328 }
289
329
290 USBSpaceWire_RegisterReceiveOnAllPorts(hDevice); // Register to receive on all ports
330 USBSpaceWire_RegisterReceiveOnAllPorts(hDevice); // Register to receive on all ports
291 USBSpaceWire_ClearEndpoints(hDevice); // clear the USB endpoints
331 USBSpaceWire_ClearEndpoints(hDevice); // clear the USB endpoints
292 USBSpaceWire_SetTimeout(hDevice,1.0);
332 USBSpaceWire_SetTimeout(hDevice,1.0);
293 SocExplorerEngine::message(this->plugin,"The driver's current send buffer size is " + QString::number(USBSpaceWire_GetDriverSendBufferSize(hDevice)) + " bytes");
333 SocExplorerEngine::message(this->plugin,"The driver's current send buffer size is " + QString::number(USBSpaceWire_GetDriverSendBufferSize(hDevice)) + " bytes");
294 SocExplorerEngine::message(this->plugin,"The driver's current read buffer size is " + QString::number(USBSpaceWire_GetDriverReadBufferSize(hDevice)) + " bytes");
334 SocExplorerEngine::message(this->plugin,"The driver's current read buffer size is " + QString::number(USBSpaceWire_GetDriverReadBufferSize(hDevice)) + " bytes");
295 SocExplorerEngine::message(this->plugin,"USBSpaceWire_IsReadThrottling is " + QString::number(USBSpaceWire_IsReadThrottling(hDevice)));
335 SocExplorerEngine::message(this->plugin,"USBSpaceWire_IsReadThrottling is " + QString::number(USBSpaceWire_IsReadThrottling(hDevice)));
296 this->connected = true;
336 this->connected = true;
297 this->handleMutex->unlock();
337 this->handleMutex->unlock();
298 return true;
338 return true;
299 }
339 }
300
340
301 bool stardundeeSPW_USB_Manager::disconnectBridge()
341 bool stardundeeSPW_USB_Manager::disconnectBridge()
302 {
342 {
303 this->handleMutex->lock();
343 this->handleMutex->lock();
304 USBSpaceWire_Close(hDevice); // Close the device
344 USBSpaceWire_Close(hDevice); // Close the device
305 SocExplorerEngine::message(this->plugin,"stardundee *** Close *** USBSpaceWire_Close, device: " + QString::number(0));
345 SocExplorerEngine::message(this->plugin,"stardundee *** Close *** USBSpaceWire_Close, device: " + QString::number(0));
306 USBSpaceWire_UnregisterReceiveOnAllPorts(hDevice); // Stop receiving on all ports
346 USBSpaceWire_UnregisterReceiveOnAllPorts(hDevice); // Stop receiving on all ports
307 this->handleMutex->unlock();
347 this->handleMutex->unlock();
308 return true;
348 return true;
309 }
349 }
310
350
311 int stardundeeSPW_USB_Manager::getRMAPtransactionID()
351 int stardundeeSPW_USB_Manager::getRMAPtransactionID()
312 {
352 {
313 this->RMAP_pending_transaction_IDsMtx->lock();
353 this->RMAP_pending_transaction_IDsMtx->lock();
314 int ID=0;
354 int ID=0;
315 bool found=true;
355 bool found=true;
316 while(ID<65536)
356 while(ID<65536)
317 {
357 {
318 for(int i=0;i<RMAP_pending_transaction_IDs.count();i++)
358 for(int i=0;i<RMAP_pending_transaction_IDs.count();i++)
319 {
359 {
320 if(RMAP_pending_transaction_IDs[i]==ID)found=false;
360 if(RMAP_pending_transaction_IDs[i]==ID)found=false;
321 }
361 }
322 if(found==true)break;
362 if(found==true)break;
323 ID++;
363 ID++;
324 found = true;
364 found = true;
325 }
365 }
326 if(found)
366 if(found)
327 {
367 {
328 RMAP_pending_transaction_IDs.append(ID);
368 RMAP_pending_transaction_IDs.append(ID);
329 }
369 }
330 this->RMAP_pending_transaction_IDsMtx->unlock();
370 this->RMAP_pending_transaction_IDsMtx->unlock();
331 return ID;
371 return ID;
332 }
372 }
333
373
334 int stardundeeSPW_USB_Manager::getRMAPanswer(int transactionID, char **buffer)
374 int stardundeeSPW_USB_Manager::getRMAPanswer(int transactionID, char **buffer)
335 {
375 {
336 *buffer=NULL;
376 *buffer=NULL;
337 int count=0;
377 int count=0;
338 while (*buffer==NULL)
378 while (*buffer==NULL)
339 {
379 {
340 this->RMAP_AnswersMtx->lock();
380 this->RMAP_AnswersMtx->lock();
341 for(int i=0;i<RMAP_Answers.count();i++)
381 for(int i=0;i<RMAP_Answers.count();i++)
342 {
382 {
343 if(RMAP_Answers[i]->transactionID==transactionID)
383 if(RMAP_Answers[i]->transactionID==transactionID)
344 {
384 {
345 this->RMAP_pending_transaction_IDsMtx->lock();
385 this->RMAP_pending_transaction_IDsMtx->lock();
346 for(int j=0;j<RMAP_pending_transaction_IDs.count();j++)
386 for(int j=0;j<RMAP_pending_transaction_IDs.count();j++)
347 {
387 {
348 if(RMAP_pending_transaction_IDs[j]==transactionID)
388 if(RMAP_pending_transaction_IDs[j]==transactionID)
349 {
389 {
350 RMAP_pending_transaction_IDs.removeAt(j);
390 RMAP_pending_transaction_IDs.removeAt(j);
351 }
391 }
352 }
392 }
353 this->RMAP_pending_transaction_IDsMtx->unlock();
393 this->RMAP_pending_transaction_IDsMtx->unlock();
354 *buffer = RMAP_Answers[i]->data;
394 *buffer = RMAP_Answers[i]->data;
355 count = RMAP_Answers[i]->len;
395 count = RMAP_Answers[i]->len;
356 RMAP_Answer* tmp=RMAP_Answers[i];
396 RMAP_Answer* tmp=RMAP_Answers[i];
357 RMAP_Answers.removeAt(i);
397 RMAP_Answers.removeAt(i);
358 delete tmp;
398 delete tmp;
359 }
399 }
360 }
400 }
361 this->RMAP_AnswersMtx->unlock();
401 this->RMAP_AnswersMtx->unlock();
362 //if no answer found in the stack wait until a new packet is pushed
402 //if no answer found in the stack wait until a new packet is pushed
363 if(!buffer)
403 if(!buffer)
364 {
404 {
365 SocExplorerEngine::message(this->plugin,"waiting until a new packet is pushed");
405 SocExplorerEngine::message(this->plugin,"waiting until a new packet is pushed");
366 this->RMAP_AnswersSem->acquire();
406 this->RMAP_AnswersSem->acquire();
367 }
407 }
368 }
408 }
369 return count;
409 return count;
370 }
410 }
371
411
372 bool stardundeeSPW_USB_Manager::sendPacket(char *packet, int size)
412 bool stardundeeSPW_USB_Manager::sendPacket(char *packet, int size)
373 {
413 {
374 USB_SPACEWIRE_STATUS result;
414 USB_SPACEWIRE_STATUS result;
375 USB_SPACEWIRE_ID pIdentifier;
415 USB_SPACEWIRE_ID pIdentifier;
376 SocExplorerEngine::message(this->plugin,"Sending SPW packet");
416 SocExplorerEngine::message(this->plugin,"Sending SPW packet");
377 this->handleMutex->lock();
417 this->handleMutex->lock();
378 result = USBSpaceWire_SendPacket(hDevice,packet,size,1, &pIdentifier);
418 result = USBSpaceWire_SendPacket(hDevice,packet,size,1, &pIdentifier);
379 if (result != TRANSFER_SUCCESS)
419 if (result != TRANSFER_SUCCESS)
380 {
420 {
381 SocExplorerEngine::message(this->plugin,"ERR sending the READ command ");
421 SocExplorerEngine::message(this->plugin,"ERR sending the READ command ");
382 this->handleMutex->unlock();
422 this->handleMutex->unlock();
383 return false;
423 return false;
384 }
424 }
385 else
425 else
386 {
426 {
387 SocExplorerEngine::message(this->plugin,"Packet sent");
427 SocExplorerEngine::message(this->plugin,"Packet sent");
388 USBSpaceWire_FreeSend(hDevice, pIdentifier);
428 USBSpaceWire_FreeSend(hDevice, pIdentifier);
389 }
429 }
390 this->handleMutex->unlock();
430 this->handleMutex->unlock();
391 return true;
431 return true;
392 }
432 }
393
433
394 void stardundeeSPW_USB_Manager::pushRmapPacket(char *packet, int len)
434 void stardundeeSPW_USB_Manager::pushRmapPacket(char *packet, int len)
395 {
435 {
396 char* packetbuffer = (char*)malloc(len);
436 char* packetbuffer = (char*)malloc(len);
397 memcpy(packetbuffer,packet,len);
437 memcpy(packetbuffer,packet,len);
398 RMAP_Answer* RMPAPpacket=new RMAP_Answer(RMAP_get_transactionID(packetbuffer+1),packetbuffer,len);
438 RMAP_Answer* RMPAPpacket=new RMAP_Answer(RMAP_get_transactionID(packetbuffer+1),packetbuffer,len);
399 RMAP_AnswersMtx->lock();
439 RMAP_AnswersMtx->lock();
400 RMAP_Answers.append(RMPAPpacket);
440 RMAP_Answers.append(RMPAPpacket);
401 RMAP_AnswersMtx->unlock();
441 RMAP_AnswersMtx->unlock();
402 }
442 }
403
443
404
444
405
445
406
446
407
447
408
448
409
449
410
450
411
451
412
452
413
453
414
454
415
455
416
456
417
457
418
458
419
459
420
460
421
461
422
462
423
463
424
464
General Comments 0
You need to be logged in to leave comments. Login now