Hello, hope you doing all well, i m trying to set an I2C communication between Wiznet w5500 Rasberry Evb pico as a master and an arduino nano rp 2040 connect as slave, the salve manage to retrieve the request from the master and execute it but the master never receives the response from the slave , here s the code for the master :
#include <Wire.h>
#define BUFFER_SIZE 55 // Adjust size according to your needs
char buffer[BUFFER_SIZE];
boolean finished=false;
boolean rd=false;
TwoWire Wire0(i2c0, 4, 5); // Use i2c0 with SDA on pin 4 and SCL on pin 5
void setup() {
Wire0.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
delay(500); // Wait a bit to ensure the slave processes the message
}
void loop() {
if(!finished) {
finished=true;
Serial.println("je suis la 1");
Wire0.requestFrom(8, BUFFER_SIZE);
int index = 0;
int count=0;
Serial.println(Wire0.available());
while (Wire0.available()) {
Serial.println("je suis la 2");
char c = Wire0.read();
count ++;
Serial.println(count);
Serial.println(c);
}
}
}
NOTE : i only did the while to just execute one request for now, as for the slave code :
Post a schematic showing how these are connected. I am curious where the I2C entered into this? The W5500 chip is a Hardwired Internet controller designed as a full hardwired TCP/IP stack with WIZnet technology. W5500 provides Internet connectivity to your embedded systems by using SPI(Serial Peripheral Interface). SPI provides easy connection via external MCU to W5500. The clock speed of W5500 SPI supports up to 80MHz.
thanks for the reply, so let me answer one by one :
1.well the big picture is that i m working on a project to add wifi to an already existing structure of lora network, in the lora network, the modules are connected via SPI to the W5500 master so almost all pins were taken, i only have 3 free pins available and looking to documentation those pins only support I2C communication to add a wifi module. i connected A4 and A5 of rp2040 to GP4 and GP5 ( and of course ground to ground).
2. as for why the time out in the slave, it s because we can specify the time between each measure as the user wishes to, i figured out yesterday that isn t the right place to add the delay, better add it between master request.
3. as for the question also of why i started the wifi in the request function on the slave and not in setup, it s because i will switch to BLE depending on master request (did n t add it yet) but the rp 2040 does n t support both wifi and BLE in, so i need to off one and power the other module.
hope my responses clarified it, as for the code , i think why i m not getting a return is due to the delay i added but still i have a little problem, in the I2C communication you need to specify the number of bytes the master needs to read, well since the rssi reading may differ in terms of length ( depending on number of digits and also the presence of the sign "-" or not ), any tips to how to deal with this ?.
here s a more reformed code for the use case i want too (omitting wifi part for now and replacing it with a simple message ) ,this is the slave equivalent code :
#include <Wire.h>
String response = "Hello World"; // Example string to send
int stringLength;
bool isConfigDone = false;
void setup() {
Wire.begin(8); // Join I2C bus with address #8
Wire.onReceive(receiveEvent); // Register receive event
Wire.onRequest(requestEvent); // Register request event
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
// Perform other tasks here if needed
delay(100); // A short delay in the loop is generally acceptable
}
void receiveEvent(int howMany) {
if (!isConfigDone) {
// Read the configuration parameters sent by the master
char config[howMany+1];
for (int i = 0; i < howMany; i++) {
config[i] = Wire.read();
}
config[howMany] = '\0'; // Null-terminate the string
Serial.println(config);
// Process configuration parameters
// Here we just simulate processing time with delay
delay(5000); // Simulate a 5-second configuration process
isConfigDone = true;
Serial.println("Configuration done");
}else {
// Read the command from the master
command = Wire.read();
Serial.println("*********");
Serial.println(command);
}
}
// Function that executes whenever data is requested by master
void requestEvent() {
if (isConfigDone) {
if(command=='R') {
String msg=String(response.length());
for (int i = 0; i < response.length(); i++) {
Wire.write(response[i]); // Send each character of the response string
}
command = '\0'; // Reset command after processing
}else {
Wire.write("S"); // Respond with 'S' to indicate configuration is successful
}
} else {
Wire.write("F"); // Respond with 'F' to indicate configuration is not yet done
}
}
everything works fine, but as you can see in master code , i m specifying that i want to read 11 bytes so i m getting the hello world without any problem, but in my case of concerne the length of the message varies, i was thinking of first sending to the master the length of the message, but still even that is still a a problem ( wire.write send char *) so if the length of the message is 2 digits it will require two byte if 3 digits then 3 bytes ( since i m sending as char )
This may help with the delay in the slave. This may be needed in your master due to the delays in the slave, like starting and stopping the wifi and BLE. Have you determined how long the wifi/BLE start and stop takes?
The data sent by the slave can be set to a specific length by filling the return string with leading zeros or spaces.
Looks like your test code eliminated the major delays in the slave.