Hi,
I'm trying to implement an I2C address scanner on the Stellaris LM4F120H5QR but the code below deliveres wrong data.
Here is what I would expect to see (probing the I2C bus with my Bus Pirate V3):
but this is what I get from my Stellaris program:
The addresses found by my code were starting from 0xC0 (=192) and were ending with 0XD9 (=217).
What I want the program to do is placing a slaves address to the bus and see if the device acknowledges it. I'm using repeated start and write some dummy data (0x00) on the bus at the moment, but I doubt that this is the way to do it.
So, thank you for your support/advice!
Rgds
aBUGSworstnightmare
//*****************************************************************************
//
//! Probes the selected I2C bus for available slave devices
//!
//! \param ulI2CBase is the base for the I2C module.
//!
//! This function scans the selected I2C bus for available 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
I2CBusScan(unsigned long ulI2CBase)
{
unsigned char ucProbeAdress;
unsigned long ucerrorstate;
//
// Check the arguments.
//
//ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
for (ucProbeAdress = 0; ucProbeAdress < 255; ucProbeAdress++)
{
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, (ucProbeAdress>>1), false);
ROM_SysCtlDelay(50000);
//
// Place the command to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, 0x00);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);
ROM_SysCtlDelay(500000);
ucerrorstate = ROM_I2CMasterErr(ulI2CBase);
//
// Check for errors.
//
if(ucerrorstate & I2C_MASTER_ERR_ADDR_ACK)
{
//
// device at selected address did not respond
//UARTprintf("Address not found: %d\n",ucProbeAdress);
//ROM_SysCtlDelay(1500000);
}
else
{
UARTprintf("Address found: %d\n",ucProbeAdress);
ROM_SysCtlDelay(1500000);
}
}
//
// End transfer of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
UARTprintf("I2C Bus-Scan done...\n");
//
// Return 1 if there is no error.
//
return 1;
}