cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Contributor
Contributor
3,811 Views
Registered: ‎10-29-2016

"size_t length" in mmap function and Address Range of IPs

Jump to solution

Hello, everyone.

 

There is an example in 5.4 Controlling LEDs and switches in Linux exampleZynq ZedBoard Concepts, Tools, and Techniques,  ZedBoard (Vivado 2014.2).


In this example, mmap function is used to access the leds, switches.

 

switches = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41200000);
leds = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41210000);
buttons = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41220000);

 
The addresses range of switches, leds and buttons are all 64K according to the Figure 5-16: Address Editor Offset Addresses for GPIO peripherals, Page 72.

 

vivado.PNG

 

So I have two questions: 


1, Why is the second parameter of the mmap function not 64K, but getpagesize() as 4096?

2, Why the default address range is 64K in the Figure 5-16? Because the address bits of sws, leds and btns are 16bits each? 

 

The full c program is as below:

 

/* THIS IS THE FILE THAT writes to LEDs and reads from switches..linux
 * Copyright (c) 2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>


int main()
{
	volatile unsigned int *leds;
	volatile unsigned int *switches;
	volatile unsigned int *buttons;

	unsigned int leds_val = 0;
	unsigned int switches_val = 0;
	unsigned int buttons_val = 0;

	int i = 0;

	int fd = open("/dev/mem", O_RDWR|O_SYNC);

	if (fd < 0) {
		printf("Error...");
		exit (1);
	}

    printf("Hello World! \n");
    printf("This program will run an infinite loop. \r\n");
    printf("To end the program, press any of the five directional buttons. \r\n");

	switches = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41200000);
	printf("Switches mmap Good... \r\n");

	leds = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41210000);
	printf("LEDs mmap Good... \r\n");

	buttons = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x41220000);
	printf("Buttons mmap Good... \r\n");

	while(1){
		switches_val = *switches;
		buttons_val = *buttons;

		printf("Readback value of the switches is %x \r\n", switches_val);
		*leds = switches_val;

		if (buttons_val != 0){
			break;
		}

		for (i = 0; i < 99999; i++){}
	}

	printf("BYE! \r\n");
    return 0;
}

 

Best regards,

feng

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Xilinx Employee
Xilinx Employee
6,478 Views
Registered: ‎10-06-2016

Hi Feng,

 

1. The registers of this IPs are not longer than a page size so there is no reason to map all 64k. Take into account that mmap length size only can be Page size.

 

2. Not sure for the response but I would say that 64K default size is just for easier mapping address. Using 0x10000 chunks makes easier to map the IPs.

 

Kind Regards,

Ibai Erkiaga


Ibai
Don’t forget to reply, kudo, and accept as solution.

View solution in original post

0 Kudos
2 Replies
Highlighted
Xilinx Employee
Xilinx Employee
6,479 Views
Registered: ‎10-06-2016

Hi Feng,

 

1. The registers of this IPs are not longer than a page size so there is no reason to map all 64k. Take into account that mmap length size only can be Page size.

 

2. Not sure for the response but I would say that 64K default size is just for easier mapping address. Using 0x10000 chunks makes easier to map the IPs.

 

Kind Regards,

Ibai Erkiaga


Ibai
Don’t forget to reply, kudo, and accept as solution.

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor
3,727 Views
Registered: ‎10-29-2016

Hi Ibai.

 

Thanks for your answer.

 

Best regards,

feng

0 Kudos