I use the following code to extract the board revision.
unsigned gpioHardwareRevision(void)
{
static unsigned rev = 0;
FILE * filp;
char buf[512];
char term;
if (rev) return rev;
filp = fopen ("/proc/cpuinfo", "r");
if (filp != NULL)
{
while (fgets(buf, sizeof(buf), filp) != NULL)
{
if (!strncasecmp("revision\t", buf, 9))
{
if (sscanf(buf+strlen(buf)-5, "%x%c", &rev, &term) == 2)
{
if (term == '\n') break;
rev = 0;
}
}
}
fclose(filp);
}
return rev;
}
See http://elinux.org/RPi_HardwareHistory for the meaning of the returned numbers.
There are three different pinouts according to the revision.
If Revision < 4
pinout type 1
else if Revision < 16
pinout type 2
else
pinout type 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Type.1 X X - - X - - X X X X X - - X X
Type.2 - - X X X - - X X X X X - - X X
Type.3 X X X X X X X X X X X X X X
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Type.1 - X X - - X X X X X - - - - - -
Type.2 - X X - - - X X X X - X X X X X
Type.3 X X X X X X X X X X X X - - - -