@Dimitri - Indeed, Serial2 { or Serial1 as below } configures UART hardware and connects software to interrupts to handle interrupt driven data transfer. And it seems PIN control is altered from what worked on T_3.x.

Originally Posted by
sbfreddie
You wouldn't have that code handy would you?
I have been struggling with this exact same issue for a long time. Usually I just use a long delay to wait for the Ublox to finish the 100 or so bytes that it sends over serial 1.
A long time ago I was looking in the core for teensy 3.x and the data sheet for the processor, I found an internal interrupt that can be enabled to do exactly this function, but this interrupt was not implemented in the core code and at the time I did not have sufficient mental horsepower to implement it.
Regards,
Ed
I have the code sample from larger old uNav thread - updated and working on T_3.5, but the same code fails to execute the same on T_4.1 ???? 
>> Can anyone give a clue why T_3.x code won't work the same on T_4.1? It seems Serial1 use on T_4.x prevents independent use of that pin as it worked on T_3.x. And doing the attach also breaks the UART function so no Serial1 data arrives.
It uses Serial1 - with Pin 0 wired to Pin 1 to loop the Serial RX to TX.
It was originally conceived and written on T_3.6 and worked there. But looking at it now, it won't run the same on a T_4.1 using the same _isr() process?
Code below - added debug pins to follow operation and the _isr() fires ONCE - but never again on T_4.1?
>> Note: only ran ONCE when the attachInterrupt() is before Serial1.begin() - if moved to end of setup() it never toggles PIN_DEBUG_AVAIL 18 as no data ever comes in on Serial1.
Code:
// T_4.1 shows first '>' char recevied - but never again?
C:\T_Drive\tCode\TIME\ISR_serRx\ISR_serRx.ino Oct 3 2022 14:07:20
>DELAY TIME Cycle Counts =4248135880 :: DELAY TIME AVG =354011323
Versus T_3.5 running as expected:
Code:
C:\T_Drive\tCode\TIME\ISR_serRx\ISR_serRx.ino Oct 3 2022 14:05:18
>DELAY TIME Cycle Counts =3583339571 :: DELAY TIME AVG =298611630
>DELAY TIME Cycle Counts =2544
>DELAY TIME Cycle Counts =1839
>DELAY TIME Cycle Counts =3214
>DELAY TIME Cycle Counts =2514
>DELAY TIME Cycle Counts =1699
>DELAY TIME Cycle Counts =3154
>DELAY TIME Cycle Counts =2349
>DELAY TIME Cycle Counts =1639
>DELAY TIME Cycle Counts =3054
>DELAY TIME Cycle Counts =2324 :: DELAY TIME AVG =2433
Setup enabled FALLING interrupt on Rx pin, when _isr() is called it disables the interrupt. The interrupt is enabled again after message is received and no more .available():
Code:
volatile uint32_t ccTimeRcv;
#define SOME_BAUD 58600
#define PIN_SRX 0
#define PIN_DEBUG_SEND 28
#define PIN_DEBUG_AVAIL 18
// REQUIRED CODE
void serialrx_isr() {
ccTimeRcv = ARM_DWT_CYCCNT; // Record time as desired
// Detach _isr() to avoid chatter/repeats during character transmission
detachInterrupt(PIN_SRX); // must re-attach interrupt on receive complete
/* disable the IRQ - would be for the PORT
* NVIC_DISABLE_IRQ(IRQ_FTM0);
delay(1000);
NVIC_ENABLE_IRQ(IRQ_FTM0);
*/
digitalToggle( LED_BUILTIN ); // TOGGLE LED to show functionality
}
// Code to trigger _isr() when incoming Serial message starts
// code marked as follows is all that is needed :: // REQUIRED CODE
// This sample uses Serial1 and requires a wire jumper pin 0 to pin 1
elapsedMillis WhenSend;
void setup( ) {
if ( ARM_DWT_CYCCNT == ARM_DWT_CYCCNT ) { // Enable CPU Cycle Counter - Already active on T_4.x
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
}
// REQUIRED CODE
attachInterrupt( PIN_SRX, serialrx_isr, FALLING); // Attach Serial_RX pin for interrupt
// Do app specific setup here including Serial port .begin
Serial1.begin(SOME_BAUD); // REQUIRED CODE
pinMode( LED_BUILTIN, OUTPUT );
pinMode( PIN_DEBUG_AVAIL, OUTPUT ); // debug monitor
pinMode( PIN_DEBUG_SEND, OUTPUT ); // debug monitor
while (!Serial && millis() < 5000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
while ( Serial1.available() ) Serial1.read(); // discard test char
WhenSend = 0;
}
uint32_t ccTimeSend;
uint32_t ccTimeSum = 0;
uint32_t ccii = 11; // set to discard first avg after Serial1 first xfer
void loop() {
if ( WhenSend > 1000 ) { // REMOVE this for external Serial1 connection
WhenSend = 0;
// attachInterrupt( PIN_SRX, serialrx_isr, FALLING); // (RE)Attach Serial_RX pin for interrupt
ccTimeSend = ARM_DWT_CYCCNT;
// This sample sends to itself to show functionality :: WIRE PIN 0 Rx to PIN 1 Tx for Serial1
digitalToggle( PIN_DEBUG_SEND ); // TOGGLE LED to show functionality
Serial1.print( ">" );
}
if ( Serial1.available() ) {
digitalToggle( PIN_DEBUG_AVAIL ); // TOGGLE LED to show functionality
while ( Serial1.available() ) {
Serial.print( (char)Serial1.read() ); // discard test char
}
DoStats(); // extraneous timing to monitor Tx to Rx time, varies with buad
// REQUIRED CODE
if ( 1 ) // Do this when data transmission is done to be ready for next Start
{
attachInterrupt( PIN_SRX, serialrx_isr, FALLING); // (RE)Attach Serial_RX pin for interrupt
}
}
}
void DoStats() {
Serial.print( "DELAY TIME Cycle Counts =" );
Serial.print( (ccTimeRcv - ccTimeSend));
ccii++;
ccTimeSum += (ccTimeRcv - ccTimeSend);
if ( ccii >= 10 ) {
Serial.print( " :: DELAY TIME AVG =" );
Serial.print( ccTimeSum / ccii );
ccii = 0;
ccTimeSum = 0;
Serial.println();
}
Serial.println();
}