hallo,
hat oder kennt hier jemand funktionierenden Code in C für den Raspi, um via I2C einen Arduino als Multiplexer Board zu nutzen?
Es müssen vom Raspi als Master nur "rohe" byte arrays (Größe bis 30 bytes) auf den Arduino übertragen (abwechselnd geschrieben/gelesen) werden.
Per UART habe ich es problemlos hinbekommen (sowohl Arduino/Arduino als auch Raspi/Arduino),
und per I2C klappt auch Arduino zu Arduino einwandfrei.
Auch Auslesen eines I2C Sensors (CMPS11) klappt wunderbar mit dem Raspi, und ntl auch mit dem Arduino.
Nur leider scheitert es mit Raspi als i2c-Master zum Arduino, und mir fehlen leider die C- und device tree- Kenntnisse für den Raspi, um das umzusetzen. So schöne einfache Dinge wie
Wire.requestFrom(address, count)
Wire.beginTransmission(address)
Wire.endTransmission()
Wire.send()
Wire.available()
Wire.receive()
Wire.onReceive(handler)
Wire.onRequest(handler)
etc. gibt es ja beim Raspi nicht.
Hat oder kennt wer fertigen Code, den ich dafür verwenden könnte?
edit:
dies ist der Arduino Slave code, der ansonsten gut mit dem entsprechenden Arduino-Master-Code zusammen funktioniert
- jetzt soll ein passender Raspi-Master-Code her:
Code:// Arduino code to send/receive byte arrays // Arduino as an I2C slave // compiles for MEGA and DUE, IDE 1.6.5 // ver. 0.001 #include <Wire.h> #define SLAVE_ADDRESS 0x04 #define MSGSIZE 30 byte recvarray[MSGSIZE]; // 0=0xff; 1=chksum; ...data...; MSGSIZE-1=SLAVE_ADDRESS byte sendarray[MSGSIZE]; volatile int8_t flag=0; //===================================================================================== //===================================================================================== void setup() { int32_t i=0; // Serial terminal window i=115200; Serial.begin(i); Serial.print("Serial started, baud="); Serial.println(i); // Wire (i2c) Wire.begin(SLAVE_ADDRESS); // start Arduino as a I2C slave, addr=0x04 (7-bit coded) Wire.onReceive(receiveData ); // event when master array is sent Wire.onRequest(sendData ); // event when master requests array to read memset(sendarray, 0, sizeof(sendarray) ); // init send- and recv arrays memset(recvarray, 0, sizeof(recvarray) ); Serial.print("I2C init: my slave address= "); Serial.println(SLAVE_ADDRESS); Serial.println("I2C init: done."); Serial.println(); Serial.println("setup(): done."); } //===================================================================================== uint8_t calcchecksum(uint8_t array[]) { int32_t sum=0; for(int i=2; i<MSGSIZE; ++i) sum+=(array[i]); return (sum & 0x00ff); } //===================================================================================== void loop() { char sbuf[128]; Serial.println(); Serial.println(); // do something with the received data // and then do something to build the sendarray [3]...[MSG_SIZE-2] sendarray[0] = 0xff; // 0 = start: 0xff == msg start flag sendarray[2] = flag; // 2 = send back msg error flag sendarray[MSGSIZE-1] = SLAVE_ADDRESS; // end of array: ID check sendarray[1] = calcchecksum(sendarray); // 1 = calc new chksum flag=0; // debug output sprintf(sbuf, "Sendarr[4]=%4d, [5]=%4d, Recvarr[4]=%4d, [5]=%4d", sendarray[4], sendarray[5], recvarray[4], recvarray[5]) ; Serial.println(sbuf); delay(2); // short break for the cpu and the bus } //===================================================================================== void receiveData(int byteCount) { int32_t i; byte val; while(Wire.available()<MSGSIZE) ; // wait for all bytes to complete i=0; // init counter var while(Wire.available()&& (i<MSGSIZE) ) // read all recv array bytes { val=Wire.read(); recvarray[i++]=val; } // check for transmission error if( (recvarray[0] == 0xff) && (recvarray[1] == calcchecksum(recvarray)) && (recvarray[MSGSIZE-1] == SLAVE_ADDRESS ) ) flag=1; // data ok else flag=127; // data faulty => handle rcv-error => flag =127 } //===================================================================================== void sendData(){ // Wire.write writes data from a slave device in response to a request from a master Wire.write(sendarray, MSGSIZE); // send own byte array back to master.. } //=====================================================================================







Zitieren

Lesezeichen