Fix wait periods for MCUs with tick period > 1ms#1
Open
kindlehl wants to merge 1 commit intotolacika:mainfrom
Open
Fix wait periods for MCUs with tick period > 1ms#1kindlehl wants to merge 1 commit intotolacika:mainfrom
kindlehl wants to merge 1 commit intotolacika:mainfrom
Conversation
Added function wait_lcd_cycles() to replace vTaskDelay with pdMS_TO_TICKS() pdMS_TO_TICKS(ms) returns 0 when ms < tick period. If the tick period is 2 seconds, vTaskDelay gets 0, which invokes the scheduler and gives another task the chance to run. If there is no other task, the calling task just gets rescheduled immediately. This is not a long enough delay and leads to inconsistent behavior and garbled text most often on my LCD. wait_lcd_cycles() should be used as it makes the task wait without invoking the scheduler. It should only be used for brief pauses in execution for a few microseconds.
kindlehl
commented
Mar 31, 2026
| }; | ||
| ESP_ERROR_CHECK(i2c_transmit_with_enable_toggle(nibbles[0])); | ||
| ESP_ERROR_CHECK(i2c_transmit_with_enable_toggle(nibbles[1])); | ||
| vTaskDelay(pdMS_TO_TICKS(1)); |
Author
There was a problem hiding this comment.
with the wait inside i2c_transmit_with_enable_toggle, this wait is not really doing anything.
Author
|
upon further debugging and deeper reading of datasheets, I think this is a skill issue on my part. I believe my real problem was a classic multitasking issue where there was a race condition causing two tasks to try writing to the i2c bus and clobbering the other's data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This was a nice little example for me to jump off of. I just noticed that my LCD started spewing garbled text or wouldn't display anything after I commented out all of the
ESP_LOGIcalls. It turns out that the time that the ESP was spending writing logs over serial was enough delay to allow the LCD to finish executing its commands. Without that serial write period, it was going to fast and leaving the LCD in a bad state. On my board, pdMS_TO_TICKS(1) returns 0, so I was not getting any wait time.This function isn't perfect. It doesn't actually match the lcd cycles, but it should give a reasonable delay. I've tested it with all the serial stuff commented out and it handles double-clears and long writes just fine.
Added function wait_lcd_cycles() to replace vTaskDelay with pdMS_TO_TICKS()
pdMS_TO_TICKS(ms) returns 0 when ms < tick period. If the tick period is 2 seconds, vTaskDelay gets 0, which invokes the scheduler and gives another task the chance to run. If there is no other task, the calling task just gets rescheduled immediately. This is not a long enough delay and leads to inconsistent behavior and garbled text most often on my LCD.
wait_lcd_cycles() should be used as it makes the task wait without invoking the scheduler. It should only be used for brief pauses in execution for a few microseconds.