Page 1 of 1

Quick Question (Bare metal sound)

Posted: Thu Dec 13, 2012 9:49 pm
by Shawty
While I'm in the forums... quick question...

There's TON's of code and material on Bare Metal graphics programming, but has anyone tried to do any bare metal sound code yet?

I've been through everything I can find (Inc baking Pi) and many other forums, and it's all Graphics, I'd be interested if anyone has tried sound yet, and if so do you have any code your willing to release, or are there any posts on here already covering the subject that I may have missed.



Re: Quick Question (Bare metal sound)

Posted: Fri Dec 14, 2012 2:54 am
by DexOS
A friend of mine called krom, did some work on sound for the pi, he could not get it working, but he did code the basic function that are needed.
I am sure he will help in any way he can.
Maybe you could PM him.
Good luck, as sound would be a nice thing to have in bare metal.

Re: Quick Question (Bare metal sound)

Posted: Fri Dec 14, 2012 10:09 am
by Shawty
Great stuff, Thanks Dex I'll be sure to try that.

It's nice having an ARM to program at such a low-level again, and sound is the only thing stopping me from porting all my old Acorn Archimedes demo's to the Pi.

I'm hoping that once I can overcome the sound hurdle, that I can then write a couple of Bare Metal tutorials using one or two of my demo's as the materiel, they may be demo's but they have most of the elements needed for games.

Speaking of games by the way, when's the next version of Dex-Basic coming out, you have an awesome idea there, one that when I have some time to help on it I'll be more than happy too.


Re: Quick Question (Bare metal sound)

Posted: Fri Dec 14, 2012 5:45 pm
by Joeboy
I've just been investigating this, and not getting very far. I managed to get some bleeping noises out of the headphone jack by using 'oscillator' as the clock source to the pwm. I can share the code for this if you like, but I'm fairly sure I'm not doing it right.

I tried reading PWM_CLOCK_CNTRL in Raspbian (where the audio was working), and the SRC bits are set to 0b100 (ie. 4), which according to the peripherals guide is 'PLLA'. But when I try clocking the pwm from PLLA I get no output. Maybe some magic needs to happen to set up PLLA? It seems to be completely undocumented as far as I can tell.

If anybody has any clues, please report back!

Re: Quick Question (Bare metal sound)

Posted: Fri Dec 14, 2012 6:03 pm
by dom
There is some userland linux code here from millerpuckette for driving PWM audio: ... 38#p173107

which should be applicable to bare metal. Read the whole thread - there's some good audio related stuff in it.

Re: Quick Question (Bare metal sound)

Posted: Sat Dec 15, 2012 12:38 am
by Shawty
@dom - great pointer I'll be sure to read it all.

@joebgoy - please do share your code, I (and others) may not be able to understand / advise / help with your problem, but by god we can certainly try.

Let me give you a little bit of (what I hope is inspiration) back in the late 1970's / Early 1980's when I first started dabbling with computers here in the UK. We had NO INTERNET, we had no stack overflow, Google search or raspberry pi forums :-)

We succeeded by trial and error, we tried things that may (or may not have) broken our hardware, we poked and peeked random values to random hardware addresses until something happened.

Even if what we thought was supposed to happen didn't actually happen, we still counted that something as a victory, because we where learning, we where discovering things by fair means or foul.

You got some bleeps and bloops out of the chip, that my friend is a victory, keep at it and those bleeps and bloops will turn into real sounds.

When we where able to share our code it was via primitive BBS systems running across systems such as fidonet, where we had to hijack our parents telephone lines using 300 baud dial up modems, connections that easily ran up £100+ pound telephone bills in the space of a week or two. We shared our code, and 4/5/6 weeks later someone would reply with their own findings, putting two or more of these answers together would SOMETIMES get you the answer you wanted, however more often than enough it would give you several more questions on top of the question you where trying to answer.

With the Raspberry Pi, I'm back in those days, only now we have Google, the internet, stack overflow where more connected than ever. I'll part with my knowledge where ever I can, and IF I can answer a question I will, because I believe that it's in everyone's future to pass the knowledge on.

Despite the knowledge I've accumulated since those early days, I will openly admit there are things I have still yet to discover (and I've been doing this s**t now for at least 30 years) I openly encourage you to share your code and your Ideas and explain what you have and have not tried, once you do then many people with differing levels of knowledge and experience can all offer their input, and all it takes is ONE idea to spark a revolution :-)

Back in 1985 I was one of the few people (along with Foz, Grez and the other Yorkshire Boys although not at the same time) to figure out that a bug in the BBC Model B micro sound chip (a Texas Instruments SN76489 if memory serves me right) allowed us to play back raw PCM sampled sound, once we released our code (Kudos to the Yorkshire boys for getting there before I did) almost over night software sprung up that made use of this technique.

In my mind this is what a lot of the idea behind the Raspberry Pi is, to get the current generation thinking about computing principles again, and not just being www jockeys.

Anyway, that's enough now... I'm getting tooooo carried away :-D I'll real my neck back in before it get's chopped off.

Bottom line....

Share your code, the least expected person you know might just solve your problem.

Re: Quick Question (Bare metal sound)

Posted: Sat Dec 15, 2012 2:09 am
by Joeboy
Ok, here's some code that outputs a gnarly sawtooth wave to the audio jack, but I think the pwm needs to be driven by a faster clock in order to get decent quality audio. Hope it's of some use, and maybe somebody can provide some enlightenment as to how to do it properly.


Code: Select all

extern void PUT32 ( unsigned int, unsigned int );
extern unsigned int GET32 ( unsigned int );
extern void dummy ( unsigned int );

#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE         (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PWM_BASE          (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */
#define CLOCK_BASE        (BCM2708_PERI_BASE + 0x101000)

#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define BCM2835_PWM_CONTROL 0
#define BCM2835_PWM_STATUS  1
#define BCM2835_PWM0_RANGE  4
#define BCM2835_PWM0_DATA   5
#define BCM2835_PWM_FIFO    6
#define BCM2835_PWM1_RANGE  8
#define BCM2835_PWM1_DATA   9

#define BCM2835_PWMCLK_CNTL   40
#define BCM2835_PWMCLK_DIV    41

#define BCM2835_PWM1_MS_MODE    0x8000  /*  Run in MS mode                   */
#define BCM2835_PWM1_USEFIFO    0x2000  /*  Data from FIFO                   */
#define BCM2835_PWM1_REVPOLAR   0x1000  /* Reverse polarity             */
#define BCM2835_PWM1_OFFSTATE   0x0800  /* Ouput Off state             */
#define BCM2835_PWM1_REPEATFF   0x0400  /* Repeat last value if FIFO empty   */
#define BCM2835_PWM1_SERIAL     0x0200  /* Run in serial mode             */
#define BCM2835_PWM1_ENABLE     0x0100  /* Channel Enable             */

#define BCM2835_PWM0_MS_MODE    0x0080  /* Run in MS mode             */
#define BCM2835_PWM0_USEFIFO    0x0020  /* Data from FIFO             */
#define BCM2835_PWM0_REVPOLAR   0x0010  /* Reverse polarity             */
#define BCM2835_PWM0_OFFSTATE   0x0008  /* Ouput Off state             */
#define BCM2835_PWM0_REPEATFF   0x0004  /* Repeat last value if FIFO empty   */
#define BCM2835_PWM0_SERIAL     0x0002  /* Run in serial mode             */
#define BCM2835_PWM0_ENABLE     0x0001  /* Channel Enable             */

#define BCM2835_BERR  0x100
#define BCM2835_GAPO4 0x80
#define BCM2835_GAPO3 0x40
#define BCM2835_GAPO2 0x20
#define BCM2835_GAPO1 0x10
#define BCM2835_RERR1 0x8
#define BCM2835_WERR1 0x4
#define BCM2835_EMPT1 0x2
#define BCM2835_FULL1 0x1

#define PM_PASSWORD 0x5A000000 

#define GPFSEL1 0x20200004
#define GPSET0  0x2020001C
#define GPCLR0  0x20200028

#define ERRORMASK (BCM2835_GAPO2 | BCM2835_GAPO1 | \
    BCM2835_RERR1 | BCM2835_WERR1)
#define MAXPRINT 5
volatile unsigned* gpio = (void*)GPIO_BASE;
volatile unsigned* clk = (void*)CLOCK_BASE;
volatile unsigned* pwm = (void*)PWM_BASE;

void pause(int t) {
    // Pause for about t ms
    int i;
    for (;t>0;t--) {
        for (i=5000;i>0;i--) dummy(i);

static void audio_init(void)
    SET_GPIO_ALT(40, 0);
    SET_GPIO_ALT(45, 0);
    *(clk + BCM2835_PWMCLK_CNTL) = PM_PASSWORD | (1 << 5); // stop clock

    //vals read from raspbian:
    //PWMCLK_CNTL = 148 = 10010100
    //PWMCLK_DIV = 16384
    //PWM_CONTROL=9509 = 10010100100101

    int idiv = 2; // raspbian has idiv set as 16384
    *(clk + BCM2835_PWMCLK_DIV)  = PM_PASSWORD | (idiv<<12);
    *(clk + BCM2835_PWMCLK_CNTL) = PM_PASSWORD | 16 | 1; // enable + oscillator
                                                         // raspbian has this as plla

    // disable PWM
    *(pwm + BCM2835_PWM_CONTROL) = 0;

    *(pwm+BCM2835_PWM0_RANGE) = 0x400;
    *(pwm+BCM2835_PWM1_RANGE) = 0x400;

    *(pwm+BCM2835_PWM_CONTROL) =
          BCM2835_PWM1_USEFIFO | 
//          BCM2835_PWM1_REPEATFF |
          BCM2835_PWM1_ENABLE | 
          BCM2835_PWM0_USEFIFO | 
//          BCM2835_PWM0_REPEATFF |  */
          BCM2835_PWM0_ENABLE | 1<<6;


int notmain ( unsigned int earlypc )
    int i=0;
    long status;

    while (1) {
        status =  *(pwm + BCM2835_PWM_STATUS);
        if (!(status & BCM2835_FULL1)) {
            *(pwm+BCM2835_PWM_FIFO) = 20*(i & 0x1f) ;
        if ((status & ERRORMASK)) {
//                uart_print("error: ");
//                hexstring(status);
//                uart_print("\r\n");
            *(pwm+BCM2835_PWM_STATUS) = ERRORMASK;

Re: Quick Question (Bare metal sound)

Posted: Sat Dec 15, 2012 3:57 am
by DexOS
Shawty wrote:Speaking of games by the way, when's the next version of Dex-Basic coming out, you have an awesome idea there, one that when I have some time to help on it I'll be more than happy too.

That would be great, i will be releasing a new ver in about a week or so, and than another in January, i was going to wait to release it in one go, but i have had so many requests, i will release it in two half's, as the fat code is taking time in full assembly.
If i get time i may look into sound, i will send you the code if i get anything working.

Re: Quick Question (Bare metal sound)

Posted: Sat Dec 15, 2012 8:15 am
by Shawty
@dex yea that would be great :-)

In the mean time I'm going to get some time to play over the holidays, it's gonna be great to get some full time interesting coding in rather than what I usually do (Enterprise systems on the Microsoft stack in C#) and well It's nice to get back to basics :-)

I used to love doing ARM assembly back in the day on my Acorn A5000, in fact I still have a number of books kicking around somewhere (I think anyway)

Re: Quick Question (Bare metal sound)

Posted: Sun Dec 16, 2012 3:40 pm
by Joeboy
I accidentally started writing a sequencer. God help me.

It's not big or clever but it kind of works: ... /pitracker

Re: Quick Question (Bare metal sound)

Posted: Sun Dec 16, 2012 4:31 pm
by Shawty
That is absolutely freaking awesome!!!

What a great bit of code for me to start with.....

Don't worry about a loader, It might be after Xmas now, but you can have the C sources to my pro-tracker replay loader routines well once I find them. There in among 15+years of archives somewhere :-)

Re: Quick Question (Bare metal sound)

Posted: Sun Dec 16, 2012 5:10 pm
by Joeboy
Glad you like it. It was a lot of fun to do. To be honest I'm not really planning to turn it into a fully featured tracker or anything, it was just an experiment that got out of hand (you know how it is...). You're very welcome to fork it or nick bits of it to work on yourself though.

Re: Quick Question (Bare metal sound)

Posted: Sun Dec 16, 2012 8:03 pm
by tufty
That's extremely cool. Well done.