File: e2app.cpp

package info (click to toggle)
ponyprog 3.1.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,904 kB
  • sloc: cpp: 35,932; python: 981; sh: 565; xml: 67; makefile: 45; ansic: 38
file content (615 lines) | stat: -rw-r--r-- 13,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2025   Claudio Lanconelli                           //
//                                                                         //
//  https://github.com/lancos/ponyprog                                        //
//                                                                         //
//-------------------------------------------------------------------------//
//                                                                         //
// This program is free software; you can redistribute it and/or           //
// modify it under the terms of the GNU  General Public License            //
// as published by the Free Software Foundation; either version2 of        //
// the License, or (at your option) any later version.                     //
//                                                                         //
// This program is distributed in the hope that it will be useful,         //
// but WITHOUT ANY WARRANTY; without even the implied warranty of          //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
// General Public License for more details.                                //
//                                                                         //
// You should have received a copy of the GNU  General Public License      //
// along with this program (see LICENSE);     if not, write to the         //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
//                                                                         //
//=========================================================================//

#include "e2profil.h"
#include "e2awinfo.h"
#include "e2app.h"        // Header file

#include <QCoreApplication>
#include <QString>
#include <QtCore>
#include <QDebug>

#ifdef Q_OS_LINUX
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#endif

#include "microbus.h"
#include "interfconv.h"

//const int idAskToSave = 100; // Dummy Command

e2App::e2App() :
	awip(0),
	port_number(0)
{
	// Constructor
	qDebug() << Q_FUNC_INFO;

	//         awinfo = 0;

	s2430B.SetOrganization(ORG8);           //Default X2444Bus organization is ORG16, but S24H30 organization is ORG8
	//      mega103B.SetPageSize(256);
	//      mega103B.SetFlashPagePolling(false);
	//      atMegaB.SetPageSize(256);
	//      atMegaB.SetFlashPagePolling(true);
	//      mega16xB.SetPageSize(128);
	//      mega16xB.SetFlashPagePolling(true);
	//      mega8xB.SetPageSize(64);
	//      mega8xB.SetFlashPagePolling(true);
	//      tiny2xB.SetPageSize(32);
	//      tiny2xB.SetFlashPagePolling(true);

	//AutoTag
	//Initialize Bus Pointers vector
	busvetp[I2C - 1] = &iicB;
	busvetp[AT90S - 1] = &at90sB;
	//busvetp[ATMEGAB-1] = &atMegaB;
	//busvetp[AT1200S-1] = &at1200B;
	busvetp[AT89S - 1] = &at89sB;
	busvetp[AT93C - 1] = &at93cB;
	busvetp[AT250 - 1] = &at250B;
	busvetp[AT250BIG - 1] = &at250BigB;
	busvetp[PICB - 1] = &picB;
	busvetp[PIC12B - 1] = &pic12B;
	//busvetp[SXB - 1] = &sxB;
	busvetp[SDEB - 1] = &sdeB;
	busvetp[PICNEWB - 1] = &picNewB;
	busvetp[IMBUS - 1] = &imB;
	busvetp[X2444B - 1] = &x2444B;
	busvetp[S2430B - 1] = &s2430B;

	//duplicated call in initSettings()
	//SetInterfaceType();     //Set default interface

	initSettings();
}


e2App::~e2App()
{
	qDebug() << Q_FUNC_INFO;
}


void e2App::initSettings()
{
	QString str;

	qDebug() << "Settings file: " << E2Profile::GetConfigFile();	// << " - Format: " << E2Profile::s->format();

	QString currentAppDir = qApp->applicationDirPath();

	helpfile = currentAppDir + "/ponyprog.html";
	ok_soundfile = currentAppDir + "/oksound.wav";
	err_soundfile = currentAppDir + "/errsound.wav";

	QString sp2 = E2Profile::GetLanguageCode();

	if (sp2.length() > 0)
	{
		str = sp2;

		E2Profile::SetLanguageCode(str.toLatin1());

		if (str == "default")
		{
			sp2 = "";
		}
		else if (str == "locale")
		{
			sp2 = translate(STR_LANGUAGE_CODE);
		}
		else
		{
			sp2 = str;
		}

		setlocale(LC_ALL, sp2.toLatin1());
	}

	// Read parameters from INI file
	SetInterfaceType(E2Profile::GetParInterfType());
	SetPort(E2Profile::GetPortNumber());
	SetPolarity(E2Profile::GetPolarityLines());
#if 0
	// Make sure all parameters (even default values) are written
	//   to the INI file.
	E2Profile::GetPowerUpDelay();
	E2Profile::GetSPIResetPulse();
	E2Profile::GetSPIDelayAfterReset();
	E2Profile::GetSPIPageWrite();
	E2Profile::GetMegaPageDelay();
	E2Profile::GetAVRProgDelay();
	E2Profile::GetAVREraseDelay();
	E2Profile::GetAT89DelayAfterReset();
	E2Profile::GetAVRDelayAfterReset();
	//
	E2Profile::GetI2CSpeed();
	E2Profile::GetSPISpeed();
	E2Profile::GetMicroWireSpeed();
	E2Profile::GetPICSpeed();
	E2Profile::GetSDESpeed();
	//
	E2Profile::GetLogEnabled();
	E2Profile::GetSoundEnabled();
	E2Profile::GetSkipStartupDialog();
	E2Profile::GetVerifyAfterWrite();
	E2Profile::GetClearBufBeforeLoad();
	E2Profile::GetClearBufBeforeRead();
	E2Profile::GetAutoDetectPorts();
	E2Profile::GetAt89PageOp();
	E2Profile::Get8253FallEdge();

#ifdef Q_OS_LINUX
	E2Profile::GetLockDir();
	E2Profile::GetDevDir();
#endif

	E2Profile::GetDevName();
#endif

	scriptMode = false;
	returnValue = 0;
	script_name = "";
}
//=====================>>> e2App::OpenPort <<<==============================
int e2App::OpenPort(int port)
{
	qDebug() << Q_FUNC_INFO << "(" << port << ")";

	if (port >= 0)
	{
		SetPort(port);
	}

	ClosePort();
	return iniBus->Open(GetPort());
}


//=====================>>> e2App::ClosePort <<<==============================
void e2App::ClosePort()
{
	qDebug() << Q_FUNC_INFO << " iniBus=" << (Qt::hex) << iniBus << (Qt::dec);
	iniBus->Close();
}

//=====================>>> e2App::TestPort <<<==============================
int e2App::TestPort(int port, bool open_only)
{
	qDebug() << Q_FUNC_INFO << "(port=" << port << ", open_only=" << open_only << ")";

	int rv = (open_only) ?
			 busIntp->TestOpen(port) :
			 busIntp->TestPort(port);

	qDebug() << Q_FUNC_INFO << " = " << rv;

	return rv;
}

//=====================>>> e2App::OpenBus <<<==============================
int e2App::OpenBus(BusIO *p)
{
	qDebug() << Q_FUNC_INFO << "(" << (Qt::hex) << p << (Qt::dec) << ")";

	iniBus->Close();

	qDebug() << Q_FUNC_INFO << " ** Close";

	iniBus = p;
	int rv = iniBus->Open(GetPort());

	qDebug() << Q_FUNC_INFO << " ** Open = " << rv;

	if (rv == OK)
	{
		rv = busIntp->SetPower(true);

		qDebug() << Q_FUNC_INFO << " ** SetPower";

		//Power up delay
		busIntp->WaitMsec(E2Profile::GetPowerUpDelay());

		qDebug() << Q_FUNC_INFO << " ** Reset";

		iniBus->Reset();        //28/10/98
	}

	qDebug() << Q_FUNC_INFO << " = " << rv;

	return rv;
}

//=====================>>> e2App::SleepBus <<<==============================
void e2App::SleepBus()
{
	qDebug() << Q_FUNC_INFO << " iniBus=" << (Qt::hex) << iniBus << (Qt::dec);

	busIntp->WaitMsec(5);		// 08/04/98 -- power hold time
	busIntp->SetPower(false);
	iniBus->Close();			// 28/09/98 -- richiude la porta dopo averla usata
}


// EK 2017
// TODO to remove this to init function
#if 0
//=====================>>> e2App::NewAppWin <<<==========================
vWindow *e2App::NewAppWin(vWindow *win, char *name,
						  int w, int h, vAppWinInfo *winInfo)
{
#ifdef Q_OS_WIN32            // Yes it's a dirty hack here but clean in Windows sense, heha 130406
	HKEY key;       // But I wouldn't change the very incomplete V-lib source.

	if (!RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\h#s\\PonyProg", 0, KEY_QUERY_VALUE, &key))
	{
		WINDOWPLACEMENT wp;
		wp.length = sizeof wp;
		HWND w = theApp->winHwnd();

		if (GetWindowPlacement(w, &wp))
		{
			DWORD size = sizeof wp.rcNormalPosition;

			if (!RegQueryValueEx(key, "WinPos", NULL, NULL, (LPBYTE)&wp.rcNormalPosition, &size)
					&& size == sizeof wp.rcNormalPosition)
			{
				SetWindowPlacement(w, &wp);
			}
		}

		RegCloseKey(key);
	}

#endif
	vWindow *thisWin = win;                 //local copy to use
	vAppWinInfo *awinfo = winInfo;
	char *appname = name;

	if (!*name)
	{
		appname = "No Name";            // Default name
	}

	qDebug() << "e2App::NewAppWin(" << appname << ")";

	// Create the first window using provided CmdWindow
	winCounter++;           //incremente il numero di finestre

	if (!thisWin)           //need to new a window
	{
		thisWin = new e2CmdWindow(appname, w, h);
	}

	if (!awinfo)
	{
		awinfo = new e2AppWinInfo((vCmdWindow *)thisWin, name, busvetp);
	}

	return vApp::NewAppWin(thisWin, appname, w, h, awinfo);
}

#endif

int e2App::Calibration()
{
	qDebug() << Q_FUNC_INFO;

	int rv = OK;
	//      OpenBus(&iicB);         //aggiunto il 06/03/98
	LookForBogoMips();
	//      rv = iicB.Calibration();
	//      SleepBus();                     //aggiunto il 06/03/98
	return rv;
}

void e2App::SetInterfaceType(HInterfaceType type)
{
	switch (type)
	{
	//Interface initializers
	case SIPROG_IO:
		iType = SIPROG_IO;
		busIntp = &siprog_ioI;
		break;

	case EASYI2C_API:
		iType = EASYI2C_API;
		busIntp = &easyi2c_apiI;
		easyi2c_apiI.SetIOmode(false);
		easyi2c_apiI.Close();
		break;

	case EASYI2C_IO:
		iType = EASYI2C_IO;
		busIntp = &easyi2c_ioI;
		easyi2c_ioI.SetIOmode(true);
		easyi2c_ioI.Close();
		break;

	case AVRISP:
		iType = AVRISP;
		busIntp = &avrisp_apiI;
		avrisp_apiI.SetIOmode(false);
		avrisp_apiI.Close();
		break;

	case AVRISP_IO:
		iType = AVRISP_IO;
		busIntp = &avrisp_ioI;
		avrisp_ioI.SetIOmode(true);
		avrisp_ioI.Close();
		break;

	case JDM_API:
		iType = JDM_API;
		busIntp = &jdm_apiI;
		jdm_apiI.SetCmd2CmdDelay(E2Profile::GetJDMCmd2CmdDelay());
		break;

	case DT006_API:
		iType = DT006_API;
		busIntp = &dt006_apiI;
		dt006_apiI.SetIOmode(false);
		dt006_apiI.Close();
		break;

	case DT006_IO:
		iType = DT006_IO;
		busIntp = &dt006_ioI;
		dt006_ioI.SetIOmode(true);
		dt006_ioI.Close();
		break;

	case LINUXSYSFS_IO:
		iType = LINUXSYSFS_IO;
		busIntp = &linuxsysfs_ioI;
		break;

	case FTDI_JTAGKEY:
		iType = FTDI_JTAGKEY;
		busIntp = &jtagkeyI;
		break;

	case PONYPROG_FT:
		iType = PONYPROG_FT;
		busIntp = &ponyprog_ftI;
		break;

	case EGGLINK_V3:
		iType = EGGLINK_V3;
		busIntp = &egglink_v3I;
		break;

	case SIPROG_API:
	default:	             //20/07/99 -- to prevent crash
		iType = SIPROG_API;
		busIntp = &siprog_apiI;
		break;
	}

	busIntp->SetUSBVidPid(TypeToInterfVidPid(iType));
	InterfPins pins;
	E2Profile::GetInterfacePins(iType, pins);
	busIntp->ConfigPins(pins);

	for (int k = 0; k < NO_OF_BUSTYPE; k++)
	{
		busvetp[k]->SetBusInterface(busIntp);
	}
}

#ifdef  __unix__
static volatile int alarm_caught;

static void alarm(int i)
{
	(void)i;
	alarm_caught = 1;
}
#endif

#define N_SAMPLE        4
#define N_CICLI 8000
#define UDELAY  40
#define MSLICE  ((float)UDELAY * N_CICLI / 1000.0)

void e2App::LookForBogoMips()
{
#ifdef  __unix__
	struct itimerval it;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;

	it.it_value.tv_sec = 0;
	it.it_value.tv_usec = 500000;

	signal(SIGALRM, alarm);

	long ndel[N_SAMPLE];
	int k;

	for (k = 0; k < N_SAMPLE; k++)
	{
		setitimer(ITIMER_REAL, &it, NULL);
		alarm_caught = 0;
		long j = 0;

		while (!alarm_caught)
		{
			j++;
		}

		ndel[k] = j;

		//      fprintf(stderr, "** %ld\n", j);
	}

	long sum = 0;

	for (k = 0; k < N_SAMPLE; k++)
	{
		sum += ndel[k];
	}

	E2Profile::SetBogoMips((int)(sum / (500 * N_SAMPLE)));
#else
	DWORD t0;
	DWORD count;
	DWORD multiplier = 1;

	QString strbuf;
	QFileInfo fi(E2Profile::GetConfigFile());
	if (fi.exists())
	{
		strbuf = fi.canonicalPath();
	}
	else
	{
		strbuf = qApp->applicationDirPath();
	}

	strbuf.append("/bogomips.out");

	QFile fh(strbuf);

	if (!fh.open(QIODevice::WriteOnly | QIODevice::Text))
	{
		return;
	}

	QTextStream out(&fh);

	Wait w;
	int k;

	w.SetHwTimer(0);        //Disable Hw timer for bogomips calibration

	// First BogoMIPS evaluation
	do
	{
		multiplier *= 5;

		E2Profile::SetBogoMips(700 * multiplier);
		w.SetBogoKips();

		t0 = GetTickCount();

		for (k = 0; k < N_CICLI / 10; k++)
		{
			w.WaitUsec(UDELAY);
		}

		count = GetTickCount() - t0;

		out << "bogo = " << E2Profile::GetBogoMips() << ", count =" << count << "\n";
	}
	while (count <= MSLICE / 11);

	t0 = GetTickCount();

	for (k = 0; k < N_CICLI; k++)
	{
		w.WaitUsec(UDELAY);
	}

	count = GetTickCount() - t0;

	out << "1) count = " << count << " ** mslice = " << MSLICE << " *** bogo = " << E2Profile::GetBogoMips() << "\n";

	int j;

	//Fast correction
	for (j = 0; count > (MSLICE * 1.01) && j < 10; j++)
	{
		int nv;

		nv = (DWORD)((float) E2Profile::GetBogoMips() * (MSLICE / (float)count) + 0.5);
		E2Profile::SetBogoMips(nv);
		w.SetBogoKips();

		t0 = GetTickCount();

		for (k = 0; k < N_CICLI; k++)
		{
			w.WaitUsec(UDELAY);
		}

		count = GetTickCount() - t0;

		out << "2) count = " << count << " ** mslice = " << MSLICE << " *** bogo = " << E2Profile::GetBogoMips() << "\n";
	}

	//Fine correction
	for (j = 0; count < (MSLICE * 0.99) && j < 10; j++)
	{
		int nv;

		nv = (DWORD)((float) E2Profile::GetBogoMips() * 1.01 + 0.5);
		E2Profile::SetBogoMips(nv);
		w.SetBogoKips();

		t0 = GetTickCount();

		for (k = 0; k < N_CICLI; k++)
		{
			w.WaitUsec(UDELAY);
		}

		count = GetTickCount() - t0;

		out << "3) count = " << count << " ** mslice = " << MSLICE << " *** bogo = " << E2Profile::GetBogoMips() << "\n";
	}

	w.CheckHwTimer();       //Check to enable again Hw timer

	if (w.GetHwTimer())
	{
		out << "Hardware timer is OK.\n";
	}
	else
	{
		out << "Hardware timer is too slow, use bogomips (" << E2Profile::GetBogoMips() << ")\n";
	}

	fh.close();
#endif
}


int e2App::LoadDriver(int start)
{
	int rv = OK;

	(void)start;

	return rv;
}