PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9KB

  1. // -------------------------------------------------------------------------------------------
  2. // Basic Master
  3. // -------------------------------------------------------------------------------------------
  4. //
  5. // This creates a simple I2C Master device which when triggered will send/receive a text
  6. // string to/from a Slave device. It is intended to pair with a Slave device running the
  7. // basic_slave sketch.
  8. //
  9. // Pull pin12 input low to send.
  10. // Pull pin11 input low to receive.
  11. //
  12. // This example code is in the public domain.
  13. //
  14. // -------------------------------------------------------------------------------------------
  15. #include <i2c_t3.h>
  16. // Memory
  17. #define MEM_LEN 256
  18. char databuf[MEM_LEN];
  19. int count;
  20. void setup()
  21. {
  22. pinMode(LED_BUILTIN,OUTPUT); // LED
  23. digitalWrite(LED_BUILTIN,LOW); // LED off
  24. pinMode(12,INPUT_PULLUP); // Control for Send
  25. pinMode(11,INPUT_PULLUP); // Control for Receive
  26. // Setup for Master mode, pins 18/19, external pullups, 400kHz, 200ms default timeout
  27. Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
  28. Wire.setDefaultTimeout(200000); // 200ms
  29. // Data init
  30. memset(databuf, 0, sizeof(databuf));
  31. count = 0;
  32. Serial.begin(115200);
  33. }
  34. void loop()
  35. {
  36. uint8_t target = 0x66; // target Slave address
  37. // Send string to Slave
  38. //
  39. if(digitalRead(12) == LOW)
  40. {
  41. digitalWrite(LED_BUILTIN,HIGH); // LED on
  42. // Construct data message
  43. sprintf(databuf, "Data Message #%d", count++);
  44. // Print message
  45. Serial.printf("Sending to Slave: '%s' ", databuf);
  46. // Transmit to Slave
  47. Wire.beginTransmission(target); // Slave address
  48. Wire.write(databuf,strlen(databuf)+1); // Write string to I2C Tx buffer (incl. string null at end)
  49. Wire.endTransmission(); // Transmit to Slave
  50. // Check if error occured
  51. if(Wire.getError())
  52. Serial.print("FAIL\n");
  53. else
  54. Serial.print("OK\n");
  55. digitalWrite(LED_BUILTIN,LOW); // LED off
  56. delay(100); // Delay to space out tests
  57. }
  58. // Read string from Slave
  59. //
  60. if(digitalRead(11) == LOW)
  61. {
  62. digitalWrite(LED_BUILTIN,HIGH); // LED on
  63. // Print message
  64. Serial.print("Reading from Slave: ");
  65. // Read from Slave
  66. Wire.requestFrom(target, (size_t)MEM_LEN); // Read from Slave (string len unknown, request full buffer)
  67. // Check if error occured
  68. if(Wire.getError())
  69. Serial.print("FAIL\n");
  70. else
  71. {
  72. // If no error then read Rx data into buffer and print
  73. Wire.read(databuf, Wire.available());
  74. Serial.printf("'%s' OK\n",databuf);
  75. }
  76. digitalWrite(LED_BUILTIN,LOW); // LED off
  77. delay(100); // Delay to space out tests
  78. }
  79. }