Simple GPIO

Kampi
 
Posts: 14
Joined: Tue Nov 25, 2014 6:50 am

Simple GPIO

Tue Nov 25, 2014 2:18 pm

Hello,

I try to write a simple IO Lib for me. For that I use a modification of a existing code:

Code: Select all
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define BLOCK_SIZE         4096
#define GPIO_Basis         0x10000600

struct RT5350_Peripherie {
   unsigned long Adresse;
   int Memory;
   void *Map;
   volatile unsigned int *Addr;
};

struct RT5350_Peripherie GPIO = {GPIO_Basis};

int RAM_Map(struct RT5350_Peripherie *Peripherie);

// Funktion zum markieren der Speicherbereiche
int RAM_Map(struct RT5350_Peripherie *Peripherie)
{
   Peripherie->Memory = open("/dev/mem", O_RDWR | O_SYNC);

   if(Peripherie->Memory < 0)
   {
      printf("Oeffnen von /dev/mem fehlgeschlagen!\n");
      return -1;
   }

   Peripherie->Map = mmap(
      NULL,
      BLOCK_SIZE,
      PROT_READ | PROT_WRITE,
      MAP_SHARED,
      Peripherie->Memory,
      Peripherie->Adresse
   );

   if(Peripherie->Map < 0)
   {
      printf("Mapping fehlgeschlagen!\n");
      return -1;
   }

    Peripherie->Addr = (volatile unsigned int *)Peripherie->Map;
   return 0;
}

int main()
{
   if(RAM_Map(&GPIO) == -1)
   {
      printf("Mappen der GPIO fehlgeschlagen!\n");
      return -1;
   }

   *(GPIO.Addr + 0x74) = 16;
   *(GPIO.Addr + 0x78) = 16;
}



I want to set GPIO 26 to High-State, but I got a "Segmentation fault" error.
Did I use the wrong adresses?
Thanks!

Kampi
 
Posts: 14
Joined: Tue Nov 25, 2014 6:50 am

Re: Simple GPIO

Thu Nov 27, 2014 7:48 am

Nobody an idea :(?

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: Simple GPIO

Thu Nov 27, 2014 6:55 pm

Kampi wrote:Nobody an idea :(?



I will try this out after holidays, and will come with a solution.

I'm very interested also.

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: Simple GPIO

Thu Nov 27, 2014 7:16 pm

Pyrofer posted a snippet of working code that shows how to map the hardware registers into application address space and use that to control the GPIO directly. I suggest you start with that working code.

viewtopic.php?f=10&t=87&start=10

Kampi
 
Posts: 14
Joined: Tue Nov 25, 2014 6:50 am

Re: Simple GPIO

Thu Nov 27, 2014 9:49 pm

Yes, but the Problem is, that I don understand why he use the addres 0x10000000. I take a look into the Datasheet and see that the addres of the PIO Controller is 0x10000600. :(

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: Simple GPIO

Fri Nov 28, 2014 12:03 am

These statements are using pointer arithmetic to add offsets to that base address:

Code: Select all
#define set(pin) gpio[0x62c>>2]=1<<pin
#define clear(pin) gpio[0x630>>2]=1<<pin


If you work out the resulting addresses I expect you'll find they match the addresses of the corresponding GPIO registers.

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: Simple GPIO

Fri Nov 28, 2014 5:50 am

guys please a full working example.

Kampi
 
Posts: 14
Joined: Tue Nov 25, 2014 6:50 am

Re: Simple GPIO

Fri Nov 28, 2014 5:58 am

@ Peter:
Yes I know this technique.
As you can see on my screenshot the PIO Base-Register is 0x1000_0600. So why he use 0x1000_0000.
I want understand my Problem and not the single solution.

Edit:
During writing these text, the solution of the Problem appears in my head..
I will write a full solution for GPIO + Explanation when I´m back at home!
Attachments
Unbenannt.png
Unbenannt.png (88.36 KiB) Viewed 274041 times

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: Simple GPIO

Fri Nov 28, 2014 12:08 pm

Kampi wrote:As you can see on my screenshot the PIO Base-Register is 0x1000_0600. So why he use 0x1000_0000.


The expression Pyrofer uses to access the I/O control registers adds an offset to that base address to calculate the address of the particular register that is to be addressed, as shown in Pyrofer's code I posted previously. I don't know why Pyrofer chose to map the whole address range instead of just the PIO registers, but perhaps he wanted a more general solution which gave him access to other registers too.

Kampi
 
Posts: 14
Joined: Tue Nov 25, 2014 6:50 am

Re: Simple GPIO

Fri Nov 28, 2014 12:55 pm

I think you must access the PIO Controller over Palmbus.
Take a look at the Device Tree:

https://dev.openwrt.org/browser/trunk/t ... ?rev=36246

You can see, that the GPIO-Controller is a child of the Palmbus. So I think you must access the Palmbus (over address 0x10000000) and then you can access the GPIO-Controller.
You see the Startaddress of gpio0 ist 0x600 (Offset to the Palmbus).
If you want access this peripheral components you have to address the Bus and with a Offsetaddress from the Bus you can access peripheral components.

Next
Return to VoCore & VoCore+Dock

Who is online

Users browsing this forum: No registered users and 30 guests