Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: I2C Slave on Teensy 4

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    7

    I2C Slave on Teensy 4

    Hi. I've just started using a Teensy for the first time. I'm trying to get a Raspberry Pi to talk to a Teensy 4 over I2C. I've configured the Teensy to listen in slave mode but it doesn't seem to be listening. (See code below)

    I did a bit of digging and found this in WireIMXRT.cpp.
    Code:
    void TwoWire::begin(uint8_t address)
    {
    	// TODO: slave mode
    }
    Does this mean that the Teensy 4 doesn't support I2C slave mode? If not, what protocol would you suggest for communication to a Raspberry Pi?

    Here's my code for the Teensy. Is there something wrong with it?

    Code:
    #include <Arduino.h>
    #include <Wire.h>
    void i2c_slave_request_ISR();
    void i2c_slave_receive_ISR(int count);
    void setup() {
      Wire.onRequest(i2c_slave_request_ISR);
      Wire.onReceive(i2c_slave_receive_ISR);
      Wire.begin(8);
    
      Serial.begin(9600);
      Serial.println("Started");
    }
    void loop() {
      delay(1000);
    }
    void i2c_slave_request_ISR() {
      Serial.println("Got I2C Request");
    }
    void i2c_slave_receive_ISR(int count) {
      Serial.println(" Got I2C Receive Request");
    }
    thanks very much,
    Richard

  2. #2
    Junior Member
    Join Date
    Oct 2019
    Posts
    7
    To answer my own question...

    I2C Slave mode is definitely not supported by the current version of Wire. The Teensy 4 isn't supported by the i2c_t3 library either.

    I've started to write a new driver that extends Paul's version to add Slave mode. I'll be happy to share this with everyone as soon as it's usable. Please let me know if you're interested in using it. If you've got any other problems with I2C and the Teensy 4 I might be able to deal with them as well.

    cheers,
    Richard

  3. #3
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,273
    You are correct. I2C slave mode isn't supported yet on the T4. Currently in the wire lib there are places that identify that.

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,235
    FWIW, the NXP SDK for the 1060 has an I2C slave example
    boards/evkmimxrt1060/driver_examples/lpi2c/interrupt_b2b_transfer/slave/

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    7
    Thanks manitou. I didn't know about the SDK. I'll check it out.

  6. #6
    Junior Member
    Join Date
    Oct 2019
    Posts
    7
    I've created an I2C library which implements slave mode. You can find it here: https://github.com/Richard-Gemmell/teensy4_i2c.

    cheers,
    Richard

  7. #7
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,273
    @RichardG - have you thought about porting your changes over to the existing Wire library so there are not 2 I2C libraries?

  8. #8
    Junior Member
    Join Date
    Oct 2019
    Posts
    7
    Hi @mjs513.

    The library already includes a wrapper which implements the wire API. See https://github.com/Richard-Gemmell/t..._driver_wire.h. I've put some of the standard Wire examples in there so you can see it in use. e.g. https://github.com/Richard-Gemmell/t...ave_sender.ino

    The Arduino IDE will pick it up as "Teensy4 I2C" if you paste the entire project into your Arduino custom libraries folder and restart. This includes adding the examples to list under File | Examples.

    There are a couple of other interfaces for those who would rather avoid Wire. You can use the driver directly or use one of the classes that makes it easy to handle the usual I2C register pattern. e.g. https://github.com/Richard-Gemmell/t...ster_slave.ino

    I'll be happy to tweak it to make it more useful for other people. All comments and suggestions are welcome.

    cheers,
    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •