Creating an i2c address scanner

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating an i2c address scanner

Jump to solution
2,942 Views
allgood38
Contributor I

Hello,

I'm looking to write some code that will scan the i2c bus for devices. From what I understand from existing i2cspy implementations, you:

  • send a start condition
  • send an address over the bus
  • check to see if an ack was received
  • send a stop condition
  • repeat

So I'm still learning the libraries provided by processor expert, and attempted to do this with the select slave method and receive block, so it would look like:

  LDD_TDeviceData iic_handle = CI2C1_Init( &flags );

  for( byte addr = 0x10; addr < 0x70; addr++ ) {

      printf("S: %02x ", addr);

      while( CI2C1_SelectSlaveDevice( iic_handle, LDD_I2C_ADDRTYPE_7BITS, addr ) != ERR_OK );

      CI2C1_MasterSendBlock( iic_handle, NULL, 0, LDD_I2C_SEND_STOP );

      while( flag == 0 );

      // ...

  }

Where the flags variable is set by an interrupt event, and would be checked later to see what kind of event it was. I also tried this using the non-LDD "Internal I2C" device, and found that it had a nice event for SCL Timeout, which doesn't appear to be present in the LDD version.


Has anyone tried doing something like this before? I'm going to see if I can access more fine grained control over the device by looking at the generated code and the Technical manual for the device family, but wanted to know if anyone else had tried this first.


Thanks

Accidentally posted half the message instead of the whole thing

Tags (4)
0 Kudos
1 Solution
2,027 Views
BlackNight
NXP Employee
NXP Employee

Hi Stephen,

Yes, that's what's happening.

I have attached you an example project (with the generated sources) for the FRDM-KL25Z. The function is in ScanDevices() which calls I2C2_ScanDevice().

The project is on GitHub too: mcuoneclipse/Examples/FRDM-KL25Z/Freedom_I2CSpy at master · ErichStyger/mcuoneclipse · GitHub

I hope this helps,

Erich

View solution in original post

0 Kudos
3 Replies
2,027 Views
BlackNight
NXP Employee
NXP Employee

I'm using that I2CSpy to scan the bus for 'unknown' devices, or to verify the I2C device address.

http://mcuoneclipse.com/2012/12/23/csi-crime-scene-investigation-with-i2cspy-and-freedom-board/

Erich

0 Kudos
2,027 Views
allgood38
Contributor I

Hi Erich,

Thank you for the reply, and that's a really great post!

So after a little searching through your repo, I found this snippet:

https://github.com/ErichStyger/McuOnEclipse_PEx/blob/master/Drivers/sw/GenericI2C.drv#L1086

Which seems to be responsible for the actual check on an address. So from what I can tell, it:

  • calls select slave on the address
  • sets up a timer
  • Lopps until either the timer to expires or the device to ACKs
  • Checks the error mask to see if there was no ACK
  • Manually sends a stop condition

Is this roughly what is happening? I haven't looked at the events file yet, its a little difficult to navigate the (*.drv) file with the PEx markup. Of course it is really cool that the PEx beans are able to make the code as flexible as it is.

0 Kudos
2,028 Views
BlackNight
NXP Employee
NXP Employee

Hi Stephen,

Yes, that's what's happening.

I have attached you an example project (with the generated sources) for the FRDM-KL25Z. The function is in ScanDevices() which calls I2C2_ScanDevice().

The project is on GitHub too: mcuoneclipse/Examples/FRDM-KL25Z/Freedom_I2CSpy at master · ErichStyger/mcuoneclipse · GitHub

I hope this helps,

Erich

0 Kudos