KurtE
Senior Member+
And now for another random idea
Yesterday I was playing around with SPI test case where I have a new member that once I launch an async SPI request, I can wait for it to complete. With this I was testing a flag and if not done call yield... And I was getting longer gaps of time than I would like, so I replaced the default yield in my sketch with:
So was wondering in cases like this should internal APIs call yield? Or should just loop or ...
Which made me wonder if it would make sense to maybe update the yield function... Right now on a Teensy 3.6/5 it will do a call to Serial.available for all 6 possible usarts and if there is anything on them will then call serialEvent... for each of these.
So if your code does not actually use the serialEvent construct it may be doing a lot of calls...
What I am wondering is can I automatically short circuit it? One idea that I am thinking of doing is:
1) create a global bitmask variable: maybe something like: uint8_t g_serial_events_active = 0;
2) Then update all of the Serial lass code, that when the user calls Serial.begin() maybe set bit 0 in the variable, if you call Serial1.begin() maybe set bit 1...
3) In yield code: change lines like: to something like: if ((Serial.available()) serialEvent();
to something like: if ((g_serial_events_active & 0x1) && Serial.available()) serialEvent();
4) change the default place holder SerialEvent implementations, to turn off their g_serial_events_active bit
like: void serialEvent1() {g_serial_events_active &= ~0x2;}
5) at the start of yield check to see if there are any handlers. So yield might look like:
So in theory if you don't use serialEvent and after any serial port that you use receives it's first character, the yield function will end up just begin a quick test of one variable and a return.
Thoughts?
Yesterday I was playing around with SPI test case where I have a new member that once I launch an async SPI request, I can wait for it to complete. With this I was testing a flag and if not done call yield... And I was getting longer gaps of time than I would like, so I replaced the default yield in my sketch with:
Code:
void yield() {
}
So was wondering in cases like this should internal APIs call yield? Or should just loop or ...
Which made me wonder if it would make sense to maybe update the yield function... Right now on a Teensy 3.6/5 it will do a call to Serial.available for all 6 possible usarts and if there is anything on them will then call serialEvent... for each of these.
So if your code does not actually use the serialEvent construct it may be doing a lot of calls...
What I am wondering is can I automatically short circuit it? One idea that I am thinking of doing is:
1) create a global bitmask variable: maybe something like: uint8_t g_serial_events_active = 0;
2) Then update all of the Serial lass code, that when the user calls Serial.begin() maybe set bit 0 in the variable, if you call Serial1.begin() maybe set bit 1...
3) In yield code: change lines like: to something like: if ((Serial.available()) serialEvent();
to something like: if ((g_serial_events_active & 0x1) && Serial.available()) serialEvent();
4) change the default place holder SerialEvent implementations, to turn off their g_serial_events_active bit
like: void serialEvent1() {g_serial_events_active &= ~0x2;}
5) at the start of yield check to see if there are any handlers. So yield might look like:
Code:
void yield(void)
{
static uint8_t running=0;
if (!g_serial_events_active || running) return; // TODO: does this need to be atomic?
running = 1;
if ((g_serial_events_active & 0x01) && Serial.available()) serialEvent();
if ((g_serial_events_active & 0x02) && Serial1.available()) serialEvent1();
if ((g_serial_events_active & 0x04) && Serial2.available()) serialEvent2();
if ((g_serial_events_active & 0x08) && Serial3.available()) serialEvent3();
#ifdef HAS_KINETISK_UART3
if ((g_serial_events_active & 0x10) && Serial4.available()) serialEvent4();
#endif
#ifdef HAS_KINETISK_UART4
if ((g_serial_events_active & 0x20) && Serial5.available()) serialEvent5();
#endif
#if defined(HAS_KINETISK_UART5) || defined (HAS_KINETISK_LPUART0)
if ((g_serial_events_active & 0x40) && Serial6.available()) serialEvent6();
#endif
running = 0;
};
So in theory if you don't use serialEvent and after any serial port that you use receives it's first character, the yield function will end up just begin a quick test of one variable and a return.
Thoughts?