File: e24xx.cpp

package info (click to toggle)
ponyprog 3.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,488 kB
  • sloc: cpp: 35,376; python: 981; sh: 565; makefile: 41; ansic: 38
file content (494 lines) | stat: -rw-r--r-- 10,605 bytes parent folder | download | duplicates (2)
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
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2021   Claudio Lanconelli                           //
//                                                                         //
//  http://ponyprog.sourceforge.net                                        //
//                                                                         //
//-------------------------------------------------------------------------//
//                                                                         //
// 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 <QString>

#include "types.h"
#include "e2cmdw.h"
#include "e2profil.h"
#include "e2awinfo.h"
#include "e24xx.h"              // Header file
#include "errcode.h"
#include "eeptypes.h"

#include <QDebug>

E24xx::E24xx(e2AppWinInfo *wininfo, BusIO *busp, int max_no_of_bank, int def_banksize)
	:       Device(wininfo, busp, def_banksize),
			max_bank(max_no_of_bank),
			timeout_loop(200),
			n_bank(0),
			sequential_read(1),             // lettura di un banco alla volta
			writepage_size(1)               // scrittura di un byte alla volta (no page write)
{
	//      qDebug() << "E24xx" << THEAPP;
	base_addr = E2Profile::GetI2CBaseAddr();
	E2Profile::SetI2CBaseAddr(base_addr);
	/***
	        //Inizializza il vettori degli indirizzi di default
	        //  questo nel caso non venga determinata automaticamente
	        //  la dimensione con il Probe()
	        int addr, k;
	        for (addr = base_addr, k = 0; k < max_bank; k++, addr += 2)
	                        eeprom_addr[k] = addr;
	***/
}

E24xx::~E24xx()
{
}

//Determina a quali indirizzi I2C si mappa, e
// se probe_size == 1 anche la dimensione (numero di banchi)
int E24xx::Probe(int probe_size)
{
	int addr, error, k;
	uint8_t ch;

	qDebug() << "E24xx::Probe(" << probe_size << ") - IN";

	n_bank = 0;

	for (addr = base_addr, k = 0; k < max_bank; k++, addr += 2)
	{
		if (GetBus()->Read(addr, &ch, 1) != 1)
		{
			if ((error = GetBus()->Error()) != IICERR_NOADDRACK)
			{
				//      GetBus()->Open(E2Profile::GetPort());
				return error;
			}
		}
		else
		{
			eeprom_addr[n_bank++] = addr;
		}
	}

	if (probe_size)
	{
		SetNoOfBank(n_bank);
	}

	qDebug() << "E24xx::Probe() = " << n_bank << " - OUT";

	return n_bank;
}


int E24xx::Read(int probe, int type)
{
	int error = Probe(probe || GetNoOfBank() == 0);

	if (error < 0)
	{
		return error;
	}

	GetBus()->ReadStart();

	if (type & PROG_TYPE)
	{
		int k;

		// legge il contenuto attuale della EEPROM in memoria
		//      for (k = 0; k < GetNoOfBank(); k++)
		//modificata in modo che legga sempre tutto l'intero
		// contenuto del dispositivo indipendentemente dalla
		// capacita` correntemente selezionata
		for (k = 0; k < n_bank; k++)
		{
			if ((error = bank_in(GetBufPtr() + k * GetBankSize(), k)))
			{
				//      GetBus()->Open(E2Profile::GetPort());              //ormai obsoleta
				return error;
			}

			if (GetBus()->ReadProgress((k + 1) * 100 / n_bank))
			{
				return OP_ABORTED;
			}
		}
	}

	GetBus()->ReadEnd();

	if (n_bank > 0)
	{
		return GetNoOfBank();
	}
	else
	{
		return 0;
	}
}

int E24xx::Write(int probe, int type)
{
	int error = Probe(probe || GetNoOfBank() == 0);

	if (error < 0)
	{
		return error;
	}

	GetBus()->WriteStart();

	if (type & PROG_TYPE)
	{
		// program the eeprom
		int k;

		for (k = 0; k < GetNoOfBank(); k++)
		{
			if ((error = bank_out(GetBufPtr() + k * GetBankSize(), k)))
			{
				//      GetBus()->Open(E2Profile::GetPort());
				return error;
			}

			if (GetBus()->WriteProgress((k + 1) * 100 / GetNoOfBank()))
			{
				return OP_ABORTED;
			}
		}
	}

	GetBus()->WriteEnd();

	return GetNoOfBank();
}

int E24xx::Verify(int type)
{
	Probe();        //Moved here from 7 lines above (10/12/99)

	if (GetNoOfBank() == 0)
	{
		return BADPARAM;
	}

	unsigned char *localbuf = new unsigned char[GetBankSize()];

	GetBus()->ReadStart();

	int rval = 1;

	if (type & PROG_TYPE)
	{
		// legge il contenuto attuale della EEPROM in memoria
		int k;

		for (k = 0; k < GetNoOfBank(); k++)
		{
			int error;

			if ((error = bank_in(localbuf, k)))
			{
				//      GetBus()->Open(E2Profile::GetPort());
				rval = error;
				break;
			}

			if (memcmp(GetBufPtr() + k * GetBankSize(), localbuf, GetBankSize()) != 0)
			{
				rval = 0;
				break;
			}

			if (GetBus()->ReadProgress((k + 1) * 100 / GetNoOfBank()))
			{
				rval = OP_ABORTED;
				break;
			}
		}
	}

	GetBus()->ReadEnd();
	delete[] localbuf;
	return rval;
}

#define MAX_BANK_SIZE	256

// questa routine si aspetta che in ingresso i 256 bytes da programmare l'eeprom
// siano memorizzati nel iicbuffer nelle locazioni da 1 (non da 0!) a 256
int E24xx::bank_out(uint8_t const *copy_buf, int bank, long size, long idx)
{
	int k, j;
	uint8_t buffer[MAX_BANK_SIZE + 1];

	if (size <= 0)
	{
		size = GetBankSize();
	}

	qDebug() << "E24xx::bank_out() = bank " << bank << ", size = " << size << ", idx = " << idx;

	if (copy_buf == 0 || bank >= GetNoOfBank() || size > MAX_BANK_SIZE)
	{
		return BADPARAM;
	}

	memcpy(buffer + 1, copy_buf, size);
#if 0

	for (j = 0; j < size; j++)
	{
		buffer[j] = j;

		if (GetBus()->Write(eeprom_addr[bank], buffer + j, 2) != 2)
		{
			return GetBus()->Error();
		}

		for (k = timeout_loop; k > 0 && GetBus()->Read(eeprom_addr[bank], buffer, 1) != 1; k--)
			;

		if (k == 0)
		{
			return E2P_TIMEOUT;
		}
	}

#else

	for (j = 0; j < size; j += writepage_size)
	{
		buffer[j] = j;

		if (GetBus()->Write(eeprom_addr[bank], buffer + j, 1 + writepage_size) != (1 + writepage_size))
		{
			return GetBus()->Error();
		}

		for (k = timeout_loop; k > 0 && GetBus()->Read(eeprom_addr[bank], buffer, 1) != 1; k--)
		{
			qApp->processEvents();
		}

		if (k == 0)
		{
			return E2P_TIMEOUT;
		}
	}

#endif

	return OK;
}

int E24xx::bank_in(uint8_t *copy_buf, int bank, long size, long idx)
{
	uint8_t ch;

	if (copy_buf == 0)   // || bank >= GetNoOfBank())
	{
		return BADPARAM;
	}

	if (size <= 0)
	{
		size = GetBankSize();
	}

	ch = (uint8_t)idx;

	if (GetBus()->StartWrite(eeprom_addr[bank], &ch, 1) != 1)
	{
		return GetBus()->Error();
	}

	if (sequential_read)
	{
		if (GetBus()->Read(eeprom_addr[bank], copy_buf, size) < size)
		{
			return GetBus()->Error();
		}
	}
	else
	{
		int k;

		for (k = 0; k < size; k++)
		{
			ch = (uint8_t)(k + idx);

			if (GetBus()->StartWrite(eeprom_addr[bank], &ch, 1) != 1)
			{
				return GetBus()->Error();
			}

			if (GetBus()->Read(eeprom_addr[bank], copy_buf + k, 1) != 1)
			{
				return GetBus()->Error();
			}
		}
	}

	return OK;
}

#define CMP_LEN 16

int E24xx::BankRollOverDetect(int force)
{
	int rlv = 0;

	if (GetNoOfBank() > 1)
	{
		uint8_t index;
		uint8_t buf[CMP_LEN + 1], buf1[CMP_LEN], buf2[CMP_LEN];
#if 0
		//Lettura dal primo banco
		index = 0;

		if (GetBus()->StartWrite(eeprom_addr[0], &index, 1) != 1)
		{
			return GetBus()->Error();
		}

		if (GetBus()->Read(eeprom_addr[0], buf1, CMP_LEN) != CMP_LEN)
		{
			return GetBus()->Error();
		}

		//Lettura dal secondo banco
		index = 0;

		if (GetBus()->StartWrite(eeprom_addr[1], &index, 1) != 1)
		{
			return GetBus()->Error();
		}

		if (GetBus()->Read(eeprom_addr[1], buf2, CMP_LEN) != CMP_LEN)
		{
			return GetBus()->Error();
		}

#else
		int error;

		if ((error = bank_in(buf1, 0, CMP_LEN)))
		{
			return error;
		}

		if ((error = bank_in(buf2, 1, CMP_LEN)))
		{
			return error;
		}

#endif

		//Se i primi due banchi sono differenti e` possibile
		//  determinare il bank-rollover
		if (memcmp(buf1, buf2, CMP_LEN) != 0)
		{
			//lettura iniziando dalla fine del primo banco
			index = GetBankSize() - 1;
#if 0

			if (GetBus()->StartWrite(eeprom_addr[0], &index, 1) != 1)
			{
				return GetBus()->Error();
			}

			if (GetBus()->Read(eeprom_addr[0], buf, CMP_LEN + 1) != CMP_LEN + 1)
			{
				return GetBus()->Error();
			}

#else

			if ((error = bank_in(buf, 0, CMP_LEN + 1, GetBankSize() - 1)))
			{
				return error;
			}

#endif

			rlv = (memcmp(buf + 1, buf1, CMP_LEN) == 0) ? 1 : 2;
		}
		else if (force)
		{
			//i banchi sono uguali: modifichiamo temporaneamente
			//  il primo byte del secondo banco (inversione di bit)
			buf[1] = ~buf2[0];      //inversione di bit
			buf[0] = 0;                     //indice (address byte)

			if (GetBus()->Write(eeprom_addr[1], buf, 2) != 2)
			{
				return GetBus()->Error();
			}

			int k;

			for (k = timeout_loop; k > 0 && GetBus()->Read(eeprom_addr[1], buf, 1) != 1; k--)
				;

			if (k == 0)
			{
				return E2P_TIMEOUT;
			}

			//Ok, ora procedi all roll-over detect
			index = GetBankSize() - 1;

			if (GetBus()->StartWrite(eeprom_addr[0], &index, 1) != 1)
			{
				return GetBus()->Error();
			}

			if (GetBus()->Read(eeprom_addr[0], buf, CMP_LEN + 1) != CMP_LEN + 1)
			{
				return GetBus()->Error();
			}

			rlv = (memcmp(buf + 1, buf1, CMP_LEN) == 0) ? 1 : 2;

			//rimetti a posto il contenuto dell'eeprom
			buf[1] = buf2[0];       //contenuto originale
			buf[0] = 0;                     //indice (address byte)

			if (GetBus()->Write(eeprom_addr[1], buf, 2) != 2)
			{
				return GetBus()->Error();
			}

			for (k = timeout_loop; k > 0 && GetBus()->Read(eeprom_addr[1], buf, 1) != 1; k--)
				;

			if (k == 0)
			{
				return E2P_TIMEOUT;
			}
		}
	}

	return rlv;
}