Skip to content

Allow for higher resolution with micros() #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2021

Conversation

sebromero
Copy link
Collaborator

@sebromero sebromero commented Mar 9, 2021

Makes micros() use a higher resolution timer while keep using a low power timer for millis().
Test sketch:

unsigned long start, end;

void runTest(){
  for(int i = 1; i <= 64; ++i){
    start = micros();
    delayMicroseconds(i);
    end = micros();
    Serial.print("Delay: " + String(i) + " measured: ");
    Serial.println(end - start); 
  }
  Serial.println("-----------------------");
}

void setup() {
  Serial.begin(115200);  
  while(!Serial);

  Serial.println(millis());
  runTest();
  Serial.println(millis());
}

void loop() {}

@sebromero sebromero requested a review from facchinm March 9, 2021 10:19
@sebromero sebromero changed the title Low power timer Make low power timer optional Mar 9, 2021
@sebromero sebromero changed the title Make low power timer optional Make low power timer opt-in Mar 9, 2021
@sebromero
Copy link
Collaborator Author

@facchinm We could also make low power the default option of course.

@github-actions
Copy link

github-actions bot commented Mar 9, 2021

Memory usage change @ cebd497

Board flash % RAM for global variables %
arduino:mbed:envie_m4 🔺 +64 - +64 +0.01 - +0.01 🔺 +24 - +32 +0.01 - +0.01
arduino:mbed:envie_m7 🔺 +64 - +128 +0.01 - +0.02 🔺 +32 - +32 +0.01 - +0.01
arduino:mbed:nano33ble 🔺 +64 - +128 +0.01 - +0.01 🔺 +32 - +32 +0.01 - +0.01
Click for full report table
Board libraries/Scheduler/examples/MultipleBlinks
flash
% libraries/Scheduler/examples/MultipleBlinks
RAM for global variables
% libraries/doom/examples/Doom
flash
% libraries/doom/examples/Doom
RAM for global variables
% libraries/KernelDebug/examples/KernelDebug
flash
% libraries/KernelDebug/examples/KernelDebug
RAM for global variables
% libraries/Portenta_SDCARD/examples/TestSDCARD
flash
% libraries/Portenta_SDCARD/examples/TestSDCARD
RAM for global variables
% libraries/Portenta_Video/examples/Envie_video_coreboot
flash
% libraries/Portenta_Video/examples/Envie_video_coreboot
RAM for global variables
% libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo
flash
% libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo
RAM for global variables
% libraries/Portenta_System/examples/PortentaH7_updateBootloader
flash
% libraries/Portenta_System/examples/PortentaH7_updateBootloader
RAM for global variables
% libraries/ThreadDebug/examples/ThreadDebug
flash
% libraries/ThreadDebug/examples/ThreadDebug
RAM for global variables
% libraries/USBHOST/examples/KeyboardController
flash
% libraries/USBHOST/examples/KeyboardController
RAM for global variables
% libraries/USBHOST/examples/Shell
flash
% libraries/USBHOST/examples/Shell
RAM for global variables
% libraries/WiFi/examples/PortentaWiFiFirmwareUpdater
flash
% libraries/WiFi/examples/PortentaWiFiFirmwareUpdater
RAM for global variables
% libraries/PDM/examples/PDMSerialPlotter
flash
% libraries/PDM/examples/PDMSerialPlotter
RAM for global variables
%
arduino:mbed:envie_m4 64 0.01 24 0.01 64 0.01 32 0.01 64 0.01 24 0.01 64 0.01 24 0.01 64 0.01 32 0.01
arduino:mbed:envie_m7 128 0.02 32 0.01 128 0.02 32 0.01 64 0.01 32 0.01 64 0.01 32 0.01 64 0.01 32 0.01 128 0.02 32 0.01 128 0.02 32 0.01 64 0.01 32 0.01 64 0.01 32 0.01 128 0.02 32 0.01 128 0.02 32 0.01
arduino:mbed:nano33ble 64 0.01 32 0.01 128 0.01 32 0.01 128 0.01 32 0.01
Click for full report CSV
Board,libraries/Scheduler/examples/MultipleBlinks<br>flash,%,libraries/Scheduler/examples/MultipleBlinks<br>RAM for global variables,%,libraries/doom/examples/Doom<br>flash,%,libraries/doom/examples/Doom<br>RAM for global variables,%,libraries/KernelDebug/examples/KernelDebug<br>flash,%,libraries/KernelDebug/examples/KernelDebug<br>RAM for global variables,%,libraries/Portenta_SDCARD/examples/TestSDCARD<br>flash,%,libraries/Portenta_SDCARD/examples/TestSDCARD<br>RAM for global variables,%,libraries/Portenta_Video/examples/Envie_video_coreboot<br>flash,%,libraries/Portenta_Video/examples/Envie_video_coreboot<br>RAM for global variables,%,libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo<br>flash,%,libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo<br>RAM for global variables,%,libraries/Portenta_System/examples/PortentaH7_updateBootloader<br>flash,%,libraries/Portenta_System/examples/PortentaH7_updateBootloader<br>RAM for global variables,%,libraries/ThreadDebug/examples/ThreadDebug<br>flash,%,libraries/ThreadDebug/examples/ThreadDebug<br>RAM for global variables,%,libraries/USBHOST/examples/KeyboardController<br>flash,%,libraries/USBHOST/examples/KeyboardController<br>RAM for global variables,%,libraries/USBHOST/examples/Shell<br>flash,%,libraries/USBHOST/examples/Shell<br>RAM for global variables,%,libraries/WiFi/examples/PortentaWiFiFirmwareUpdater<br>flash,%,libraries/WiFi/examples/PortentaWiFiFirmwareUpdater<br>RAM for global variables,%,libraries/PDM/examples/PDMSerialPlotter<br>flash,%,libraries/PDM/examples/PDMSerialPlotter<br>RAM for global variables,%
arduino:mbed:envie_m4,64,0.01,24,0.01,64,0.01,32,0.01,64,0.01,24,0.01,64,0.01,24,0.01,64,0.01,32,0.01
arduino:mbed:envie_m7,128,0.02,32,0.01,128,0.02,32,0.01,64,0.01,32,0.01,64,0.01,32,0.01,64,0.01,32,0.01,128,0.02,32,0.01,128,0.02,32,0.01,64,0.01,32,0.01,64,0.01,32,0.01,128,0.02,32,0.01,128,0.02,32,0.01
arduino:mbed:nano33ble,64,0.01,32,0.01,,,,,,,,,,,,,,,,,,,,,,,,,128,0.01,32,0.01,,,,,,,,,,,,,128,0.01,32,0.01

@facchinm
Copy link
Member

facchinm commented Mar 9, 2021

What do you think about going high res for micros() and low power for millis() instead? It would turn into a much simpler patch with virtually no drawback ( micros() would stop counting during sleep but we can safely add an API to forcefully avoid sleeping when you need precise timing, wrapping sleep_manager_lock_deep_sleep() in something a bit more arduinish 🙂 )

@sebromero
Copy link
Collaborator Author

@facchinm Legit 😄 . I changed it. Is there a signification penalty in using two timers concurrently?
Am I right in the assumption that some devices may set DEVICE_LPTICKER=0 as they don't support a LP ticker? I added a fallback for that case.
I'd probably wait with adding a function to avoid sleeping until we have a use case. I currently can't think of one where you need micro seconds resolution during sleep.

@sebromero sebromero changed the title Make low power timer opt-in Allow for higher resolution with micros() Mar 9, 2021
@github-actions
Copy link

github-actions bot commented Mar 9, 2021

Memory usage change @ de988ef

Board flash % RAM for global variables %
arduino:mbed:envie_m4 🔺 +64 - +128 +0.01 - +0.01 🔺 +24 - +24 +0.01 - +0.01
arduino:mbed:envie_m7 🔺 +64 - +128 +0.01 - +0.02 🔺 +24 - +24 0.0 - 0.0
arduino:mbed:nano33ble 🔺 +64 - +128 +0.01 - +0.01 🔺 +24 - +24 +0.01 - +0.01
Click for full report table
Board libraries/Scheduler/examples/MultipleBlinks
flash
% libraries/Scheduler/examples/MultipleBlinks
RAM for global variables
% libraries/doom/examples/Doom
flash
% libraries/doom/examples/Doom
RAM for global variables
% libraries/KernelDebug/examples/KernelDebug
flash
% libraries/KernelDebug/examples/KernelDebug
RAM for global variables
% libraries/Portenta_SDCARD/examples/TestSDCARD
flash
% libraries/Portenta_SDCARD/examples/TestSDCARD
RAM for global variables
% libraries/Portenta_Video/examples/Envie_video_coreboot
flash
% libraries/Portenta_Video/examples/Envie_video_coreboot
RAM for global variables
% libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo
flash
% libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo
RAM for global variables
% libraries/Portenta_System/examples/PortentaH7_updateBootloader
flash
% libraries/Portenta_System/examples/PortentaH7_updateBootloader
RAM for global variables
% libraries/ThreadDebug/examples/ThreadDebug
flash
% libraries/ThreadDebug/examples/ThreadDebug
RAM for global variables
% libraries/USBHOST/examples/KeyboardController
flash
% libraries/USBHOST/examples/KeyboardController
RAM for global variables
% libraries/USBHOST/examples/Shell
flash
% libraries/USBHOST/examples/Shell
RAM for global variables
% libraries/WiFi/examples/PortentaWiFiFirmwareUpdater
flash
% libraries/WiFi/examples/PortentaWiFiFirmwareUpdater
RAM for global variables
% libraries/PDM/examples/PDMSerialPlotter
flash
% libraries/PDM/examples/PDMSerialPlotter
RAM for global variables
%
arduino:mbed:envie_m4 64 0.01 24 0.01 64 0.01 24 0.01 128 0.01 24 0.01 64 0.01 24 0.01 64 0.01 24 0.01
arduino:mbed:envie_m7 64 0.01 24 0.0 64 0.01 24 0.0 64 0.01 24 0.0 64 0.01 24 0.0 64 0.01 24 0.0 128 0.02 24 0.0 128 0.02 24 0.0 64 0.01 24 0.0 64 0.01 24 0.0 128 0.02 24 0.0 128 0.02 24 0.0
arduino:mbed:nano33ble 64 0.01 24 0.01 128 0.01 24 0.01 128 0.01 24 0.01
Click for full report CSV
Board,libraries/Scheduler/examples/MultipleBlinks<br>flash,%,libraries/Scheduler/examples/MultipleBlinks<br>RAM for global variables,%,libraries/doom/examples/Doom<br>flash,%,libraries/doom/examples/Doom<br>RAM for global variables,%,libraries/KernelDebug/examples/KernelDebug<br>flash,%,libraries/KernelDebug/examples/KernelDebug<br>RAM for global variables,%,libraries/Portenta_SDCARD/examples/TestSDCARD<br>flash,%,libraries/Portenta_SDCARD/examples/TestSDCARD<br>RAM for global variables,%,libraries/Portenta_Video/examples/Envie_video_coreboot<br>flash,%,libraries/Portenta_Video/examples/Envie_video_coreboot<br>RAM for global variables,%,libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo<br>flash,%,libraries/Portenta_System/examples/PortentaH7_getBootloaderInfo<br>RAM for global variables,%,libraries/Portenta_System/examples/PortentaH7_updateBootloader<br>flash,%,libraries/Portenta_System/examples/PortentaH7_updateBootloader<br>RAM for global variables,%,libraries/ThreadDebug/examples/ThreadDebug<br>flash,%,libraries/ThreadDebug/examples/ThreadDebug<br>RAM for global variables,%,libraries/USBHOST/examples/KeyboardController<br>flash,%,libraries/USBHOST/examples/KeyboardController<br>RAM for global variables,%,libraries/USBHOST/examples/Shell<br>flash,%,libraries/USBHOST/examples/Shell<br>RAM for global variables,%,libraries/WiFi/examples/PortentaWiFiFirmwareUpdater<br>flash,%,libraries/WiFi/examples/PortentaWiFiFirmwareUpdater<br>RAM for global variables,%,libraries/PDM/examples/PDMSerialPlotter<br>flash,%,libraries/PDM/examples/PDMSerialPlotter<br>RAM for global variables,%
arduino:mbed:envie_m4,64,0.01,24,0.01,64,0.01,24,0.01,128,0.01,24,0.01,64,0.01,24,0.01,64,0.01,24,0.01
arduino:mbed:envie_m7,64,0.01,24,0.0,64,0.01,24,0.0,64,0.01,24,0.0,64,0.01,24,0.0,64,0.01,24,0.0,128,0.02,24,0.0,128,0.02,24,0.0,64,0.01,24,0.0,64,0.01,24,0.0,128,0.02,24,0.0,128,0.02,24,0.0
arduino:mbed:nano33ble,64,0.01,24,0.01,,,,,,,,,,,,,,,,,,,,,,,,,128,0.01,24,0.01,,,,,,,,,,,,,128,0.01,24,0.01

sebromero and others added 2 commits March 16, 2021 15:29
@facchinm facchinm merged commit 2ac005f into arduino:master Mar 16, 2021
@sebromero sebromero deleted the low-power-timer branch March 17, 2021 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants