Test of I2C Demo with "I2C3" on Apalis T30 on Ixora Board

Hello,

i encountered some problems with the I2C Library.

I have checked it with your “I2CDemo” C# i changed 2 lines:

private Byte i2cEepromAddress = 0x57;  ///< EEPROM Device address      
i2cHandle = i2c.I2c_Init("I2C3");              ///< get handle to I2C device

I connected a scope to the I2C3 and noticed, that the i2c.I2c_Read(i2cHandle, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght); function does not do anything on the bus.

In contrast to this i2c.I2c_Write(i2cHandle, (IntPtr)for_Casting_Intptr_to_Byte, eepromAddressWriteOffset); works and i can get a reading on the scope and the EEPROM also responds.

I also noticed the existing Topic: I2C3 bus on T30 module - Toradex Community
But unfortunately i can’t get it to work. Maybe something more has changed in the last years…

Have a nice Weekend,
Best regards, Stefan

Dear @Stefan.P,

Could you verify bufferLenght must be greater than 0? If you pass 0 then library would not reach to Tegra I2c read API.

returnValue = i2c.I2c_Read(i2cHandle, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght);               ///< Read data written above from EEPROM

We have native code I2c demo(…\libDemos\I2c_Demo) application insdie Toradex CE libraries, Could you test the same on there also.

Thank you. I missed that you need to write first in that demo …
But unfortunately we still have problems using this function in VB.net. It reads but the Bytes can’t be transfered back, they are always 0.

Public Declare Function TegI2c_Read Lib "TdxAllLibrariesDll.dll" (ByVal hPort As IntPtr, ByVal pbuf As IntPtr, ByVal numberOfFrames As UInt32) As UInt32

Function ReadI2C(ByVal hPort As IntPtr, ByRef pBuffer As UInt32, ByVal bytesToRead As Byte, ByVal numberOfBytesRead As UInt32) As UInteger
        'making a intpointer and allocating the memory the size of pBuffer

        Dim localBuffer As New UInt32
        Dim bytePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf((localBuffer)))

        'https://social.msdn.microsoft.com/Forums/en-US/bf3e300b-f35b-4940-8bb9-c8c3a8caee5f/how-to-access-a-dll-function-having-a-void-pointer-argument?forum=vblanguage
        'converting the Structure to the pointer
        Marshal.StructureToPtr(localBuffer, bytePtr, False)

        'calling the Read function
        ReadI2C = i2c_teg.TegI2c_Read(hPort, bytePtr, numberOfBytesRead)
        'freeing the memory

        'Dim respBuffer() As Byte
        'localBuffer = Marshal.PtrToStructure(bytePtr, localBuffer.GetType())
        Marshal.PtrToStructure(bytePtr, localBuffer)

        Try
            pBuffer = localBuffer And &HFF
            Marshal.FreeHGlobal(bytePtr)
        Catch ex As Exception

        End Try

        Return ReadI2C
    End Function

The code will not work correctly, but in Debug Mode there were never Data in localbuffer.

Best regards,
Stefan

Dear @Stefan.P,

Thank you for your reply.

Sorry for asking more questions, I would like to understand more about the issue.

  1. Did you test Dotnet I2cDemo(…\libDemos\dotNet\I2CDemo) application? Is it working for Read and Write?
  2. Could you share VB.Net Demo code with us? It will help us to reproduce the issue.
  3. There are other .NET demo applications on the Toradex CE libraries, Would you refer those projects for code reference like how to use Marshal.StructureToPtr then read or write APIs?, one such example demo code : …\libDemos\dotNet\CanDemo\Demo.cs

If native demo is working properly then more over problem would be there on marshaling data on the managed code, Could you please look over there.