//**************************************************************************** // //! 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::endTransmission(uint8_t sendStop) { uint8_t error = I2C_MASTER_ERR_NONE; if(TX_BUFFER_EMPTY) return 0; MAP_I2CMasterSlaveAddrSet(I2C_BASE, txAddress, false); MAP_I2CMasterDataPut(I2C_BASE, txBuffer[txReadIndex]); I2CTransact(I2C_MASTER_CMD_BURST_SEND_START); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; while(!TX_BUFFER_EMPTY) { MAP_I2CMasterDataPut(I2C_BASE, txBuffer[txReadIndex]); I2CTransact(I2C_MASTER_CMD_BURST_SEND_CONT); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; } if(sendStop) { I2CTransact(I2C_MASTER_CMD_BURST_SEND_STOP); } else { currentState = MASTER_TX; } /* Indicate that we are done transmitting */ transmitting = 0; return error; }
unsigned long I2CBufferWrite(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, false); // Write the first byte to the controller. MAP_I2CMasterDataPut(I2CA0_BASE,ucBuffer[0]); MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); if( ulSize == 1) { MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_SINGLE_SEND); } else { // Continue the transfer. MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_START); // Wait until the current byte has been transferred. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } for(ulNdx=1; ulNdx < ulSize-1; ulNdx++) { // Write the next byte to the controller. MAP_I2CMasterDataPut(I2CA0_BASE,ucBuffer[ulNdx]); // Clear Master Interrupt MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); // Continue the transfer. MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); // Wait until the current byte has been transferred. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } } // Write the last byte to the controller. MAP_I2CMasterDataPut(I2CA0_BASE, ucBuffer[ulSize-1]); MAP_I2CMasterIntClearEx(I2CA0_BASE, I2C_INT_MASTER); // End the transfer. MAP_I2CMasterControl(I2CA0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); } // Wait until the current byte has been transferred. while((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & I2C_INT_MASTER) == 0) { } return 0; }
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; }
void I2C_WriteMultiBegin(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_I2CMasterDataPut(I2CBase, data); MAP_I2CMasterControl(I2CBase, I2C_MASTER_CMD_BURST_SEND_CONT); I2C_WaitForBus(I2C_MASTER_CMD_BURST_SEND_ERROR_STOP); }
enum i2c_ack_type i2c_start(i2c_connection conn, uint16_t addr, enum i2c_rw mode) { struct i2c_state *c = (struct i2c_state *) conn; /* CC3200 does not support 10 bit addresses. */ if (addr > 0x7F) return I2C_ERR; MAP_I2CMasterSlaveAddrSet(c->base, addr, (mode == I2C_READ)); c->first = 1; if (mode == I2C_WRITE) { /* Start condition is set only with data to send. */ return I2C_ACK; } else { c->first = 1; return i2c_command(c, I2C_MASTER_CMD_BURST_RECEIVE_START); } }
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; }