cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Visitor
Visitor
2,873 Views
Registered: ‎05-03-2018

Confused about mmap gpio access

Jump to solution

Hi, I am currently using a zedboard with xillinux on it. For my internship I must realise a high speed communication board to board under linux.

At first I tried the gpio driver with /sys/gpio but unfortunately it is too slow for my application, so naturally I tried using mmap() but i can't figure how to set port in/out or even  writing value to a particular port.

 

I can't manage to find good tutorial on this, most of the explanations i found was for raspberry pi, to be precise when I look at  a guide like this one :

https://elinux.org/EBC_Exercise_11b_gpio_via_mmap

I don't find corresponding address for the zeboard (right now I have only found the start and the end address for the gpio driver 0xE000A000 and 0xE000AFFF).

 

So if someone could explain to me how to set the led 4 (port 61 for /sys/gpio with the linux offset of 54) to output and making it blink, and how to find how to do it with other ports that would be great.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Visitor
Visitor
3,248 Views
Registered: ‎05-03-2018

Re: Confused about mmap gpio access

Jump to solution

Finally I found the solution by trials and errors.

It was because I was using Bank 0 but the leds are on bank 2.

View solution in original post

0 Kudos
7 Replies
Highlighted
Contributor
Contributor
2,854 Views
Registered: ‎04-04-2018

Re: Confused about mmap gpio access

Jump to solution

Download a copy of the the Zynq TRM: UG-585, then read Chapter 14: "General Purpose I/O (GPIO)" and review Appendix B.19: "General Purpose I/O (gpio)". These two sections will likely answer most of your questions about the gpio. If you want to code some blinkenlights, pay close attention to the MASK_DATA_X and DATA_X registers. You'll also need to review the OEN and DIRM registers.

 

Highlighted
Visitor
Visitor
2,812 Views
Registered: ‎05-03-2018

Re: Confused about mmap gpio access

Jump to solution

Thanks for the documentation it helps me a lot.

But I still have some questions, do I need to set up a clock to use gpio with mmap as seen here :

https://forums.xilinx.com/t5/Embedded-Linux/Zynq-mmap-GPIO/td-p/368601

 

And here is my code where I tried to turn led 7 on but it seems to do nothing :

 

I added in attachment my constraint file.

 

#define GPIO_BASE_ADDR 0xE000A000
#define GPIO_END_ADDR 0xE000AFFF
#define GPIO_MAP_SIZE 0x1000
#define GPIO_OFFSET_DATA_BANK0 (0x0040)
#define GPIO_OFFSET_DIRM_0 (0x0204)
#define GPIO_OFFSET_OEN_0 (0x0208)

int gpio_Mem_Set_Value(unsigned int port);

int main(int argc, char *argv[]) {
gpio_Mem_Set_Value(10);
return 0;
}
int gpio_Mem_Set_Value(unsigned int port) {
int fd = open("/dev/mem", O_RDWR);
volatile void *gpio_ptr = mmap(NULL, GPIO_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE_ADDR);
if(gpio_ptr == MAP_FAILED){
perror("Clk memory mapping failed");
exit(EXIT_FAILURE);
}
volatile unsigned int *setDirectionAddr;
volatile unsigned int *setValueAddr;
volatile unsigned int *setOutputEnableAddr;
setDirectionAddr = gpio_ptr + GPIO_OFFSET_DIRM_0;
setOutputEnableAddr = gpio_ptr + GPIO_OFFSET_OEN_0;
setValueAddr = gpio_ptr + GPIO_OFFSET_DATA_BANK0;

// Direction out
*setDirectionAddr = (unsigned) (1 << port);
*setOutputEnableAddr = (unsigned) (1 << port);
sleep(1);
// Writing data
*setValueAddr = (unsigned) (1 << port);
close(fd);
return 0;

 

0 Kudos
Highlighted
Adventurer
Adventurer
2,806 Views
Registered: ‎06-10-2014

Re: Confused about mmap gpio access

Jump to solution

Hello,

 

This may not be directly in your intended solution path, but since you have Xillinux up and running, you also have Xillybus and Xillybus Lite already installed and ready for use.

 

You can therefore use Xillybus streams to transport data at a much higher rate than direct GPIO bit banging would allow. And then sense / toggle I/O by virtue of logic on the PL side.

 

However this requires some basic logic design. On the other hand, using GPIO on a Zynq and complaining it's too slow, is a bit like pushing a Ferrari instead of turning the engine on and drive away.

 

And if you insist on bit banging, and don't get the GPIO memory mapping thing sorted out, you can take a look on Xillybus Lite, which is a ready-to-go memory mapped interface. It will still require a couple of lines of Verilog / VHDL, but it's a relatively low hanging fruit.

 

Regards,

   Eli

Highlighted
Visitor
Visitor
2,796 Views
Registered: ‎05-03-2018

Re: Confused about mmap gpio access

Jump to solution

Thanks for you answer,

 

Although it would be easier to use xillybus, I must stick to bit banging for portability reason.

So my question stay the same, why does this chunk of code does not turn the led on, is it missing something ?

 

Regards,

Mathieu.

0 Kudos
Highlighted
Visitor
Visitor
3,249 Views
Registered: ‎05-03-2018

Re: Confused about mmap gpio access

Jump to solution

Finally I found the solution by trials and errors.

It was because I was using Bank 0 but the leds are on bank 2.

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor
2,777 Views
Registered: ‎04-04-2018

Re: Confused about mmap gpio access

Jump to solution

Hi Mathieu,

 

I'm glad you have things working. The LD9 LED is connected to MIO7 (bank 0) ... so that's available as well.

 

Best Regards,

--Scott

 

0 Kudos
Highlighted
Visitor
Visitor
1,243 Views
Registered: ‎07-09-2019

Re: Confused about mmap gpio access

Jump to solution

I facing exactly the same problem. I am trying to write to a PS_gpio pin(to the one connected to LD9 on zedboard-MIO-0 7 ) using mmap but I am unable to read from or write to any memory location. I am not exactly sure where the mistake is. Even selecting BANK2 did not solve the problem

The default zedboard device tree from "meta-xilinx" git was used while building linux. Tomorrow, I will like to try running the same program after removing the GPIO driver from the device tree. I am quite new in using linux so any help is welcome. Thanks.

0 Kudos