Wanting to know when GPS signal transmission started I put an _isr on the Rx line to capture message start bit. There was a lot of other code running calcs and the time to process/receive the first GPS byte was arbitrary.
I factored that code into a self testing sample here for Serial1 Rx wired to Tx. That code used CycleCounter as it is more accurate and faster to read the micros or millis so it is here:
Code:
#define qBlink() (GPIOC_PTOR = 32) // Pin13 on T3.x & LC // requires: pinMode(LED_BUILTIN, OUTPUT);
volatile uint32_t ccTimeRcv;
#define SOME_BAUD 115200
#define PIN_SRX 0
// 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
qBlink(); // 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( ) {
// Enable CPU Cycle Counter - if desired for time base
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
// REQUIRED CODE
// Attach Serial_RX pin for interrupt
attachInterrupt( PIN_SRX, serialrx_isr, FALLING);
// Do app specific setup here including Serial port .begin
Serial1.begin(SOME_BAUD); // REQUIRED CODE
pinMode( LED_BUILTIN, OUTPUT );
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;
ccTimeSend = ARM_DWT_CYCCNT;
// This sample sends to itself to show functionality
Serial1.write( ' ' );
}
if ( Serial1.available() ) {
while ( Serial1.available() ) 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
{
// (RE)Attach Serial_RX pin for interrupt
attachInterrupt( PIN_SRX, serialrx_isr, FALLING);
}
}
}
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();
}