69 #include <sys/ioctl.h>
70 #include <linux/i2c.h>
71 #include <linux/i2c-dev.h>
110 #define I2CBUF_SIZE 150
111 #define APPDATABUF_SIZE 150
112 #define MODE2BUF_SIZE 1024 // Must be power of 2
113 #define MIN_MSG_LEN 6
114 #define SLEEPuS ((ULONG)(1000))
115 #define SEC_1 (((ULONG)(1000000))/SLEEPuS)
127 static int ThreadRunState;
130 static struct i2c_rdwr_ioctl_data msg_rdwr;
131 static struct i2c_msg i2cmsg;
138 static char *pBundleIdString;
139 static char *pBundleSeedIdString;
146 int I2cRead(
int fd,
unsigned int addr,
char *buf,
unsigned char len);
147 int I2cWrite(
int fd,
unsigned int addr,
char *buf,
unsigned char len);
157 #define BUFBytesFree (((Mode2InBuf.OutPtr - Mode2InBuf.InPtr) - 1) & ((UWORD)(MODE2BUF_SIZE - 1)))
158 #define BUFBytesAvail ((Mode2InBuf.InPtr - Mode2InBuf.OutPtr) & ((UWORD)(MODE2BUF_SIZE - 1)))
159 #define BUFAddInPtr(Val) (Mode2InBuf.InPtr = ((Mode2InBuf.InPtr + Val) & ((UWORD)(MODE2BUF_SIZE - 1))))
160 #define BUFAddOutPtr(Val) (Mode2InBuf.OutPtr = ((Mode2InBuf.OutPtr + Val) & ((UWORD)(MODE2BUF_SIZE - 1))))
162 #define DISCONNDueToErr {\
170 RESULT Result = FAIL;
172 BtFile = open(BT_DEVICE_NAME, O_RDWR);
180 pMode2WriteBuf = pWriteBuf;
181 pBundleIdString = pBundleId;
182 pBundleSeedIdString = pBundleSeedId;
191 if (BtFile >= MIN_HANDLE)
202 char *message =
"I2c Func thread";
204 Mode2InBuf.
InPtr = 0;
209 I2cRet = pthread_create(&I2cFunc,
NULL,
I2cCtrl, (
void*) message);
227 if (I2cFile >= MIN_HANDLE)
240 Mode2InBuf.
InPtr = 0;
243 I2cFile = open(
"/dev/i2c-1", O_RDWR);
248 printf(
"Can't open device file /dev/i2c-1\n");
253 ioctl(I2cFile,I2C_SLAVE, 0);
282 while(ThreadRunState)
313 printf(
"\r\nERROR - OVERRUN\r\n");
321 printf(
"\r\nERROR - CRC\r\n");
329 printf(
"\r\nERROR - INCORRECT_ACTION\r\n");
337 printf(
"\r\nERROR - UNEXPECTED\r\n");
345 printf(
"\r\nERROR - RAW_OVERRUN\r\n");
354 printf(
"\r\nERROR - UNKNOWN\r\n");
381 printf(
"\r\n'A' Read error \r\n");
386 printf(
"\r\nA - Reading mode2 data from decoding to Tx on Bluetooth\r\n");
387 for(Test = 0; Test < Size; Test++)
389 printf(
"Buf[%d] = %02X\r\n",Test,Buf[Test]);
407 printf(
"\r\nW - Waiting for more mode2 data\r\n");
413 if (BytesToTx > (
UWORD)(Buf[1]))
416 BytesToTx = (
UWORD)(Buf[1]);
420 printf(
"\r\n Remote data:");
422 for (TmpCnt = 0; TmpCnt < BytesToTx; TmpCnt++)
425 printf(
"%02X, ",Mode2InBuf.
Buf[Mode2InBuf.
OutPtr]);
427 TmpBuf[TmpCnt] = Mode2InBuf.
Buf[Mode2InBuf.
OutPtr];
435 printf(
"\r\n'W' write error \r\n");
454 printf(
"\r\nData to decoding from Bluetooth = %d, Inptr = %d, OutPtr = %d\r\n",BytesAvail,Mode2InBuf.
InPtr,Mode2InBuf.
OutPtr);
461 TmpBuf[TmpCnt + 1] = Mode2InBuf.
Buf[Mode2InBuf.
OutPtr];
472 printf(
"\r\n.... Remote data to the Pic, Bytes transferred %d\r\n",ByteCnt);
483 ByteCnt = pMode2WriteBuf->
InPtr - pMode2WriteBuf->
OutPtr;
494 memcpy((
char *)&(TmpBuf[1]), (
char *)&(pMode2WriteBuf->
Buf[pMode2WriteBuf->
OutPtr]), ByteCnt);
495 pMode2WriteBuf->
OutPtr += ByteCnt;
497 if (pMode2WriteBuf->
OutPtr == pMode2WriteBuf->
InPtr)
500 pMode2WriteBuf->
InPtr = 0;
501 pMode2WriteBuf->
OutPtr = 0;
511 printf(
"\r\n.... Application data to the Pic, Bytes to send %d\r\n",ByteCnt);
518 printf(
"\r\nI - Idle state\r\n");
537 pReadBuf->
InPtr = Size;
541 printf(
"\r\nR - %d bytes of App data read \r\n",Size);
542 for(Test = 0; Test < Size; Test++)
544 printf(
"AppData[%d] = %02X\r\n",Test,pReadBuf->
Buf[Test]);
562 strcpy(&IdString[1],(
char*)pBundleSeedIdString);
563 strcat(&IdString[1],(
char*)pBundleIdString);
565 IdString[0] = (
UBYTE)(strlen(&(IdString[1])));
581 printf(
"\r\nS - Reset source = %02X, Status = %02X\r\n",Buf[1], Buf[2]);
589 printf(
"\r\nERROR - failure on reset\r\n");
599 printf(
"\r\nStatus return not recognized....\r\n");
609 if (I2cFile >= MIN_HANDLE)
615 pthread_exit((
void *)
RtnMsg);
627 read(BtFile, Buf, 1);
647 write(BtFile, Set,
sizeof(Set));
661 write(BtFile, Clr,
sizeof(Clr));
675 write(BtFile, Set,
sizeof(Set));
689 write(BtFile, Clr,
sizeof(Clr));
703 write(BtFile, HiImp,
sizeof(HiImp));
716 int I2cRead(
int fd,
unsigned int addr,
char *buf,
unsigned char len)
723 fprintf(stderr,
"I can only write I2CBUF_SIZE bytes at a time!\r\n");
729 msg_rdwr.msgs = &i2cmsg;
733 i2cmsg.flags = I2C_M_RD;
737 if((i=ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0)
740 printf(
"\r\nIOCTL Read error, Len = %d, i2cmsg.len = %d \r\n",len, i2cmsg.len);
750 int I2cWrite(
int fd,
unsigned int addr,
char *buf,
unsigned char len)
754 msg_rdwr.msgs = &i2cmsg;
762 if((i = ioctl(fd,I2C_RDWR,&msg_rdwr)) < 0)
765 printf(
"\r\nIOCTL write error, Len = %d, i2cmsg.len = %d \r\n",len, i2cmsg.len);
779 UWORD BytesFreeInBuf;
780 UWORD BytesTransferred;
789 if (Length > BytesFreeInBuf)
791 BytesTransferred = BytesFreeInBuf;
795 BytesTransferred = Length;
798 for(Cnt = 0; Cnt < BytesTransferred; Cnt++)
800 Mode2InBuf.
Buf[Mode2InBuf.
InPtr] = pBuf[Cnt];
803 BytesAccepted = BytesTransferred;
805 return(BytesAccepted);
#define BUFAddOutPtr(Val)
UBYTE cBtI2cBufReady(void)
void I2cHiImpPIC_EN(void)
void DecodeMode1(UBYTE BufNo)
UBYTE I2cGetBootStatus(void)
UWORD DataToMode2Decoding(UBYTE *pBuf, UWORD Length)
void I2cClearPIC_EN(void)
#define MAX_BUNDLE_ID_SIZE
void * I2cCtrl(void *ptr)
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
UWORD cBtI2cToBtBuf(UBYTE *pBuf, UWORD Size)
int I2cRead(int fd, unsigned int addr, char *buf, unsigned char len)
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
UBYTE I2cReadStatus(UBYTE *pBuf)
RESULT I2cInit(READBUF *pBuf, WRITEBUF *pWriteBuf, char *pBundleId, char *pBundleSeedId)
int I2cWrite(int fd, unsigned int addr, char *buf, unsigned char len)
void I2cClearPIC_RST(void)
#define MAX_BUNDLE_SEED_ID_SIZE