element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Avnet Boards Forums
  • Products
  • Dev Tools
  • Avnet Boards Community
  • Avnet Boards Forums
  • More
  • Cancel
Avnet Boards Forums
Software Application Development Problem using mmap() function
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Avnet Boards Forums to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 314 subscribers
  • Views 147 views
  • Users 0 members are here
Related

Problem using mmap() function

Former Member
Former Member over 10 years ago

I am trying to execute the following code in linaro on zed board. My problem is that I am trying to access the Physical locations by mapping them to virtual locations and the problem is I am trying to access consecutive memory locations 0x1ff00000 0x1ff00004 0x1ff00008 0x1ff00012. mmap is allocating a valid address to 0x1ff00000 and for rest all it is returning a virtual address of 0xffffffff.

Here is my source code

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>

#definetttDMA_BASEADDRtt0x40400000
#definetttS2MM_DMACRtt0xC
#definetttS2MM_DMASRtt0xD
#definetttS2MM_DAttt0x12
#definetttS2MM_LENGTHtt0x16
#definetttCOUNTER_BASEADDRt0x43C00000
#definetttDEST_ADDRtt0x1FF00000
#definetttPAGE_SIZEtt0x1000
#define                 SECOND_BYTE             0x1FF00004
#define                 THIRD_BYTE              0x1FF00008
#define                 FOURTH_BYTE             0x1FF00012

int main (void) {

int fd=0;
if ( (fd = open("/dev/mem", O_RDWR)) < 1)tttt
{
perror("
Error opening device file
");
return -1;
}
fprintf(stdout, "
Opened device file successfully
");

unsigned int *dma_base_address;
unsigned int *counter_base_address;
dma_base_address=(unsigned int*)DMA_BASEADDR;
counter_base_address=(unsigned int*)COUNTER_BASEADDR;

printf("Original DMA Base:0x%08x
",dma_base_address);
printf("Original Counter Base:0x%08x
",counter_base_address);

unsigned int *virtual_dma_base_address;
unsigned int *virtual_counter_base_address;

virtual_dma_base_address=(unsigned int*)mmap(NULL, 65 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, DMA_BASEADDR);
if (virtual_dma_base_address == NULL)
        perror("
Unable fetch virt. addr. for AXI DMA
");

printf("Memory mapped DMA Base Address: %08x
",virtual_dma_base_address);

virtual_counter_base_address=(unsigned int*)mmap(NULL, 16 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, COUNTER_BASEADDR);
if (virtual_counter_base_address == NULL)
        perror("
Unable fetch virt. addr. for AXI Counter
");

printf("Memory mapped counter Base Address: %08x
",virtual_counter_base_address);

unsigned int *virtual_address_firstchunk;
unsigned int *virtual_address_secondchunk;
unsigned int *virtual_address_thirdchunk;
unsigned int *virtual_address_fourthchunk;

virtual_address_firstchunk=(unsigned int*)mmap(NULL, 4 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, DEST_ADDR);
virtual_address_secondchunk=(unsigned int*)mmap(NULL, 4 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, SECOND_BYTE);
virtual_address_thirdchunk=(unsigned int*)mmap(NULL, 4 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, THIRD_BYTE);
virtual_address_fourthchunk=(unsigned int*)mmap(NULL, 4 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, FOURTH_BYTE);

printf("First read addr virtual addr: 0x%08x
",virtual_address_firstchunk);
printf("Second read addr virtual addr: 0x%08x
",virtual_address_secondchunk);
printf("Third read addr virtual addr: 0x%08x
",virtual_address_thirdchunk);
printf("Fourth read addr virtual addr: 0x%08x
",virtual_address_fourthchunk);

}

Executing the code gives the following output


Opened device file successfully
Original DMA Base:0x40400000
Original Counter Base:0x43c00000
Memory mapped DMA Base Address: b6f9d000
Memory mapped counter Base Address: b6f9c000
First read addr virtual addr: 0xb6f84000
Second read addr virtual addr: 0xffffffff
Third read addr virtual addr: 0xffffffff
Fourth read addr virtual addr: 0xffffffff

Please could someone help me why this occurs

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube