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

Bo Berglund bo.berglund at gmail.com
Wed Apr 5 00:33:50 CEST 2023


On Tue, 4 Apr 2023 23:43:21 +0200, Christo Crause via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:


>> 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).
>>
>
>This is the correct way of passing the starting address of your data
>buffer.  Is your startaddress variable an unsigned 16 bit integer
>(word/uint16)? If not, type cast this to a uint16 to ensure the compiler
>calls the correct overloaded method.

OK, I have tested various similar ways but all fail to work.

>
>
>Note that I haven't tested WriteByteToReg with a device supporting 10 bit
>register addresses, or in your case 16 bit...

Actually it is more like 14 bits (16 kbytes)

>
>Passing the address of the first element of a TBytes array as you did above
>is correct.

OK

>
>I will look at the EEPROM datasheet, in case the details do not agree with
>the WriteBytesToReg implementation.

I have two cases here, writing a single byte, which works and writing an array
of byte which does not work...
Everything else seems to work fine.


So I have been fiddling with this the whole evening and I am not getting it to
work the supposed way...

So I changed as a test to use the following code instead:
Source is a TBytes container, startaddress and addr are uint16

    j := 0;
    for i := 0 to Length(Source)-1 do
    begin
      addr := startaddress+i;
      if not i2cMaster.WriteByteToReg(FEEPROMAddr, addr, source[i]) then break;
      Inc(j);
      Delay(10);//Delay(1) does not work...
    end;
    Writeln('Wrote ' + IntToStr(j) + 'bytes');

With this in place it works to write a TBytes data to the EEPROM but only 100
bytes/second.

But this does not work (nothing winds up into the EEPROM):

pSource := @source[0];
NumBytes := Length(source);
i2cMaster.WriteBytesToReg(FEEPROMAddr, startaddress, pSource, NumBytes)

This does not generate any errors but nothing winds up into the EEPROM...

Note: I have not changed anything inside the i2c.pas file...


-- 
Bo Berglund
Developer in Sweden



More information about the fpc-pascal mailing list