Italian community of Lazarus and Free Pascal

Programmazione => Generale => Topic aperto da: sekoto - Agosto 18, 2014, 10:23:15 pm

Titolo: aiuto nel sviluppare una procedura-driver per dialogare col bus i2c
Inserito da: sekoto - Agosto 18, 2014, 10:23:15 pm
salve a tutti finalmente dopo tanto tempo risprendo in mano un vecchio progetto sperando di portarlo avanti, ma purtroppo devo spesso ricorrere al vostro aiuto.

devo sviluppare una procedura da inserire in una applicazione che riesca a dialogare col bus i2c che è a bordo del raspberry V2.

a quest indirizzo http://www.vincenzov.net/tutorial/RaspberryPi/i2c.htm (http://www.vincenzov.net/tutorial/RaspberryPi/i2c.htm) si trovano le istruzioni in formato bash per dialogare con il bus. Nell'applicazione ho cercato di emulare le istruzioni ma sono abbastanza complicate e scomode quindi pensavo ad un driver o una procedura dove "passare" indirizzo del device e "azione da fare"... qualcuno mi può aiutare?? grazie mille...
Titolo: Re:aiuto nel sviluppare una procedura-driver per dialogare col bus i2c
Inserito da: Stilgar - Agosto 19, 2014, 01:18:58 am
Hai provato a vedere il http://www.vincenzov.net/tutorial/RaspberryPi/i2c_c.htm (http://www.vincenzov.net/tutorial/RaspberryPi/i2c_c.htm)
Iniziare con un sorgente potrebbe essere d'aiuto.
Bash ha le sue madonne ;)

http://www.vincenzov.net/tutorial/RaspberryPi/LM92.c (http://www.vincenzov.net/tutorial/RaspberryPi/LM92.c)
questo legge il sensore di temperatura.
Codice: [Seleziona]

#define I2C_ADDR 0x4B            // Device adress

#define LM92_RES 0.0625                         // Resolution (see data sheet)

static const char *device = "/dev/i2c-1";   // I2C bus

static void exit_on_error (const char *s)   // Exit and print error code
{    perror(s);
     abort();
}

int main(int argc, char *argv[])
{
   int fd;
   
   uint8_t  buffer[2];
        int16_t  data;
        double   temperature;
       
        printf("Rapsberry Pi: I2C in C - Versione 0.61 - Luglio 2013\n");
        printf("Copyright (c) 2013, Vincenzo Villa (http://www.vincenzov.net)\n");
        printf("Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported.\n");
        printf("Creative Commons | Attribution-Share Alike 3.0 Unported\n");
        printf("http://www.vincenzov.net/tutorial/elettronica-di-base/RaspberryPi/i2c-c.htm\n\n");                                       

          // Open I2C device
          if ((fd = open(device, O_RDWR)) < 0) exit_on_error ("Can't open I2C device");

   // Set I2C slave address
   if (ioctl(fd, I2C_SLAVE,I2C_ADDR) < 0) exit_on_error ("Can't talk to slave");
       
        // Read from LM92 (from Power-up default register)     
        printf("Reading from LM92 at address 0x%.2X on %s\n\n", I2C_ADDR, device);
        if (read(fd,buffer,2) != 2 ) exit_on_error ("Failed to read from the i2c bus"); 
        printf("Data read from Power-up default register: 0x%.2X%.2X\n\n", buffer[0], buffer[1]);     
       
        data =  buffer[0];
        data = data << 8;
       
        data = data | buffer[1];
        data = data >> 3;
        temperature = LM92_RES * data;
       
        printf("Temperature: %2.1f °C\n\n", temperature);
       
        close(fd);

   return (0);
}

potrebbe diventare

Codice: [Seleziona]
const 
 I2C_ADDR=$4B;            // Device adress
 LM92_RES=0.0625;      // Resolution (see data sheet)
device:String= '/dev/i2c-1';   // I2C bus
procedure exit_on_error (const message : string)   // Exit and print error code
 begin   
perror(s); halt(1);
end;
var
   fd: File; buffer: array [0..1] of byte; data: smallint; temperature:double   ;
 begin
 writeln("Rapsberry Pi: I2C in C - Versione 0.61 - Luglio 2013\n"); writeln("Copyright (c) 2013, Vincenzo Villa (http://www.vincenzov.net)\n");
writeln("Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported.\n"); writeln("Creative Commons | Attribution-Share Alike 3.0 Unported\n");
 writeln("http://www.vincenzov.net/tutorial/elettronica-di-base/RaspberryPi/i2c-c.htm\n\n"); // Open I2C device
fd := FileOpen(device, O_RDWR);
if (fc <= 0) then
  exit_on_error ("Can't open I2C device"); // Set I2C slave address
 if (fpioctl(fd, I2C_SLAVE,I2C_ADDR) < 0) then exit_on_error ("Can't talk to slave"); // Read from LM92 (from Power-up default register)
writeln(Format("Reading from LM92 at address 0x%.2X on %s\n\n", [I2C_ADDR, device]); if (read(fd,buffer,2) <> 2 ) then exit_on_error ("Failed to read from the i2c bus"); Writeln(Format("Data read from Power-up default register: 0x%.2X%.2X\n\n", [buffer[0], buffer[1]]); data :=  buffer[0]; data := data shl 8; data := data or buffer[1]; data := data shr 3; temperature := LM92_RES * data; writeln("Temperature: %2.1f °C\n\n", [temperature]); close(fd);
end;

Non l'ho passato al compilatore.
Ci sono evidenti errori di sintassi per il pascal (esempio le stringhe ;) sono pigro :D )

Una volta capito come operare potresti fare una class TI2CDriver del tipo:
Codice: [Seleziona]
type
TWire = class(THandleStream)
protected
  FI2CAddr: word;
  FMaster : boolean;
  FRead : Boolean;
public
  constructor create(address: word; asMaster : Boolean);
...
end;

l'idea della classe non è nuova.
Dai un'occhiata https://github.com/arduino/Arduino/blob/master/libraries/Wire/Wire.h (https://github.com/arduino/Arduino/blob/master/libraries/Wire/Wire.h)

Un esempio (a caso, ma caso caso) per vedere come viene usata la libreria Wire
http://playground.arduino.cc/Main/WiiChuckClass (http://playground.arduino.cc/Main/WiiChuckClass)

hihi. Il prossimo passo è comandare i led da nunchack delle wii ;)
Nomore che ne dici? :p

Stilgar
Titolo: Re:aiuto nel sviluppare una procedura-driver per dialogare col bus i2c
Inserito da: nomorelogic - Agosto 19, 2014, 10:01:23 am
mica male come idea ;)
Titolo: Re:aiuto nel sviluppare una procedura-driver per dialogare col bus i2c
Inserito da: Stilgar - Agosto 19, 2014, 10:03:54 am
hahahahaha
Con arduino l'ho fatto.
10 secondi di soddisfazione. Poi smontato il circuito e andato a prendere il classic controller.
Poi più trovato il tempo di fare la libreria per contollarlo.
;)

Stilgar