File: i2cbus.cpp

package info (click to toggle)
ponyprog 3.0.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,752 kB
  • sloc: cpp: 33,187; python: 981; sh: 507; makefile: 36
file content (624 lines) | stat: -rw-r--r-- 11,341 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
616
617
618
619
620
621
622
623
624
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2019   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 "types.h"
#include "globals.h"
#include "e2profil.h"


#include "i2cbus.h"
#include "errcode.h"

#include "e2cmdw.h"

#include <QDebug>
#include <QtCore>

#ifdef Q_OS_LINUX
#  include <unistd.h>
#else
#  ifdef        __BORLANDC__
#    define     __inline__
#  else // _MICROSOFT_ VC++
#    define     __inline__ __inline
#    define _export
#  endif
#endif

#define SCLTIMEOUT      900     // enable SCL check and timing (for slaves that hold down the SCL line to slow the transfer)

#define BUSYDELAY       100
#define SDATIMEOUT      200

// Constructor
I2CBus::I2CBus(BusInterface *ptr)
	: BusIO(ptr)
{
	shot_delay = 0;
}

// Destructor
I2CBus::~I2CBus()
{
	Close();
}

int I2CBus::CheckBusy()
{
	register int count;

	for (count = BUSYDELAY; count > 0 && isSCLSDAuno(); count--)
	{
		WaitUsec(1);
	}

	return (count ? IICERR_BUSBUSY : 0);
}

int I2CBus::SendStart()
{
	register int err;

	setSCLSDA();
#ifdef  SCLTIMEOUT

	if (getSCL() == 0)
	{
		int k;

		for (k = SCLTIMEOUT; getSCL() == 0 && k > 0; k--)
		{
			WaitUsec(1);
		}

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

#else
	WaitUsec(shot_delay / 2);
#endif

	if ((err = CheckBusy()))
	{
		return err;
	}

	clearSDA();
	WaitUsec(shot_delay);   // tHD;STA = 4 usec
	clearSCL();
	WaitUsec(shot_delay / 2);

	return 0;
}

int I2CBus::SendStop()
{
	clearSCL();
	WaitUsec(shot_delay + 1);
	clearSDA();
	WaitUsec(shot_delay + 1);
	setSCL();
#ifdef  SCLTIMEOUT

	if (getSCL() == 0)
	{
		int k;

		for (k = SCLTIMEOUT; getSCL() == 0 && k > 0; k--)
		{
			WaitUsec(1);
		}

		if (k == 0)
		{
			//      qDebug() << "I2CBus::SendStop() *** SCL error";
			return IICERR_SCLCONFLICT;
		}
	}

#else
	WaitUsec(shot_delay / 2);
#endif
	WaitUsec(shot_delay + 1); // tSU;STOP = 4.7 usec
	setSDA();
	WaitUsec(shot_delay / 2 + 1);

	if (getSDA() == 0)
	{
		//      qDebug() << "I2CBus::SendStop() *** SDA error";
		return IICERR_SDACONFLICT;
	}

	//tBUF = 4.7 usec
	WaitUsec(shot_delay);

	return 0;
}

int I2CBus::SendBitMast(int b)
{
	bitSDA(b);
	WaitUsec(shot_delay / 2 + 1);   // tSU;DAT = 250 nsec (tLOW / 2 = 2 usec)
	setSCL();

	/* Se SCL e` ancora 0 significa che uno Slave sta` rallentando
	 * il trasferimento, attendi che SCL si rialzi */
	if (getSCL() == 0)
	{
#ifdef  SCLTIMEOUT
		int k;

		for (k = SCLTIMEOUT; getSCL() == 0 && k > 0; k--)
		{
			WaitUsec(1);
		}

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

#endif
	}

	WaitUsec(shot_delay / 2); // tHIGH / 2 = 2 usec

	if (!getSDA() != !b)
	{
		return IICERR_SDACONFLICT;
	}

	WaitUsec(shot_delay / 2); // tHIGH / 2 = 2 usec
	clearSCL();
	WaitUsec(shot_delay / 2); // tHD;DATA = 300 nsec (tLOW / 2 = 2 usec)

	return 0;
}

// returns a negative number if there's an error, 0 or 1 otherwise
int I2CBus::RecBitMast()
{
	register uint8_t b;

	setSDA();               // to receive data SDA must be high
	WaitUsec(shot_delay / 2 + 1);   // tSU;DAT = 250 nsec (tLOW / 2 = 2 usec)
	setSCL();

	/* Se SCL e` ancora 0 significa che uno Slave sta` rallentando
	 * il trasferimento, attendi che SCL si rialzi */
	if (getSCL() == 0)
	{
#ifdef  SCLTIMEOUT
		int k;

		for (k = SCLTIMEOUT; getSCL() == 0 && k > 0; k--)
		{
			WaitUsec(1);
		}

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

#endif
	}

	WaitUsec(shot_delay / 2); // tHIGH / 2 = 2 usec
	b = getSDA();
	WaitUsec(shot_delay / 2); // tHIGH / 2 = 2 usec
	clearSCL();
	WaitUsec(shot_delay / 2); // tHD;DATA = 300 nsec (tLOW / 2 = 2 usec)

	return b;
}

// OK, ora ci alziamo di un livello: operiamo sul byte
int I2CBus::SendByteMast(int by)
{
	int lrb, k;

	for (k = 7; k >= 0; k--)
		if ((lrb = SendBitMast(by & (1 << k))))
		{
			return lrb;
		}

	lrb = RecBitMast();     // acknowledge bit

	if (lrb < 0)            // < 0 means that an error occured
	{
		return lrb;
	}

	if (lrb)
	{
		return IICERR_NOTACK;
	}

	return 0;
}

int I2CBus::SendByteMastLSB(int by)
{
	int lrb, k;

	for (k = 0; k < 8; k++)
		if ((lrb = SendBitMast(by & (1 << k))))
		{
			return lrb;
		}

	lrb = RecBitMast();     // acknowledge bit

	if (lrb < 0)            // < 0 means that an error occured
	{
		return lrb;
	}

	if (lrb)
	{
		return IICERR_NOTACK;
	}

	return 0;
}

int I2CBus::RecByteMast(int ack)
{
	int k, lrb, val = 0;

	for (k = 7; k >= 0; k--)
	{
		lrb = RecBitMast();

		if (lrb < 0)
		{
			return lrb;
		}

		if (lrb)
		{
			val |= 1 << k;
		}
	}

	if ((k = SendBitMast(ack)))     // send the ack
	{
		return k;
	}

	setSDA();       // release SDA line to the slave trasmitter

	return val;
}

int I2CBus::RecByteMastLSB(int ack)
{
	int k, lrb, val = 0;

	for (k = 0; k < 8; k++)
	{
		lrb = RecBitMast();

		if (lrb < 0)
		{
			return lrb;
		}

		if (lrb)
		{
			val |= 1 << k;
		}
	}

	if ((k = SendBitMast(ack)))     // send the ack
	{
		return k;
	}

	setSDA();       // release SDA line to the slave trasmitter

	return val;
}

void I2CBus::SetDelay()
{
	int val = E2Profile::GetI2CSpeed();
	int n;

	switch (val)
	{
	case TURBO:
		n = 0;          // as fast as your PC can
		break;

	case FAST:
		n = 2;          // > 100 Khz, < 400 Khz
		break;

	case SLOW:
		n = 20;         // (< 25 Khz)
		break;

	case VERYSLOW:
		n = 100;
		break;

	case ULTRASLOW:
		n = 1000;
		break;

	default:
		n = 5;          //Default (< 100KHz)
		break;
	}

	BusIO::SetDelay(n);

	qDebug() << "I2CBus::SetDelay() = " << n;
}

long I2CBus::Read(int slave, uint8_t *data, long length, int page_size)
{
	long len;

	qDebug() << "I2CBus::Read(" << slave << ", " << (void *) data << ", " << length << ") - IN";
	len = StartRead(slave, data, length);

	if (len == length)
		if (err_no || Stop())
		{
			len = 0;
		}

	qDebug() << "I2CBus::Read() = " << len << ", err_no = " << err_no << " - OUT";

	return len;
}

long I2CBus::Write(int slave, uint8_t const *data, long length, int page_size)
{
	long len;

	qDebug() << "I2CBus::Write(" << slave << ", " << (hex) << data << ", " << (dec) << length << ") - IN";

	len = StartWrite(slave, data, length);

	if (len == length)
		if (err_no || Stop())
		{
			len = 0;
		}

	qDebug() << "I2CBus::Write() = " << len << ", err_no = " << err_no << " - OUT";

	return len;
}

int I2CBus::ReadByte(int ack, int lsb)
{
	if (lsb)
	{
		return RecByteMastLSB(ack);
	}
	else
	{
		return RecByteMast(ack);
	}
}

int I2CBus::WriteByte(int by, int lsb)
{
	if (lsb)
	{
		return SendByteMastLSB(by);
	}
	else
	{
		return SendByteMast(by);
	}
}

int I2CBus::Start(uint8_t slave)
{
	int temp;

	// send Start
	if ((temp = SendStart()))
	{
		err_no = temp;
		return err_no;
	}

	if ((temp = SendByteMast(slave)) != 0)
	{
		err_no = (temp == IICERR_NOTACK) ? IICERR_NOADDRACK : temp;
		last_addr = slave;
		return err_no;
	}

	return 0;
}

/* se length e` zero, viene ricevuto un byte senza ack per cercare di
 * liberare il bus da uno slave che per qualche motivo e` ancora in
 * trasmissione. In questo caso data deve puntare ad un buffer di (almeno)
 * un byte.
 */
long I2CBus::StartRead(uint8_t slave, uint8_t *data, long length)
{
	int temp;
	long len = length;

	qDebug() << "I2CBus::StartRead(" << slave << ", " << (hex) << data << ", " << (dec) << length << ") - IN";

	if (len > 0)
	{
		// send Start
		if ((temp = SendStart()))
		{
			err_no = temp;
			return 0;
		}

		if ((temp = SendByteMast(slave | 1)) != 0)
		{
			err_no = (temp == IICERR_NOTACK) ? IICERR_NOADDRACK : temp;
			last_addr = slave | 1;
			return 0;
		}

		while (len > 1)
		{
			if ((temp = RecByteMast(0)) < 0)
			{
				err_no = temp;
				goto fineR;
			}

			*data++ = (uint8_t)temp;
			len--;
		}
	}

	// last byte received without acknowledge
	if ((temp = RecByteMast(1)) < 0)
	{
		err_no = temp;
		goto fineR;
	}

	len--;

	*data = (uint8_t)temp;
	err_no = 0;

fineR:
	qDebug() << "I2CBus::StartRead() = " << (long)(length - len) << ", err_no = " << err_no << " - OUT";

	return length - len;
}

long I2CBus::StartWrite(uint8_t slave, uint8_t const *data, long length)
{
	int error;
	long len = length;

	qDebug() << "I2CBus::StartWrite(" << slave << ", " << (hex) << data << ", " << (dec) <<  length << ") - IN";

	if (len == 0)
	{
		return 0;
	}

	if ((error = SendStart()))
	{
		err_no = error;
		return 0;
	}

	if ((error = SendByteMast(slave & 0xFE)))
	{
		err_no = (error == IICERR_NOTACK) ? IICERR_NOADDRACK : error;
		last_addr = slave & 0xFE;
		return 0;
	}

	while (len > 0)
	{
		if ((error = SendByteMast(*data++)) != 0)
		{
			err_no = error;
			goto fineW;
		}

		len--;
	}

fineW:
	qDebug() << "I2CBus::StartWrite() = " << (long)(length - len) << ", err_no = " << err_no << " - OUT";

	return length - len;
}

int I2CBus::Stop(void)
{
	qDebug() << "I2CBus::Stop() - IN";

	err_no = SendStop() ? IICERR_STOP : 0;

	qDebug() << "I2CBus::Stop() = " << err_no << " - OUT";

	return err_no;
}

int I2CBus::Reset(void)
{
	qDebug() << "I2CBus::Reset() - IN";

	SetDelay();

	uint8_t c;
	Read(0x00, &c, 0);
	setSCLSDA();
	WaitMsec(100);          //tolto il commento il 25/01/1999 e raddoppiato per permettere il funzionamento della 2402 non Cmos

	qDebug() << "I2CBus::Reset() - OUT";

	return OK;
}

void I2CBus::Close(void)
{
	qDebug() << "I2CBus::Close() busI=" << (hex) << busI << (dec);

	setSCLSDA();
	BusIO::Close();
}

int I2CBus::TestPort(int port)
{
	int ret_val = busI->TestSave(port);

	if (ret_val == OK)
	{
		// Send general call address 0x00
		int err = Start(0x00 | 1);

		if (err != OK  &&  err != IICERR_NOADDRACK)
		{
			ret_val = err;
		}
	}

	busI->TestRestore();

	return ret_val;
}