Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to use the altenate function of GPIO 4 namely the GPCLK0 function. How can one set a GPIO to its alternate function? Many alternate functions are summarized here: elinux-wiki

The best would be if it is possible with the wiringPi Library. But i do not mind to use arbitrary C-code if someone tells me how to do it. After entering an alternative function, i think it is needed to configure it?

share|improve this question

You can use the following C Macro to change a given GPIO to an alternate function as specified by this table: http://elinux.org/RPi_BCM2835_GPIOs

volatile unsigned *gpio;

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

Example (from http://elinux.org/RPi_Low-level_peripherals#GPIO_hardware_hacking)

The following C Macros would change GPIO g to its ALT0 function:


Always use INP_GPIO(x) before using SET_GPIO_ALT(x,y)

#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
share|improve this answer
Wow. That first macro is...unbelievable. Would you mind explaining how it works? – InkBlend Mar 5 '13 at 0:38

here are some other threads (on www.raspberrypi.org) that are dealing with the GPCLK0 function.

the second link has an exact C example on how to set the GPCLK0 alternate function.

/* Setup GPIO 4 as ALT0 function i.e. GPCLK0 */
mov r0, #4
mov r1, #4 /* ALT0 */
share|improve this answer
Thanks for that answer, i tried a littlebit with that assembler-code, but i think assembler is not my thing. This takes longer than exspected.. – user61664 Nov 4 '12 at 8:13

This tutorial shows different ways you can configure the GPIO pins, such as bash, python, and C using wiringPi. It's as easy as an Arduino.


You'll need to look into the WiringPi library a little more to know exactly what to set for the specific "alternate function" you want, eg SPI, PWM, I²C etc, since you didn't specify.

share|improve this answer

Use this code to set the alt function

static void bcm2708_set_gpio_alt(int pin, int alt)
     * This is the common way to handle the GPIO pins for
     * the Raspberry Pi.
     * TODO This is a hack. Use pinmux / pinctrl.
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)        <<(((g)%10)*3))
    unsigned int *gpio;
    gpio = ioremap(GPIO_BASE, SZ_16K);
    SET_GPIO_ALT(pin, alt);
#undef INP_GPIO
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.