13#error This is a C++ include file and cannot be used from plain C
20#define ngcbI2C_TX_SIZE7(n) ((((n + 1) * 8) + 2) * 3)
23#define ngcbI2C_TX_SIZE10(n) ((((n + 2) * 8) + 2) * 3)
30#define ngcbI2C_ERR_CLK 0x0000000A
33#define ngcbI2C_ERR_ACK 0x0000000B
51 explicit ngcbI2C(
int cnt) {Init_(0, 1, cnt);}
57 ngcbI2C(
int scl,
int sda) {Init_(scl, sda, 0);}
68 ngcbI2C(
int scl,
int sda,
int cnt) {Init_(scl, sda, cnt);}
74 ngcbI2C(
int scl,
int sda,
int sclIn,
int sdaIn,
int ackIn,
int cnt) {
97 void SDA_IN(
int bit) {sdaIn_ = bit; ackIn_ = sdaIn_;}
113 if (rx < 0) rxStretching_ = 0;
114 else rxStretching_ = rx;
119 if (tx < 0) txStretching_ = 0;
120 else txStretching_ = tx;
125 if (cnt < 0) clkStretchCnt_ = 0;
126 else clkStretchCnt_ = cnt;
133 int Write(
int addr,
const char *
byte,
int n) {
157 *b++ = ((addr >> i) & 0x1) << sda_;
158 *b++ = (((addr >> i) & 0x1) << sda_) | (1 << scl_);
159 *b++ = ((addr >> i) & 0x1) << sda_;
172 res = SendByte_(byteBuf);
188 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
189 *b++ = (((
byte[i] >> j) & 0x1) << sda_) | (1 << scl_);
190 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
193 res = SendByte_(byteBuf);
213 int Write10(
int addr,
const char *
byte,
int n) {
218 int a0 = addr & 0xff;
219 int a1 = 0xf0 | ((addr >> 7) & 0x6);
238 *b++ = ((a1 >> i) & 0x1) << sda_;
239 *b++ = (((a1 >> i) & 0x1) << sda_) | (1 << scl_);
240 *b++ = ((a1 >> i) & 0x1) << sda_;
242 res = SendByte_(byteBuf);
254 *b++ = ((a0 >> i) & 0x1) << sda_;
255 *b++ = (((a0 >> i) & 0x1) << sda_) | (1 << scl_);
256 *b++ = ((a0 >> i) & 0x1) << sda_;
258 res = SendByte_(byteBuf);
274 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
275 *b++ = (((
byte[i] >> j) & 0x1) << sda_) | (1 << scl_);
276 *b++ = ((
byte[i] >> j) & 0x1) << sda_;
279 res = SendByte_(byteBuf);
299 int Read(
int addr,
char *
byte,
int n) {
323 *b++ = ((addr >> i) & 0x1) << sda_;
324 *b++ = (((addr >> i) & 0x1) << sda_) | (1 << scl_);
325 *b++ = ((addr >> i) & 0x1) << sda_;
331 *b++ = (0x1 << sda_) | (0x0 << scl_);
332 *b++ = (0x1 << sda_) | (0x1 << scl_);
333 *b++ = (0x1 << sda_) | (0x0 << scl_);
338 res = SendByte_(byteBuf);
349 res = RecvByte_(&
byte[i], (i == (n-1)));
369 int Read10(
int addr,
char *
byte,
int n) {
374 int a0 = addr & 0xff;
375 int a1 = 0xf1 | ((addr >> 7) & 0x6);
394 *b++ = ((a1 >> i) & 0x1) << sda_;
395 *b++ = (((a1 >> i) & 0x1) << sda_) | (1 << scl_);
396 *b++ = ((a1 >> i) & 0x1) << sda_;
398 res = SendByte_(byteBuf);
410 *b++ = ((a0 >> i) & 0x1) << sda_;
411 *b++ = (((a0 >> i) & 0x1) << sda_) | (1 << scl_);
412 *b++ = ((a0 >> i) & 0x1) << sda_;
414 res = SendByte_(byteBuf);
425 res = RecvByte_(&
byte[i]);
446 int s = lastState_ & (1 << sda_) & (1 << scl_);
452 virtual int OutCB(
int d,
int *resultCode,
char *erms) {
457 fprintf(stdout,
"<%s>\n", s);
462 virtual int InCB(
int *d,
int *resultCode,
char *erms) {
513 void Init_(
int scl,
int sda,
int cnt) {
516 clkStretchCnt_ = cnt;
536 buf[0] = (0x1 << sda_) | (0x1 << scl_);
537 buf[1] = (0x0 << sda_) | (0x1 << scl_);
538 return (Out_(buf, 2));
544 buf[0] = (0x0 << sda_) | (0x1 << scl_);
545 buf[1] = (0x1 << sda_) | (0x1 << scl_);
546 return (Out_(buf, 2));
550 int SendByte_(
const int *buf) {
557 res = Out_(buf, 8 * 3, txStretching_);
584 sprintf(
ErrMsg(),
"no acknowledge from slave-device");
593 int RecvByte_(
char *
byte,
int last=0) {
600 if (sdaDirSwitch_ >= 0)
605 ds = 1 << sdaDirSwitch_;
606 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
620 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
621 buf[1] = (0x1 << sda_) | (0x1 << scl_) | ds;
622 res = Out_(buf, 2, rxStretching_);
640 buf[0] = (0x1 << sda_) | (0x0 << scl_) | ds;
650 *
byte |= (char)(((d >> sdaIn_) & 0x1) << i);
653 if (sdaDirSwitch_ >= 0)
658 buf[0] = (0x1 << sda_) | (0x0 << scl_);
671 buf[0] = (0x0 << sda_) | (0x0 << scl_);
672 buf[1] = (0x0 << sda_) | (0x1 << scl_);
673 buf[2] = (0x0 << sda_) | (0x0 << scl_);
680 buf[0] = (0x1 << sda_) | (0x0 << scl_);
681 buf[1] = (0x1 << sda_) | (0x1 << scl_);
682 buf[2] = (0x1 << sda_) | (0x0 << scl_);
684 res = Out_(buf, 3, txStretching_);
694 int GetAck_(
int *a) {
703 buf[0] = (0x1 << sda_) | (0x0 << scl_);
704 buf[1] = (0x1 << sda_) | (0x1 << scl_);
705 res = Out_(buf, 2, (!ignoreAck_ && rxStretching_) || txStretching_);
713 if ((sdaDirSwitch_ >= 0) && ((ackIn_ == sdaIn_) || (ackIn_ == sda_)))
718 buf[0] = (0x1 << sda_) | (0x1 << scl_) | (1 << sdaDirSwitch_);
729 res = In_(a, ackIn_);
746 buf[0] = (0x1 << sda_) | (0x0 << scl_);
757 int Out_(
const int *buf,
int size,
int doStretching=0) {
765 if (
OutCB(buf[i], &r, erms_) != 0)
777 if (doStretching && (clkStretchCnt_ > 0))
779 int thisClkState = (buf[i] >> scl_) & 0x1;
781 if ((lastClkState_ == 0) && (thisClkState == 1))
786 if (sclDirSwitch_ >= 0)
791 if (
OutCB(lastState_ | (1 << sclDirSwitch_), &r, erms_) != 0)
801 while (!((b >> sclIn_) & 0x1) && (cnt < clkStretchCnt_))
803 if (
InCB(&b, &r, erms_) != 0)
809 if (cnt >= clkStretchCnt_)
811 sprintf(
ErrMsg(),
"clock line is forced low");
818 if (sclDirSwitch_ >= 0)
823 if (
OutCB(lastState_, &r, erms_) != 0)
835 lastClkState_ = (buf[i] >> scl_) & 0x1;
842 int In_(
int *d) {
return (In_(d, sdaIn_));}
845 int In_(
int *d,
int line) {
851 if (
InCB(&b, &r, erms_) != 0)
860 *d = (b >> line) & 0x1;
866 void Int2Bin_(
char *str,
int in) {
872 if ((x & 0x00000001) == 1) str[31 - i]=
'1';
873 else str[31 - i]=
'0';
ngcbI2C()
Definition ngcbI2C.hpp:44
void AckOff()
Disable acknowledge handling.
Definition ngcbI2C.hpp:91
void AckOn()
Enable acknowledge handling.
Definition ngcbI2C.hpp:88
virtual int OutCB(int d, int *resultCode, char *erms)
Output call-back (1 word)
Definition ngcbI2C.hpp:452
void ClkStretching(int rx, int tx)
Set clock-stretching capabilities.
Definition ngcbI2C.hpp:109
void ClkStretchCnt(int cnt)
Set counter for clock-stretching.
Definition ngcbI2C.hpp:124
char * ErrMsg()
Return error-message buffer.
Definition ngcbI2C.hpp:85
int LastState()
Return last state of SDA and SCL.
Definition ngcbI2C.hpp:445
int Write10(int addr, const char *byte, int n)
10-bit write operation
Definition ngcbI2C.hpp:213
virtual ~ngcbI2C()
Destructor.
Definition ngcbI2C.hpp:82
int Read(int addr, char *byte, int n)
Standard 7-bit read operation.
Definition ngcbI2C.hpp:299
ngcbI2C(int cnt)
Definition ngcbI2C.hpp:51
void SDA_IN(int bit)
Define data-line bit for read (by default ack-line is the same)
Definition ngcbI2C.hpp:97
int Read10(int addr, char *byte, int n)
10-bit read operation
Definition ngcbI2C.hpp:369
void SCL_IN(int bit)
Define clock-line bit fot read (clock-stretching)
Definition ngcbI2C.hpp:94
void ACK_IN(int bit)
Define ack-line bit.
Definition ngcbI2C.hpp:100
ngcbI2C(int scl, int sda, int cnt)
Definition ngcbI2C.hpp:68
void SCL_DIR_SWITCH(int bit)
Define SCL direction switch bit.
Definition ngcbI2C.hpp:106
int ClkStretchCnt()
Return clock-stretching counter.
Definition ngcbI2C.hpp:130
ngcbI2C(int scl, int sda, int sclIn, int sdaIn, int ackIn, int cnt)
Definition ngcbI2C.hpp:74
virtual int InCB(int *d, int *resultCode, char *erms)
Input call-back (1 word)
Definition ngcbI2C.hpp:462
int Write(int addr, const char *byte, int n)
Standard 7-bit write operation.
Definition ngcbI2C.hpp:133
void SDA_DIR_SWITCH(int bit)
Define SDA direction switch bit.
Definition ngcbI2C.hpp:103
ngcbI2C(int scl, int sda)
Definition ngcbI2C.hpp:57
#define ngcbI2C_ERR_CLK
I2C-bus clock line forced low.
Definition ngcbI2C.hpp:30
#define ngcbI2C_ERR_ACK
I2C-bus no acknowledge.
Definition ngcbI2C.hpp:33