tS8 i2cMyWrite(tU8 addr, tU8 *pData, tU16 len) { tS8 retCode = 0; tU8 i = 0; do { /* generate Start condition */ retCode = i2cStart(); if (retCode != I2C_CODE_OK) break; /* write address */ retCode = i2cWriteWithWait(addr); if (retCode != I2C_CODE_OK) break; for (i = 0; i < len; i++) { retCode = i2cWriteWithWait(*pData); if (retCode != I2C_CODE_OK) break; pData++; } } while (0); /* generate Stop condition */ i2cStop(); return retCode; }
/****************************************************************************** * * Description: * Communicate with PCA9532 * * Params: * [in] pBuf - buffer of bytes to write * [in] len - length of pBuf * [in] pBuf2 - buffer for received data * [in] len - length of pBuf2 * * Returns: * I2C status code * *****************************************************************************/ tS8 pca9532(tU8* pBuf, tU16 len, tU8* pBuf2, tU16 len2) { tS8 retCode = 0; tU8 status = 0; tU16 i = 0; do { /* generate Start condition */ retCode = i2cStart(); if (I2C_CODE_OK != retCode) { break; } /* write pca9532 address */ retCode = i2cWriteWithWait(0xc0); if (I2C_CODE_OK != retCode) { break; } /* write data */ for (i = 0; i < len; i++) { retCode = i2cWriteWithWait(*pBuf); if (I2C_CODE_OK != retCode) { break; } pBuf++; } } while (0); if (len2 > 0) { /* Generate Start condition */ retCode = i2cRepeatStart(); /* Transmit device address */ if (I2C_CODE_OK == retCode) { /* Write SLA+R */ retCode = i2cPutChar(0xc0 + 0x01); while (I2C_CODE_BUSY == retCode) { retCode = i2cPutChar(0xc0 + 0x01); } } /* Wait until SLA+R transmitted */ while (1) { /* Get new status */ status = i2cCheckStatus(); if (0x40 == status) { /* Data transmitted and ACK received */ break; } else if (0xf8 != status) { /* error */ retCode = I2C_CODE_ERROR; break; } } if (I2C_CODE_OK == retCode) { /* wait until address transmitted and receive data */ for (i = 1; i <= len2; i++) { /* wait until data transmitted */ while (1) { /* Get new status */ status = i2cCheckStatus(); if ((0x40 == status) || (0x48 == status) || (0x50 == status)) { /* Data received */ if (i == len2) { /* Set generate NACK */ retCode = i2cGetChar(I2C_MODE_ACK1, pBuf2); } else { retCode = i2cGetChar(I2C_MODE_ACK0, pBuf2); } /* Read data */ retCode = i2cGetChar(I2C_MODE_READ, pBuf2); while (I2C_CODE_EMPTY == retCode) { retCode = i2cGetChar(I2C_MODE_READ, pBuf2); } pBuf2++; break; } else if (0xf8 != status) { /* ERROR */ i = len2; retCode = I2C_CODE_ERROR; break; } } } } } /* Generate Stop condition */ i2cStop(); return retCode; }