GPIO Function Select


7 posts
by screennameless » Sat Jun 23, 2012 7:21 pm
Hello! I am learning bare-metal assembly on the Raspberry Pi using fasmARM.

My first project was to write a program that blinks the OK LED rapidly. It works, but I keep hearing mention of the GPIO Function Select register. Can someone explain to me what this does and how it works? I honestly am having a hard time understanding why my code works.

There is hardly any information available that will just tell me what it does in terms I can really understand (as someone who is a high-level programmer and has never used assembly), and the C examples don't help because I am only using assembly. And the Linux examples (just write to the sys file) doesn't help either because I'm not using an OS.

Sorry if this is blatantly obvious, I am still very new to assembly. I decided to use the Pi to teach myself assembly.

Also, here is the code for my project if it helps at all.
Posts: 1
Joined: Sat Jun 23, 2012 7:08 pm
by Grumpy Mike » Thu Jun 28, 2012 10:53 pm
screennameless wrote:My first project was to write a program that blinks the OK LED rapidly. It works, but I keep hearing mention of the GPIO Function Select register. Can someone explain to me what this does and how it works? I honestly am having a hard time understanding why my code works.


The GPIO pin of the OK LED has been initialised to be an output by the system. Therefore all you need to do is to toggle the appropriate bit in the appropriate register or memory location to control it.
If you were to use any other pin the GPIO function register would have to be set up first to make that pin into an output. The unused I/O brought out to the header default to being inputs and Linux only changes GPIO 14 & 15 to be a UART TX & RX.
If you look at the part data sheet we have access to you will see the addresses of these registers and what the bit fields do. Basically one function select register covers ten GPIO lines, with groups of three bits being assigned to one line. This gives 8 possible states for these bits.
Bit Value Result
000 GPIOn is an input
001 GPIOn is an output
100 GPIIOn is alternative function 0
101 GPIIOn is alternative function 1
and so on for up to 6 alternative functions. Note not all alternative functions are valid to use.

Using the GPSET registers any bit that is a 1 will have the corresponding GPIO bit set high, if it has previously been set to be an output.
Likewise using the GPCLR registers any bit that is a 1 will have the corresponding GPIO bit set low.
This is shown on page 90 of the data sheet.

If you use an assembly language or a high level language all you are doing is changing bits in memory locations.
User avatar
Posts: 784
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
by jon889 » Sat Dec 29, 2012 2:00 am
What exactly is an alternative function?
surely a pin is either an input or output, so if you set it as an "alternate function" then is it an input or output?
Posts: 2
Joined: Sat Dec 29, 2012 1:56 am
by mahjongg » Sat Dec 29, 2012 2:12 am
All GPIO pins can be used as simple GPIO (general purpose I/O) pins, but many also have a secondary special use dedicated to them, that can also be used (selected). For example those for the UART are switched on by default, and create the RxD and TxD pins. Others are used for "inter IC bus" (I2C) functions, as PWM outputs, or for SPI, among others. The new 8-pins header has dedicated functions for an audio code (i2S) interface, so you can add audio ADC and DAC's an audio mixer and microphone input etc. these devices (except the UART) need specific drivers. Some of which are still in development.
User avatar
Forum Moderator
Forum Moderator
Posts: 5571
Joined: Sun Mar 11, 2012 12:19 am
by Gert van Loo » Sun Dec 30, 2012 10:07 am
jon889 wrote:What exactly is an alternative function?
surely a pin is either an input or output, so if you set it as an "alternate function" then is it an input or output?

Or bi-directional. The I2C bus is a typical bi-directional bus. Data is read (input) or written (output) using the same pin (SDA)
Also the I2C clock is an output, but is also read at the same time in case the slave pulls the clock pin low to signal
the master that it needs a bit more time.

You are right in so far that if an alternative function is chosen the pin direction is still an input or output (or bi-directional)
But the direction is chosen by the function selected. A UART TX pin is always set to output mode.

But the input signal is till connected to the GPIO input read register. Which means you could use the GPIO input register to see what the UART TX pin status is.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2060
Joined: Tue Aug 02, 2011 7:27 am
by crossboy007 » Sun Dec 30, 2012 10:35 am
if you are stuck understanding what the code does. have a look at this from amazon

http://www.amazon.co.uk/Raspberry-User-Guide-Eben-Upton/dp/111846446X/ref=sr_1_1?ie=UTF8&qid=1356863644&sr=8-1
Posts: 12
Joined: Fri Dec 28, 2012 8:44 pm
by aTao » Sun Dec 30, 2012 11:52 am
jon889 wrote:What exactly is an alternative function?
surely a pin is either an input or output, so if you set it as an "alternate function" then is it an input or output?


As Gert said a pin can physically only be in or out or both. But the alternative function control is more to do with where the input or output signal goes to or from.
If the pin is set to input then the signal that an external circuit exerts on the pin is reflected in a memory mapped bit. Likewise, if the pin is set to output then setting bits in a memory mapped locations (GPSET and GPCLR) determines the output of the pin.
If you set a pin function to PWM then the pin will output a pulse width modulated square wave. There are a whole host of settings that are relevant to what the square wave does, from continuous set frequency, set mark:space ratio to PWM encoding information from a small(32 bytes,words?) or from a DMA setup. so, in this case it is an output, but its not just outputting a single bit value.
>)))'><'(((<
User avatar
Posts: 457
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg