Hello people!
I'm having some problems establishing an I2C communication between two mebds where I put one as Master and the other as Slave, everytime i try sending information from one side to the other, my "returns" from the functions write and read are never 0, hence are never ACK. To tell the truth I'm no pro in I2C, but reading some blogs I think I got a hint of what is causing the problems, I just don't know how to code the solutions. 1- I don't exactly how to referentiate the internal registers of the mbed to perform the reads and writes, I tried making I2C communication without making any reference to them, just passing information as strings but I don't know if it's possible.2- Maybe I'm having some problems because my Slave is actually a microprocessor and my clock stretching is not working properly, not giving enough time for my slave mbed to perform its interruption and save its stateand then sending information.
Well, the codes are below, if anybody could give me a hand I'd very much appreciate it.
PS: All those waits in the code were just unsuccessful attempts to give some time for the communication to take place.
MASTER:
#include "mbed.h"
I2C i2c(p28, p27);
Serial pc(USBTX, USBRX);
int main() {
int addr = 0x01;
int a,b;
char buff[10]; const char data[]="message master";
i2c.frequency(10000);
while(1) {
i2c.start();
a=i2c.read(addr, buff,10);
wait(0.75);
pc.printf("Read %d", a);// only to check if there was an ACK, till now, not working
i2c.stop();
pc.printf("MASTER READ: %s \n\r", buff);
i2c.start();
b=i2c.write( addr, data, 10);
wait(0.75);
i2c.stop();
pc.printf("Write %d", b); // only to check if there was an ACK, till now, not working
for(int i = 0; i < 10; i++) buff[i] = 0;
}
}
SLAVE:
#include "mbed.h"
I2CSlave slave(p9,p10);
Serial pc(USBTX, USBRX);
int main() {
const char test[]= "message slave";
char buf[10];
int i, b;
slave.frequency(10000);
slave.address(0x01);
while(1) {
i = slave.receive();
wait(0.75);
switch (i) {
case I2CSlave::ReadAddressed:
slave.stop();
b=slave.write(test, strlen(test) + 1);
wait(0.75);
pc.printf("Return Slave %d", b);// only to check return value
break;
case I2CSlave::WriteGeneral:
slave.stop();
slave.read(buf, 10);
wait(0.75);
pc.printf("SLAVE Read G: %s\n\r", buf);
break;
case I2CSlave::WriteAddressed:
slave.stop();
slave.read(buf, 10);
wait(0.75);
pc.printf("SLAVE Read A: %s\n\r", buf);
break;
}
for(int i = 0; i < 10; i++) buf[i] = 0;
}
}
Hello people! I'm having some problems establishing an I2C communication between two mebds where I put one as Master and the other as Slave, everytime i try sending information from one side to the other, my "returns" from the functions write and read are never 0, hence are never ACK. To tell the truth I'm no pro in I2C, but reading some blogs I think I got a hint of what is causing the problems, I just don't know how to code the solutions. 1- I don't exactly how to referentiate the internal registers of the mbed to perform the reads and writes, I tried making I2C communication without making any reference to them, just passing information as strings but I don't know if it's possible.2- Maybe I'm having some problems because my Slave is actually a microprocessor and my clock stretching is not working properly, not giving enough time for my slave mbed to perform its interruption and save its stateand then sending information. Well, the codes are below, if anybody could give me a hand I'd very much appreciate it. PS: All those waits in the code were just unsuccessful attempts to give some time for the communication to take place.