Ejemplo n.º 1
0
unsigned char I2C_read_byte(unsigned char slave_address, unsigned char reg_address)
{
  unsigned char DATA;
  //DISABLE INTERRUPTS
  //ROM_IntDisable(I2C0_BASE);
  ROM_I2CMasterSlaveAddrSet(I2C0_BASE, slave_address, false);
  //BufferIn(reg_address);

  ROM_I2CMasterDataPut(I2C0_BASE, reg_address);
  ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);

  while(ROM_I2CMasterBusy(I2C0_BASE));
  ROM_I2CMasterSlaveAddrSet(I2C0_BASE, slave_address, true);

  ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

  while(ROM_I2CMasterBusy(I2C0_BASE))
  {

  }

  DATA = ROM_I2CMasterDataGet(I2C0_BASE);

  //ENABLE INTERRUPTS

  //ROM_IntEnable(I2C0_BASE);

  return DATA;

}
Ejemplo n.º 2
0
void I2C_Read_Packet_From_Sensor(int *readData,unsigned char slave_add,int dataLength,unsigned char offset)
{
  unsigned char j;
  //DISABLE INTERRUPTS
  //ROM_IntDisable(I2C0_BASE);
  ROM_I2CMasterSlaveAddrSet(I2C0_BASE, slave_add, false);
  //BufferIn(reg_address);
  // SysCtlDelay(53333);
  ROM_I2CMasterDataPut(I2C0_BASE, offset);
  ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
  //SysCtlDelay(53333);
  while(ROM_I2CMasterBusy(I2C0_BASE));
  ROM_I2CMasterSlaveAddrSet(I2C0_BASE, slave_add, true);
  ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
  while(ROM_I2CMasterBusy(I2C0_BASE));
  readData[0] = ROM_I2CMasterDataGet(I2C0_BASE);
  if(dataLength>1){
    for(j = 1 ; j < dataLength - 1 ; j++ )
    {
      ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
      while(ROM_I2CMasterBusy(I2C0_BASE));
      readData[j] = ROM_I2CMasterDataGet(I2C0_BASE);
    }
    ROM_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    while(ROM_I2CMasterBusy(I2C0_BASE));
    readData[dataLength-1] = ROM_I2CMasterDataGet(I2C0_BASE);
  }
  //ROM_IntEnable(I2C0_BASE);
  // SysCtlDelay(53333);
}
Ejemplo n.º 3
0
uint8_t TwoWire::sendTxData(unsigned long cmd, uint8_t data) {
    while(ROM_I2CMasterBusy(MASTER_BASE));
    ROM_I2CMasterDataPut(MASTER_BASE, data);

    HWREG(MASTER_BASE + I2C_O_MCS) = cmd;
    while(ROM_I2CMasterBusy(MASTER_BASE));
    uint8_t error = ROM_I2CMasterErr(MASTER_BASE);
    if (error != I2C_MASTER_ERR_NONE)
        ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
    return(getError(error));
}
Ejemplo n.º 4
0
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
    uint8_t error = I2C_MASTER_ERR_NONE;

    if(TX_BUFFER_EMPTY) return 0;
    //Wait for any previous transaction to complete
    while(ROM_I2CMasterBusBusy(MASTER_BASE));
    while(ROM_I2CMasterBusy(MASTER_BASE));

    //Select which slave we are requesting data from
    //false indicates we are writing to the slave
    ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false);

    while(ROM_I2CMasterBusy(MASTER_BASE));
    unsigned long cmd = RUN_BIT | START_BIT;

    error = sendTxData(cmd,txBuffer[txReadIndex]);
    txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
    if(error) return error;
    while(!TX_BUFFER_EMPTY) {
        error = sendTxData(RUN_BIT,txBuffer[txReadIndex]);
        txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
        if(error) return getError(error);
    }
    if(txWriteRomQuantity!=0)	// Hey we have Rom Block data to transmit!
    {
        while(txWriteRomIndex != txWriteRomQuantity)
        {
            uint8_t c = *PtrTxRomBuffer++;
            error = sendTxData(RUN_BIT,c);
            txWriteRomIndex++;
            if(error) return getError(error);
        }
        txWriteRomQuantity=0;	// Clear to disable further tx
    }

    if(sendStop) {
        while(ROM_I2CMasterBusy(MASTER_BASE));
        HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT;
        while(ROM_I2CMasterBusy(MASTER_BASE));
        currentState = IDLE;
    }
    else {
        currentState = MASTER_TX;
    }

    // indicate that we are done transmitting
    transmitting = 0;
    return error;

}
Ejemplo n.º 5
0
uint8_t TwoWire::getRxData(unsigned long cmd) {

    if (currentState == IDLE) while(ROM_I2CMasterBusBusy(MASTER_BASE));
    HWREG(MASTER_BASE + I2C_O_MCS) = cmd;
    while(ROM_I2CMasterBusy(MASTER_BASE));
    uint8_t error = ROM_I2CMasterErr(MASTER_BASE);
    if (error != I2C_MASTER_ERR_NONE) {
        ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
    }
    else {
        while(ROM_I2CMasterBusy(MASTER_BASE));
        rxBuffer[rxWriteIndex] = ROM_I2CMasterDataGet(MASTER_BASE);
        rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH;
    }
    return error;

}
Ejemplo n.º 6
0
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
  uint8_t error = I2C_MASTER_ERR_NONE;
  
  digitalWrite(RED_LED, HIGH);

  if(TX_BUFFER_EMPTY) return 0;
  //Wait for any previous transaction to complete
  while(ROM_I2CMasterBusBusy(MASTER_BASE)); // Here it hangs sometimes!
  digitalWrite(RED_LED, LOW);
  while(ROM_I2CMasterBusy(MASTER_BASE));

  //Select which slave we are requesting data from
  //false indicates we are writing to the slave
  ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false);

  while(ROM_I2CMasterBusy(MASTER_BASE));
  unsigned long cmd = RUN_BIT | START_BIT;

  error = sendTxData(cmd,txBuffer[txReadIndex]);
  txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
  if(error) return error;
  while(!TX_BUFFER_EMPTY) {
	  error = sendTxData(RUN_BIT,txBuffer[txReadIndex]);
  	  txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
	  if(error) return getError(error);
  }

  if(sendStop) {
	  while(ROM_I2CMasterBusy(MASTER_BASE));
	  HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT;
	  while(ROM_I2CMasterBusy(MASTER_BASE));
	  currentState = IDLE;
  }
  else {
	  currentState = MASTER_TX;
  }

  // indicate that we are done transmitting
  transmitting = 0;
  return error;

}
Ejemplo n.º 7
0
void I2C_write(unsigned char slave_address,unsigned char reg_address,unsigned char data)
{
  ROM_I2CMasterSlaveAddrSet(I2C0_BASE,slave_address,false);

  ROM_I2CMasterDataPut(I2C0_BASE,reg_address);
  ROM_I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_START);
  while(ROM_I2CMasterBusy(I2C0_BASE));

  //ROM_I2CMasterSlaveAddrSet(I2C0_BASE, slave_address, false);
  ROM_I2CMasterDataPut(I2C0_BASE,data);
  ROM_I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
}
Ejemplo n.º 8
0
uint8_t TwoWire::sendTxData(unsigned long cmd, uint8_t data) {
    if(speedMode==I2C_SPEED_STANDARD) //Fast mode works without delay (Tested on stellaris&GY-80)
        delayMicroseconds(SLOWMODE_DELAYUS);
    ROM_I2CMasterDataPut(MASTER_BASE, data);

    HWREG(MASTER_BASE + I2C_O_MCS) = cmd;
    while(ROM_I2CMasterBusy(MASTER_BASE));
    uint8_t error = ROM_I2CMasterErr(MASTER_BASE);
    if (error != I2C_MASTER_ERR_NONE)
        ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
    return(getError(error));
}
Ejemplo n.º 9
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
    uint8_t error = 0;
    uint8_t oldWriteIndex = rxWriteIndex;
    uint8_t spaceAvailable = (rxWriteIndex >= rxReadIndex) ?
                             BUFFER_LENGTH - (rxWriteIndex - rxReadIndex) : (rxReadIndex - rxWriteIndex);

    if (quantity > spaceAvailable)
        quantity = spaceAvailable;
    if (!quantity) return 0;

    //Select which slave we are requesting data from
    //true indicates we are reading from the slave
    ROM_I2CMasterSlaveAddrSet(MASTER_BASE, address, true);

    unsigned long cmd = 0;

    if((quantity > 1) || !sendStop)
        cmd = RUN_BIT | START_BIT | ACK_BIT;
    else cmd = RUN_BIT | START_BIT | (sendStop << 2);

    error = getRxData(cmd);
    if(error) return 0;

    currentState = MASTER_RX;

    for (int i = 1; i < quantity; i++) {
        //since NACK is being sent on last byte, a consecutive burst read will
        //need to send a start condition
        if(i == (quantity - 1))
            cmd = RUN_BIT;
        else
            cmd = RUN_BIT | ACK_BIT;

        error = getRxData(cmd);
        if(error) return i;
    }

    if(sendStop) {
        HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT;
        while(ROM_I2CMasterBusy(MASTER_BASE));
        currentState = IDLE;
    }

    uint8_t bytesWritten = (rxWriteIndex >= oldWriteIndex) ?
                           BUFFER_LENGTH - (rxWriteIndex - oldWriteIndex) : (oldWriteIndex - rxWriteIndex);

    return(bytesWritten);

}
Ejemplo n.º 10
0
uint8_t TwoWire::getRxData(unsigned long cmd) {

    if (currentState == IDLE) while(ROM_I2CMasterBusBusy(MASTER_BASE));
    HWREG(MASTER_BASE + I2C_O_MCS) = cmd;
    while(ROM_I2CMasterBusy(MASTER_BASE));
    uint8_t error = ROM_I2CMasterErr(MASTER_BASE);
    if (error != I2C_MASTER_ERR_NONE) {
        ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
    }
    else {
        if(speedMode==I2C_SPEED_STANDARD) //Fast modes works without delay (Tested on stellaris&GY-80)
            delayMicroseconds(SLOWMODE_DELAYUS);
        rxBuffer[rxWriteIndex] = ROM_I2CMasterDataGet(MASTER_BASE);
        rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH;
    }
    return error;

}
Ejemplo n.º 11
0
//*****************************************************************************
//
//! Reads the I2C slave register.
//!
//! \param ACCEL_I2C_MASTER_BASE is the base for the I2C module.
//! \param SlaveID is the 7-bit address of the slave to be addressed.
//! \param addr is the register to read from.
//!
//! This function initiates a read from the slave device.
//! The ACCEL_I2C_MASTER_BASE parameter is the I2C modules master base address.
//! \e ACCEL_I2C_MASTER_BASE parameter can be one of the following values:
//!
//! \return Register value in an unsigned long format.  Note that 0 will be
//! returned if there is ever an error, 1 if there was not.
//
//*****************************************************************************
uint8_t i2c_ReadByte(uint8_t SlaveID, uint8_t addr) {
	unsigned long ulRegValue = 0;

//      LED_TOGGLE(BLUE);

// Wait until master module is done transferring.
//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Tell the master module what address it will place on the bus when
	// writing to the slave.
	//
	ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, 0);

	//
	// Place the command to be sent in the data register.
	//
	ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, addr);

	//
	// Initiate send of data from the master.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 2;
	}

	//
	// Tell the master module what address it will place on the bus when
	// reading from the slave.
	//
	ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, 1);

	//
	// Tell the master to read data.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

	//
	// Wait until master module is done receiving.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 1;
	}

	//
	// Read the data from the master.
	//
	ulRegValue = ROM_I2CMasterDataGet(ACCEL_I2C_MASTER_BASE);

	//
	// Return the register value.
	//
	return 3;
//	return ulRegValue;
}
Ejemplo n.º 12
0
int32_t i2c_WriteBuf(uint8_t SlaveID, uint8_t addr, int32_t nBytes,
		uint8_t* pBuf) {
	uint8_t nBytesCount; // local variable used for byte counting/state determination
	uint16_t MasterOptionCommand; // used to assign the commands for ROM_I2CMasterControl() function

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Tell the master module what address it will place on the bus when
	// writing to the slave.
	//
	ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, false);

	//
	// Place the value to be sent in the data register.
	//
	ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, addr);

	//
	// Initiate send of data from the master.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE,
			I2C_MASTER_CMD_BURST_SEND_START);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 0;
	}

	//
	// Start with CONT for more than one byte to write
	//
	MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_CONT;

	for (nBytesCount = 0; nBytesCount < nBytes; nBytesCount++) {
		//
		// The second and intermittent byte has to be send with CONTINUE control word
		//
		if (nBytesCount == 1)
			MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_CONT;

		//
		// The last byte has to be send with FINISH control word
		//
		if (nBytesCount == nBytes - 1)
			MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_FINISH;

		//
		// Re-configure to SINGLE if there is only one byte to write
		//
		if (nBytes == 1)
			MasterOptionCommand = I2C_MASTER_CMD_SINGLE_SEND;

		//
		// Send data byte
		//
		ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, pBuf[nBytesCount]);

		//
		// Initiate send of data from the master.
		//
		ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, MasterOptionCommand);

		//
		// Wait until master module is done transferring.
		//
		while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
		};

		//
		// Check for errors.
		//
		if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
			return 0;
		}
	}

	//
	// Return 1 if there is no error.
	//
	return 1;
}
Ejemplo n.º 13
0
//*****************************************************************************
//
//! Writes to the specified I2C slave register.
//!
//! \param ACCEL_I2C_MASTER_BASE is the base for the I2C module.
//! \param SlaveID is the 7-bit address of the slave to be addressed.
//! \param addr is the register to write data to.
//! \param data is the 8-bit data to be written.
//!
//! This function initiates a read from the I2C slave device.
//! The ACCEL_I2C_MASTER_BASE parameter is the I2C modules master base address.
//! \e ACCEL_I2C_MASTER_BASE parameter can be one of the following values:
//!
//! \return Register value in an unsigned long format.  Note that 0 will be
//! returned if there is ever an error, 1 if there was not.
//
//int32_t  i2c_XmtByte(uint8_t SlaveID, uint8_t addr, uint8_t data);
//*****************************************************************************
int32_t i2c_WriteByte(uint8_t SlaveID, uint8_t addr, uint8_t data) {

//      LED_TOGGLE(GREEN);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Tell the master module what address it will place on the bus when
	// writing to the slave.
	//
	ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, 0);

	//
	// Place the command to be sent in the data register.
	//
	ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, addr);

	//
	// Initiate send of data from the master.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE,
			I2C_MASTER_CMD_BURST_SEND_START);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 0;
	}

	//
	// Place the value to be sent in the data register.
	//
	ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, data);

	//
	// Initiate send of data from the master.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 0;
	}

	//
	// Initiate send of data from the master.
	//
	ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE,
			I2C_MASTER_CMD_BURST_SEND_FINISH);

	//
	// Wait until master module is done transferring.
	//
	while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) {
	};

	//
	// Check for errors.
	//
	if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) {
		return 0;
	}

	//
	// Return 1 if there is no error.
	//
	return 1;
}
Ejemplo n.º 14
0
//*****************************************************************************
//
//! Reads one/multiple bytes of data from an I2C slave device.
//!
//! \param ulI2CBase is the base for the I2C module.
//! \param ucSlaveAdress is the 7-bit address of the slave to be addressed.
//! \param ucReg is the register to start reading from.
//! \param cReadData is a pointer to the array to store the data.
//! \param uiSize is the number of bytes to read from the slave.
//!
//! This function reads one/multiple bytes of data from an I2C slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return 0 if there was an error or 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CReadData(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
        unsigned char ucReg, char* cReadData, unsigned int uiSize)
{
    unsigned int uibytecount;       // local variable used for byte counting/state determination
    int MasterOptionCommand;        // used to assign the commands for ROM_I2CMasterControl() function

    //
    // Check the arguments.
    //
    ASSERT(I2CMasterBaseValid(ulI2CBase));

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Tell the master module what address it will place on the bus when
    // writing to the slave.
    //
    ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);

    //
    // Place the command to be sent in the data register.
    //
    ROM_I2CMasterDataPut(ulI2CBase, ucReg);

    //
    // Initiate send of data from the master.
    //
    ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND);

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Check for errors.
    //
    if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
    {
        return 0;
    }


    //
    // Tell the master module what address it will place on the bus when
    // reading from the slave.
    //
    ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, true);

    //
    // Start with BURST with more than one byte to write
    //
    MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_START;


    for(uibytecount = 0; uibytecount < uiSize; uibytecount++)
    {
        //
        // The second and intermittent byte has to be read with CONTINUE control word
        //
        if(uibytecount == 1)
            MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_CONT;

        //
        // The last byte has to be send with FINISH control word
        //
        if(uibytecount == uiSize - 1)
            MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;

        //
        // Re-configure to SINGLE if there is only one byte to read
        //
        if(uiSize == 1)
            MasterOptionCommand = I2C_MASTER_CMD_SINGLE_RECEIVE;

        //
        // Initiate read of data from the slave.
        //
        ROM_I2CMasterControl(ulI2CBase, MasterOptionCommand);

        //
        // Wait until master module is done reading.
        //
        while(ROM_I2CMasterBusy(ulI2CBase))
        {
        };

        //
        // Check for errors.
        //
        if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
        {
            return 0;
        }

        //
        // Move byte from register
        //
        cReadData[uibytecount] = I2CMasterDataGet(ulI2CBase);
    }

    // send number of received bytes
    return uibytecount;
}
Ejemplo n.º 15
0
unsigned long
I2CRegWrite(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
         unsigned char ucReg, unsigned char ucValue)
{
    //
    // Check the arguments.
    //
    ASSERT(I2CMasterBaseValid(ulI2CBase));

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Tell the master module what address it will place on the bus when
    // writing to the slave.
    //
    ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);

    //
    // Place the command to be sent in the data register.
    //
    ROM_I2CMasterDataPut(ulI2CBase, ucReg);

    //
    // Initiate send of data from the master.
    //
    ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Check for errors.
    //
    if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
    {
        return 0;
    }

    //
    // Place the value to be sent in the data register.
    //
    ROM_I2CMasterDataPut(ulI2CBase, ucValue);

    //
    // Initiate send of data from the master.
    //
    ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_CONT);

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Check for errors.
    //
    if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
    {
        return 0;
    }

    //
    // Initiate send of data from the master.
    //
    ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_FINISH);

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(ulI2CBase))
    {
    };

    //
    // Check for errors.
    //
    if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
    {
        return 0;
    }

    //
    // Return 1 if there is no error.
    //
    return 1;
}
//*****************************************************************************
//
//! Reads one/multiple bytes of data from an I2C slave device.
//!
//! \param I2C_PORT is the base for the I2C module.
//! \param SlaveID is the 7-bit address of the slave to be addressed.
//! \param addr is the register to start reading from.
//! \param pBuf is a pointer to the array to store the data.
//! \param nBytes is the number of bytes to read from the slave.
//!
//! This function reads one/multiple bytes of data from an I2C slave device.
//! The I2C_PORT parameter is the I2C modules master base address.
//! \e I2C_PORT parameter can be one of the following values:
//!
//!
//! \return 0 if there was an error or 1 if there was not.
//
//int32_t  i2c_RcvBuf(uint8_t SlaveID, uint8_t addr, int32_t nBytes, uint8_t* pBuf);
//*****************************************************************************
int32_t i2c_RcvBuf(uint8_t SlaveID, uint8_t addr, int32_t nBytes , uint8_t* pBuf )
{
        uint8_t nBytesCount;            // local variable used for byte counting/state determination
        uint16_t MasterOptionCommand;           // used to assign the commands for ROM_I2CMasterControl() function

        //
        // Wait until master module is done transferring.
        //
        while(ROM_I2CMasterBusy(I2C_PORT))
        {
        };

    //
    // Tell the master module what address it will place on the bus when
    // writing to the slave.
    //
    ROM_I2CMasterSlaveAddrSet(I2C_PORT, SlaveID, 0);

    //
    // Place the command to be sent in the data register.
    //
    ROM_I2CMasterDataPut(I2C_PORT, addr);

    //
    // Initiate send of data from the master.
    //
    ROM_I2CMasterControl(I2C_PORT, I2C_MASTER_CMD_SINGLE_SEND);

    //
    // Wait until master module is done transferring.
    //
    while(ROM_I2CMasterBusy(I2C_PORT))
    {
    };

    //
    // Check for errors.
    //
    if(ROM_I2CMasterErr(I2C_PORT) != I2C_MASTER_ERR_NONE)
    {
        return 0;
    }


        //
        // Tell the master module what address it will place on the bus when
        // reading from the slave.
        //
        ROM_I2CMasterSlaveAddrSet(I2C_PORT, SlaveID, true);

        //
        // Start with BURST with more than one byte to write
        //
        MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_START;


        for(nBytesCount = 0; nBytesCount < nBytes; nBytesCount++)
        {
                //
                // The second and intermittent byte has to be read with CONTINUE control word
                //
                if(nBytesCount == 1)
                        MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_CONT;

                //
                // The last byte has to be send with FINISH control word
                //
                if(nBytesCount == nBytes - 1)
                        MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;

                //
                // Re-configure to SINGLE if there is only one byte to read
                //
                if(nBytes == 1)
                        MasterOptionCommand = I2C_MASTER_CMD_SINGLE_RECEIVE;

                //
                // Initiate read of data from the slave.
                //
                ROM_I2CMasterControl(I2C_PORT, MasterOptionCommand);

                //
                // Wait until master module is done reading.
                //
                while(ROM_I2CMasterBusy(I2C_PORT))
                {
                };

                //
        // Check for errors.
        //
        if(ROM_I2CMasterErr(I2C_PORT) != I2C_MASTER_ERR_NONE)
        {
            return 0;
        }

        //
        // Move byte from register
        //
                pBuf[nBytesCount] = I2CMasterDataGet(I2C_PORT);
        }

        // send number of received bytes
        return nBytesCount;
}