10

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?

Ghanima
  • 15,855
  • 15
  • 61
  • 119
user61664
  • 209
  • 1
  • 2
  • 3

6 Answers6

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))
Rob_Bishop
  • 311
  • 2
  • 5
1

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

$ pigs m 4 0

Ref.: changing mode of GPIO

J. Piel
  • 11
  • 1
0

I found the pigpio library can do this for several languages, including python (which is where I needed it).

#!/usr/bin/env python3
import pigpio

RTS=17 pi = pigpio.pi() if pi.connected: # Configure RTS0 for alt function 3 pi.set_mode(RTS, pigpio.ALT3)

Phil Hord
  • 101
  • 2
0

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);
    INP_GPIO(pin);
    SET_GPIO_ALT(pin, alt);
    iounmap(gpio);
#undef INP_GPIO
#undef SET_GPIO_ALT
}
optimus prime
  • 171
  • 1
  • 10
0

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 */
Andrew
  • 216
  • 4
  • 18
parasew
  • 211
  • 2
  • 5
  • 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 04 '12 at 08:13
0

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.

jbyrnes
  • 266
  • 2
  • 5
  • Link requires account to read, if it's even still alive. – Phil Hord Oct 31 '22 at 20:14
  • Looks like it might have been taken over, or being migrated. I found it in the wayback cache at https://web.archive.org/web/20121214004835/http://log.liminastudio.com/writing/tutorials/tutorial-how-to-use-your-raspberry-pi-like-an-arduino

    Short version in case that disappears too (this may be way out of date, since it was 2013):

    Python: sudo pip install rpi.gpio

    GPIO.setup(7, GPIO.OUT)
    GPIO.output(7, True)
    GPIO.output(7,False)```
    
    C: get WiringPi library from http://wiringpi.com/ - it is not on github.
    
    – jbyrnes Nov 01 '22 at 02:36