##// END OF EJS Templates
Added host endianness management for AHB Uart Plugin.
jeandet -
r42:b9dafb3bcaed default
parent child
Show More
@@ -1,490 +1,508
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the SocExplorer Software
3 3 -- Copyright (C) 2011, 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@lpp.polytechnique.fr
21 21 ----------------------------------------------------------------------------*/
22 22 #include <socexplorerengine.h>
23 23 #include "ahbuartplugin.h"
24 24 #include <unistd.h>
25 25 #include <errno.h>
26 26 #include <QApplication>
27 27 #include <QProgressBar>
28 28 #include <stdio.h>
29 29 #include <QThread>
30 30 #include "ahbuartpywrapper.h"
31 31 #include <QCompleter>
32 32 #include <QStringList>
33 33 #include <QLineEdit>
34 34 #include <socexplorerproxy.h>
35 35
36 36 ahbuartplugin::ahbuartplugin(QWidget *parent):socexplorerplugin(parent,false)
37 37 {
38 38 this->port =(rs232port_t)NULL;
39 39 this->portMutex = new QMutex(QMutex::Recursive);
40 40 this->UI = new ahbUartPluginUI();
41 41 this->setWidget((QWidget*)this->UI);
42 42 QObject::connect(this,SIGNAL(activateSig(bool)),this->UI,SLOT(setConnected(bool)));
43 43 QObject::connect(this->UI,SIGNAL(connectPortsig(QString,int)),this,SLOT(togglePort(QString,int)));
44 44 this->pyObject = new ahbuartPywrapper(this);
45 45 QObject::connect(((ahbuartPywrapper*)this->pyObject),SIGNAL(open(QString,int)),this,SLOT(open(QString,int)));
46 46 QObject::connect(((ahbuartPywrapper*)this->pyObject),SIGNAL(close()),this,SLOT(close()));
47 47 QObject::connect(((ahbuartPywrapper*)this->pyObject),SIGNAL(ReadBytes(uint,uint)),this,SLOT(ReadBytes(uint,uint)));
48 48 QObject::connect(((ahbuartPywrapper*)this->pyObject),SIGNAL(WriteBytes(uint,QList<QVariant>)),this,SLOT(WriteBytes(uint,QList<QVariant>)));
49 49 QObject::connect(this->UI,SIGNAL(rescanPorts()),this,SLOT(updatePortList()));
50 50 QObject::connect(this,SIGNAL(addReadBytes(int)),this->UI,SLOT(addReadBytes(int)));
51 51 QObject::connect(this,SIGNAL(addWritenBytes(int)),this->UI,SLOT(addWritenBytes(int)));
52 52 this->portListcompleter = NULL;
53 53 this->scanDone = false;
54 54 updatePortList();
55 55 }
56 56
57 57
58 58 ahbuartplugin::~ahbuartplugin()
59 59 {
60 60 if(this->port!=(rs232port_t)NULL)
61 61 {
62 62 rs232close(this->port);
63 63 this->port = (rs232port_t)NULL;
64 64 }
65 65 this->UI->close();
66 66 this->UI->~ahbUartPluginUI();
67 67 }
68 68
69 69
70 70 void ahbuartplugin::closeMe()
71 71 {
72 72 if(this->port!=(rs232port_t)NULL)
73 73 {
74 74 rs232close(this->port);
75 75 this->port = (rs232port_t)NULL;
76 76 }
77 77 emit this->closePlugin(this);
78 78 }
79 79
80 80 int ahbuartplugin::registermenu(QMainWindow *menuHolder)
81 81 {
82 82 this->menu = menuHolder->menuBar()->addMenu(tr("&AHB UART"));
83 83 this->closeAction = this->menu->addAction(tr("Close plugin"));
84 84 QObject::connect(this->closeAction,SIGNAL(triggered()),this,SLOT(closeMe()));
85 85 return 1;
86 86 }
87 87
88 88
89 89 bool ahbuartplugin::checkConnection()
90 90 {
91 91 QTime timeout;
92 92 char test[5] ={(char)0x80,(char)0x80,(char)0,(char)0,(char)0};
93 93 char test2[1024];
94 94 int writen =0;
95 95 int read = 0;
96 96 timeout.start();
97 97 SocExplorerEngine::message(this,"Check connection",2);
98 98 QMutexLocker lock(portMutex);
99 99 while(writen!=5)
100 100 {
101 101 writen+=rs232write(this->port,test+writen,5-writen);
102 102 if(timeout.elapsed()>1000)
103 103 {
104 104 SocExplorerEngine::message(this,"Can't write any data on serial port",2);
105 105 return false;
106 106 }
107 107 }
108 108 #ifdef WIN32
109 109 usleep(1000);
110 110 #endif
111 111 timeout.restart();
112 112 int avail = 0;
113 113 do
114 114 {
115 115 avail = rs232availablebytes(this->port);
116 116 if(timeout.elapsed()>1000)
117 117 {
118 118 if(avail)
119 119 rs232read(this->port,test2,avail);
120 120 SocExplorerEngine::message(this,"Connection Error",2);
121 121 return false;
122 122 }
123 123 }while(avail<4);
124 124 read = rs232read(this->port,test2,avail);
125 125 if(read>0)
126 126 {
127 127 SocExplorerEngine::message(this,"Connection Ok",2);
128 128 return true;
129 129 }
130 130 else
131 131 {
132 132 SocExplorerEngine::message(this,"Connection Error",2);
133 133 return false;
134 134 }
135 135 }
136 136
137 137 void ahbuartplugin::connectPort(QString PortName, int baudrate)
138 138 {
139 139 QTime timeout;
140 140 SocExplorerEngine::message(this,"Try to connect to port "+PortName,2);
141 141 timeout.start();
142 142 QMutexLocker lock(portMutex);
143 143 if(this->port==(rs232port_t)NULL)
144 144 {
145 145 SocExplorerEngine::message(this,"Open port "+PortName,2);
146 146 this->port=rs232open((char*)PortName.toStdString().c_str());
147 147 }
148 148 if(this->port!=badPortValue)
149 149 {
150 150 SocExplorerEngine::message(this,"Port opened "+PortName,2);
151 151 SocExplorerEngine::message(this,"Configure port "+PortName,2);
152 152 rs232setup(this->port,8,baudrate,rs232parityNo,rs232OneStop);
153 153 char test[7] ={(char)0x55,(char)0x51,(char)0x80,(char)0x80,(char)0x0,(char)0x0,(char)0x14};
154 154 char test2[1024];
155 155 SAFEWRITE(test,1,timeout,2000,return);
156 156 SAFEWRITE((test+1),1,timeout,2000,return);
157 157 rs232read(this->port,test2,512);
158 158 int read = 0;
159 159 for(int i=0;i<10;i++)
160 160 {
161 161 SocExplorerEngine::message(this,"Send test patern :0x55,0x55,0x80,0x80,0x0,0x0,0x14",2);
162 162 SAFEWRITE(test+2,5,timeout,2000,return);
163 163 SocExplorerEngine::message(this,"Read Result",2);
164 164 read=rs232read(this->port,test2+read,16);
165 165 SocExplorerEngine::message(this,QString("Get ") + QString::number(read) + " bytes",2);
166 166 if(read>0)
167 167 {
168 168 SocExplorerEngine::message(this,"Flush port ",2);
169 169 while(rs232read(this->port,test2,1)>0);
170 170 this->Connected = true;
171 171 SocExplorerEngine::message(this,QString("Connection success on ")+PortName,2);
172 172 emit this->activate(true);
173 173 if(this->scanDone==false)
174 174 {
175 175 socexplorerproxy::loadChildSysDriver(this,"AMBA_PLUGIN");
176 176 this->scanDone=true;
177 177 }
178 178 break;
179 179 }
180 180 }
181 181 }
182 182 else
183 183 {
184 184 SocExplorerEngine::message(this,QString("Port not opened ")+PortName,2);
185 185 this->port = (rs232port_t)NULL;
186 186 this->Connected = false;
187 187 emit this->activateSig(false);
188 188 return;
189 189 }
190 190 if(this->Connected == false)
191 191 {
192 192 SocExplorerEngine::message(this,QString("Port not opened ")+PortName,2);
193 193 rs232close(this->port);
194 194 this->port = (rs232port_t)NULL;
195 195 emit this->activateSig(false);
196 196 }
197 197
198 198 }
199 199
200 200 bool ahbuartplugin::open(QString PortName,int baudrate)
201 201 {
202 202 if(this->port!=(rs232port_t)NULL)
203 203 this->close();
204 204 this->UI->setconfig(PortName,baudrate);
205 205 this->connectPort(PortName,baudrate);
206 206 return (this->port!=(rs232port_t)NULL);
207 207 }
208 208
209 209 void ahbuartplugin::close()
210 210 {
211 211 if(this->port!=(rs232port_t)NULL)
212 212 {
213 213 rs232close(this->port);
214 214 this->port = (rs232port_t)NULL;
215 215 this->Connected = false;
216 216 emit this->activateSig(false);
217 217 }
218 218 }
219 219
220 220 void ahbuartplugin::togglePort(QString PortName,int baudrate)
221 221 {
222 222 if(this->port!=(rs232port_t)NULL)
223 223 {
224 224 this->close();
225 225 }
226 226 else
227 227 {
228 228 this->connectPort(PortName,baudrate);
229 229 }
230 230 }
231 231
232 232
233 233 unsigned int ahbuartplugin::Read(unsigned int *Value,unsigned int count,unsigned int address)
234 234 {
235 235 QTime timeout;
236 236 timeout.start();
237 237 unsigned int read=0;
238 238 unsigned int cnt=count;
239 239 unsigned int nextUpdateTrig=0,updateStep=512;
240 240 SocExplorerEngine::message(this,QString("Read ")+ QString::number(count) + QString(" words @0x")+ QString::number(address,16),2);
241 241 if((this->port!= badPortValue)||(this->port!=(rs232port_t)NULL))
242 242 {
243 243 QMutexLocker lock(portMutex);
244 244 if(!this->checkConnection())
245 245 {
246 246 this->Connected = false;
247 247 emit this->activateSig(false);
248 248 this->portMutex->unlock();
249 249 return 0;
250 250 }
251 251 QProgressBar* progress=NULL;
252 252 if(cnt>128)
253 253 progress= SocExplorerEngine::getProgressBar("Reading on uart @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
254 254 char CMD[5];
255 255 char* result = (char*)malloc(count*4);
256 256 while(count>32)
257 257 {
258 258 CMD[0] = 0x80 | (32-1);
259 259 CMD[1] = (char)((address>>24)&0xFF);
260 260 CMD[2] = (char)((address>>16)&0xFF);
261 261 CMD[3] = (char)((address>>8)&0xFF);
262 262 CMD[4] = (char)((address)&0xFF);
263 263 SAFEWRITE(CMD,5,timeout,1000,return 0);
264 264 timeout.restart();
265 265 int avail=0;
266 266 do{
267 267 avail=rs232availablebytes(this->port);
268 268 if(timeout.elapsed()>1000)
269 269 {
270 270 rs232close(this->port);
271 271 this->port = (rs232port_t)NULL;
272 272 this->Connected = false;
273 273 emit this->activateSig(false);
274 274 return 0;
275 275 }
276 276 }while(avail<(32*4));
277 277 rs232read(this->port,result+((cnt-count)*4),32*4);
278 278 count-=32;
279 279 address+=32*4;
280 280 if(cnt>128)
281 281 {
282 282
283 283 if((cnt-count)>=nextUpdateTrig)
284 284 {
285 285 progress->setValue(cnt-count);
286 286 qApp->processEvents();
287 287 nextUpdateTrig+=updateStep;
288 288 }
289 289 }
290 290 }
291 291 if(count>0)
292 292 {
293 293 CMD[0] = 0x80 | (count-1);
294 294 CMD[1] = (char)((address>>24)&0xFF);
295 295 CMD[2] = (char)((address>>16)&0xFF);
296 296 CMD[3] = (char)((address>>8)&0xFF);
297 297 CMD[4] = (char)((address)&0xFF);
298 298 SAFEWRITE(CMD,5,timeout,1000,return 0);
299 299 timeout.restart();
300 300 int avail=0;
301 301 do{
302 302 avail=rs232availablebytes(this->port);
303 303 if(timeout.elapsed()>1000)
304 304 {
305 305 rs232close(this->port);
306 306 this->port = (rs232port_t)NULL;
307 307 this->Connected = false;
308 308 emit this->activateSig(false);
309 309 return 0;
310 310 }
311 311 }while(avail<(count*4));
312 312 rs232read(this->port,result+((cnt-count)*4),count*4);
313 313 }
314 314 if(cnt>128)
315 315 {
316 316 progress->setValue(cnt-count);
317 317 qApp->processEvents();
318 318 }
319 319 for(int i=0;(unsigned int)i<cnt;i++)
320 320 {
321 for(int j =0;j<4;j++)
322 {
323 Value[i]= ((unsigned char)(result[i*4+j])) + Value[i]*256;
324 }
325 read = cnt*4;
326
321 #if __BYTE_ORDER == __LITTLE_ENDIAN
322 // for(int j =0;j<4;j++)
323 // {
324 // Value[i]= ((unsigned char)(result[i*4+j])) + Value[i]*256;
325 // }
326 Value[i]= socexplorerBswap32(((uint32_t*)result)[i]);
327 #elif __BYTE_ORDER == __BIG_ENDIAN
328 Value[i]= ((uint32_t*)result)[i];
329 #endif
327 330 }
331 read = cnt*4;
328 332 if(cnt>128)
329 333 SocExplorerEngine::deleteProgressBar(progress);
330 334 free(result);
331 335 }
332 336 emit this->addReadBytes(read);
333 337 return read/4;
334 338 }
335 339
336 340 unsigned int ahbuartplugin::Write(unsigned int *Value,unsigned int count, unsigned int address)
337 341 {
338 342 QTime timeout;
339 343 timeout.start();
340 344 unsigned int writen=0;
341 345 unsigned int nextUpdateTrig=0,updateStep=512;
342 346 SocExplorerEngine::message(this,QString("Write ")+ QString::number(count) + QString(" words @0x")+ QString::number(address,16),2);
343 347 if((this->port!= badPortValue)||(this->port!=(rs232port_t)NULL))
344 348 {
345 349 QMutexLocker lock(portMutex);
346 350 if(!this->checkConnection())
347 351 {
348 352 emit this->activateSig(false);
349 353 this->Connected = false;
350 354 this->portMutex->unlock();
351 355 return 0;
352 356 }
353 357 QProgressBar* progress = NULL;
354 358 if(count>128)
355 359 progress = SocExplorerEngine::getProgressBar("Writing on uart @0x"+QString::number(address,16)+" %v of "+QString::number(count)+" words ",count);
356 360 int offset = 0;
357 361 char* CMD= (char*)malloc((32*4)+5);
358 362 writen=0;
359 363 while(count>32)
360 364 {
361 365 CMD[0] = 0xC0 | (32-1);
362 366 CMD[1] = (char)(((unsigned int)address>>24)&0xFF);
363 367 CMD[2] = (char)(((unsigned int)address>>16)&0xFF);
364 368 CMD[3] = (char)(((unsigned int)address>>8)&0xFF);
365 369 CMD[4] = (char)(((unsigned int)address)&0xFF);
366 370 for(int i=0;i<32;i++)
367 371 {
372 #if __BYTE_ORDER == __LITTLE_ENDIAN
368 373 CMD[(i*4)+5] = (char)(((unsigned int)Value[i+offset]>>24)&0xFF);
369 374 CMD[(i*4)+6] = (char)(((unsigned int)Value[i+offset]>>16)&0xFF);
370 375 CMD[(i*4)+7] = (char)(((unsigned int)Value[i+offset]>>8)&0xFF);
371 376 CMD[(i*4)+8] = (char)(((unsigned int)Value[i+offset])&0xFF);
377 #elif __BYTE_ORDER == __BIG_ENDIAN
378 CMD[(i*4)+5] = (char)(((unsigned int)Value[i+offset])&0xFF);
379 CMD[(i*4)+6] = (char)(((unsigned int)Value[i+offset]>>8)&0xFF);
380 CMD[(i*4)+7] = (char)(((unsigned int)Value[i+offset]>>16)&0xFF);
381 CMD[(i*4)+8] = (char)(((unsigned int)Value[i+offset]>>24)&0xFF);
382 #endif
372 383 }
373 384 SAFEWRITE(CMD,((32*4)+5),timeout,1000,return 0);
374 385 writen+=32;
375 386 count-=32;
376 387 offset+=32;
377 388 address+=32*4;
378 389 if(offset>=nextUpdateTrig && progress!=NULL)
379 390 {
380 391 progress->setValue(offset);
381 392 qApp->processEvents();
382 393 nextUpdateTrig +=updateStep;
383 394 }
384 395
385 396 }
386 397 if(count>0)
387 398 {
388 399 CMD[0] = 0xC0 | (count-1);
389 400 CMD[1] = (char)(((unsigned int)address>>24)&0xFF);
390 401 CMD[2] = (char)(((unsigned int)address>>16)&0xFF);
391 402 CMD[3] = (char)(((unsigned int)address>>8)&0xFF);
392 403 CMD[4] = (char)(((unsigned int)address)&0xFF);
393 404 for(int i=0;(unsigned int) i<(count);i++)
394 405 {
406 #if __BYTE_ORDER == __LITTLE_ENDIAN
395 407 CMD[(i*4)+5] = (char)(((unsigned int)Value[i+offset]>>24)&0xFF);
396 408 CMD[(i*4)+6] = (char)(((unsigned int)Value[i+offset]>>16)&0xFF);
397 409 CMD[(i*4)+7] = (char)(((unsigned int)Value[i+offset]>>8)&0xFF);
398 410 CMD[(i*4)+8] = (char)(((unsigned int)Value[i+offset])&0xFF);
411 #elif __BYTE_ORDER == __BIG_ENDIAN
412 CMD[(i*4)+5] = (char)(((unsigned int)Value[i+offset])&0xFF);
413 CMD[(i*4)+6] = (char)(((unsigned int)Value[i+offset]>>8)&0xFF);
414 CMD[(i*4)+7] = (char)(((unsigned int)Value[i+offset]>>16)&0xFF);
415 CMD[(i*4)+8] = (char)(((unsigned int)Value[i+offset]>>24)&0xFF);
416 #endif
399 417 }
400 418 SAFEWRITE(CMD,((count*4)+5),timeout,1000,return 0);
401 419 writen+=count;
402 420 }
403 421 if(progress!=NULL)
404 422 {
405 423 progress->setValue(writen);
406 424 qApp->processEvents();
407 425 SocExplorerEngine::deleteProgressBar(progress);
408 426 }
409 427 free(CMD);
410 428 emit this->addWritenBytes(writen*4);
411 429 return writen;
412 430 }
413 431 return 0;
414 432 }
415 433
416 434
417 435
418 436
419 437 void ahbuartplugin::updatePortList()
420 438 {
421 439 if(this->portListcompleter==(QCompleter*)NULL)
422 440 {
423 441 this->portListcompleter=new QCompleter(this);
424 442 this->portListcompleter->setCaseSensitivity(Qt::CaseInsensitive);
425 443 this->portListcompleterModel = new QStringListModel(this);
426 444 this->portListcompleter->setModel(this->portListcompleterModel);
427 445 this->UI->setCompleter(this->portListcompleter);
428 446 }
429 447 rs232portslist_t* portlist = rs232getportlist();
430 448 rs232portslist_t* portlistenum = portlist;
431 449 QStringList wordList;
432 450 while(portlistenum!=NULL)
433 451 {
434 452 wordList << portlistenum->name;
435 453 portlistenum = portlistenum->next;
436 454 }
437 455 rs232deleteportlist(portlist);
438 456 this->portListcompleterModel->setStringList(wordList);
439 457 }
440 458
441 459 QVariantList ahbuartplugin::ReadBytes(unsigned int address, unsigned int count)
442 460 {
443 461 unsigned int data[(count/4)+1];
444 462 QVariantList result;
445 463 this->Read(data,(count/4)+1,address);
446 464 for(unsigned int i = 0;i<count/4;i++)
447 465 {
448 466 result.append(QVariant((int)(0x0FF&(data[i]>>24))));
449 467 result.append(QVariant((int)(0x0FF&(data[i]>>16))));
450 468 result.append(QVariant((int)(0x0FF&(data[i]>>8))));
451 469 result.append(QVariant((int)(0x0FF&(data[i]))));
452 470 }
453 471
454 472 for(int i=0;i<(count%4);i++)
455 473 {
456 474 result.append(QVariant((int)(0x0FF&(data[count/4]>>((3-i)*8)))));
457 475 }
458 476
459 477 return result;
460 478 }
461 479
462 480 void ahbuartplugin::WriteBytes(unsigned int address, QList<QVariant> dataList)
463 481 {
464 482 unsigned int data[dataList.count()/4];
465 483 for(int i = 0;i<(dataList.count()/4);i++)
466 484 {
467 485 data[i] = 0x0FF & ((unsigned int)dataList.at(4*i).toUInt());
468 486 data[i] = (data[i]<<8) + (0x0FF & ((unsigned int)dataList.at((4*i)+1).toUInt()));
469 487 data[i] = (data[i]<<8) + (0x0FF & ((unsigned int)dataList.at((4*i)+2).toUInt()));
470 488 data[i] = (data[i]<<8) + (0x0FF & ((unsigned int)dataList.at((4*i)+3).toUInt()));
471 489 }
472 490 this->Write(data,dataList.count()/4,address);
473 491 }
474 492
475 493
476 494
477 495
478 496
479 497
480 498
481 499
482 500
483 501
484 502
485 503
486 504
487 505
488 506
489 507
490 508
General Comments 0
You need to be logged in to leave comments. Login now