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
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:
Reprex void() code:
Reprex.h is located in librairies folder
And Teensy Global variable file (Global_Variable_Reprex.h) also located in libraries folder
=================================================================================
2-On Arduino Side
Arduino Nano code
Global variables Arduino file (located in libraries folder)
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
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
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