Recent

Author Topic: Large system, i2C to go with Freepascal on AVR, code help  (Read 4631 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 266
Re: Large system, i2C to go with Freepascal on AVR, code help
« Reply #15 on: October 02, 2023, 02:04:28 pm »
Code: Pascal  [Select][+][-]
  1. unit i2c;
  2.  
  3. interface
  4.  
  5. type
  6.   { TI2CMaster }
  7.   TI2CMaster = object
  8.     retVal: byte;
  9.     procedure init(bitRate: byte);
  10.     // Low level functions
  11.     // Address should be left adjusted 7-bit value
  12.     function start(address: byte; readTransaction: boolean): boolean;
  13.     function readByte(out data: byte; ack: boolean): boolean;
  14.     function writeByte(data: byte): boolean;
  15.     function stop: boolean;
  16.  
  17.     // Higher level implementation
  18.     function ReadByteFromReg(i2caddress, regAddress: byte; out data: byte): boolean; overload;
  19.     function ReadBytesFromReg(i2caddress, regAddress: byte; data: PByte; size: byte): boolean; overload;
  20.     function ReadBytes(i2caddress: byte; const data: PByte; size: byte): boolean; overload;
  21.  
  22.     function WriteByteToReg(i2caddress, regAddress: byte; const data: byte): boolean; overload;
  23.     function WriteBytesToReg(i2caddress, regAddress: byte; data: PByte; size: byte
  24.       ): boolean; overload;
  25.     function WriteBytes(address: byte; const data: PByte; size: byte): boolean;
  26.  
  27.     // data pointer should be pointer to flash memory
  28.     function WriteBytesToReg_P(i2caddress, regAddress: byte; data: PByte; size: byte
  29.       ): boolean;
  30.  
  31.     function clearStalledBus: boolean;
  32.   end;

Still working on my system, in the meanwhile i want to know, if there is a better solution using:

Code: Pascal  [Select][+][-]
  1. function readByte(out data: byte; ack: boolean): boolean;

Because it is a bi-directional bus.




ccrause

  • Hero Member
  • *****
  • Posts: 988
Re: Large system, i2C to go with Freepascal on AVR, code help
« Reply #16 on: October 02, 2023, 03:14:08 pm »
Still working on my system, in the meanwhile i want to know, if there is a better solution using:

Code: Pascal  [Select][+][-]
  1. function readByte(out data: byte; ack: boolean): boolean;

Because it is a bi-directional bus.

It isn't clear what you want to implement.  I2C has a bi-directional bus yes, but it is limited to half-duplex communication where a master must initiate a transaction with a slave (read or write).  So a master can a) write data to a slave -> the slave reads the data, or b) request (read) data from a slave -> the slave writes the data.

pascalbythree

  • Sr. Member
  • ****
  • Posts: 266
Re: Large system, i2C to go with Freepascal on AVR, code help
« Reply #17 on: October 02, 2023, 05:11:18 pm »
I more alike neem example code to send a byte from master to slave AND slave to master

So from i2c.pas back to i2cslave_unit.pas

through the same  init, like from TI2CMaster and backwards

without starting a new i2c_master.init(I2C_50kHz);

Is this possible somehow or do i always have to use 2 settings fore, fore and back ?

Reformulation: read and write not possible over the same stack ? Ju always need to start a new one? is this "half-duplex" ?
« Last Edit: October 02, 2023, 05:18:30 pm by pascalbythree »

pascalbythree

  • Sr. Member
  • ****
  • Posts: 266
Re: Large system, i2C to go with Freepascal on AVR, code help
« Reply #18 on: October 09, 2023, 04:15:44 pm »
Than you it got to work! Now to the etching!

 

TinyPortal © 2005-2018