Forum > Linux

WVW's Equalizer project, how to I2C from AVR to RPIZERO.

(1/1)

pascalbythree:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program ei_eio_i2c_55; (* This is a companion to ei_eio_serial.ino, which is assumed to be compiled    *)(* with I2C support enabled and to respond to "EI\r" with "EIO\r". MarkMLl.     *) {$mode objfpc}{$H+}{$assertions on } uses  Sysutils, BaseUnix; const  I2C_SLAVE = 1795; type  responseType= (none, bad, ok); var  slave: dword= $18;  fd: cint;  scratch: integer;  ei: string[15]= 'EI' + #$18;  response: string;    (* Convert the Data parameter from a word to a pointer. This should have    lots of sanity checks, i.e. that the IOCTL number is known to be one    that doesn't take a pointer, that the value being cast is actually small    and so on.  *)  function fpIoctl(Handle: cint; Ndx: TIOCtlRequest; Data: dword): cint;   begin    Assert(Ndx = I2C_SLAVE, 'Bad IOCTL number');    Assert(Data < 256, 'Bad IOCTL parameter');    result := BaseUnix.FpIOCtl(Handle, Ndx, pointer(ptruint(Data)))  end { fpIoctl } ;    (* Assume that messages are fixed-format and that we're looking for EIO    followed by stuff.  *)  function getResponse(Handle: cint): responseType; // Apparently an Arduino I2C message is restricted by buffer length to 32 bytes,// and a serial message to 64 bytes. This suggests that, assuming text-format// rather than binary data, individual lines are probably best kept below 32// characters and possibly when generated zero-padded to precisely 32 bytes even// if trimmed before being sent as text.   begin    response := '........................';     (* 24 characters                *)    Sleep(100);                         (* Milliseconds                         *)    if fpRead(Handle, response[1], 24) <> 24 then      if response[1] = '.' then         (* No response at all                   *)        exit(none)      else                              (* Didn't get to \r                     *)        exit(bad);    if (Pos('EIO', response) <> 1) or (Pos(#$0d, response) = 0) then      exit(bad)                         (* Not quite right                      *)    else begin                          (* Got it                               *)      SetLength(response, Pos(#$0d, response) - 1);      exit(ok)    end  end { getResponse } ;  begin  fd := fpOpen('/dev/i2c-1', O_RDWR);  Assert(fd >= 0, 'Open failed');  scratch := fpIoctl(fd, I2C_SLAVE, slave);  Assert(scratch = 0, 'Slave assignment failed');  scratch := fpWrite(fd, ei[1], 3);  Assert(scratch = 3, 'Write failed');  case getResponse(fd) of    none: WriteLn('Read timed out');    bad:  WriteLn('Read incomplete');  otherwise    WriteLn('Good response');    WriteLn(response);  end;  scratch := fpClose(fd);  Assert(scratch = 0, 'Close failed');  WriteLn('OK')end.


Navigation

[0] Message Index

Go to full version