8000 proposal for I2C master/slave example by suculent · Pull Request #5360 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

proposal for I2C master/slave example #5360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a1b60e0
added proposal for I2C peer-to-peer communication (master/slave) exam…
Nov 21, 2018
ace0b37
fixes with regards to Travis CI help
Nov 21, 2018
ba488f7
oh, the warnings. now the goes to default but address needs to be set…
Nov 21, 2018
716f3a0
proposing new init method with pins and address, because pins() is de…
Nov 21, 2018
f692b85
Merge branch 'master' into i2c_master_slave_example
devyte Nov 23, 2018
0473dbd
type fixed in 3rd party code, removing I2C scanner (duplicate of anot…
Nov 26, 2018
d558bfe
type fix in onReceive callback
Nov 26, 2018
21dfde2
implemented changes proposed by code review
Nov 26, 2018
8d1066a
Merge branch 'i2c_master_slave_example' of github.com:suculent/Arduin…
Nov 26, 2018
57bcd96
Merge commit 'cd05bae0e8b69eea94adbf153eca4e7c9b96a901' into i2c_mast…
Nov 26, 2018
88de8da
partial fixes after travis check; still missing merge of yesterday's …
Nov 26, 2018
fe21c28
Merge branch 'master' into i2c_master_slave_example
suculent Nov 28, 2018
9f019a8
Merge branch 'master' into i2c_master_slave_example
suculent Dec 8, 2018
983fc47
no message
Dec 8, 2018
4faa715
fixes after functional testing
Dec 8, 2018
d18f57e
Merge branch 'master' into i2c_master_slave_example
devyte Dec 8, 2018
fabca64
minor style fixes
Dec 8, 2018
35f4dac
Merge branch 'i2c_master_slave_example' of github.com:suculent/Arduin…
Dec 8, 2018
a8f7d63
style fix and unused variable fix
Dec 9, 2018
0272be2
style check fix
Dec 9, 2018
20e78d0
Merge branch 'master' into i2c_master_slave_example
devyte Dec 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
partial fixes after travis check; still missing merge of yesterday's …
…work...
  • Loading branch information
Matěj Sychra committed Nov 26, 2018
commit 88de8da920167845fd096e1079dafd60f3736648
44 changes: 24 additions & 20 deletions libraries/Wire/examples/i2c-P2P-master/i2c-P2P-master.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ MessageData encodeMessage(const String &instring) {

msgdata.terminator = '.';

int datasize = sizeof(msgdata.data);
size_t datasize = sizeof(msgdata.data);
Serial.print("\nEncoding data of size: ");
Serial.println(datasize);

Expand All @@ -85,7 +85,10 @@ MessageData encodeMessage(const String &instring) {
}

// should be in shared header
bool validateMessage(const char* bytes, MessageData &tmp) {
bool validateMessage(char* message_bytes, MessageData &tmp) {

MessageData tmp;
memcpy(&tmp, message_bytes, sizeof(tmp));

// Validate PROTOCOL terminator
if (tmp.terminator != '.') {
Expand Down Expand Up @@ -116,7 +119,7 @@ bool validateMessage(const char* bytes, MessageData &tmp) {
Serial.print("tmp CRC16: ");
Serial.println(tmp.crc16, HEX);
Serial.println("[ERROR] Request retransfer exception.");
return true;
return false;
}

// Validate sequence number
Expand Down Expand Up @@ -152,7 +155,7 @@ void sendMessage(const int seq, const String &msg) {
Serial.print("Encoding struct of size: ");
Serial.println(sizeof(struct_data));
memcpy(buf, &struct_data, sizeof(struct_data));
for (int i = 0; i < sizeof(struct_data); ++i) {
for (unsigned int i = 0; i < sizeof(struct_data); ++i) {
Wire.write(buf[i]);
Serial.print(buf[i]);
Serial.print(" ");
Expand All @@ -167,7 +170,7 @@ void receiveEvent(const size_t howMany) {

char incoming[howMany];
incoming[howMany - 1] = '\0';
int index = 0;
unsigned int index = 0;

digitalWrite(LED_PIN, LOW);

Expand Down Expand Up @@ -229,9 +232,9 @@ void setup() {

delay(2000);

Wire.pins(SDA_PIN, SCL_PIN);
Wire.begin(I2C_MASTER);
//Wire.begin(SDA_PIN, SCL_PIN, I2C_MASTER); // new syntax: join i2c bus (address optional for master)
//Wire.pins(SDA_PIN, SCL_PIN);
//Wire.begin(I2C_MASTER);
Wire.begin(SDA_PIN, SCL_PIN, I2C_MASTER); // new syntax: join i2c bus (address optional for master)
Wire.onReceive(receiveEvent);

Serial.begin(230400); // keep serial fast as possible for debug logging
Expand All @@ -240,31 +243,32 @@ void setup() {
digitalWrite(LED_PIN, HIGH);
}

unsigned long secondTimer = millis() + 1000;
unsigned long second_timer = millis() + 1000;

void loop() {

if (millis() > secondTimer) {
if (millis() > second_timer) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work as expected on millis() rollover (will enter if statement again immediately in that case instead of waiting). Consider delta time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this as part of the polledTimeout cleanup later

Serial.print("Errors/Bytes: ");
Serial.print(errorCount);
Serial.print(error_count);
Serial.print("/");
Serial.print(byteCount);
Serial.print(byte_count);
Serial.println(" per second");
errorCount = 0;
byteCount = 0;
secondTimer = millis() + 1000;
error_count = 0;
byte_count = 0;
second_timer = millis() + 1000;
}

if (millis() > nextPing) {
if (millis() > next_ping) {
digitalWrite(LED_PIN, LOW);
nextPing = millis() + 200;
next_ping = millis() + 200;
Serial.print("[MASTER] Sequence » ");
Serial.println(sequence);
digitalWrite(LED_PIN, HIGH);
sendMessage(sequence, "MESA");
if (expectPong) {
errorCount++;
if (expect_pong) {
error_count++;
}
expectPong = true;
expect_pong = true;
sequence++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seq++ => ++seq

}

Expand Down
29 changes: 12 additions & 17 deletions libraries/Wire/examples/i2c-P2P-slave/i2c-P2P-slave.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ uint16_t calculateCRC16(uint8_t *data, size_t length);
void sendEvent(int a, String msg);
void sendMessage(int seq, String msg);
void receiveEvent(const size_t howMany);
MessageData validateMessage(char* message_bytes);
MessageData validateMessage(char* bytes_message);

//

Expand All @@ -54,23 +54,21 @@ void setup() {

Serial.begin(230400);
while (!Serial); // if you want to wait for first messages
delay(2000);


// Enable internal pullup (there's always one from the master side)
//digitalWrite(SDA_PIN, HIGH);
//digitalWrite(SCL_PIN, HIGH);

pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);

Wire.pins(SDA_PIN, SCL_PIN);
Serial.println("I2C Slave begin...");
Wire.begin(I2C_SLAVE);
//Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)

//Wire.pins(SDA_PIN, SCL_PIN);
//Wire.begin(I2C_SLAVE);
Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)

delay(2000);
digitalWrite(LED_PIN, HIGH);

Serial.println("I2C Slave attaching callback...");
Wire.onReceive(receiveEvent);

Serial.print("I2C Slave started on address: 0x0");
Expand Down Expand Up @@ -103,10 +101,10 @@ MessageData encodeMessage(String bytes) {
}

// should be in shared header
MessageData validateMessage(char* message_bytes) {
bool validateMessage(const char* bytes, MessageData &tmp) {

MessageData tmp;
memcpy(&tmp, message_bytes, sizeof(tmp));
memcpy(&tmp, bytes_message, sizeof(tmp));

// Validate terminator
if (tmp.terminator == '.') {
Expand Down Expand Up @@ -145,8 +143,8 @@ MessageData validateMessage(char* message_bytes) {
Serial.print("tmp CRC16: ");
Serial.println(tmp.crc16, HEX);
Serial.print("SLAVE Incoming message: "); Serial.println(String(inmsg));
Serial.println("[ERROR] TODO: Request retransfer exception.");
return tmp;
Serial.println("[ERROR] Request retransfer exception.");
return false;
}

// Validate sequence number ( should be already updated from byteParser )
Expand All @@ -155,10 +153,7 @@ MessageData validateMessage(char* message_bytes) {
Serial.println((char*)tmp.data);
}

msgdata = tmp; // tmp is valid, assign to result address

return msgdata;

return true;
}

void receiveEvent(const size_t howMany) {
Expand Down
0