I still try to read/write data with a register address with the new pre-release library. So I took a new Ixora carrier board V1.0A which has an EEPROM (latest doesnt’) and took this example and modified it a little bit.
- Library: latest prelease provided in this thread
- OS: WinCE 2013 V2.0
- Module: Apalis T30 1GB IT V1.1A
I can read the RTC but I can’t write and read the EEPROM. Return value gives me the right number of bytes for read and write operation, but its wrong. To verify if any data are written I used the example with the old library. With the old example I’m able to read and write (so no hardware fault) successful.
Test code #1:
private void Test1()
{
IntPtr i2CHandler;
//get handle to I2C device
i2CHandler = HwInterface.i2c.I2c_Init("I2C1");
if (i2CHandler == IntPtr.Zero)
{
MessageBox.Show("Can't initialize I2C");
return;
}
//open I2c for communication
if (!HwInterface.i2c.I2c_Open(i2CHandler))
{
MessageBox.Show("An error occurred while opening the I2C bus.");
return;
}
uint returnValue;
uint bufferLenght = 0;
Byte i2cEepromAddress = 80;
Byte[] i2cEepromDataAddress = new Byte[2] { 00, 00 };
const int eepromAddressWriteOffset = 2;
string text = "Hello";
//Get lenght of text entered into textbox
bufferLenght = (uint)text.Length;
Byte[] i2cEepromWrite = new Byte[bufferLenght + eepromAddressWriteOffset + 1]; //Make buffer for writing to eeprom chip
Byte[] temp = new Byte[bufferLenght]; //Temporary buffer to hold data from textbox
temp = Encoding.ASCII.GetBytes(text); //Convert string to byte array
Buffer.BlockCopy(i2cEepromDataAddress, 0, i2cEepromWrite, 0, eepromAddressWriteOffset);///< Copy eeprom starting address 0x00 in starting of buffer
Buffer.BlockCopy(temp, 0, i2cEepromWrite, eepromAddressWriteOffset, (int)bufferLenght);///< Concatenate data from textbox to buffer
try
{
HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "SlaveAddr", i2cEepromAddress, HwInterface.TdxCommon.ParamStorageType.StoreVolatile);
unsafe
{
fixed (byte* for_Casting_Intptr_to_Byte = i2cEepromWrite)
{
returnValue = HwInterface.i2c.I2c_Write(i2CHandler, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght + eepromAddressWriteOffset); ///< write buffer to EEPROM starting address 0X00
if (returnValue == 0)
throw new Exception(); ///< If Write operation returns 0
}
}
}
catch
{
MessageBox.Show("Write failed");
return;
}
//now read the written data from the EEPROM
Byte[] i2cEepromRead = new Byte[bufferLenght];
try
{
HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "SlaveAddr", i2cEepromAddress, HwInterface.TdxCommon.ParamStorageType.StoreVolatile);
unsafe
{
fixed (byte* for_Casting_Intptr_to_Byte = i2cEepromDataAddress)
{
returnValue = HwInterface.i2c.I2c_Write(i2CHandler, (IntPtr)for_Casting_Intptr_to_Byte, eepromAddressWriteOffset); ///< Write starting address
if (returnValue == 0)
throw new Exception(); ///< If Write operation returns 0
}
System.Threading.Thread.Sleep(10);
fixed (byte* for_Casting_Intptr_to_Byte = i2cEepromRead)
{
returnValue = HwInterface.i2c.I2c_Read(i2CHandler, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght);///< Read data written above from EEPROM
if (returnValue == 0)
throw new Exception(); //< If Read operation returns 0
MessageBox.Show(System.Text.Encoding.ASCII.GetString(i2cEepromRead, 0, i2cEepromRead.Length));
}
}
}
catch
{
MessageBox.Show("Read failed");
}
if (i2CHandler != IntPtr.Zero)
{
HwInterface.i2c.I2c_Close(i2CHandler); ///< Close I2C library
HwInterface.i2c.I2c_Deinit(i2CHandler); ///< Deinit I2C Handle
}
}
Now if I read the help there are parameters “RegisterAddr” and “RegisterAddrSize”. Should I not set these parameters? Tried this with a second code but it doesn’t work either. At read operation it’s blocked, write doesn’t write (but tells me correct number of bytes written).
Test code 2:
private void Test2()
{
IntPtr i2CHandler;
//get handle to I2C device
i2CHandler = HwInterface.i2c.I2c_Init("I2C1");
if (i2CHandler == IntPtr.Zero)
{
MessageBox.Show("Can't initialize I2C");
return;
}
//open I2c for communication
if (!HwInterface.i2c.I2c_Open(i2CHandler))
{
MessageBox.Show("An error occurred while opening the I2C bus.");
return;
}
string text = "World";
Byte i2cEepromAddress = 80;
uint returnValue = 0;
bool success;
uint numberOfBytes = (uint)text.Length;
uint address = 0;
address = BitConverter.ToUInt16(new Byte[2] { 00, 00 }, 0);
success = HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "RegisterAddrSize", 16, HwInterface.TdxCommon.ParamStorageType.StoreVolatile); // 16 bit register address.
success = HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "RegisterAddr", address, HwInterface.TdxCommon.ParamStorageType.StoreVolatile); //address register to write
//write EEPROM
Byte[] writeBytes = new Byte[numberOfBytes]; //Temporary buffer to hold data from textbox
writeBytes = Encoding.ASCII.GetBytes(text);
try
{
HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "SlaveAddr", i2cEepromAddress, HwInterface.TdxCommon.ParamStorageType.StoreVolatile);
unsafe
{
fixed (byte* for_Casting_Intptr_to_Byte = writeBytes)
{
//returnValue is number of written bytes
returnValue = HwInterface.i2c.I2c_Write(i2CHandler, (IntPtr)for_Casting_Intptr_to_Byte, (uint)writeBytes.Length);
if (returnValue == -1)
{
MessageBox.Show("Write failed");
return;
}
}
}
}
catch (Exception exp)
{
MessageBox.Show("Write failed" + exp.Message);
return;
}
//now read written date from EEPROM
Byte[] i2cDataRead = new Byte[numberOfBytes];
byte[] readBytes = new Byte[numberOfBytes];
try
{
HwInterface.i2c.I2c_SetConfigInt(i2CHandler, "SlaveAddr", i2cEepromAddress, HwInterface.TdxCommon.ParamStorageType.StoreVolatile);
unsafe
{
fixed (byte* for_Casting_Intptr_to_Byte = i2cDataRead)
{
returnValue = HwInterface.i2c.I2c_Read(i2CHandler, (IntPtr)for_Casting_Intptr_to_Byte, numberOfBytes);
if (returnValue == -1)
{
MessageBox.Show("Read failed");
success = false;
}
}
}
readBytes = i2cDataRead;
}
catch (Exception exp)
{
MessageBox.Show("Read failed" + exp.Message);
}
if (i2CHandler != IntPtr.Zero)
{
HwInterface.i2c.I2c_Close(i2CHandler); ///< Close I2C library
HwInterface.i2c.I2c_Deinit(i2CHandler); ///< Deinit I2C Handle
}
}
So I’m confused what’s correct way to use the new library and I still think there is an issue in the new library.