Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:01:03+00:00 2026-05-24T05:01:03+00:00

First off i’m new to Linux programming so apologies if this makes no sense

  • 0

First off i’m new to Linux programming so apologies if this makes no sense or I’m barking up the wrong tree, point me in the correct direction.

I’m trying to write a cpp app to talk to a FPGA over a pci bus, in userland.

The code I have written so far, enumerates over the directories in /sys/bus/pci/devices checking the device and vendor files to locate the correct one.

Once I’ve found the device I know that the mapped regions I need to write to are somehow represented by resource[n] files, but i’m not sure how to use them to read/write some values.

From the code written for another OS I know I want to talk to BAR1 of the PCI device, the way i’ve (tried to) do this is with mmap (is this the right way to go about it?). First I get a filehandle to /sys/bus/pci/devices/[device_addr]/resource1 with O_RDWR, then call mmap like so:

char *map = (char*)mmap(NULL, FPGA_MEM_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

Where fd is the file descriptor from open.

Am I going about this the correct way or are the better functions/calls to use? I realise I could write a kernel module to do this, but I’d rather not have to get into kernel module programming just yet.

If it helps I can pastebin the code for a short while, but i’m not really after a critique of the code, just some pointers on the best method to do it.

If there are any details i’m missing just ask.

I’m using a 2.6 Linux kernel, based on ubuntu 11.04 (running on a live usb), the hardware is x86 based.

Thanks

UPDATE:
After even more googling about and trial and error with the code I got it working.

The steps I followed where:

  • Identify device using /sys/bus/pci/devices/[device]/[vendor|device]
  • Parse /sys/bus/pci/devices/[device]/resource, for each line the first
    column is the start address and the second is the end address of the
    mapped region. I wanted BAR1 so the second line had the values I
    needed.
  • open("/dev/mem", O_RDWR) to get file descriptor fd
  • call mmap as above, but pass in the start address as the offset (last
    argument) and use the start and end address to get the mapped regions
    size (second argument).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:01:04+00:00Added an answer on May 24, 2026 at 5:01 am

    That’s a perfectly acceptable mmap call and should work correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, apologies if this question has been asked before but I couldn't find
First off, I know this may be a very stupid question, so don't shoot
First off I'm really new to android (< 4 days). I'm trying to wrap
First off, please do not tl;dr this. It just a lot of quickly-scannable code.
First off, I'm aware that there are many questions related to this, but none
First off, I am sorry if this is a duplicate, but every time I
First off, I'm not really sure how much information is necessary to include because
First off, if there is a true, official way of having flash/flex's NetConnection s
First off, I’m not concerned with portability, and can safely assume that the endianness
First off let me say I am maintaining someone else's poorly designed code so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.