12#error This is a C++ include file and cannot be used from plain C
19#define ngcbI2C_TX_SIZE7(n) ((((n + 1) * 8) + 2) * 3)
22#define ngcbI2C_TX_SIZE10(n) ((((n + 2) * 8) + 2) * 3)
29#define ngcbI2C_ERR_CLK 0x0000000A
32#define ngcbI2C_ERR_ACK 0x0000000B
50 explicit ngcbI2C(
int cnt) {Init_(0, 1, cnt);}
56 ngcbI2C(
int scl,
int sda) {Init_(scl, sda, 0);}
67 ngcbI2C(
int scl,
int sda,
int cnt) {Init_(scl, sda, cnt);}
73 ngcbI2C(
int scl,
int sda,
int sclIn,
int sdaIn,
int ackIn,
int cnt) {
96 void SDA_IN(
int bit) {sdaIn_ = bit; ackIn_ = sdaIn_;}
112 if (rx < 0) rxStretching_ = 0;
113 else rxStretching_ = rx;
118 if (tx < 0) txStretching_ = 0;
119 else txStretching_ = tx;
124 if (cnt < 0) clkStretchCnt_ = 0;
125 else clkStretchCnt_ = cnt;
132 int Write(
int addr,
const char *
byte,
int n) {
156 *b++ = ((addr >> i) & 0x1) << sda_;
157 *b++ = (((addr >> i) & 0x1) << sda_) | (1 << scl_);
158 *b++ = ((addr >> i) & 0x1) << sda_;
171 res = SendByte_(byteBuf);
187 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
188 *b++ = (((
byte[i] >> j) & 0x1) << sda_) | (1 << scl_);
189 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
192 res = SendByte_(byteBuf);
212 int Write10(
int addr,
const char *
byte,
int n) {
217 int a0 = addr & 0xff;
218 int a1 = 0xf0 | ((addr >> 7) & 0x6);
237 *b++ = ((a1 >> i) & 0x1) << sda_;
238 *b++ = (((a1 >> i) & 0x1) << sda_) | (1 << scl_);
239 *b++ = ((a1 >> i) & 0x1) << sda_;
241 res = SendByte_(byteBuf);
253 *b++ = ((a0 >> i) & 0x1) << sda_;
254 *b++ = (((a0 >> i) & 0x1) << sda_) | (1 << scl_);
255 *b++ = ((a0 >> i) & 0x1) << sda_;
257 res = SendByte_(byteBuf);
273 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
274 *b++ = (((
byte[i] >> j) & 0x1) << sda_) | (1 << scl_);
275 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
278 res = SendByte_(byteBuf);
298 int Read(
int addr,
char *
byte,
int n) {
322 *b++ = ((addr >> i) & 0x1) << sda_;
323 *b++ = (((addr >> i) & 0x1) << sda_) | (1 << scl_);
324 *b++ = ((addr >> i) & 0x1) << sda_;
330 *b++ = (0x1 << sda_) | (0x0 << scl_);
331 *b++ = (0x1 << sda_) | (0x1 << scl_);
332 *b++ = (0x1 << sda_) | (0x0 << scl_);
337 res = SendByte_(byteBuf);
348 res = RecvByte_(&
byte[i], (i == (n-1)));
368 int Read10(
int addr,
char *
byte,
int n) {
373 int a0 = addr & 0xff;
374 int a1 = 0xf1 | ((addr >> 7) & 0x6);
393 *b++ = ((a1 >> i) & 0x1) << sda_;
394 *b++ = (((a1 >> i) & 0x1) << sda_) | (1 << scl_);
395 *b++ = ((a1 >> i) & 0x1) << sda_;
397 res = SendByte_(byteBuf);
409 *b++ = ((a0 >> i) & 0x1) << sda_;
410 *b++ = (((a0 >> i) & 0x1) << sda_) | (1 << scl_);
411 *b++ = ((a0 >> i) & 0x1) << sda_;
413 res = SendByte_(byteBuf);
424 res = RecvByte_(&
byte[i]);
445 int s = lastState_ & (1 << sda_) & (1 << scl_);
451 virtual int OutCB(
int d,
int *resultCode,
char *erms) {
456 fprintf(stdout,
"<%s>\n", s);
461 virtual int InCB(
int *d,
int *resultCode,
char *erms) {
512 void Init_(
int scl,
int sda,
int cnt) {
515 clkStretchCnt_ = cnt;
535 buf[0] = (0x1 << sda_) | (0x1 << scl_);
536 buf[1] = (0x0 << sda_) | (0x1 << scl_);
537 return (Out_(buf, 2));
543 buf[0] = (0x0 << sda_) | (0x1 << scl_);
544 buf[1] = (0x1 << sda_) | (0x1 << scl_);
545 return (Out_(buf, 2));
549 int SendByte_(
const int *buf) {
556 res = Out_(buf, 8 * 3, txStretching_);
583 sprintf(
ErrMsg(),
"no acknowledge from slave-device");
592 int RecvByte_(
char *
byte,
int last=0) {
599 if (sdaDirSwitch_ >= 0)
604 ds = 1 << sdaDirSwitch_;
605 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
619 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
620 buf[1] = (0x1 << sda_) | (0x1 << scl_) | ds;
621 res = Out_(buf, 2, rxStretching_);
639 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
649 *
byte |= (char)(((d >> sdaIn_) & 0x1) << i);
652 if (sdaDirSwitch_ >= 0)
657 buf[0] = (0x1 << sda_) | (0x0 << scl_);
670 buf[0] = (0x0 << sda_) | (0x0 << scl_);
671 buf[1] = (0x0 << sda_) | (0x1 << scl_);
672 buf[2] = (0x0 << sda_) | (0x0 << scl_);
679 buf[0] = (0x1 << sda_) | (0x0 << scl_);
680 buf[1] = (0x1 << sda_) | (0x1 << scl_);
681 buf[2] = (0x1 << sda_) | (0x0 << scl_);
683 res = Out_(buf, 3, txStretching_);
693 int GetAck_(
int *a) {
702 buf[0] = (0x1 << sda_) | (0x0 << scl_);
703 buf[1] = (0x1 << sda_) | (0x1 << scl_);
704 res = Out_(buf, 2, (!ignoreAck_ && rxStretching_) || txStretching_);
712 if ((sdaDirSwitch_ >= 0) && ((ackIn_ == sdaIn_) || (ackIn_ == sda_)))
717 buf[0] = (0x1 << sda_) | (0x1 << scl_) | (1 << sdaDirSwitch_);
728 res = In_(a, ackIn_);
745 buf[0] = (0x1 << sda_) | (0x0 << scl_);
756 int Out_(
const int *buf,
int size,
int doStretching=0) {
764 if (
OutCB(buf[i], &r, erms_) != 0)
776 if (doStretching && (clkStretchCnt_ > 0))
778 int thisClkState = (buf[i] >> scl_) & 0x1;
780 if ((lastClkState_ == 0) && (thisClkState == 1))
785 if (sclDirSwitch_ >= 0)
790 if (
OutCB(lastState_ | (1 << sclDirSwitch_), &r, erms_) != 0)
800 while (!((b >> sclIn_) & 0x1) && (cnt < clkStretchCnt_))
802 if (
InCB(&b, &r, erms_) != 0)
808 if (cnt >= clkStretchCnt_)
810 sprintf(
ErrMsg(),
"clock line is forced low");
817 if (sclDirSwitch_ >= 0)
822 if (
OutCB(lastState_, &r, erms_) != 0)
834 lastClkState_ = (buf[i] >> scl_) & 0x1;
841 int In_(
int *d) {
return (In_(d, sdaIn_));}
844 int In_(
int *d,
int line) {
850 if (
InCB(&b, &r, erms_) != 0)
859 *d = (b >> line) & 0x1;
865 void Int2Bin_(
char *str,
int in) {
871 if ((x & 0x00000001) == 1) str[31 - i]=
'1';
872 else str[31 - i]=
'0';
ngcbI2C()
Definition ngcbI2C.hpp:43
void AckOff()
Disable acknowledge handling.
Definition ngcbI2C.hpp:90
void AckOn()
Enable acknowledge handling.
Definition ngcbI2C.hpp:87
virtual int OutCB(int d, int *resultCode, char *erms)
Output call-back (1 word)
Definition ngcbI2C.hpp:451
void ClkStretching(int rx, int tx)
Set clock-stretching capabilities.
Definition ngcbI2C.hpp:108
void ClkStretchCnt(int cnt)
Set counter for clock-stretching.
Definition ngcbI2C.hpp:123
char * ErrMsg()
Return error-message buffer.
Definition ngcbI2C.hpp:84
int LastState()
Return last state of SDA and SCL.
Definition ngcbI2C.hpp:444
int Write10(int addr, const char *byte, int n)
10-bit write operation
Definition ngcbI2C.hpp:212
virtual ~ngcbI2C()
Destructor.
Definition ngcbI2C.hpp:81
int Read(int addr, char *byte, int n)
Standard 7-bit read operation.
Definition ngcbI2C.hpp:298
ngcbI2C(int cnt)
Definition ngcbI2C.hpp:50
void SDA_IN(int bit)
Define data-line bit for read (by default ack-line is the same)
Definition ngcbI2C.hpp:96
int Read10(int addr, char *byte, int n)
10-bit read operation
Definition ngcbI2C.hpp:368
void SCL_IN(int bit)
Define clock-line bit fot read (clock-stretching)
Definition ngcbI2C.hpp:93
void ACK_IN(int bit)
Define ack-line bit.
Definition ngcbI2C.hpp:99
ngcbI2C(int scl, int sda, int cnt)
Definition ngcbI2C.hpp:67
void SCL_DIR_SWITCH(int bit)
Define SCL direction switch bit.
Definition ngcbI2C.hpp:105
int ClkStretchCnt()
Return clock-stretching counter.
Definition ngcbI2C.hpp:129
ngcbI2C(int scl, int sda, int sclIn, int sdaIn, int ackIn, int cnt)
Definition ngcbI2C.hpp:73
virtual int InCB(int *d, int *resultCode, char *erms)
Input call-back (1 word)
Definition ngcbI2C.hpp:461
int Write(int addr, const char *byte, int n)
Standard 7-bit write operation.
Definition ngcbI2C.hpp:132
void SDA_DIR_SWITCH(int bit)
Define SDA direction switch bit.
Definition ngcbI2C.hpp:102
ngcbI2C(int scl, int sda)
Definition ngcbI2C.hpp:56
#define ngcbI2C_ERR_CLK
I2C-bus clock line forced low.
Definition ngcbI2C.hpp:29
#define ngcbI2C_ERR_ACK
I2C-bus no acknowledge.
Definition ngcbI2C.hpp:32