// -------------------------------------------------------------------------------------------
// Basic Master
// -------------------------------------------------------------------------------------------
//
// This creates a simple I2C Master device which when triggered will send/receive a text
// string to/from a Slave device. It is intended to pair with a Slave device running the
// basic_slave sketch.
//
// Pull pin12 input low to send.
// Pull pin11 input low to receive.
//
// This example code is in the public domain.
//
// -------------------------------------------------------------------------------------------
#include <i2c_t3.h>
// Memory
#define MEM_LEN 256
char databuf[MEM_LEN];
int count;
void setup()
{
pinMode(LED_BUILTIN,OUTPUT); // LED
digitalWrite(LED_BUILTIN,LOW); // LED off
//pinMode(12,INPUT_PULLUP); // Control for Send
//pinMode(11,INPUT_PULLUP); // Control for Receive
pinMode(12,INPUT); // Control for Send
pinMode(11,INPUT); // Control for Receive
// Setup for Master mode, pins 18/19, external pullups, 400kHz, 200ms default timeout
Wire1.begin(I2C_MASTER, 0x00, I2C_PINS_29_30, I2C_PULLUP_EXT, 400000);
Wire1.setDefaultTimeout(200000); // 200ms
// Data init
memset(databuf, 0, sizeof(databuf));
count = 0;
Serial.begin(115200);
}
void loop()
{
uint8_t target = 0x66; // target Slave address
size_t idx;
//Set pin 12 LOW to send
delay(1000);
digitalWrite(12,LOW);
// if(digitalRead(12) == LOW)
// {
// Serial.printf("\nSet pin 12 LOW to send\n");
// }
// Send string to Slave
//
if(digitalRead(12) == LOW)
{
digitalWrite(LED_BUILTIN,HIGH); // LED on
// Construct data message
sprintf(databuf, "Data Message #%d", count++);
// Print message
Serial.printf("Sending to Slave: '%s' ", databuf);
// Transmit to Slave
Wire1.beginTransmission(target); // Slave address
for(idx = 0; idx <= strlen(databuf); idx++) // Write string to I2C Tx buffer (incl. string null at end)
Wire1.write(databuf[idx]);
Wire1.endTransmission(); // Transmit to Slave
// Check if error occured
if(Wire1.getError())
Serial.print("FAIL\n");
else
Serial.print("OK\n");
digitalWrite(LED_BUILTIN,LOW); // LED off
delay(100); // Delay to space out tests
}
//Set pin 12 HIGH to stop sending
//digitalWrite(12,HIGH);
// if(digitalRead(12) == HIGH)
// {
// Serial.printf("Set pin 12 HIGH to stop sending\n\n");
// }
//Set pin 11 LOW to receive
delay(1000);
digitalWrite(11,LOW);
// if(digitalRead(11) == LOW)
// {
// Serial.printf("Set pin 11 LOW to receive\n");
// }
//
// Read string from Slave
//
if(digitalRead(11) == LOW)
{
digitalWrite(LED_BUILTIN,HIGH); // LED on
// Print message
Serial.print("Reading from Slave: ");
// Read from Slave
Wire1.requestFrom(target, (size_t)MEM_LEN); // Read from Slave (string len unknown, request full buffer)
// Check if error occured
if(Wire1.getError())
Serial.print("FAIL\n");
else
{
// If no error then read Rx data into buffer and print
idx = 0;
while(Wire1.available())
databuf[idx++] = Wire1.readByte();
Serial.printf("'%s' OK\n",databuf);
}
digitalWrite(LED_BUILTIN,LOW); // LED off
delay(100); // Delay to space out tests
}
//Set pin 11 HIGH to stop recieving
//digitalWrite(11,HIGH);
// if(digitalRead(11) == HIGH)
// {
// Serial.printf("Set pin 11 HIGH to stop receiving\n\n");
// }
}