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.

87 lines
2.4KB

  1. // -------------------------------------------------------------------------------------------
  2. // Interrupt
  3. // -------------------------------------------------------------------------------------------
  4. //
  5. // This creates an I2C master device which will issue I2C commands from inside a periodic
  6. // interrupt (eg. similar to reading a sensor at regular time intervals). For this test the
  7. // Slave device will be assumed to be that given in the basic_slave sketch.
  8. //
  9. // The test will start when the Serial monitor opens.
  10. //
  11. // This example code is in the public domain.
  12. //
  13. // -------------------------------------------------------------------------------------------
  14. #include <i2c_t3.h>
  15. // Function prototypes
  16. void rwSlave(void);
  17. // Timer
  18. IntervalTimer slaveTimer;
  19. // Memory
  20. #define MEM_LEN 32
  21. char databuf[MEM_LEN];
  22. size_t count=0;
  23. uint8_t target=0x66;
  24. void setup()
  25. {
  26. pinMode(LED_BUILTIN,OUTPUT); // LED
  27. // Setup for Master mode, pins 18/19, external pullups, 400kHz, 10ms default timeout
  28. Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
  29. Wire.setDefaultTimeout(10000);
  30. // Data init
  31. memset(databuf, 0, sizeof(databuf));
  32. count = 0;
  33. // setup Serial and wait for monitor to start
  34. Serial.begin(115200);
  35. while(!Serial);
  36. // Start reading Slave device
  37. slaveTimer.begin(rwSlave,1000000); // 1s timer
  38. }
  39. void loop()
  40. {
  41. delay(1);
  42. }
  43. void rwSlave(void)
  44. {
  45. digitalWrite(LED_BUILTIN,HIGH); // pulse LED when reading
  46. // Construct data message
  47. sprintf(databuf, "Msg #%d", count++);
  48. // Transmit to Slave
  49. Wire.beginTransmission(target); // Slave address
  50. Wire.write(databuf,strlen(databuf)+1); // Write string to I2C Tx buffer (incl. string null at end)
  51. Wire.endTransmission(); // Transmit to Slave
  52. // Check if error occured
  53. if(Wire.getError())
  54. Serial.print("Write FAIL\n");
  55. else
  56. {
  57. // Read from Slave
  58. Wire.requestFrom(target, (size_t)MEM_LEN); // Read from Slave (string len unknown, request full buffer)
  59. // Check if error occured
  60. if(Wire.getError())
  61. Serial.print("Read FAIL\n");
  62. else
  63. {
  64. // If no error then read Rx data into buffer and print
  65. Wire.read(databuf, Wire.available());
  66. Serial.printf("'%s' OK\n",databuf);
  67. }
  68. }
  69. digitalWrite(LED_BUILTIN,LOW);
  70. }