Hello, community!
I was working on a program for Teensy 4.1 and I met a strange behavior of the controller when working with UART. In my program, I need to process data from two UART channels and change the baud rates of these two channels “on the fly” depending on the user's actions. In one moment I noticed that I can change the baud rate in the channels only 6 times, when I try to do this for the seventh time, the Teensy hangs forever. The program stops working, and the USB channel to the terminal also falls off, and the only way to return the teensy to a working state is to reset the power or upload firmware to it by pressing a button on the teensy board.
Further investigation showed, that I can call these strings only 6 times in total before the Teensy hangs
regardless of which channel they are called for and regardless of whether the baud rate actually changes or remains constant. For example, if I change the baud rate 3 times for Serial1 and then 3 times for Serial2, then the next attempt to change the baud rate for any of the channels leads to the hanging of Teensy 4. I tried this for Teense4.0 and for teensy4.1, their behavior is the same.
Finally, I found that the program only hangs if I declare
and/or
even if they are empty!
If there is no declaration serialEventX() in the program I can change the baud rate for UARTs with no limitations.
So, can anybody explain what is it, and what should I do to force the program to work correctly?
Here is a program that demonstrates this behavior:
I was working on a program for Teensy 4.1 and I met a strange behavior of the controller when working with UART. In my program, I need to process data from two UART channels and change the baud rates of these two channels “on the fly” depending on the user's actions. In one moment I noticed that I can change the baud rate in the channels only 6 times, when I try to do this for the seventh time, the Teensy hangs forever. The program stops working, and the USB channel to the terminal also falls off, and the only way to return the teensy to a working state is to reset the power or upload firmware to it by pressing a button on the teensy board.
Further investigation showed, that I can call these strings only 6 times in total before the Teensy hangs
Code:
Serial1.end ();
Serial1.begin (baud);
Finally, I found that the program only hangs if I declare
Code:
void serialEvent1 (void) {}
Code:
void serialEvent2 (void){}
If there is no declaration serialEventX() in the program I can change the baud rate for UARTs with no limitations.
So, can anybody explain what is it, and what should I do to force the program to work correctly?
Here is a program that demonstrates this behavior:
Code:
// the setup function runs once when you press reset or power the board
uint8_t Key1 = 0;
uint8_t OldKey1 = 0;
void setup() {
Serial.begin(115200);
while (Serial) { ; }
Serial2.begin(115200);
pinMode(0, INPUT_PULLUP); // hardware button
pinMode(13, OUTPUT);
}
// the loop function runs over and over again until power down or reset
void loop() {
static uint8_t Counter = 0;
//--------
Key1 = digitalReadFast(0); // read the hardware button and call Serial2.end(); Serial2.begin(115200); on each pressing
if ((Key1 == 0) && (OldKey1 == 1)) { //
Serial2.end();
Counter++;
Serial.println(Counter);
Serial2.begin(115200);
}
OldKey1 = Key1;
//--------
delay(10);
if ((millis() % 320) < 160) { digitalWriteFast(13, 0); } // blink with led to see if the program works or not
else { digitalWriteFast(13, 1); }
}
void serialEvent2(void) // if I comment out this declaration the program starts to work correctly
{
}