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

Bit timing inaccurate with multi threading #71

Open
ssharks opened this issue Dec 26, 2024 · 3 comments
Open

Bit timing inaccurate with multi threading #71

ssharks opened this issue Dec 26, 2024 · 3 comments

Comments

@ssharks
Copy link

ssharks commented Dec 26, 2024

The bit timing is arranged with delayMicroseconds(500);. On a single threaded application this works reasonable stable although rounding errors tend to accumulate.
As soon as multiple threads are used on an ESP32 or ESP8266, this is no longer guaranteed to take 500 us. Depending on the OS scheduling this causes some communication to work, sometimes not to work.

Would it be better to use a timer for the bit timing? This guarantees the bit timing independent of scheduling and does not accumulate timing errors.

A workaround on the ESP32: Ensure all Opentherm communication is on 1 core, ensure the parrallel tasks are running on the other core.

@Laxilef
Copy link
Contributor

Laxilef commented Dec 30, 2024

delayMicroseconds() blocks the core, unlike delay() which calls vTaskDelay():

void ARDUINO_ISR_ATTR delayMicroseconds(uint32_t us) {
  uint64_t m = (uint64_t)esp_timer_get_time();
  if (us) {
    uint64_t e = (m + us);
    if (m > e) {  //overflow
      while ((uint64_t)esp_timer_get_time() > e) {
        NOP();
      }
    }
    while ((uint64_t)esp_timer_get_time() < e) {
      NOP();
    }
  }
}

@ssharks
Copy link
Author

ssharks commented Jan 30, 2025

Thank for the suggestion. vTaskDelay() does not work in this situation as the resolution for vTaskDelay() is either 1 or 10 ms, which prohibits from implementing a 0.5 ms delay.

A more accurate time is achievable using the timers of the ESP32, like with https://docs.espressif.com/projects/arduino-esp32/en/latest/api/timer.html

@Laxilef
Copy link
Contributor

Laxilef commented Jan 30, 2025

delayMicroseconds() does not call vTaskDelay().

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

2 participants