/********************************************************************* * * _Write * * Function description * Writes to a device on I2C. */ static U8 _Write(U8 Addr, U8 * pData, U16 NumBytesToWrite, U8 PartOfWriteRead) { U8 r; U16 i; r = _I2C_Start(); // Generate Start condition if(r == I2C_CODE_OK) { r = _I2C_WriteWithWait(Addr); // Write address (SLA+W) if (r == I2C_CODE_OK) { // // Write data // for (i = 0; i < NumBytesToWrite; i++) { r = _I2C_WriteWithWait(*pData); if (r != I2C_CODE_OK) { PartOfWriteRead = 0; // Override to generate stop condition on error break; } pData++; } } } if (PartOfWriteRead == 0) { _I2C_Stop(); // Generate Stop condition } return r; }
uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't // happen which means only the value of the slave address was send. Keep looping until // the slave address and write bit are actually sent. do{ _I2C_Stop(I2CDEV_M); // output stop state on I2C bus _I2C_Start(I2CDEV_M); // output start state on I2C bus while ((I2C_status != I2C_I2STAT_M_TX_START) && (I2C_status != I2C_I2STAT_M_TX_RESTART) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address LPC_I2C1->I2DAT = (sla <<1) & I2C_I2DAT_BITMASK; // transmit slave address & write bit LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC; while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK) && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for slaw to finish }while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK)); return 1; }
/********************************************************************* * * I2C_PCA9532_WriteRead() */ unsigned char I2CPCA9532_WriteRead(unsigned char * pData, unsigned short NumBytesToWrite, unsigned char * pBuf, unsigned short NumBytesToRead) { unsigned char r = 0; unsigned char Status = 0; unsigned short i = 0; r = _I2C_Start(); // Generate Start condition if(r != I2C_CODE_OK) { goto STOPWRITE; } r = _I2C_WriteWithWait(0xC0); // Write PCA9532 address if (r != I2C_CODE_OK) { goto STOPWRITE; } // // Write data // for (i = 0; i < NumBytesToWrite; i++) { r = _I2C_WriteWithWait(*pData); if (r != I2C_CODE_OK) { goto STOPWRITE; } pData++; } STOPWRITE: if (NumBytesToRead > 0) { r = _I2C_RepeatStart(); // Generate Start condition // // Transmit device address // if (r == I2C_CODE_OK) { r = _I2C_PutChar(0xc0 + 0x01); // Write SLA+R while(r == I2C_CODE_BUSY) { r = _I2C_PutChar(0xc0 + 0x01); } } // // Wait until SLA+R transmitted // while (1) { Status = _I2C_CheckStatus(); if (Status == 0x40) { // Data transmitted and ACK received break; } else if (Status != 0xF8) { // Error r = I2C_CODE_ERROR; break; } } if (r == I2C_CODE_OK) { // // Wait until address transmitted and receive data // for (i = 1; i <= NumBytesToRead; i++) { // // Wait until data transmitted // while (1) { Status = _I2C_CheckStatus(); if ((Status == 0x40) || (Status == 0x48) || (Status == 0x50 )) { // Data received // // Set generate NACK // if (i == NumBytesToRead) { r = _I2C_GetChar(I2C_MODE_ACK1, pBuf); } else { r = _I2C_GetChar(I2C_MODE_ACK0, pBuf); } r = _I2C_GetChar(I2C_MODE_READ, pBuf); // Read data while (r == I2C_CODE_EMPTY) { r = _I2C_GetChar(I2C_MODE_READ, pBuf); } pBuf++; break; } else if (Status != 0xF8) { // Error i = NumBytesToRead; r = I2C_CODE_ERROR; break; } } } } } _I2C_Stop(); // Generate Stop condition return r; }
/********************************************************************* * * _Read * * Function description * Reads from a device on I2C. */ static U8 _Read(U8 Addr, U8 * pData, U16 NumBytesToRead, U8 PartOfWriteRead) { U16 i; U8 Status; U8 r; // // Generate start condition // if (PartOfWriteRead) { r = _I2C_RepeatStart(); // Generate Start condition } else { r = _I2C_Start(); // Generate Start condition } // // Transmit device address // if (r == I2C_CODE_OK) { r = _I2C_PutChar(Addr + 0x01); // Write address (SLA+R) while (r == I2C_CODE_BUSY) { r = _I2C_PutChar(Addr + 0x01); } } // // Wait until SLA+R transmitted // while (1) { Status = _I2C_CheckStatus(); if (Status == 0x40) { // Data transmitted and ACK received break; } else if (Status != 0xF8) { // Error r = I2C_CODE_ERROR; break; } } if (r == I2C_CODE_OK) { // // Wait until address transmitted and receive data // for (i = 1; i <= NumBytesToRead; i++) { // // Wait until data transmitted // while (1) { Status = _I2C_CheckStatus(); if ((Status == 0x40) || (Status == 0x48) || (Status == 0x50)) { // Data received // // Set generate NACK // if (i == NumBytesToRead) { r = _I2C_GetChar(I2C_MODE_ACK1, pData); } else { r = _I2C_GetChar(I2C_MODE_ACK0, pData); } r = _I2C_GetChar(I2C_MODE_READ, pData); // Read data while (r == I2C_CODE_EMPTY) { r = _I2C_GetChar(I2C_MODE_READ, pData); } pData++; break; } else if (Status != 0xF8) { // Error i = NumBytesToRead; r = I2C_CODE_ERROR; break; } } } } _I2C_Stop(); // Generate Stop condition return r; }