I'm doing my own UART processing and noticed that when doing Serial output during the ISR, the Teensy LC freezes. I'm trying to figure out why this is happening. I've created a contrived example. To use it, put serial data having 250kbaud, 8N2 onto RX3 (pin 7) (this is DMX data).
The code:
After running for a while, the program freezes after about 1500-2500 bytes. I'm trying to figure out the technical reason for this. What am I doing wrong or assuming? Do I need to flush the USB, for example, every so often? Etc.
The code:
Code:
// Tests Serial output in the UART handling ISR.
// Trying to diagnose a freeze on a Teensy LC.
// Data timeout, in milliseconds.
constexpr int kTimeout = 1000;
// UART ISR declaration.
void uart_rx_isr();
// UART-specific variables
auto irq = IRQ_UART2_STATUS;
auto port = &KINETISK_UART2;
auto &hwSerial = Serial3;
volatile bool latch = false; // Indicates that there's data
int count = 0; // The byte count
elapsedMillis lastDataTimer{0}; // For detecting data timeouts
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 4000) {
// Wait for Serial initialization
}
Serial.println("Starting.");
// Configure the LED and turn it off
pinMode(LED_BUILTIN, OUTPUT);
digitalWriteFast(LED_BUILTIN, LOW);
// Start the serial monitoring
hwSerial.begin(250000, SERIAL_8N2);
port->C2 = UART_C2_RE | UART_C2_RIE; // Receive only
attachInterruptVector(irq, &uart_rx_isr);
}
void loop() {
bool flag;
// Read and set `latch` atomically
NVIC_DISABLE_IRQ(irq);
flag = latch;
latch = false;
NVIC_ENABLE_IRQ(irq);
if (flag) {
digitalWriteFast(LED_BUILTIN, HIGH);
lastDataTimer = 0;
}
if (lastDataTimer >= kTimeout) {
digitalWriteFast(LED_BUILTIN, LOW);
}
}
// Receives and processes one byte.
void receiveByte(uint8_t b) {
latch = true;
Serial.printf("%d: hello\n", ++count);
}
// The UART ISR.
void uart_rx_isr() {
if ((port->S1 & UART_S1_RDRF) != 0) {
receiveByte(port->D);
}
}
After running for a while, the program freezes after about 1500-2500 bytes. I'm trying to figure out the technical reason for this. What am I doing wrong or assuming? Do I need to flush the USB, for example, every so often? Etc.
Last edited: