Recent

Author Topic: Platform eXtended Library (PXL) to AVR, howto I2C  (Read 1817 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Platform eXtended Library (PXL) to AVR, howto I2C
« on: October 17, 2023, 02:26:39 pm »
Afther your help at this forum, i want to I2C from my Raspberry to my AVR. Using Asphyre PXL. Can anybody help me out how to use this function ? Need to convert a buffer pointer and buffersize to a Incomming String variable. Because i can not work on pointers myself.

PXL.Boards.RPi.pas
Code: Pascal  [Select][+][-]
  1. function TFastSPI.Read(const Buffer: Pointer; const BufferSize: Cardinal): Cardinal;
  2. begin
  3.   Result := Transfer(Buffer, nil, BufferSize);
  4. end;
  5.  

Own code:
Code: Pascal  [Select][+][-]
  1. constructor TApplication.Create;
  2. begin
  3.   FSystemCore := TFastSystemCore.Create;
  4.   FGPIO := TFastGPIO.Create(FSystemCore);
  5.   FDataPort := TFastI2C.Create(FGPIO);
  6.  
  7.   // I2C Write and slave commands
  8.  
  9. end;

Please help... Greets, Wouter


Dzandaa

  • Sr. Member
  • ****
  • Posts: 303
  • From C# to Lazarus
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #1 on: October 17, 2023, 03:20:39 pm »
Hi,

I don't know if it is you are waiting for (or even if this is working):

Code: Pascal  [Select][+][-]
  1.  Str := '';
  2.  SetString(Str, PChar( Buffer), BufferSize);  
  3.  

B->
« Last Edit: October 17, 2023, 04:13:14 pm by Dzandaa »
Dzandaa

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #2 on: October 17, 2023, 05:22:06 pm »
I forgot about Github: rpiplc_pas/native/examples/i2c_write_test.lpr

Code: Pascal  [Select][+][-]
  1. program i2c_write_test;
  2.  
  3. uses
  4.   sysutils, i2c;
  5.  
  6. var
  7.   i2cMaster: TI2cMaster;
  8.   startAddr: uint16 = $38;
  9.   data: TBytes;
  10.   i: integer;
  11.   S:String;
  12.  
  13. begin
  14.   S := 'Hello World!                   ';
  15.  
  16.   i2cMaster := TI2cMaster.Create;
  17.  
  18.   if not i2cMaster.Initialize(i2c_1) then
  19.   begin
  20.     WriteLn('Error opening i2c device i2c_1');
  21.     i2cMaster.Free;
  22.     Halt(1);
  23.   end;
  24.    
  25.   if i2cMaster.WriteBytes(byte(startAddr), @data[0], Length(data)) then
  26.   begin    
  27.     WriteLn('Success: WriteBytes');
  28.   end
  29.   else
  30.     WriteLn('Error: WriteBytes');
  31.  
  32.   i2cMaster.Free;
  33. end.

Can somebody help me out changing this code to write the S string through $38 ? Or loop and write a byte ?

Code: Pascal  [Select][+][-]
  1. i2cMaster.WriteBytes(byte(startAddr), @data[0], Length(data))

in the Unit i2c.pas

Code: Pascal  [Select][+][-]
  1. function WriteBytes(address: byte; const data: PByte; size: byte): boolean;



« Last Edit: October 17, 2023, 05:23:52 pm by pascalbythree »

cdbc

  • Hero Member
  • *****
  • Posts: 1224
    • http://www.cdbc.dk
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #3 on: October 17, 2023, 05:29:54 pm »
Hi
I think you should be able to do this:
Code: Pascal  [Select][+][-]
  1. if i2cMaster.WriteBytes(byte(startAddr), @S[1], Length(S)) then
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #4 on: October 17, 2023, 05:52:27 pm »
Yay ! Again in a lucky day!

From PXL on my RPI to FPC on my AVR works fine!


Now i am going to try from AVR to FPC

You all will hear back from me soon!

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #5 on: January 25, 2024, 06:17:44 pm »
I'm Back!

Code: Pascal  [Select][+][-]
  1. Read(const Buffer: Pointer; const BufferSize: Cardinal): Cardinal;

Anybody got a snip to receive values on the RPI from the AVR as Master ?

cdbc

  • Hero Member
  • *****
  • Posts: 1224
    • http://www.cdbc.dk
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #6 on: January 25, 2024, 06:48:22 pm »
Hi
Dunno, if you want bytes or string?!? But:
Code: Pascal  [Select][+][-]
  1. // string first
  2. var
  3.   Res: cardinal;
  4.   S: string;
  5. begin {Read(const Buffer: Pointer; const BufferSize: Cardinal): Cardinal;}
  6.   setlength(S,1024);
  7.   Res:= Read(@S[1],length(S));
  8.   setlength(S,Res);
  9.   ...
  10. end;
  11. // or bytes
  12. var
  13.   Res: cardinal;
  14.   B: TBytes;
  15. begin
  16.   setlength(B,1024);
  17.   Res:= Read(@B[0],length(B));
  18.   setlength(B,Res);
  19.   ...
  20. end;
  21.  
I think that should do the trick...  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #7 on: January 26, 2024, 10:58:36 am »
Benny? It does not seem to connect.

Can you see what i did wrong ?

Grtz...Wouter


AVR side:

Code: Pascal  [Select][+][-]
  1. program WVW_AVR_SDIE;
  2.  
  3. uses
  4.   uart, i2c, delay, simplemath;
  5.  
  6. var
  7.   i2c_master: TI2CMaster;
  8.   S:String;
  9.   i, value:byte;
  10.   counter:integer;
  11.  
  12. const
  13.   BAUD_Rate = 115200;
  14.   BAUD_SETTING = (((F_CPU + 4*BAUD_Rate) shr 3) div BAUD_Rate) - 1;
  15.  
  16.   //****************************************************************************************************************
  17.     function IntToStr(const i: Integer): string;
  18.     begin
  19.      Str(i, Result);
  20.     end;
  21.      
  22.     function StrToInt(const s: string): Integer;
  23.     var
  24.      code: Integer;
  25.     begin
  26.      Val(s, Result, code);
  27.     end;
  28. //****************************************************************************************************************     
  29.        
  30.  
  31. begin
  32.   uart_init1(115200,true);  
  33.   i2c_master.init(I2C_50kHz);
  34.  
  35.   uart_transmit('Starting up...'#13);
  36.  
  37.   S := 'Hello World!';
  38.  
  39.   i2c_master.start($18, false); //shl 1;
  40.  
  41.   counter := 1;
  42.   repeat    
  43.     counter := counter + 1;
  44.         if counter = 254 then counter := 1;
  45.     uart_transmit('Counter='+IntToStr(counter)+#10+#13);
  46.                
  47.         for i:=1 to length(S)-1 do
  48.     begin
  49.             value := ord(S[i]);
  50.         i2c_master.WriteByte(value);
  51.             delay_ms(2);       
  52.         end;   
  53.        
  54.         i2c_master.WriteByte(Ord(#10));
  55.         i2c_master.WriteByte(Ord(#13));
  56.  
  57.         delay_ms(2);   
  58.   until 1=2;  
  59.   i2c_master.stop;
  60. end.


RPI Side:

Code: Pascal  [Select][+][-]
  1. program WVW_RPI_SIDE;
  2.  
  3. uses
  4.   SysUtils, PXL.TypeDef, PXL.Boards.Types, PXL.Sysfs.UART, PXL.Boards.RPi, crt;
  5.  
  6. var    
  7.   SystemCore: TFastSystemCore;
  8.   GPIO: TFastGPIO;
  9.   PortI2C: TFastI2C;
  10.   AddrI2c: Byte; // Address of the component
  11.   counter: byte;
  12.      
  13.   Res: cardinal;
  14.   S: string;
  15.   B: TBytes;
  16.  
  17. begin
  18.   SystemCore:= TFastSystemCore.Create;
  19.   GPIO:= TFastGPIO.Create(SystemCore);
  20.   PortI2C:= TFastI2C.Create(GPIO);
  21.   AddrI2c:= $18; // Address of my test component
  22.   PortI2C.SetAddress(AddrI2C);
  23.  
  24.   counter := 0;
  25.   repeat
  26.      counter := counter + 1;
  27.          if counter = 255 then counter := 1;
  28.          writeln('counter='+IntToStr(counter));
  29.  
  30.      setlength(S,1024);
  31.      Res := PortI2C.Read(@S[1],length(S));
  32.      setlength(S,Res);
  33.  
  34.      writeln('Res=' + IntToStr(res));
  35.      writeln('S[1]='   + S[1]);                  
  36.          writeln('S='   + S);            
  37.                        
  38.          delay(55);
  39.   until keypressed;      
  40.  
  41.   PortI2C.Free;
  42.   GPIO.Free;
  43.   SystemCore.Free;  
  44. end.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1746
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #8 on: January 26, 2024, 11:12:03 am »
I guess you are using two I2C-masters. And that will never work.
So, the question is: why do you use a I2C-master on the AVR ?

cdbc

  • Hero Member
  • *****
  • Posts: 1224
    • http://www.cdbc.dk
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #9 on: January 26, 2024, 11:24:04 am »
Hi
What DonAlfredo says, he beat me to it and probably knows way more about this than I do  :)
Hmmm, it seems, on one side of the connection you're writing a lump of bytes using:
Quote
i2c_master.WriteByte(value);
followed by a CrLf pair, over and over again.
Then on the other side of the connection, you're reading a "string" in one fell swoop?!? It seems to me, that at least you should send and receive the same datatype... Also have you checked, that you can e.g.: send 1 BYTE +crlf and receive 1 BYTE +crlf successfully? Then after this, is it a must, that you send the bytes separate, because your 'read' seems to accept a @buffer --> ? to 1 or more bytes ?
Too many unknowns, for me to figure out, you should have another look at the documentation for your kit.
Regards Benny
« Last Edit: January 26, 2024, 11:25:54 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #10 on: January 26, 2024, 11:38:40 am »

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1746
Re: Platform eXtended Library (PXL) to AVR, howto I2C
« Reply #11 on: January 26, 2024, 11:55:09 am »
Again. Make the AVR the slave. This will solve all your issues and will make the RPi a simple polling device instead of something that requires threads to be able to respond to data.

 

TinyPortal © 2005-2018