[fpc-pascal] FPC/Lazarus on RPi4 - How to read/write I2C connected EEPROM?
christo.crause at gmail.com
Tue Apr 4 23:43:21 CEST 2023
See comments below.
On Tue, Apr 4, 2023 at 6:52 PM Bo Berglund via fpc-pascal <
fpc-pascal at lists.freepascal.org> wrote:
> I use the method:
> i2cMaster.WriteBytesToReg(i2caddress: byte; regAddress: uint16; data:
> size: byte): boolean;)
> With data specified as follows in the call:
> if not i2cMaster.WriteBytesToReg(FEEPROMAddr, startaddress, @source,
> 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.
> When I step through this in the debugger and get into the method itself I
> see that the source argument contains the data I have loaded into it, so
> data is there.
> When I run that method to the end and it finishes without error, I still
> 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) then
> This uses the method to write a single byte to the i2c device and it works
> So either there is a problem with the WriteBytesToReg() method or else my
> use of
Note that I haven't tested WriteByteToReg with a device supporting 10 bit
register addresses, or in your case 16 bit...
> What comes to mind is the data type PByte, which I have never used before
> and do
> not know if it is valid.
PByte is a pointer to a byte.
> I am keeping the data in a dynamic array of bytes (the pascal type TBytes)
> send the address of element 0 of that array as the argument to the method.
> Is this wrong?
Passing the address of the first element of a TBytes array as you did above
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)
No, this does not take the address of source, which is waht is required.
> Seems like I am getting close but missing something important...
I will look at the EEPROM datasheet, in case the details do not agree with
the WriteBytesToReg implementation.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal