Simple GPIO
- Kampi
- Posts: 14
- Joined: Tue Nov 25, 2014 6:50 am
- Peter H
- Posts: 58
- Joined: Wed Nov 12, 2014 10:24 pm
Re: Simple GPIO
Was there a question? You have already been shown working code with an explanation of how it works.
- Kampi
- Posts: 14
- Joined: Tue Nov 25, 2014 6:50 am
Re: Simple GPIO
Sorry for confusion.
The Code doesn't work
I try to set a GPIO as an Outout, but if I run this Programm and read out the "direction" file of that GPIO he is already marked as an input.
This is my GPIO Lib (use a fix GPIO in the "Set_Dir" Function:
The Code doesn't work

I try to set a GPIO as an Outout, but if I run this Programm and read out the "direction" file of that GPIO he is already marked as an input.
This is my GPIO Lib (use a fix GPIO in the "Set_Dir" Function:
- Code: Select all
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#define BLOCK_SIZE 4096
#define Bus_Base 0x10000000
struct RT5350_Peripherie {
unsigned long Adresse;
int Memory;
void *Map;
volatile unsigned int *Addr;
};
struct RT5350_Peripherie Bus = {Bus_Base};
int Wert = 0;
int RAM_Map(struct RT5350_Peripherie *Peripherie)
{
Peripherie->Memory = open("/dev/mem", O_RDWR);
if(Peripherie->Memory < 0)
{
printf("Kein Zugriff auf /dev/mem!\n\r");
return -1;
}
Peripherie->Map = mmap(
NULL,
BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
Peripherie->Memory,
Peripherie->Adresse
);
if(Peripherie->Map < 0)
{
printf("Peripherie mappen fehlgeschlagen!\n\r");
return -1;
}
Peripherie->Addr = (volatile unsigned int *)Peripherie->Map;
return 0;
}
int GPIO_Init(void)
{
if(RAM_Map(&Bus) == -1)
{
printf("GPIO Mappen fehlgeschlagen!\n\r");
return -1;
}
return 0;
}
int Set_Dir(int IO, char *Dir)
{
int Offset = 0;
int Shift = 0;
/*if(IO < 22)
{
Offset = (0x624>>2);
Shift = IO;
}
else if((IO > 21) & (IO < 29))
{
Offset = (0x674>>2);
Shift = IO - 22;
}
else
{
printf("Ungueltiger Wert fuer IO!\n\r");
return -1;
}
if(Dir == "I")
{
*(Bus.Addr + Offset) |= (1 << Shift);
}
else if(Dir == "O")
{
*(Bus.Addr + Offset) &= ~(1 << Shift);
}
else
{
printf("Ungueltiger Wert fuer Dir!\n\r");
return -1;
}*/
Wert = *(Bus.Addr + (0x67c >> 2));
printf("Offset: %i\n\r", Wert);
printf("IO: %i\n\r", (1 << Shift));
return 0;
}
- Greenwire-Elektronik
- Posts: 101
- Joined: Thu Dec 04, 2014 6:31 am
Re: Simple GPIO
i just tried what you have done - set the direction in 0x624h to 1 for the corresponding pin and then toggled the pin (0x634h) - value changes as does the voltage do - it is just a problem with the OS which will still show IN as direction - i do not think that i will be updated because this would mean that the system polls the status of all GPIOs.
You should validate your code with the voltage meter, not with the direction file
Rgds,
Jonas
You should validate your code with the voltage meter, not with the direction file

Rgds,
Jonas
- Kampi
- Posts: 14
- Joined: Tue Nov 25, 2014 6:50 am
Re: Simple GPIO
Great! Thanks for your response!
I will try it later with my oscilloscope
I will try it later with my oscilloscope

- Kampi
- Posts: 14
- Joined: Tue Nov 25, 2014 6:50 am
Re: Simple GPIO
Hey,
I try the same with this Code:
This should toggle Pin 2, but nothing happens. I check it with my oscilloscope.
I try the same with this Code:
- Code: Select all
*(Bus.Addr + (0x624 >> 2)) = 1 << 2;
*(Bus.Addr + (0x634 >> 2)) = 1 << 2;
This should toggle Pin 2, but nothing happens. I check it with my oscilloscope.

- Greenwire-Elektronik
- Posts: 101
- Joined: Thu Dec 04, 2014 6:31 am
Re: Simple GPIO
Kampi wrote:Hey,
I try the same with this Code:
- Code: Select all
*(Bus.Addr + (0x624 >> 2)) = 1 << 2;
*(Bus.Addr + (0x634 >> 2)) = 1 << 2;
This should toggle Pin 2, but nothing happens. I check it with my oscilloscope.
No wonder - if you look up the directory /sys/class/gpio you find all gpios which are exported - means available in user space. gpio 2 is not available, i think this might be the reason for your problem. You can try it with e.g. gpio14.
On a second thought (without checking) it could also be a problem with the multiplexed function on that pin which might interfere here.
Rgds
Jonas
- Vonger
- Posts: 910
- Joined: Sun Oct 19, 2014 6:00 am
Re: Simple GPIO
First make sure GPIO 26 is able to be used as GPIO. It is not be taken be other protocol.
Then make sure export the GPIO first(if you directly write to register, that is not necessary)
If the two steps are all right, it should be OK to use.
Then make sure export the GPIO first(if you directly write to register, that is not necessary)
If the two steps are all right, it should be OK to use.
- franki
- Posts: 7
- Joined: Wed Nov 05, 2014 2:31 pm
- Location: Usa
Re: Simple GPIO
I have vocore mounted in a self-made board... to verify the GPIO out from GPIO22 to GPIO26, a LED with a 330 ohms resistor in every out pin.... and the code works for me, but a little change....
The registers for de GPIO22 to GPIO26 are offset 0x670 to 0x684....
and this is the result for toggle is OK!!!


The registers for de GPIO22 to GPIO26 are offset 0x670 to 0x684....
- Code: Select all
*(gpio + (0x674 >> 2)) = 31;
*(gpio + (0x684 >> 2)) = 31;
and this is the result for toggle is OK!!!


- Kampi
- Posts: 14
- Joined: Tue Nov 25, 2014 6:50 am
Re: Simple GPIO
Thanks it works now, but I think I found a misstake in the Datasheet (see attachment).
I think they swap the Description of Set and Clear....
I think they swap the Description of Set and Clear....
- Attachments
-
- Error.png (38.79 KiB) Viewed 258451 times
Who is online
Users browsing this forum: No registered users and 54 guests