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

Thread: Teensy 3.6 Wire library question

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    1

    Teensy 3.6 Wire library question

    Hello,

    My project is to read multiple slave microcontrollers' data (up to 8 microcontrollers, 16 Bytes per each) using one master microcontroller.
    In the case of the slave microcontrollers, each slave reads 4 sensors data and the sampling rate of all four sensors' data is 40 Hz. Also, I have to use ATmega328P as a slave microcontroller.
    My code was working fine when I used an Arduino Uno as a master. However, if I run the same code on Teensy 3.6, the code did not work.

    One thing I have figured out is that on the contrary to Arduino, once Teensy is used as a master it can't be used as a slave.
    In other words, once Wire.beginTransmission(), Wire.write(), Wire.endTransmission() are used in Teensy, Wire.onReceive doesn't work at all.
    For example, in the below code, if I made "sendEvent(42);" as a comment, a master Teensy received data well from a slave ATmega328P.
    However, if "sendEvent" function is used, the Teensy can't receive data from a slave.
    Code:
    #include <Wire.h>
    #define num 4 //number of slaves
    #define reqbyte 16 // Bytes to be requested to slaves
    // flag
    byte stat = 0;
    const byte to_slave = 1;
    volatile byte from_slave = 0;
    // I2C Address
    #define MY_ADDRESS 25
    #define SLAVE_ADDRESS 42;
    
    void setup()
    { 
      while (!Serial);
      delay(1000);
      
      Serial.begin(115200);  
      delay(15);
     
      Wire.begin(MY_ADDRESS);     // Join I2C bus with address "MY_ADDRESS"
      delay(100);
      // Initialize communication between slaves
      Wire.onReceive(receiveEvent); // register event
      //sendEvent(42); // Initialize the first slave uC
      Serial.println("********START!*********");   
    }
    
    void loop()
    {
    }
    ///////////////////////////// Functions ////////////////////////////////////////
    void sendEvent(const byte ADDRESS){
      Wire.beginTransmission (ADDRESS);
      Wire.write (to_slave);
      Wire.endTransmission ();
    }
    
    void receiveEvent(int howMany)
     {
      from_slave = Wire.read(); //received event
      Serial.print(from_slave);
      Serial.println("Works!");   
     }
    Code:
    #include <Wire.h>
    
    // I2C Address
    #define MY_ADDRESS 42
    #define OTHER_ADDRESS 25
    
    // Slave status
    volatile boolean stat = false;
    // Send & receive event
    const byte from_master = 1;
    const byte to_master = 1; //should assign a unique number to each slave
    //////////////////////////////////////////////////////////////////////////////////////////////////
    void setup()
    {
      while (!Serial);
      delay(1000);
      
      Serial.begin(115200); // set up baud rate for serial 
      delay(15);
    
      Wire.begin(MY_ADDRESS);  // Activate I2C network
      Wire.onReceive(receiveEvent); // interrupt handler for when data is wanted
    }
    
    void loop() {
    sendEvent(OTHER_ADDRESS);
    delay(100);
    }
    ///////////////////////////// Functions ////////////////////////////////////////
    void sendEvent(const byte ADDRESS){
      Wire.beginTransmission (ADDRESS);
      Wire.write (to_master);
      Wire.endTransmission ();
    }
    
    void receiveEvent()
     {
      byte c = Wire.read(); //received event
      if (c == from_master){ 
      stat = true;
      }
      else {}
     }
    This is a bit critical because, in my code, a slave sends a byte to a master to set a flag to let the master know that 16 bytes data is ready to be transmitted.
    Is there a way to make Wire.onReceive functional in a master Teensy?
    Last edited by heesups; 03-17-2019 at 03:47 AM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,536
    No, I'm sorry, this isn't supported. Teensy's Wire library supports master only and slave only modes, but not mixing the 2 modes.

    I'll put this on my list of features to improve for the future. But I'm afraid the timeframe for supporting both modes is likely a very long way out.

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,205
    You might also look at the i2c_t3 library to see if it has the slave support you are looking for.

Posting Permissions

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