Skip to content

Fix for issue #4937 - Implement minimum PulseOut time #4964

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 10 commits into from
Jul 16, 2021

Conversation

DavePutz
Copy link
Collaborator

@DavePutz DavePutz commented Jul 5, 2021

Since PWM is being used for PulseOut, there can never be a pulseout pulse that is less than the 'high' portion of a PWM pulse. The hang reported in issue #4937 was due to the timer being set to less than the time required for a single PWM pulse. This fix calculates a minimum pulseout pulse based on the selected PWM frequency and enforces no timer delays less than that value.

Fixes #4937.

@dmcomm
Copy link

dmcomm commented Jul 6, 2021

Hi, I guess you missed my update on the issue. And I posted another example just now.

@DavePutz
Copy link
Collaborator Author

DavePutz commented Jul 6, 2021

@dmcomm - There was an issue when the timer interval being used to time the pulses was too short. I've made a modification to correct that. If you have a chance could you test again (with the fix I was successful running your newest test script).

@dmcomm
Copy link

dmcomm commented Jul 6, 2021

Sounds good! I don't have the build system set up (my comment on this pull request was only based on the description).

@dhalbert
Copy link
Collaborator

dhalbert commented Jul 7, 2021

@dmcomm - We'll wait for your testing before merging.

@dmcomm
Copy link

dmcomm commented Jul 7, 2021

OK I'll get back to you on that.

@dhalbert
Copy link
Collaborator

dhalbert commented Jul 7, 2021

@dmcomm you can get the already-built artifacts here: (got to here from the checks):
https://github.com/adafruit/circuitpython/pull/4964/checks?check_run_id=3002465675

Click on the Artifacts drop down which is toward the right:
image

@dmcomm
Copy link

dmcomm commented Jul 8, 2021

  • My example with the countdown now runs to completion.
  • PulseOut crashes with the same symptoms if the first pulse is less than 40us, at both 38kHz and 500kHz.
  • I retested the first example in PulseOut.send sometimes leaving the line high afterwards #4908 but changed to 500kHz, and that was OK for as long as I watched it for.
  • If we want to guarantee that a pulse is output for too-small values, stretching to half a cycle doesn't achieve that. I expect a full cycle is needed.

@DavePutz
Copy link
Collaborator Author

DavePutz commented Jul 8, 2021 via email

@dmcomm
Copy link

dmcomm commented Jul 9, 2021

I meant that experimentally, a duration of 13us doesn't guarantee that a pulse will be output at 38kHz 50%. I thought that might be because the 13us window fell on the wrong phase of the PWM cycle, but PulseOut timing isn't that precise anyway, so maybe not.

@DavePutz
Copy link
Collaborator Author

Changes made to correct handling of short initial pulses as well as (hopefully) improve short pulse accuracy somewhat.

@dmcomm
Copy link

dmcomm commented Jul 12, 2021

Great, I haven't managed to crash it!

A full PWM cycle still seems to better. Requesting 1ms pulses at 1kHz gives a good result:

pulseout_crashfixed_1kHz_1000us

Requesting 500us gives unwanted behaviour like this:

pulseout_crashfixed_1kHz_500us

Similarly at 38kHz with 26/13us.

import array
import board
import time
import pulseio
import pwmio

pwm = pwmio.PWMOut(board.GP0, frequency=1000, duty_cycle=0x8000)
pulseOut = pulseio.PulseOut(pwm)
x = 500
pulses = array.array('H', [x,4*x] * 8)

while True:
    time.sleep(5)
    pulseOut.send(pulses)

@tannewt
Copy link
Member

tannewt commented Jul 15, 2021

@DavePutz Any idea what could cause the second screen? Should we just merge this as-is?

@dmcomm
Copy link

dmcomm commented Jul 15, 2021

I propose making the minimum pulse time a whole PWM cycle instead of half.

@DavePutz
Copy link
Collaborator Author

DavePutz commented Jul 15, 2021 via email

@DavePutz
Copy link
Collaborator Author

@dmcomm - I agree with your proposal; I changed the minimum pulse to be a whole PWM cycle and it helps. Also, I'm not sure if your excellent test scripts are just to show a condition; but if you want more accurate pulseout timings on the RP2040 you should consider changing the PWM duty_cycle value to 65535. That helps the pulse consistency, particularly for shorter pulses. @tannewt I think we can merge this one now.

@dmcomm
Copy link

dmcomm commented Jul 16, 2021

@DavePutz thanks! Yes, these scripts are just for illustration. I did previously try to do non-modulated output with a high frequency and 100% duty cycle, but my signal was too fast for that, so using PIO now.

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.

Looks good! Thanks @DavePutz. CI issues are unrelated so I'll merge now.

@tannewt tannewt merged commit bba6113 into adafruit:main Jul 16, 2021
@DavePutz DavePutz deleted the issue_4937 branch August 24, 2021 17:02
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.

PulseOut crashes if pulse is too short
4 participants