@all
I thought I would look at the loop timing, with how much time is being used, in particular how much time is added by calling yield() and where the time is used and how to make quicker...
Right now playing with simple sketch:
Code:
uint32_t start_time;
int loop_counter = 0;
elapsedMicros micros_timer = 0;
void setup() {
pinMode(13, OUTPUT);
while (!Serial) ;
Serial.begin(115200);
micros_timer = 0;
start_time = micros();
for (loop_counter = 0; loop_counter < 1000; loop_counter++) {
digitalWriteFast(13, !digitalReadFast(13));
}
uint32_t dt = micros()-start_time;
Serial.printf("For: %d %d\n", dt, micros_timer);
delay(1000); // give lots of time to print...
loop_counter = 0;
micros_timer = 0;
start_time = micros();
}
void loop() {
digitalWriteFast(13, !digitalReadFast(13));
loop_counter++;
if (loop_counter == 1000) {
uint32_t dt = micros()-start_time;
Serial.printf("Lop: %d %d\n", dt, micros_timer);
delay(1000); // give lots of time to print...
loop_counter = 0;
start_time = micros();
micros_timer = 0;
}
}
And finding either elapsedMicros is not working or I am not using it right...
Doing some comparisons of time to do the 1000 iterations of the digitalWriteFast(13, !digitalReadFast(13))
The one called in setup in a straight loop is taking: 26 us
Loop not calling yield: 72
Loop calling yield but yield does nothing: 96
Yield only calls EventResponder::runFromYield() : 137
Add quick check at start of runFromYield ( if (!firstYield) return: 102
Will add to my PR...
Now add back: if (usb_enable_serial_event_processing && Serial.available()) serialEvent(); Plus the no recursion code: that is Yield is:
Code:
void yield(void)
{
static uint8_t running=0;
if (running) return; // TODO: does this need to be atomic?
running = 1;
// USB Serail - Add hack to minimize impact...
if (usb_enable_serial_event_processing && Serial.available()) serialEvent();
running = 0;
EventResponder::runFromYield();
};
The (usb_enable_serial_event_processing is a hack I put in to the default SerialEvent which turns it off)
Run with: 145 If I enter something in Serial Monitor: 131
Now if I add back in the call to the HardwareSerial:
rocessSerialEvents();
right after the USB test code: It jumps back up to: 163 (Down to 150 if Character entered in USB...)
Now if I move the check to see if there are any SerialEvent functions active from the processSerialEvents
into making the call conditional within yield: 154(153 alternating) and 139 after usb key in...
So may also Make that change as well...
As for elapsed Micros I probably should take a look, here is extract of the last run:
Code:
For: 26 536877268
Lop: 153 536877268
Lop: 153 536877268
Lop: 153 536877268
Lop: 154 536877268
Lop: 154 536877268
Lop: 153 536877268
Lop: 139 536877268
Lop: 139 536877268
EDIT: elapsedMicros - issue was not with it, but simply using in Serial.printf.
That is if I change first print to: Serial.printf("For: %d %d\n", dt, (uint32_t)micros_timer);
It works fine