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

Christo Crause christo.crause at gmail.com
Wed Apr 5 11:40:09 CEST 2023


On Wed, Apr 5, 2023 at 7:55 AM Bo Berglund via fpc-pascal <
fpc-pascal at lists.freepascal.org> wrote:

> On Wed, 05 Apr 2023 00:33:50 +0200, Bo Berglund via fpc-pascal
>
> I *can* write a sequence of bytes into the EEPROM succsessfully using an
> i2c-tools command like this:
>
> i2ctransfer -y 1 w18 at 0x50 0x00 0x38 0x00 0x01 0x02 0x03 0x04 0x05 0x06
> 0x07 0x08
> 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
>
> This writes data starting towards the end of the page and wraps around
> such that
> part of the data gets to the start of the page.
>

Looking at the i2ctransmit transaction data:
$ i2ctransfer 1 w18 at 0x50 0x00 0x38 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will send the following messages to device file /dev/i2c-1:
msg 0: addr 0x50, write, len 18, buf 0x00 0x38 0x00 0x01 0x02 0x03 0x04
0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

This can be simulated by calling the WriteBytes method, see new example:
https://github.com/ccrause/rpiplc_pas/blob/main/native/examples/i2c_write_test.lpr

It shows:
> - It is possible to write the data in one go
> - If the data length is past the end of the page it wraps around to the
> beginning
>
> So if the problem cannot be solved then I could either use the loop with
> delay
> or call the i2c-tools command i2ctransfer as a TProcess execution to write
> multiple bytes in one command.
>
> But I would rather not use the TProcess method at all since it relies on an
> external tool being present and the argument list can be pretty long and
> tedious
> to build...
>

Looking at the datasheet for CAT24C128, Fig. 6 Byte Write Sequence, shows
no repeated start condition between register address and data bytes. I
suspect that the second i2c message should contain the I2C_M_NOSTART flag,
else the address and data parts of the transaction will be separated by a
repeated start signal. I have modified the WriteBytesToReg methods in
i2c.pas, please feel free to pull from Github and test this again.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230405/b0b07036/attachment.htm>


More information about the fpc-pascal mailing list