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

Restore the PCM and PWM clock original states after using waves #567

Open
dspinellis opened this issue Apr 4, 2023 · 4 comments
Open

Restore the PCM and PWM clock original states after using waves #567

dspinellis opened this issue Apr 4, 2023 · 4 comments

Comments

@dspinellis
Copy link

dspinellis commented Apr 4, 2023

Currently it appears that PCM and PWM clocks are taken over and not restored when using waves. Ideally, gpioTerminate should restore these clocks. My understanding is that this could be done by saving clkReg[clkDiv] before the call to initClock(0) and restoring it through calls to initHWClk.

dspinellis added a commit to dspinellis/pigpio that referenced this issue Apr 5, 2023
@dspinellis
Copy link
Author

dspinellis commented Apr 5, 2023

I'm preparing a pull request to address this. @joan2937 can you please have a quick look at dspinellis@93f127f to check if it's in the right direction? This is still work in progress.

Currently the code is structured to save the hardware state of the five clocks, PWM, and PCM before modifying it, and to restore the saved state when gpioNotifyClose() gets called, which is also called from the Python stop() method.

dspinellis added a commit to dspinellis/pigpio that referenced this issue Apr 6, 2023
dspinellis added a commit to dspinellis/pigpio that referenced this issue Apr 6, 2023
This prevents pigpio wave output from disrupting sound output
performed at a later point.

Issue: joan2937#567
@dspinellis
Copy link
Author

Most recent work is at https://github.com/dspinellis/pigpio/tree/fix-567-pm-state. Saving and restoring seems to work, and all original tests pass. I am currently looking at ways to add unit tests.

@guymcswain
Copy link
Collaborator

Please explain the problem you encountered that motivated you to request this change.

@dspinellis
Copy link
Author

The issue is now correctly linked in the initial description and reproduced below.

The following example program sends a short pulse on GPIO 14 using pigpio.

import pigpio

pi = pigpio.pi() # connect to Pi

pi.wave_add_new()
pi.set_mode(14, pigpio.OUTPUT)

wf=[]
wf.append(pigpio.pulse(1 << 14, 0, 500))
wf.append(pigpio.pulse(0, 1 << 14, 800))

pi.wave_clear()
pi.wave_add_generic(wf)
wid = pi.wave_create()
pi.wave_send_once(wid)

while pi.wave_tx_busy():
  pass

pi.wave_clear()
pi.stop()

Even after terminating pigpiod, the sound from IQaudio sounds garbled: played intermittently at a high speed with vlc or sounding like white noise with speaker_test. Killing and rerunning pigpiod doesn't help. Only a reboot fixes the problem. Running pigpiod without running the program, or configuring pigpio without using its wave functionality doesn't create a problem.

As explained in the answer to the corresponding Raspberry Pi StackExchange question, "pigpio takes over the PWM and PCM clocks when you use waves, and there is no provision made by pigpio to restore the clocks to the previous state."

dspinellis added a commit to dspinellis/pigpio that referenced this issue Apr 7, 2023
This prevents pigpio wave output from disrupting sound output
performed at a later point.

Issue: joan2937#567
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