Send char and not const char in I2c

23 Apr 2012

Hello people, does somebody know if there's a way to send a char and not a const char through I2c form the slave to the master? Both my master and my slave are mbeds, and i was able to sendo a char from the master to the slave, but not the other way around. my code follows: Slave:

#include "mbed.h"

  I2CSlave slave(p9,p10);
  Serial pc(USBTX, USBRX); 
  

int main() {    

    char test[]="message slave";
    char buf[15];
    int i,b;
    
    slave.frequency(100000);
    slave.address(0xA0);
    
    pc.printf("Test I2C Slave:\n\r");
    

    while(1) {
        for(int j = 0; j < 15; j++) buf[j] = 0;/*effacer le buffer*/
        
        /*strcpy(test, "");*/
        
        i = slave.receive();
        
        
        
        
        
        
        switch (i) {
            
            case I2CSlave::ReadAddressed:
                /*pc.printf("Tapez la message a etre envoye vers le Master:\n\r");
        
                pc.scanf("%s", test);
                
                pc.printf("%s", test);*/                
               
                b=slave.write(test, strlen(test) + 1); 
                
                wait(0.1);/*wait nécessaire pour faire le write*/
                
                pc.printf("Write %d\n\r",b);                
                      
                
                
                break;
                
            case I2CSlave::WriteGeneral:
                               
                slave.read(buf, 15);
                wait(0.1);/*wait nécessaire pour faire le read*/
                pc.printf("SLAVE Read General: %s\n\r", buf);
                break;
                
                
            case I2CSlave::WriteAddressed:
                slave.read(buf, 15);
                wait(0.1);/*wait nécessaire pour faire le read*/
                pc.printf("SLAVE Read Addressed: %s\n\r", buf);
                break;
        }       
    }
}


Master:

#include "mbed.h"

I2C i2c(p28, p27);        
Serial pc(USBTX, USBRX); 



int main() {
    const int addr = 0xA0;
    int a,b;
    char buff[15]; char data[15]; 
    
    i2c.frequency(100000);
    
    
    
        
    while(1) { 
    
        for(int j = 0; j < 15; j++) buff[j] = 0;/*effacer le buffer*/
    
    /************************************************Ecrire************/
        strcpy(data, "");
    
        pc.printf("Tapez la message a etre envoye vers le Slave:\n\r");
        
        pc.scanf("%s", data);
        
        pc.printf("%s\n\r", data);
        
        b=i2c.write( addr, data, strlen(data)+1);
        
        wait(0.1);/*wait nécessaire pour faire le write*/
             
                
        pc.printf("Write %d\n\r", b);
        
        
        
        /*****************************Lire***************/
        
        
        a=i2c.read(addr, buff,15);
        
        wait(0.1);/*wait nécessaire pour faire le read*/
        
        pc.printf("%d", a);       
        
        pc.printf("Read %s\n\r", buff);
        
           
       
    }
}



23 Apr 2012

The problem may be that slave takes too much time to prepare the message that you have to enter from the keyboard. The master i2c.read() may timeout and fail.

In the slave code try to enter the message just once before you start the while(1) and then resend that same message when the master wants to read from the slave. Does that work?