hallo,
ich verstehe jetzt deinen Code nicht mehr -
ist es noch so wie in meinem Ausgangs-Code,
- dass der Raspi im 10ms-Takt (künftig schneller) zum Arduino 32 Bytes schickt und dann, wenn sie angekommen sid, 32 neue Bytes von ihm zurück liest
- und dass der Arduino so schnell wie möglich (also mindestens im 1ms- Takt) seinen Sende-Array zum Auslesen aktualisiert und bereitstellt?
Der Arduino muss dies automatisch tun, so schnell wie möglich, es darf keine Anforderung vom Raspi nötig sein außer ein onRequest und/oder ein onReceive(), um sie zu empfangen oder weg zu "senden".
Später in der "richtigen" Anwendung wird der Arduino seine digitalen und anlalogen Pins per Timer Interrupt (AVR) oder Task (DUE) (100µs) ständig (quasi im Hintergund bzw. per Parallel-Task) aktualisieren, damit sie beim Abruf immer in der am weitest aktualisiertesten Version zur Verfügung stehen. Kommt dann ein onRequest, kann abgerufen werden, was bis dahin bekannt ist.
Genauso muss jederzeit alles angenommen werden können, was vom Master gesendet wurde, per onReceive Event. Eine gesonderte Funktion (ebenfalls per Timer-Interrupt oder Task) verarbeitet sie dann weiter.
Der Arduino-Code soll dazu möglichst unverändert bleiben (außer clock-stretching vermeiden)
und der Raspi-Code ebenfalls, außer dass er clock-stretching besser verträgt, d.h. die normalen read() und write() Befehle für I2C müssten durch andere, clock-tolerantere, erstetzt werden.
Raspi:
Code:
// Raspberry Pi Master code to send/receive byte arrays
// to an Arduino as an I2C slave
//
// ver. 0.002b
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <errno.h>
#include <string.h>
#define MSGSIZE 32
#define ARDU1_SLV_ADDR 0x04
uint8_t calcchecksum( uint8_t array[]) {
int32_t sum=0;
for(int i=2; i<MSGSIZE; ++i) sum+=(array[i]);
return (sum & 0x00ff);
}
int main (void)
{
int fd, i ;
uint8_t test=0;
uint8_t data [MSGSIZE] ;
if ((fd = wiringPiI2CSetup ( ARDU1_SLV_ADDR) ) < 0)
{
fprintf (stderr, "Can't open RTC: %s\n", strerror (errno)) ;
exit (EXIT_FAILURE) ;
}
for (;;)
{
memset(data, 0, sizeof(data) );
data[0]= 0xff; // init for transmission error check
read (fd, data, MSGSIZE) ;
if( data[1] != calcchecksum( data ) ) {
// handle transmission error !
}
else {
printf (" read: ");
for (i = 0 ; i < 7 ; ++i)
printf (" %3d", data [i]) ;
//printf ("\n") ;
delay(10) ;
memset(data, 0, sizeof(data) );
data[5]= test++;
data[0]= 0xff;
data[MSGSIZE-1]= ARDU1_SLV_ADDR ;
data[1] = calcchecksum( data );
write(fd, data, MSGSIZE) ;
printf (" write: ");
for (i = 0 ; i < 7; ++i)
printf (" %3d", data [i]) ;
printf ("\n\n") ;
delay(10) ;
}
}
return 0 ;
}
Arduino:
Code:
// Arduino code to send/receive byte arrays
// Arduino as an I2C slave
//
// ver. 0.002b
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
#define MSGSIZE 32
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]
if (flag==1) {
//debug
sendarray[4] +=1;
}
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(1); // 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..
Lesezeichen