10000 Issue #2958 Correct calculation of subticks being returned from port_get_raw_ticks() for esp32s2 by DavePutz · Pull Request #3020 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Issue #2958 Correct calculation of subticks being returned from port_get_raw_ticks() for esp32s2 #3020

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 3 commits into from
Jun 9, 2020

Conversation

DavePutz
Copy link
Collaborator
@DavePutz DavePutz commented Jun 8, 2020

The calculations being done in port_get_raw_ticks basically insured that only seconds were being returned. Since the tv_usec ranges from 1 to 1000000, and ticks are 1/1024 of a second, the correct calculation would be to divide usec by 1024, not 32768 and then 32. A quick script to show the issue and fix:

import time
print (time.monotonic())
for a in range(10):
    time.sleep(0.9)
print (time.monotonic())
for a in range(10):
    time.sleep(1.1)
print (time.monotonic())

Before the fix the first loop will run for 10 seconds and the second loop for 20 seconds.
After the fix the timings should be correct.

DavePutz added 2 commits June 8, 2020 10:42
Issue #2958 . Correct calculation of usec back to ticks in port_get_raw_ticks().
@tannewt tannewt self-requested a review June 8, 2020 21:36
@tannewt tannewt added bug espressif applies to multiple Espressif chips labels Jun 8, 2020
Copy link
Member
@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I don't think it's right as-is.

@@ -118,11 +118,12 @@ uint32_t port_get_saved_word(void) {
uint64_t port_get_raw_ticks(uint8_t* subticks) {
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
uint64_t all_subticks = (uint64_t)tv_now.tv_usec / 32768;
// convert usec back to ticks
uint64_t all_subticks = (uint64_t)tv_now.tv_usec / 1024;
Copy link
Member

Choose a reason f 10000 or hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct because all_subticks should range from 0 to 32767 which would be achieved by dividing 1000000 by 30.517. We can approximate this with the change below. Line 125 should be preserved.

Suggested change
uint64_t all_subticks = (uint64_t)tv_now.tv_usec / 1024;
uint64_t all_subticks = (uint64_t)(tv_now.tv_usec * 2) / 71;

Copy link
Collaborator Author
@DavePutz DavePutz Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With tv_usec having a value between 1 and 1 million, multiplying it by 2 and dividing by 71 give us values from 1 to 28168. Since a tick is 1/1024th of a second, that could add to the return value by up to 27 seconds. It seems correct to me to multiply seconds by 1024, and add subticks of 1000000 divided by 1024, which is what the original fix did. That would give us subticks of 1 to 1000, which when added to seconds*1024 would be the right number of ticks.
Testing of the proposed modified fix showed times much greater than the call to sleep() requested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are you measuring the sleep duration? I think there is more going on with FreeRTOS that makes it harder to measure.

The all_subticks should be divided by 32 before being added to seconds * 1024 because one tick is 32 subticks. (Most boards have 32.768khz crystals which is 1024 * 32)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the script above to measure sleep time, running 10 sleep()s in a loop and displaying the times before and after. I realized that when I tested the suggested correction I did not restore the division by 32 in the return statement. Doing that now yields correct results, and I have updated the pull request accordingly.

changed subticks calculation to give a range from 0 to 32767.
Copy link
Member
@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Definitely an improvement.

@tannewt tannewt merged commit 9b8d648 into adafruit:master Jun 9, 2020
@jerryneedell
Copy link
Collaborator

Yay! -my "fast" blink works now -- thank you @DavePutz

@DavePutz DavePutz deleted the issue2958 branch December 30, 2020 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug espressif applies to multiple Espressif chips
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0