Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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

3 Answers 3

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:

INP_GPIO(g);
SET_GPIO_ALT(g,0);

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
1  
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.

http://log.liminastudio.com/writing/tutorials/tutorial-how-to-use-your-raspberry-pi-like-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

Your Answer

 
discard

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.