I want to make the i2c transactions as fast as possible, and I am using STM32. The first part is to emulate a i2C device, and then optimize it.
Simulate a i2C sensor with Arduino
The arduino code is:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
The example receiver would be:
#include <Arduino.h>
#include <Wire.h>
// Logic analyser
#define LA_CH0 D3
#define LA_CH1 D2
void setup() {
Wire.setSDA(PC9);
Wire.setSCL(PA8);
Wire.setClock(100000); // 100KHz Normal mode
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
pinMode(LA_CH0, OUTPUT);
pinMode(LA_CH1, OUTPUT);
}
void loop() {
digitalWrite(LA_CH0, HIGH);
Wire.requestFrom(8, 6); // request 6 bytes from peripheral device #8
digitalWrite(LA_CH0, LOW);
digitalWrite(LA_CH1, HIGH);
while (Wire.available()) { // peripheral may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
digitalWrite(LA_CH1, LOW);
delay(10);
}
And the timming analysis of this one is the next:
That is 600 us for requesting, and 10uS for receiving.
—
Arduino code:
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}
void loop()
{
Wire.beginTransmission(0); // transmit to device #4
Wire.write("hello!"); // sends five bytes
Wire.endTransmission(); // stop transmitting
delay(500);
}
I am not able to make it work, but I think is because I am not understanding who is master and slave, and the arduino libraries are not helping.