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.

basic_echo.ino 2.8KB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // -------------------------------------------------------------------------------------------
  2. // Basic Echo
  3. // -------------------------------------------------------------------------------------------
  4. //
  5. // This creates a simple I2C Slave device which will read whatever data is sent to it
  6. // on Wire1 and output that same data on Wire. It will retain the data in memory and will
  7. // send it back to a Master device if requested.
  8. //
  9. // This code demonstrates non-blocking nested operation - calling Wire in a non-blocking way
  10. // inside of Wire1 receive ISR.
  11. //
  12. // Wire1 bus is intended to pair with a Master device running the basic_master sketch.
  13. // Wire bus is intended to pair with a Slave device running the basic_slave sketch.
  14. //
  15. // This code is setup for a Teensy 3.5/3.6 device.
  16. //
  17. // This example code is in the public domain.
  18. //
  19. // -------------------------------------------------------------------------------------------
  20. #include <i2c_t3.h>
  21. // Function prototypes
  22. void receiveEvent(size_t count);
  23. void requestEvent(void);
  24. // Memory
  25. #define MEM_LEN 256
  26. uint8_t databuf[MEM_LEN];
  27. volatile uint8_t received;
  28. //
  29. // Setup
  30. //
  31. void setup()
  32. {
  33. pinMode(LED_BUILTIN,OUTPUT); // LED
  34. // Setup for Master mode, pins 18/19, external pullups, 400kHz, 200ms default timeout
  35. Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
  36. Wire.setDefaultTimeout(200000); // 200ms
  37. // Setup for Slave mode, address 0x66, pins 37/38, external pullups, 400kHz
  38. Wire1.begin(I2C_SLAVE, 0x66, I2C_PINS_37_38, I2C_PULLUP_EXT, 400000); // 3.5/3.6
  39. // Data init
  40. received = 0;
  41. memset(databuf, 0, sizeof(databuf));
  42. // register events
  43. Wire1.onReceive(receiveEvent);
  44. Wire1.onRequest(requestEvent);
  45. Serial.begin(115200);
  46. }
  47. void loop()
  48. {
  49. // print received data - this is done in main loop to keep time spent in I2C ISR to minimum
  50. if(received)
  51. {
  52. digitalWrite(LED_BUILTIN,HIGH);
  53. Serial.printf("Echoing data received: '%s'\n", databuf);
  54. received = 0;
  55. digitalWrite(LED_BUILTIN,LOW);
  56. }
  57. }
  58. //
  59. // handle Rx Event (incoming I2C data)
  60. //
  61. void receiveEvent(size_t count)
  62. {
  63. digitalWrite(LED_BUILTIN,HIGH); // LED on
  64. Wire1.read(databuf, count); // copy Rx data to databuf
  65. Wire.finish(); // finish any prev non-blocking Tx
  66. Wire.beginTransmission(0x66); // init for Tx send
  67. Wire.write(databuf, count); // send to Tx buffer
  68. Wire.sendTransmission(); // Send Tx data to Slave (non-blocking)
  69. received = count; // set received flag to count, this triggers print in main loop
  70. digitalWrite(LED_BUILTIN,LOW); // LED off
  71. }
  72. //
  73. // handle Tx Event (outgoing I2C data)
  74. //
  75. void requestEvent(void)
  76. {
  77. Wire1.write(databuf, MEM_LEN); // fill Tx buffer (send full mem)
  78. }