[fpc-pascal] FPC/Lazarus on RPi4 - How to read/write I2C connected EEPROM?

Bo Berglund bo.berglund at gmail.com
Tue Apr 4 18:52:31 CEST 2023

On Sun, 02 Apr 2023 23:21:38 +0200, Bo Berglund via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:

>On Sun, 2 Apr 2023 23:14:25 +0200, Christo Crause via fpc-pascal
><fpc-pascal at lists.freepascal.org> wrote:
>>On Sun, Apr 2, 2023 at 9:30?PM Bo Berglund via fpc-pascal <
>>fpc-pascal at lists.freepascal.org> wrote:
>>> Is there a reason that there is no i2c_write_reg demo in the github
>>> package?
>>I haven't gotten round to that, waiting for hardware to test the code with.
>>I will try to write one so I can test that as well...
>>Would be nice if you could share that :-)
>Sure, I will probably continue with this tomorrow (I'm in Europe so it is soon
>midnight here...).
>If I get it going I will post it or else I will ask about it here.
>My hardware is ready and operational, in fact my board also has another EEPROM
>(same type) sitting on the i2c-0 bus prepared for being used as a HAT EEPROM.
>Haven't gotten that far on the HAT handling though so it is totally unused.

So now I have created a new class TI2c_eeprom, that uses the i2c.pas file to do
the actual i2c handling.
In this class I have tested the read and write functionality and I have the
following findings:

1) The read works just fine for what I have done so far.
2) But I have problems with the write functions for multiple data

I use the method:
i2cMaster.WriteBytesToReg(i2caddress: byte; regAddress: uint16; data: PByte;
size: byte): boolean;)

With data specified as follows in the call:

if not i2cMaster.WriteBytesToReg(FEEPROMAddr, startaddress, @source[0],
NumBytes) then

Here the source argument is a TBytes array of bytes so I supply the address of
element 0 as the data argument (expected to be a PByte).

When I step through this in the debugger and get into the method itself I can
see that the source argument contains the data I have loaded into it, so the
data is there.

When I run that method to the end and it finishes without error, I still cannot
see the data in the EEPROM at the given address.

So for testing I changed the call to be:
if not i2cMaster.WriteByteToReg(FEEPROMAddr, startaddress, source[0]) then

This uses the method to write a single byte to the i2c device and it works just

So either there is a problem with the WriteBytesToReg() method or else my use of
What comes to mind is the data type PByte, which I have never used before and do
not know if it is valid.

I am keeping the data in a dynamic array of bytes (the pascal type TBytes) and
send the address of element 0 of that array as the argument to the method.
Is this wrong?
If so what should I use instead?
Can I typecast it like this:

if not i2cMaster.WriteBytesToReg(FEEPROMAddr, startaddress, PByte(source),
NumBytes) then
(Tried it and it does not work)


Seems like I am getting close but missing something important...

Bo Berglund
Developer in Sweden

More information about the fpc-pascal mailing list