Recent

Author Topic: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr  (Read 4127 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« on: October 18, 2023, 06:11:21 pm »
My last topic got a little confused by the code. And i did quit with the PXL library.

For last go!: How to go from ccrause Raspberry I2C example application to  ccrause AVR example application.

From Master to Slave

Can anybody adapt the code for me, so it connects ?

PS: Afther this i also need to go backwards, from my AVR to my raspberry.

Greets, Wouter van Wegen, PascalByThree

RPI code:

Code: Pascal  [Select][+][-]
  1. program wvw_i2c_write_test;
  2.  
  3. uses
  4.   sysutils, i2c;
  5.  
  6. var
  7.   i2cMaster: TI2cMaster;
  8.   i: integer;
  9.   S:String;
  10.  
  11. begin
  12. // Starting RPI I2C master
  13.   S := 'Hello World!';
  14.  
  15.   i2cMaster := TI2cMaster.Create;
  16.   if not i2cMaster.Initialize(i2c_1) then
  17.   begin
  18.     WriteLn('Error opening i2c device i2c_1');
  19.     i2cMaster.Free;
  20.     Halt(1);
  21.   end;
  22.  
  23.    for i:=1 to length(S) do
  24.    begin
  25.       i2cMaster.WriteBytes($18, @S[i], Length(S));
  26.    end;  
  27.    
  28.   i2cMaster.Free;
  29. end.
  30.  

The AVR Code:
Code: Pascal  [Select][+][-]
  1. program i2cslavedemo;
  2.  
  3. uses
  4.   uart, i2cslave_unit, delay;
  5.  
  6. const
  7.   BAUD_Rate = 19200;
  8.   BAUD_SETTING = (((F_CPU + 4*BAUD_Rate) shr 3) div BAUD_Rate) - 1;
  9.  
  10. var
  11.   counter: byte;
  12.  
  13. procedure DataReceivedHandler(data: byte; ACK: boolean);
  14. begin
  15.   uart_transmit('data=');
  16.   uart_transmit(chr(data));
  17.   uart_transmit(#10);
  18.   uart_transmit(#13);
  19. end;
  20.  
  21. procedure DataRequestHandler(out data: byte; out ACK: boolean);
  22. begin
  23. end;
  24.  
  25. procedure ResetHandler;
  26. begin
  27. end;
  28.  
  29. begin
  30.   uart_init(BAUD_SETTING);
  31.  
  32.   uart_transmit('Starting AVR I2C slave');
  33.  
  34.   counter := 0;
  35.  
  36.   i2cslave_listen($18, @DataReceivedHandler, @DataRequestHandler, @ResetHandler);
  37.  
  38.   repeat
  39.     delay_ms(100);
  40.     uart_transmit('.');
  41.     uart_transmit(#10);
  42.     uart_transmit(#13);
  43.   until false;
  44. end.
  45.  


« Last Edit: October 18, 2023, 06:12:54 pm by pascalbythree »

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #1 on: October 19, 2023, 07:54:28 am »
Can anybody adapt the code for me, so it connects ?

What error do you get?

Quote
Code: Pascal  [Select][+][-]
  1.    for i:=1 to length(S) do
  2.    begin
  3.       i2cMaster.WriteBytes($18, @S[i], Length(S));
  4.    end;
  5.  
Change this code to:
Code: Pascal  [Select][+][-]
  1.   i2cMaster.WriteBytes($18, @S[1], Length(S));

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #2 on: October 19, 2023, 10:52:09 am »
Yay! that got to work again!, now i am going to try from AVR to RPI, the other way around.

People, you will hear back from me soon !

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #3 on: October 19, 2023, 11:20:38 am »
Code: Pascal  [Select][+][-]
  1. program i2c_read_reg;
  2.  
  3. uses
  4.   sysutils, i2c;
  5.  
  6. var
  7.   bus: TI2CBus;
  8.   i2cMaster: TI2cMaster;
  9.   val: byte;
  10.  
  11. begin
  12.   bus := TI2CBus(1);
  13.  
  14.   i2cMaster := TI2cMaster.Create;
  15.   if not i2cMaster.Initialize(bus) then
  16.   begin
  17.     writeln('Error opening i2c device: ', bus);
  18.     i2cMaster.Free;
  19.     Halt(1);
  20.   end;
  21.  
  22.   if i2cMaster.ReadBytes($18, PByte(val), 255) then  
  23.   begin
  24.     Writeln(IntToStr(val));
  25.   end
  26.   else
  27.     WriteLn('Error reading');
  28.  
  29.   i2cMaster.Free;
  30. end.

Does anybody have code to write values from your AVR to your RPI.

Or, again, must i start the RPI code as slave ? Is it in the examples from RPIPLC_PAS? If not , somebody has code?

Thank you Mr, ccrause, and greets, Wouter van Wegen, PascalByThree



« Last Edit: October 19, 2023, 11:27:49 am by pascalbythree »

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #4 on: October 21, 2023, 09:13:45 am »
Does anybody have code to write values from your AVR to your RPI.

Or, again, must i start the RPI code as slave ? Is it in the examples from RPIPLC_PAS? If not , somebody has code?

It isn't clear to me whether there is a method to implement an I2C slave in user mode code. Linux have two slave examples, i2c-slave-testunit and an EEPROM simulator, both implemented as kernel drivers.

The Linux kernel documentation for the I2C slave interface suggests that user space software need to communicate with a kernel mode backend driver. Perhaps the missing piece is a generic slave backend driver where user space slaves can register and receive I2C events via some callback mechanism?

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #5 on: November 09, 2023, 04:15:52 pm »
In freepascal ?

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #6 on: November 10, 2023, 09:36:52 am »
In freepascal ?
Writing a Linux kernel module in FPC is possible, but not trivial.

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: LAST GO!: from ccrause / rpiplc_pas to ccrause / fpc-avr
« Reply #7 on: November 10, 2023, 12:52:23 pm »
One can also interact with the hardware registers on the BCM peripheral to configure I2C slave mode and read/write data, see chapter 11 of the BCM2835 manual.  This appears to be what the pigpio library does for I2C slave support.  To make this work in FPC will then require installing the pigpio package and translating enough of the C header file to Pascal to call the necessary functions from Pascal code.  Read this discussion for a general overview of how a slave can be implemented using pigpio (C/C++ example, but still straight forward enough to rewrite in Pascal I would think).

 

TinyPortal © 2005-2018