barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 11:21 am

Hello,

Strange question i'm sure you may be thinking, but is it possible to change which PWM output on the Pi is used for Audio?

Currently its set to GPIO40 and GPIO45 I believe, but there are other pins which are capable of taking on the responsibility of using that PWM function, such as GPIO12 and GPIO13, or GPIO18 and GPIO19, or GPIO41 for example.
Yes I know I wont have the on board components to deal with turning it into a suitable line out source, but is it possible to change the software to point the PWM audio out a different pin such as GPIO18?

I only want Mono Audio, not stereo. Is it also possible to do this?
I want to use the other PWM for something else entirely, not for audio.

If this is possible, is there any information anywhere that might help me?
Ideally I would like to use GPIO18 (PWM0) for Audio, and handle the filtering etc external to the PI rather than using the on board bits and pieces and the barrel jack.

Eventually I would like to move to a CM module, and have the choice of which GPIO to use for Audio, but at the moment I have a B version, so would just like to make the use of that and see if I can get PWM Audio out of GPIO18, in Mono, and disable it from using GPIO40 and GPIO45.

Is this a driver change, or kernel change? Happy with either if you can direct me where to look.

Can anyone assist please?
Thanks

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:04 pm

http://www.raspberrypi.org/documentatio ... uration.md

Change the pin configuration for the platform you want to change..

compile it to dt-blob.bin and put into your /boot directory

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

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

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:06 pm

You can disable the individual channels on gpios 40/45 by setting either or both to be inputs (mode value 0).

Then set one or both of gpios 12/13 to ALT0 (mode value 4) or one or both of gpios 18/19 to ALT5 (mode value 2). I just set gpio 12 to be PWM0 (ALT0), connected up a headphone (probably foolishly) between gpio 12 and ground, and got audio among the noise.

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:13 pm

Thanks Gsh

So if I did the dt-blob method, its just a case of setting the one I want as PWM Audio, to be PWM - like how GPIO40 and GPIO45 are currently set, and thats it? Whatever/whereever the driver is, will pick it up and just drive the one set to PWM?

And if I was then to get it to play a movie for example, if Im connected to a monitor on the Composite output (not hdmi) then the audio will just come out that GPIO I have set, I dont need to set anything else to tell it to use PWM?

Thanks Joan

Your method, I assume is not using the dt-blob ?
I am currently using the latest Kernel.
Where abouts are the edits you are talking about, made?

Thanks

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

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:21 pm

barkyj wrote: ...
Thanks Joan

Your method, I assume is not using the dt-blob ?
I am currently using the latest Kernel.
Where abouts are the edits you are talking about, made?

Thanks
I just used my pigpio library.

sudo pigpiod -x -1 # grant access to ALL gpios

pigs m 40 r # switch gpio 40 to an input (read)

I am using a B+.

pigs m 12 0 # switch gpio 12 to mode 0

If you are using a B gpio 18 will be more convenient so do

pigs m 18 5 # switch gpio 18 to mode 5

man pigs # for help.

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:23 pm

1) copy the default dt-blob.dts from the documentation page.
2) put it on your Pi
3) sudo apt-get install device_tree_compiler
4) dtc -O dtb -o dt-blob.bin dt-blob.dts
5) sudo cp dt-blob.bin /boot

Check that your Pi still boots...

If it does, just change

pin@p40 to be pin@p18 or whatever the pin is you want the functionality out of. The driver should have any knowledge of which pin each function happens on...

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 12:36 pm

Thank you Joan.

I will test your method tomorrow.

Thanks gsh.
I just tested the dt-blob and that works too, audio coming out GPIO18 and filtered externally. Sounds lovely in headphones.

Many thanks!

Still not sure how the magic works in the background though.
If I was to use 1 channel for Audio as I am, can I use the other PWM for doing PWM stuff such as controlled the brightness of a LED...? Or do the two not play ball together?
Is there some way to make it so I can use each PWM seperately and the Audio never attempts to use the other PWM pin, so its free to be used for my 'LED' ?

Thanks

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Wed Sep 10, 2014 1:27 pm

No unfortunately not, unless you can sort Alsa to route output to a single PWM and route a fixed repeating value to the other channel

Basically the two channels are interrelated

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 12:11 am

Oh dear... hmm.

I assume this is going to be a potential issue for people who use the CM as there is no 'hardware' as such like filtering and Jacks to get in the way, so people may want Mono audio and then PWM to control something else.

Im not sure what to do. I tried looking at how the snd driver (or is it ALSA?) gets the pwm mode out of the dt-blob, but I cant even find that. How does it work exactly do you know?

Does it look for any pins which have been configured for PWM and then if something is played such as from a media player, that requires audio, it steals the GPIO tagged as 'pwm' and pumps the audio out of them?
Does it check on the first time it attempts to play audio, or does it check every time audio is started? For example, if I set pwm on GPIO18 and output on GPIO40, and play audio, I get mono audio out of GPIO18 and audio isnt attempted to be played out of GPIO40, right? But if I then force GPIO40 to be pwm say via the method described above from Joan, or using some python script or something, and then play an audio track, will PWM then come out of both GPIO18 and GPIO40?

I hope there is a solution to this as it would be nice to not have Audio take both channels if only 1 is needed, leaving the other to be used for something else.

Any help would be appreciated
Thanks

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 5:36 am

The drivers do not need to know what pin it comes out of...

All the driver does is pump PWM out of the PWM peripheral. It's up to the GPIO settings which pin it then appears on

I'm sure an Alsa driver could be written to give you access to the other channel.

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 6:03 am

Thanks again Gordon.

So the blob allocates the GPIO to be pwm, which then gets labelled as PWM0 and PWM1 I assume in the background, and the ALSA driver then outputs to 1 or both depending if they are defined - I assume.

This ALSA driver, is it open source, or is it closed source?
If its open source, are you able to point me to where I might find what I am looking for? I would love to get this working and find out how to modify the ALSA drive so it only touches what it is allowed to, rather than whatever is available.
Do you know if its then possible to get it to output true mono, rather than just half of the story?

Any help is appreciated
Thanks again

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

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 7:40 am

You need to clarify what you want to achieve.

You can have audio out of one channel and PWM out of another channel. It's just that (as far as I'm aware) you'll be using the PWM peripheral to generate the audio and will have to generate the PWM for the other channel by separate means. This limitation (if indeed it exists) will be down to the way the PWM peripheral is implemented.

There are ways of generating PWM suitable for LEDs/servos/motors which don't use the PWM peripheral. pigpio does so.

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 9:03 am

Hi Joan

Thanks for that.
Well ideally I want PWM out of one channel for Audio, and then to be able to control the PWM out of another channel to control a series of LED's, to vary their brightness (via a controller) from 0-100%.
So basically I am wanting to generate a PWM of approximately 130Hz, with duty from 0 to 100%, which is not affected by the Audio stream.

I hope that is enough information.
Is this something your library you are talking about, can do?

Thanks

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

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 10:47 am

barkyj wrote:Hi Joan

Thanks for that.
Well ideally I want PWM out of one channel for Audio, and then to be able to control the PWM out of another channel to control a series of LED's, to vary their brightness (via a controller) from 0-100%.
So basically I am wanting to generate a PWM of approximately 130Hz, with duty from 0 to 100%, which is not affected by the Audio stream.

I hope that is enough information.
Is this something your library you are talking about, can do?

Thanks
Yes.

With the default settings the closest frequency to 130Hz you would be able to set would be 100Hz or 160Hz. Again by default off would be 0 and fully on 255 but you could set 0 to 1000 or 100 if you prefer.

When I get a few minutes I'll get my headphones out and actually try.

barkyj
Posts: 28
Joined: Fri Sep 05, 2014 10:07 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 10:52 am

You are amazing.

0-255 will be heaps, and 160Hz will be a good starting point.

Thank you very very much.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 1:24 pm

I looked at the DTS code and do not see a definition for SPI. Can the SPI pins be moved as well?
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

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

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 1:30 pm

Richard-TX wrote:I looked at the DTS code and do not see a definition for SPI. Can the SPI pins be moved as well?
There are alternate SPI ports on gpios numbered higher than 31. That means you should be able to remap on the compute module (I think it allows physical access to the higher numbered gpios). You don't have any choice for the standard models (however the B+ does give access to SPI1 on gpios 16-21).

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Thu Sep 11, 2014 3:22 pm

There are alternate positions for many pins, but not all functions can be put on all pins, check the BCM2835 specification to see which functions can be on which pins...

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

jomag
Posts: 6
Joined: Wed May 20, 2015 8:39 pm

Re: Pi PWM Audio Output - Moving the Pin

Wed May 20, 2015 8:46 pm

Hi. I have the same need: I want to connect a speaker directly to the GPIO header.

I have changed pin@p40 to pin@p18 in dt-blob.dts, compiled and installed it. I've verified that my changes was used as the left speaker stopped playing sound after changing this. I then restore pin 40 and did the same thing with pin 45, and then there was no sound on the right speaker.

But there is no output on GPIO18 while playing sound (pin 12 on the header)! I've verified this with an oscilloscope as well.

Any idea why one channel may work, but nothing on the other?

I'm testing on a new Raspberry Pi B+ with a clean installation of Raspbian 2015-05-05.

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

Re: Pi PWM Audio Output - Moving the Pin

Sat May 23, 2015 5:53 am

jomag wrote:Hi. I have the same need: I want to connect a speaker directly to the GPIO header.

I have changed pin@p40 to pin@p18 in dt-blob.dts, compiled and installed it. I've verified that my changes was used as the left speaker stopped playing sound after changing this. I then restore pin 40 and did the same thing with pin 45, and then there was no sound on the right speaker.

But there is no output on GPIO18 while playing sound (pin 12 on the header)! I've verified this with an oscilloscope as well.

Any idea why one channel may work, but nothing on the other?

I'm testing on a new Raspberry Pi B+ with a clean installation of Raspbian 2015-05-05.
Have you set the mode for gpio18 (ALT5)?

jomag
Posts: 6
Joined: Wed May 20, 2015 8:39 pm

Re: Pi PWM Audio Output - Moving the Pin

Mon May 25, 2015 8:52 pm

joan wrote: Have you set the mode for gpio18 (ALT5)?
Nope. Is it necessary and how do I do that?

Since I did not have to do that with the default audio outputs (40 and 45), I thought it was handled by the driver.

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Tue May 26, 2015 8:36 am

If you've changed the dt-blob.bin to move the PWM to the other set of pins then it should do this for you.

You can check using the gpiostate tool:

apt-get install raspi-gpio
gpiostate
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

jomag
Posts: 6
Joined: Wed May 20, 2015 8:39 pm

Re: Pi PWM Audio Output - Moving the Pin

Tue May 26, 2015 9:24 am

Thanks! It seems the pin has been configured correctly as a PWM:
...
GPIO 18: fsel=2 level=0, func=PWM0
...
Any other ideas?

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1258
Joined: Sat Sep 10, 2011 11:43 am

Re: Pi PWM Audio Output - Moving the Pin

Tue May 26, 2015 3:33 pm

Not sure I understand what it is that you can't get working... What is it that you are trying to move and where are you trying to move it, your description above isn't clear...

You said that it worked when you moved it to pin18?

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

tvjon
Posts: 611
Joined: Mon Jan 07, 2013 9:11 am

Re: Pi PWM Audio Output - Moving the Pin

Tue May 26, 2015 4:32 pm

gsh wrote:...
You can check using the gpiostate tool:

apt-get install raspi-gpio
gpiostate
That's a handy tool, thank you.


pi@pi2 ~ $ sudo gpiostate
Found a BCM2709

BANK0:
GPIO 00: fsel=6 level=1, func=PCLK
GPIO 01: fsel=6 level=0, func=DE
GPIO 02: fsel=6 level=0, func=LCD_VSYNC
GPIO 03: fsel=6 level=0, func=LCD_HSYNC
GPIO 04: fsel=6 level=0, func=DPI_D0
GPIO 05: fsel=6 level=0, func=DPI_D1
GPIO 06: fsel=6 level=0, func=DPI_D2
GPIO 07: fsel=6 level=0, func=DPI_D3
GPIO 08: fsel=6 level=0, func=DPI_D4
GPIO 09: fsel=6 level=0, func=DPI_D5
GPIO 10: fsel=6 level=0, func=DPI_D6
GPIO 11: fsel=6 level=0, func=DPI_D7
GPIO 12: fsel=6 level=0, func=DPI_D8
GPIO 13: fsel=6 level=0, func=DPI_D9
GPIO 14: fsel=6 level=0, func=DPI_D10
GPIO 15: fsel=6 level=0, func=DPI_D11
GPIO 16: fsel=6 level=0, func=DPI_D12
GPIO 17: fsel=6 level=0, func=DPI_D13
GPIO 18: fsel=6 level=0, func=DPI_D14
GPIO 19: fsel=6 level=0, func=DPI_D15
GPIO 20: fsel=6 level=0, func=DPI_D16
GPIO 21: fsel=6 level=0, func=DPI_D17
GPIO 22: fsel=0 level=0, func=INPUT
GPIO 23: fsel=0 level=0, func=INPUT
GPIO 24: fsel=0 level=0, func=INPUT
GPIO 25: fsel=0 level=0, func=INPUT
GPIO 26: fsel=0 level=0, func=INPUT
GPIO 27: fsel=0 level=0, func=INPUT

BANK1:
GPIO 28: fsel=4 level=1, func=SDA0
GPIO 29: fsel=4 level=1, func=SCL0
GPIO 30: fsel=0 level=0, func=INPUT
GPIO 31: fsel=1 level=1, func=OUTPUT
GPIO 32: fsel=1 level=0, func=OUTPUT
GPIO 33: fsel=0 level=0, func=INPUT
GPIO 34: fsel=0 level=1, func=INPUT
GPIO 35: fsel=0 level=1, func=INPUT
GPIO 36: fsel=0 level=1, func=INPUT
GPIO 37: fsel=0 level=0, func=INPUT
GPIO 38: fsel=1 level=1, func=OUTPUT
GPIO 39: fsel=0 level=0, func=INPUT
GPIO 40: fsel=4 level=1, func=PWM0
GPIO 41: fsel=1 level=0, func=OUTPUT
GPIO 42: fsel=0 level=1, func=INPUT
GPIO 43: fsel=0 level=1, func=INPUT
GPIO 44: fsel=4 level=1, func=GPCLK1
GPIO 45: fsel=4 level=0, func=PWM1

BANK2:
GPIO 46: fsel=0 level=0, func=INPUT
GPIO 47: fsel=1 level=0, func=OUTPUT
GPIO 48: fsel=4 level=0, func=SD0_CLK
GPIO 49: fsel=4 level=1, func=SD0_CMD
GPIO 50: fsel=4 level=1, func=SD0_DAT0
GPIO 51: fsel=4 level=1, func=SD0_DAT1
GPIO 52: fsel=4 level=1, func=SD0_DAT2
GPIO 53: fsel=4 level=1, func=SD0_DAT3
pi@pi2 ~ $

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 20 guests