-->
Page 2 of 2

Re: i2c device discovery

PostPosted: Sat Feb 07, 2015 10:06 pm
by vayu_esp
sancho wrote:This small script scans i2c bus and returns address of any device responding.
Code: Select allid=0
sda=8
scl=9

-- initialize i2c, set pin1 as sda, set pin0 as scl
i2c.setup(id,sda,scl,i2c.SLOW)

for i=0,127 do
  i2c.start(id)
  resCode = i2c.address(id, i, i2c.TRANSMITTER)
  i2c.stop(id)
  if resCode == true then print("We have a device on address 0x" .. string.format("%02x", i) .. " (" .. i ..")") end
end


Enjoy ;)



when i run the script to detect i2c device, rescode always gets 1, whether or not device is connected.
I am using latest firmware.
and return 1 in below function seems problem?


static int i2c_address( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
int address = luaL_checkinteger( L, 2 );
int direction = luaL_checkinteger( L, 3 );

MOD_CHECK_ID( i2c, id );
if ( address < 0 || address > 127 )
return luaL_error( L, "wrong arg range" );
lua_pushboolean( L, platform_i2c_send_address( id, (u16)address, direction ) );
return 1;
}

Re: i2c device discovery

PostPosted: Mon Feb 09, 2015 2:55 pm
by Fr4gg0r
There was another thread on this topic somewhere..
Anyway, I had the same problem, when the pin in question was pulled low.
In my case GPIO0 after flashing.

The "1" indicates how many arguments shall be returned in lua, it does not represent the actual return value.
The result of "platform_i2c_send_address( id, (u16)address, direction )" is returned in lua.