Recent

Author Topic: WVW's Equalizer project, how to I2C from AVR to RPIZERO.  (Read 754 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
WVW's Equalizer project, how to I2C from AVR to RPIZERO.
« on: January 25, 2024, 11:46:57 am »
This is my equalizer project,  as seen in attachment.

It is used with a audio signal input to a ADC input on my ATmega328P.

Now i am using a UART from my AVR level shifter to my RPI to receive the values and display bars on my screen. Mounted on my wall as seen in attachment.

Everything works fine.

Now i want to replace the UART with a i2C. Everything is doubly prepared in the RPIMOD in the enclosure of my EQualizer project. For UART and I2C.

Can anybody / MarkMLl help me out debugging this peice of source code to receive bytes, to make strings, with my RPI as slave? Along the way ?

With friendly greets, Wouter van Wegen, PascalByThree

Code: Pascal  [Select][+][-]
  1. program ei_eio_i2c_55;
  2.  
  3. (* This is a companion to ei_eio_serial.ino, which is assumed to be compiled    *)
  4. (* with I2C support enabled and to respond to "EI\r" with "EIO\r". MarkMLl.     *)
  5.  
  6. {$mode objfpc}{$H+}
  7. {$assertions on }
  8.  
  9. uses
  10.   Sysutils, BaseUnix;
  11.  
  12. const
  13.   I2C_SLAVE = 1795;
  14.  
  15. type
  16.   responseType= (none, bad, ok);
  17.  
  18. var
  19.   slave: dword= $18;
  20.   fd: cint;
  21.   scratch: integer;
  22.   ei: string[15]= 'EI' + #$18;
  23.   response: string;
  24.  
  25.  
  26.   (* Convert the Data parameter from a word to a pointer. This should have
  27.     lots of sanity checks, i.e. that the IOCTL number is known to be one
  28.     that doesn't take a pointer, that the value being cast is actually small
  29.     and so on.
  30.   *)
  31.   function fpIoctl(Handle: cint; Ndx: TIOCtlRequest; Data: dword): cint;
  32.  
  33.   begin
  34.     Assert(Ndx = I2C_SLAVE, 'Bad IOCTL number');
  35.     Assert(Data < 256, 'Bad IOCTL parameter');
  36.     result := BaseUnix.FpIOCtl(Handle, Ndx, pointer(ptruint(Data)))
  37.   end { fpIoctl } ;
  38.  
  39.  
  40.   (* Assume that messages are fixed-format and that we're looking for EIO
  41.     followed by stuff.
  42.   *)
  43.   function getResponse(Handle: cint): responseType;
  44.  
  45. // Apparently an Arduino I2C message is restricted by buffer length to 32 bytes,
  46. // and a serial message to 64 bytes. This suggests that, assuming text-format
  47. // rather than binary data, individual lines are probably best kept below 32
  48. // characters and possibly when generated zero-padded to precisely 32 bytes even
  49. // if trimmed before being sent as text.
  50.  
  51.   begin
  52.     response := '........................';     (* 24 characters                *)
  53.     Sleep(100);                         (* Milliseconds                         *)
  54.     if fpRead(Handle, response[1], 24) <> 24 then
  55.       if response[1] = '.' then         (* No response at all                   *)
  56.         exit(none)
  57.       else                              (* Didn't get to \r                     *)
  58.         exit(bad);
  59.     if (Pos('EIO', response) <> 1) or (Pos(#$0d, response) = 0) then
  60.       exit(bad)                         (* Not quite right                      *)
  61.     else begin                          (* Got it                               *)
  62.       SetLength(response, Pos(#$0d, response) - 1);
  63.       exit(ok)
  64.     end
  65.   end { getResponse } ;
  66.  
  67.  
  68. begin
  69.   fd := fpOpen('/dev/i2c-1', O_RDWR);
  70.   Assert(fd >= 0, 'Open failed');
  71.   scratch := fpIoctl(fd, I2C_SLAVE, slave);
  72.   Assert(scratch = 0, 'Slave assignment failed');
  73.   scratch := fpWrite(fd, ei[1], 3);
  74.   Assert(scratch = 3, 'Write failed');
  75.   case getResponse(fd) of
  76.     none: WriteLn('Read timed out');
  77.     bad:  WriteLn('Read incomplete');
  78.   otherwise
  79.     WriteLn('Good response');
  80.     WriteLn(response);
  81.   end;
  82.   scratch := fpClose(fd);
  83.   Assert(scratch = 0, 'Close failed');
  84.   WriteLn('OK')
  85. end.



« Last Edit: January 25, 2024, 11:49:35 am by pascalbythree »

 

TinyPortal © 2005-2018