Teensy3.6 keep rebooting when opening Serial Monitor (with reprex)

Status
Not open for further replies.

JORIS

Member
Dear all,

Context:
I'm performing an i2c communication between my Teensy 3.6 (MASTER) and an Arduino Nano (SLAVE)

Conf:
- Teensy3.6: I2C PIN 18 and 19 used with i2c_t3 library / Serial = 9600 baud
- Arduino Nano : I2C PIN A4 and A5 with Wire.h library

Goal of the code
Teensy performs a 4 bytes (float) WireRequest to Arduino which reply and teensy print the float value replied.

Specific stuff
In order to have a more ergonomic code, I've decided to include this request in my own void (stored as a .h file in my library folder). I also store my Global variables in another .h file(see the Reprex after)

Problem
The problem is: when I open my Serial Monitor to observe what is received, the Teensy keep rebooting and TeensyMonitor get closed / opened again / closed etc.. while rebooting

screenshot.jpg

Investigation;
I've identified the problem around the i2c communication in my void in the Teensy loop() code. When I comment i2c request it's Okay on Serial monitor (but obviously there is no i2c transfert anymore...)

REPREX
Here is representative/reproductible example of my problem:


============================================================================================
On Teensy side

1- Teensy code:

Code:
#include <i2c_t3.h>
#include <Global_Variable_Reprex.h>
#include <Reprex.h>


void setup() {
 
    pinMode(LED_BUILTIN,OUTPUT);    // LED
    digitalWrite(LED_BUILTIN,HIGH);  // LED on

    
Serial.begin(9600);
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
//Wire.begin();
while(!Serial){};

digitalWrite(LED_BUILTIN,LOW);  // LED off
delay(100);
}

void loop() {

digitalWrite(LED_BUILTIN,HIGH);
Reprex(&NBRE, ADDRESS_SLAVE,NB_BYTES_I2C);

delay(500);

digitalWrite(LED_BUILTIN,LOW);

Serial.print ("NBRE = "); Serial.println(NBRE);
Serial.println("============================================");
Serial.println();

delay(230);

}

Reprex void() code:
Reprex.h is located in librairies folder

Code:
void Reprex(float *NUMBER,
				    uint8_t adresse,
				    int nbre_bytes)

{

//=========
byte msg_received[NB_BYTES_I2C];

float_i2c number;


//===== Start i2c communication  =====
Wire.beginTransmission(adresse);
Wire.endTransmission(adresse);

//=====Launch a request to the slave=====

Wire.requestFrom(adresse, nbre_bytes);    

//===== In case of error in o2c communication with the slave  ======

if(Wire.getError())
 {
  Serial.print("FAIL\n");
 }

//===== When communication is correct with slave  ======

else
  {
   //~~~~~ While bytes are sent from slave ~~~~~
   if (Wire.available())   
   {

     for(int i=0; i<=(nbre_bytes-1); i--)
      {
        msg_received[i] = Wire.read();
      }

//~~~~~ Association 

 number.byte_val[0] = msg_received[0]; 
 number.byte_val[1] = msg_received[1]; 
 number.byte_val[2] = msg_received[2]; 
 number.byte_val[3] = msg_received[3];

//~~~~~ Update void parameter "*NUMBER" ~~~~~

*NUMBER = number.fval;

}//if

        }//else

}//void

And Teensy Global variable file (Global_Variable_Reprex.h) also located in libraries folder
Code:
union float_i2c
{
  byte byte_val[4];
  float fval;
};

union int_i2c
{
  byte byte_val[4];
  float intval;
};

float NBRE;


//===== ADDRESS EQUIPEMENT I2C =====
int ADDRESS_SLAVE = 11;
int NB_BYTES_I2C = 4;


=================================================================================
2-On Arduino Side

Arduino Nano code

Code:
#include <Global_Variables_Slave_Reprex.h>


void setup() {

//===== Initialisation Serial port=====
Serial.begin(9600);

//===== Initialisation i2c bus =====
Wire.begin(11);               

//===== Initialisation LED for blink=====
pinMode(13, OUTPUT);

//===== Initialisation i2c event request =====
Wire.onRequest(requestEvent); 



}

void loop() {
}


void requestEvent() 
 {      
  
      Wire.write(msg_i2c,NB_BYTES_I2C);
        digitalWrite(13, HIGH);   
        delay(5);               
        digitalWrite(13, LOW);    
        delay(1);
 }

Global variables Arduino file (located in libraries folder)
Code:
#include <Wire.h>

union float_i2c
{
  byte byte_val[4];
  float fval;
};

union int_i2c
{
  byte byte_val[4];
  float intval;
};


//===== Déclaration i2c parameter ======
int NB_BYTES_I2C = 4;          // 
byte msg_i2c[4];

I hope someone can help me with these elements and help me to understand what is going on
I tried to pass a twowire object in "Reprex" void parameter but without success. I guess the problem could be located around it...

Thanks for your time.

Joris
 
Try delay of the Wire.begin(). Teensy can be up and running before i2c Device is prepared.
Code:
void setup() {
 
    pinMode(LED_BUILTIN,OUTPUT);    // LED
    digitalWrite(LED_BUILTIN,HIGH);  // LED on

    
Serial.begin(9600);
while(!Serial){};
Serial.print("Online ...");
digitalWrite(LED_BUILTIN,LOW);  // LED off
delay(100);
Serial.print("Wire Begin ...");
[B]Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
//Wire.begin();
[/B]
Serial.print("Setup complete.");
}
 
Thanks for reply,

I tried your suggestion but it's still rebooting again and again after "setup Complete"

Here the screenshot (we see your print suggestion)
screen_shot_2.jpg
 
I I remove all the "Wire" functions in the void Reprex() (in Reprex.h) code works wery well
So, I'm almost sure now that my problem comes from the use of these i2c functions in my own function.

But how to manage this problem if I want performing i2c communication in my function Reprex?
 
In your reprex.h you have this

Code:
else
  {
   //~~~~~ While bytes are sent from slave ~~~~~
   if (Wire.available())   
   {

     for(int i=0; i<=(nbre_bytes-1); i--)
      {
        msg_received[i] = Wire.read();
      }

code. The for loop loops in the wrong direction. After i = 0 it will go to -2^31. Then you try to access msg_received with this index which might generate a hardware exception which might lead to the reboot?
 
Good work @luni - that i-- looks to be a problem. In reading it seemed it was dying as early as setup() - but the prints showed that is not the case.
 
Yes I confirm replace i-- by i++ in the for loop resolve the problem of reboot !

Thank you very much, I was too focus to see this detail ... thanks again to both of you !
 
Status
Not open for further replies.
Back
Top