Skip to content
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

delayNanoseconds returns too early #646

Open
slenius opened this issue Jun 1, 2022 · 0 comments
Open

delayNanoseconds returns too early #646

slenius opened this issue Jun 1, 2022 · 0 comments

Comments

@slenius
Copy link

slenius commented Jun 1, 2022

Board: Teensy 4.1
Arduino: 1.8.19
Teensyduino: 1.56
All options default

When you call delayNanoseconds(x) where x > ~470000, it returns early. I suspect there's an overflow with this line:

uint32_t cycles =   ((F_CPU_ACTUAL>>16) * nsec) / (1000000000UL>>16);
delayNanoseconds(460000) -> cycles = 276006
delayNanoseconds(470000) -> cycles = 516

Complete code that illustrates problem:

#include <stdint.h>


void setup() {
  Serial.begin(9600);
}

static constexpr int n_loops = 10000;
static int i = 0;

static uint32_t start_time_micros, end_time_micros, delta_time_micros, delay_micros, delay_nanos;


void loop() {
  if(i == 0){
    Serial.printf("Start test...\n");
  }
  if(i < n_loops){
    delay_micros = max(i, 1);
    delay_nanos = delay_micros * 1000;
    start_time_micros = micros();
    //delayMicroseconds(delay_micros); // This test passes
    delayNanoseconds(delay_nanos); // This test fails
    end_time_micros = micros();
    delta_time_micros = end_time_micros - start_time_micros;
  
    if (delta_time_micros < (delay_micros - 1)){
      Serial.printf("delay_micros = %d us\tdelta_time_micros = %lu us\n", delay_micros, delta_time_micros);
    }
  }
  if(i == n_loops){
    Serial.printf("Done!\n");
  }
  i++;
}
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

No branches or pull requests

1 participant