Access GPIO/PORTS in mini2440

Sepp bauer
Hi There,

Im Trying to access the GPIO using c++

Whenever i try to set any register the programm fails droping the error
"Segmentation fault" .. i use teh addresses.h i found in the kernel
directory, 

does anybody have any idea how to access the GPIO?

what im actually trying to do is to set the Register for the internet
serial port for the RTS signals.

Does anybody have any c/c++ examples how to access the GPIO/any other port
on the mini 2440 using the arm linux

Kindest regards
Sepp

gene.sally
Hi Sepp,

I've wimped out with the GPIO ports and I'm using the /sys file system to
handle communicating with this for me.  I did a little set-up in the
board's specific start-up code to put things in the right state, but after
that, I've used what's available in /sys to handle this for me, because
it's adequate for my application.

Anyway, here's what I did:

1- get gpio information
You can echo into /sys/class/gpio/export the GPIO number you want exported,
but to do this, you'll need to know the GPIO bank's "base".    Here's the
cheap way of finding the "base" numbers for the G

$ cd /sys/class/gpio
$ for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done
GPIOA: 0
GPIOE: 128
GPIOF: 160
GPIOG: 192
GPIOH: 224
GPIOB: 32
GPIOC: 64
GPIOD: 96

2- Calc your gpio #
If you want line 1 from GPIOE, you would add 1 to the base of GPIOE, in
this case that would equal 129

3- Echo your gpio # into 

Then you echo the number into /sys/class/gpio/export

$ echo 129 > /sys/class/gpio/export

The kernel will create a directory

/sys/class/gpio/gpio166

which contains files for the direction and value of the GPIO.  Not all of
the GPIO lines can be reached this way (I haven't investigated enough to
know why...) You can then read and write to that file like any other file.

You may need to do something more complex, but this is what worked for my
application, where I could poll the file to get the state of the GPIO and
write to it occasionally.

Regards,
gene

h4med
Hi gene.sally
Would you please write the C program that dose what you say (changing a
GPIO status).
I've seen this:
http://www.avrfreaks.net/wiki/index.php/Documentation:Linux/GPIO
but it isn't for mini2440 and dose not work...

loffe
Hi Sepp, perhaps you can  use mmap() function for accessing GPIO.

raghu
Hi ALL Please send me sample code to access GPIO's of MINI2440. how  should
i access them using system calls or by drivers ..please help me out 
thanks 
raghu

Andreo
I am about to develope a opensource solution:
https://sourceforge.net/projects/gpiolib

i will be finish in a few weeks.

But the driver must be installed with re-compilation of kernel

greetings

vinayak
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

FILE *fp; 

int main(int argc, char** argv)
{

  printf("\n**********************************\n"
      "*  Welcome to PIN Blink program  *\n"
      "**********************************\n");
  
  //create a variable to store whether we are sending a '1' or a '0'
  char set_value[20]; 
  //Integer to keep track of whether we want on or off
  int toggle = 0;
  
  //Using sysfs we need to write "160" to /sys/class/gpio/export
  //This will create the folder /sys/class/gpio/gpio160
  if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL)
    {
      printf("Cannot open export file.\n");
      exit(1);
    }
  //Set pointer to begining of the file
    rewind(fp);
    //Write our value of "160" to the file
    strcpy(set_value,"160");
    fwrite(&set_value, sizeof(char), 3, fp);
    fclose(fp);
  
  printf("...export file accessed, new pin now accessible\n");
  
  //SET DIRECTION
  //Open the LED's sysfs file in binary for reading and writing, store file
pointer in fp
  if ((fp = fopen("/sys/class/gpio/gpio160/direction", "rb+")) == NULL)
  {
    printf("Cannot open direction file.\n");
    exit(1);
  }
  //Set pointer to begining of the file
  rewind(fp);
  //Write our value of "out" to the file
  strcpy(set_value,"out");
  fwrite(&set_value, sizeof(char), 3, fp);
  fclose(fp);
  printf("...direction set to output\n");
  
  
    strcpy(set_value,"1");
    

int ch;

while(ch!=3)
  {
printf("1:LED ON\n2:LED OFF\n3:exit");
printf("\nenter your choice\n");
scanf("%d",&ch);  
switch(ch){

case 1:  
  {if ((fp = fopen("/sys/class/gpio/gpio160/value", "rb+")) == NULL)
    {
      printf("Cannot open value file.\n");
      exit(1);
    }
      //Set pointer to begining of the file
      rewind(fp);
      //Write our value of "1" to the file 
      strcpy(set_value,"1");
      fwrite(&set_value, sizeof(char), 1, fp);
      fclose(fp);
      printf("...value set to 1...\n");
  
  } break;
    case 2:
    {if ((fp = fopen("/sys/class/gpio/gpio160/value", "rb+")) == NULL)
    {
      printf("Cannot open value file.\n");
      exit(1);
    }
      //Set pointer to begining of the file
      rewind(fp);
      //Write our value of "0" to the file 
      strcpy(set_value,"0");
      fwrite(&set_value, sizeof(char), 1, fp);
      fclose(fp);
      printf("...value set to 0...\n");
  
  } break;
    
}  
  }
  return 0;
}

vinayak
the above program works for mini2440

ahamedMoussa
Hey vinayak
I tested this program I maunched in the mini 2440 I want know what pin of
the board's GPIO do you test ? is it

xyz
gpio160  There is a table somewhere (datasheet?) that maps that to a
specific GPIO pin.

xyz
http://www.avrfreaks.net/wiki/index.php/Documentation:Linux/GPIO

See GPIO numbers

anirban
i want to use port G of GPIO as input in friendly arm mini2440. Please
suggest how to do this.how can i access the control register of the GPIO
ports programatically.

Juergen Beisert
> how can i access the control register of the GPIO
> ports programatically

You can't if you run Linux. Use sysfs instead.

sathiapr
I tried to access the GPIOG0,GPIOG1 as input and GPIOF0-GPIOF3 as output in
mini2440 .I follwed the method specified by Mr."Vinayak".On implementaion
after compilation I got a message "can't open the direction file".

Then I tried /sys/class/gpio/export
echo "193" > /sys/class/gpio/gpio193/direction","in"
etc...
I can't configure GPIOF as "out" using similar commands
how can do this?help me..

davef
Can you set  any ports on GPIOG as output?

Got curious so had a look at mach-mini2440.c in Linux kernel 3.7 it is in
the directory arch/arm/mach-s3c24xx and I see that GPIOG0, 3, 5, 6, 7 and
11 seemed to be setup for special purposes.

Just a thought?

Which kernel are you using?

davef
and GPG4 as backlight and GPG8 for something to do with MMC/SD

Linux kernel 3.4 is the same, haven't got any earlier ones.

sathiapr
Mr devef I read ur reply.My kernel is 2.6.32.2 .
Which are the ports that we can access as I/o?

Juergen Beisert
sathiapr,

read the Mini2440's schematic and find free or unused GPIOs. Almost all
pins of the S3C2440 SoC can act as GPIO, but some have special meaning or
are already in use. So, you can only use the remaining GPIOs.

sathiapr
Thank You Mr.Juergen.From the schematics,I found that GPIOB 0,1 are free in
GPIO port with no other function.I set the direction of GPIOB0 as out and
gave 0 followed by 1.I saw in all condition the pin  is always HIGH.Also in
the case of GPIOB1 I can't set direction.For my work I need at least 4 GPIO
pin.How can get it?If anyone knows proper access please help me....

Samyukta Ramnath
Mr. Vinayak,
Am I supposed to compile the code on the computer or on the board? Because
the file that you specified (sys/class/gpio) is present on the PC, but in
the code you have mentioned entering a parameter to set the LED on or off,
which means I should be connected to the board, right? 
Warm regards

S RIZZUHUSSAIN
Hi vinayak
 I had compiled your program and executed  but there is no response on the
board