Orange
Posts: 44
Joined: Tue Jan 24, 2012 6:41 am

Analog audio and hardware PWM

Sun May 11, 2014 6:24 am

After I use hardware PWM I find I cannot get any sound out of the audio jack until I reboot the Pi. Does anybody know how to reset the audio without rebooting?

User avatar
joan
Posts: 14886
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Analog audio and hardware PWM

Sun May 11, 2014 7:17 am

You'll have to find a way of reinitialising the audio system.

I'd try the following (I have NOT done so, so this may or may not work).

Use lsmod to identify audio modules (the names should help). Let's assume they're x1 to xn.

Remove with

sudo rmmod x1
sudo rmmod x2
...
sudo rmmod xn

then reinitialise with

sudo modprobe x1
sudo modprobe x2
...
sudo modprobe xn

You may find a minimal set, perhaps 1 module, which will allow audio to work again.

Orange
Posts: 44
Joined: Tue Jan 24, 2012 6:41 am

Re: Analog audio and hardware PWM

Sun May 11, 2014 8:12 am

Thanks. I'll give that a try and let you know if it works.

Orange
Posts: 44
Joined: Tue Jan 24, 2012 6:41 am

Re: Analog audio and hardware PWM

Sun May 11, 2014 8:46 am

Ok, I tried:

rmmod snd_soc_bcm2708_i2s
rmmod snd_usb_audio
rmmod snd_hwdep
rmmod snd_bcm2835
rmmod snd_usbmidi_lib
rmmod snd_seq_midi
rmmod snd_seq_midi_event
rmmod snd_rawmidi
rmmod regmap_mmio
rmmod snd_soc_core
rmmod regmap_spi
rmmod snd_pcm
rmmod snd_page_alloc
rmmod regmap_i2c
rmmod snd_compress
rmmod snd_seq
rmmod snd_timer
rmmod snd_seq_device
rmmod snd

Then I added them all back again but the ALSA lib just hangs, e.g. when I use aplay.
I also tried 'alsactl init' but it didn't help.

To get it into this state all I did was use hardware PWM from the wiringPi library. Surely there must be a way to reset everything without rebooting. If anybody else has any ideas I'll gladly give them a try.

Thanks

User avatar
joan
Posts: 14886
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Analog audio and hardware PWM

Sun May 11, 2014 9:54 am

Try

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>

/*
gcc -o reset-pwm reset-pwm.c
sudo ./reset-pwm
*/

#define PWM_BASE  0x2020C000

#define PWM_LEN   0x28

static volatile uint32_t  *pwmReg  = MAP_FAILED;

static uint32_t * mapMem(int fd, unsigned base, unsigned len)
{
   return mmap
   (
      0,
      len,
      PROT_READ|PROT_WRITE|PROT_EXEC,
      MAP_SHARED|MAP_LOCKED,
      fd,
      base
   );
}

int main(int argc, char * argv[])
{
   int fd;

   fd = open("/dev/mem", O_RDWR | O_SYNC);

   if (fd<0)
   {
      printf("need to run as root, e.g. sudo %s\n", argv[0]);
      exit(1);
   }

   pwmReg  = mapMem(fd, PWM_BASE,  PWM_LEN);

   close(fd);

   pwmReg[0] = 0;
}

Orange
Posts: 44
Joined: Tue Jan 24, 2012 6:41 am

Re: Analog audio and hardware PWM

Sun May 11, 2014 11:40 am

That's an interesting solution.
I tried it but still no joy. ALSA still hangs after I run it.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5502
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Analog audio and hardware PWM

Sun May 11, 2014 12:01 pm

By default the analogue audio is running continuously (to avoid popping when starting/stopping) after first use.

If you come in and overwrite PWM registers, then it's not surprising it breaks.

Try adding:
force_pwm_open=0

to config.txt. This disables the de-popping, which means analogue audio is stopped and started each time. That may help.

If that doesn't work, you'll have to find out what registers wiringPi writes to and make sure they are returned to the original values on exit.
It's probably registers in the PWM peripheral that are causing the problem (although could be clock manager if that is changed).

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Analog audio and hardware PWM

Sun Feb 12, 2017 6:15 pm

Hey this kind of fits to my problem. I'm trying to use pi zeros pwm for the generation of audio and controlling multiple ws2812b leds. With your info of adding force_pwm_open=0 to the config.txt file, this finally seems to work.

The problem is: It only works well, if I run both applications from separate python scripts for example: First running leds.py, then audio.py and then again leds.py and so on... -> works fine.

In contrast: running both applications from just one python file will start to conflict and the script hangs. Is there a way of getting both applications running via just one python script? Kind of 'force_pwm_open=0' in python???

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5502
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Analog audio and hardware PWM

Sun Feb 12, 2017 7:49 pm

PinerdF wrote:In contrast: running both applications from just one python file will start to conflict and the script hangs. Is there a way of getting both applications running via just one python script? Kind of 'force_pwm_open=0' in python???
Are you saying both python apps output audio at the same time?
And you'd like the second python app to force the alsa connection of the first app to disconnect?

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Analog audio and hardware PWM

Mon Feb 13, 2017 5:15 pm

What I'm trying to do is: Using the pi zeros pwm to control leds and to generate audio via alsa in one python script. Since 'force_pwm_open=0' stops and starts audio generation, I'm wondering why this doesn't work for both applications in just one script. Imaging the script something like:

...
while True:
play_mp3_file(using GPIO18)
time.sleep(1)
light_up_leds(using GPIO12)
time.sleep(1)

is there a way of starting and stopping the ALSA audio in between the two functions?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5502
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Analog audio and hardware PWM

Mon Feb 13, 2017 6:04 pm

PinerdF wrote: ...
while True:
play_mp3_file(using GPIO18)
time.sleep(1)
light_up_leds(using GPIO12)
time.sleep(1)
Does play_mp3_file open the alsa device and close it again before returning?

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Analog audio and hardware PWM

Mon Feb 13, 2017 6:59 pm

dom wrote:Does play_mp3_file open the alsa device and close it again before returning?
This might be the right question. Surely this function makes use of the alsa module, since I'm either calling the python text to speech engine (engine = pyttsx.init()) or the pygame mixer (pygame.mixer.init()). But to be honest, I really don't know how to open and close alsa via the python script.

PLUS: Running the example function "play_mp3_file()" for the first time in the my python script always prints me the following errors (but stills works fine):

Code: Select all

ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
Maybe this indicates some problem already?

How does a proper way of opening and closing alsa via python would look like?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5502
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Analog audio and hardware PWM

Mon Feb 13, 2017 9:40 pm

PinerdF wrote: How does a proper way of opening and closing alsa via python would look like?
Python APIs aren't my area. But the behaviour you have described would be explained by
play_mp3_file opening alsa and not closing it (until python exits).

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Analog audio and hardware PWM

Mon Feb 13, 2017 9:48 pm

so maybe there is a way of closing alsa via the command line? And by doing so I could just

Code: Select all

import os
os.system('close alsa')
or is there any other option of resetting (i.e. closing) alsa?

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Analog audio and hardware PWM

Mon Feb 20, 2017 1:45 pm

Even though I did not find a proper solution to this issue, I want to leave a link to another thread of how I solved my specific issue here:

viewtopic.php?f=28&t=173593&p=1117036#p1117036

Abdalla
Posts: 1
Joined: Wed Jan 29, 2020 8:19 pm

Re: Analog audio and hardware PWM

Wed Jan 29, 2020 8:30 pm

PinerdF wrote:
Sun Feb 12, 2017 6:15 pm
Hey this kind of fits to my problem. I'm trying to use pi zeros pwm for the generation of audio and controlling multiple ws2812b leds. With your info of adding force_pwm_open=0 to the config.txt file, this finally seems to work.

The problem is: It only works well, if I run both applications from separate python scripts for example: First running leds.py, then audio.py and then again leds.py and so on... -> works fine.

In contrast: running both applications from just one python file will start to conflict and the script hangs. Is there a way of getting both applications running via just one python script? Kind of 'force_pwm_open=0' in python???
I have the same problem you had, i can live with sound and leds being controlled by two scripts, but i does not work, after playing the audio, the ws2812b leds start to respond weirdly, i added force_pwm_open=0 to config.txt, still had the same problem, any idea how to solve this?

Return to “General discussion”