//**************************************************************************** // //! Invokes the I2C driver APIs to read from the device. This assumes the //! device local address to read from is set using the I2CWrite API. //! //! \param ucDevAddr is the device I2C slave address //! \param ucBuffer is the pointer to the read data to be placed //! \param ulSize is the length of data to be read //! \param ucFlags Flag //! //! This function works in a polling mode, //! 1. Writes the device register address to be written to. //! 2. In a loop, reads all the bytes over I2C //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** unsigned long I2CBufferRead(unsigned char ucDevAddr, unsigned char *ucBuffer, unsigned long ulSize,unsigned char ucFlags) { unsigned long ulNdx; // Set I2C codec slave address MAP_I2CMasterSlaveAddrSet(I2CA0_BASE,ucDevAddr, true); MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); if(ulSize == 1) { // Start single transfer. MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); } else { // Start the transfer. MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); // Wait for transfer completion. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } // Read first byte from the controller. ucBuffer[0] = MAP_I2CMasterDataGet(I2CA0_BASE); for(ulNdx=1; ulNdx < ulSize-1; ulNdx++) { MT9D111Delay(10); MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); // continue the transfer. MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // Wait for transfer completion. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } // Read next byte from the controller. ucBuffer[ulNdx] = MAP_I2CMasterDataGet(I2CA0_BASE); } MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); MAP_I2CMasterControl(I2CA0_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } // Wait for transfer completion. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } // Read the last byte from the controller. ucBuffer[ulSize-1] = MAP_I2CMasterDataGet(I2CA0_BASE); return 0; }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { if (!quantity) return 0; uint8_t oldWriteIndex = rxWriteIndex; uint8_t spaceAvailable = (rxWriteIndex >= rxReadIndex) ? BUFFER_LENGTH - (rxWriteIndex - rxReadIndex) : (rxReadIndex - rxWriteIndex); if (quantity > spaceAvailable) quantity = spaceAvailable; MAP_I2CMasterSlaveAddrSet(I2C_BASE, address, true); if(quantity == 1) I2CTransact(I2C_MASTER_CMD_SINGLE_RECEIVE); else I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_START); quantity--; while(quantity) { rxBuffer[rxWriteIndex] = MAP_I2CMasterDataGet(I2C_BASE); rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH; quantity--; if(quantity) I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_CONT); else I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } rxBuffer[rxWriteIndex] = MAP_I2CMasterDataGet(I2C_BASE); rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH; uint8_t bytesWritten = (rxWriteIndex >= oldWriteIndex) ? BUFFER_LENGTH - (rxWriteIndex - oldWriteIndex) : (oldWriteIndex - rxWriteIndex); return bytesWritten; }
unsigned long I2C_ReadMultiContinue(void) { unsigned char data; MAP_I2CMasterControl(I2CBase, I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2C_WaitForBus(I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP); data = MAP_I2CMasterDataGet(I2CBase); return data; }
unsigned long I2C_ReadMultiBegin(unsigned char slaveAddress, unsigned char address) { unsigned char data; MAP_I2CMasterSlaveAddrSet(I2CBase, slaveAddress, false); MAP_I2CMasterDataPut(I2CBase, address); MAP_I2CMasterControl(I2CBase, I2C_MASTER_CMD_BURST_SEND_START); I2C_WaitForBus(I2C_MASTER_CMD_BURST_SEND_ERROR_STOP); MAP_I2CMasterSlaveAddrSet(I2CBase, slaveAddress, true); MAP_I2CMasterControl(I2CBase, I2C_MASTER_CMD_BURST_RECEIVE_START); I2C_WaitForBus(I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP); data = MAP_I2CMasterDataGet(I2CBase); return data; }