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

Fix clockCyclesPerMicrosecond Change from fixed value to current value #3993

Merged
merged 1 commit into from
Sep 30, 2020
Merged

Fix clockCyclesPerMicrosecond Change from fixed value to current value #3993

merged 1 commit into from
Sep 30, 2020

Conversation

tanakamasayuki
Copy link
Contributor

F_CPU is fixed at 240MHz.
In the case of other frequencies, the pulseIn function, etc. is not right.

Test

#define clockCyclesPerMicrosecondFix() ( (long int)getCpuFrequencyMhz() )
#define clockCyclesToMicrosecondsFix(a) ( (a) / clockCyclesPerMicrosecondFix() )
#define microsecondsToClockCyclesFix(a) ( (a) * clockCyclesPerMicrosecondFix() )

void setup() {
  Serial.begin(115200);
  delay(100);

  // It's OK
  setCpuFrequencyMhz(240);
  Serial.printf("clockCyclesPerMicrosecond() : %ld\n", clockCyclesPerMicrosecond() );
  Serial.printf("clockCyclesToMicroseconds(240) : %ld\n", clockCyclesToMicroseconds(240) );
  Serial.printf("microsecondsToClockCycles(1) : %ld\n\n", microsecondsToClockCycles(1) );

  // NG
  setCpuFrequencyMhz(160);
  Serial.printf("clockCyclesPerMicrosecond() : %ld\n", clockCyclesPerMicrosecond() );
  Serial.printf("clockCyclesToMicroseconds(160) : %ld\n", clockCyclesToMicroseconds(160) );
  Serial.printf("microsecondsToClockCycles(1) : %ld\n\n", microsecondsToClockCycles(1) );

  // Fix
  setCpuFrequencyMhz(240);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(240) : %ld\n", clockCyclesToMicrosecondsFix(240) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
  setCpuFrequencyMhz(160);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(160) : %ld\n", clockCyclesToMicrosecondsFix(160) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
  setCpuFrequencyMhz(80);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(80) : %ld\n", clockCyclesToMicrosecondsFix(80) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
  setCpuFrequencyMhz(40);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(40) : %ld\n", clockCyclesToMicrosecondsFix(40) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
  setCpuFrequencyMhz(20);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(20) : %ld\n", clockCyclesToMicrosecondsFix(20) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
  setCpuFrequencyMhz(10);
  Serial.printf("clockCyclesPerMicrosecondFix() : %ld\n", clockCyclesPerMicrosecondFix() );
  Serial.printf("clockCyclesToMicrosecondsFix(10) : %ld\n", clockCyclesToMicrosecondsFix(10) );
  Serial.printf("microsecondsToClockCyclesFix(1) : %ld\n\n", microsecondsToClockCyclesFix(1) );
}

void loop() {
}

output

clockCyclesPerMicrosecond() : 240
clockCyclesToMicroseconds(240) : 1
microsecondsToClockCycles(1) : 240

clockCyclesPerMicrosecond() : 240
clockCyclesToMicroseconds(160) : 0
microsecondsToClockCycles(1) : 240

clockCyclesPerMicrosecondFix() : 240
clockCyclesToMicrosecondsFix(240) : 1
microsecondsToClockCyclesFix(1) : 240

clockCyclesPerMicrosecondFix() : 160
clockCyclesToMicrosecondsFix(160) : 1
microsecondsToClockCyclesFix(1) : 160

clockCyclesPerMicrosecondFix() : 80
clockCyclesToMicrosecondsFix(80) : 1
microsecondsToClockCyclesFix(1) : 80

clockCyclesPerMicrosecondFix() : 40
clockCyclesToMicrosecondsFix(40) : 1
microsecondsToClockCyclesFix(1) : 40

clockCyclesPerMicrosecondFix() : 20
clockCyclesToMicrosecondsFix(20) : 1
microsecondsToClockCyclesFix(1) : 20

clockCyclesPerMicrosecondFix() : 10
clockCyclesToMicrosecondsFix(10) : 1
microsecondsToClockCyclesFix(1) : 10

@me-no-dev me-no-dev merged commit a55265f into espressif:master Sep 30, 2020
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