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?

5 Answers 5


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))
  • 3
    Wow. That first macro is...unbelievable. Would you mind explaining how it works?
    – fouric
    Mar 5, 2013 at 0:38

Using the pigpio library to setup GPIO 4 as ALT0 function i.e. GPCLK0:

$ pigs m 4 0

Ref.: changing mode of GPIO


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 */
  • 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, 2012 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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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