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