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