Nios Community Wiki > Accessing hardware registers from user space programs

Accessing hardware registers from user space programs

Table of contents
No headers

MMU-less systems allow access to memory registers very easily, but the application code is then highly coupled to the system configuration: if the NIOS system is modified, and the hardware base address changed, the application must be recompiled. Worse, if the device is used in two different systems, it may be mapped at different addresses and the application must be compiled twice.

The purpose of this Wiki is to show how to manage hardware from user space, getting its base address thanks to a small driver. This one transmits the hardware base address thanks to mmap() routines.

As an example, we will use the small device "HR Timer" (see attached file) which increments a 32 bits counter at a given frequency. The  parameter "COUNT_VALUE", with the default value 100000, gives a frequency of 1000 Hz for a system running at 100 MHz. The counter is reset to 0 at init time, but can be set later to a given value using a write access at the base address.

To access this device, one will use the driver "hrtimer.c".

This driver, see attached file "hrtimer.c", implements mainly the mmap() API.

The "userland" application has just to perform an 'open("/dev/hrtimer")' to get a handdle on the device.

The mmap() system call then permits to get the physical address to use to access the device registers.

Some other Wiki give explanations (see ModuleProgramming) on how to add a driver to the kernel, but one have mainly to modify Makefile and Kconfig in the directory where the driver will be placed, for e.g. in linux-2.6/arch/nios2/drivers.

The following lines are added in Kconfig :

config NIOS2_HRTIMER
 tristate "32 bits high-res timer"
 default N
 help
   Say Y here if you use high-res timer

and this line in Makefile

 obj-$(CONFIG_NIOS2_HRTIMER) += hrtimer.o

 

Don't forget to set a device node in uClinux-dist/vendors/Altera/nios2/romfs_list.

 

Once the kernel has been completed with the driver, and regenerate, the userland program can access the hrtimer without read() nor ioctl(), using the simple code like:

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

static volatile unsigned long* pHrTimer = 0;
static int hrtimerFd = -1;

/* init */
int HrTimerInit()
{
  if ((hrtimerFd = open("/dev/hrtimer", O_RDWR)) != -1) {
    pHrTimer = (unsigned long*)mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED, hrtimerFd, 0);
    if (pHrTimer == MAP_FAILED)
      return -1;
  }
  return hrtimerFd;
}

/* utilization */
int TickGet()
{
  return *pHrTimer;
}

/* deInit */
int HrTimerEnd()
{
  if (pHrTimer != MAP_FAILED)
    munmap((void*)pHrTimer, 4);
  if (hrtimerFd != -1)
    close(hrtimerFd);
}

Tag page

Files 2

FileSizeDateAttached by 
 hrtimer.c
Linux driver for "hrtimer" device
3.41 kB18:01, 3 Mar 2009frdrc66Actions
 hrtimer.vhd
Hardware Device Code (VHDL file)
1090 bytes18:02, 3 Mar 2009frdrc66Actions
Viewing 9 of 9 comments: view all
this works well for me. I was able to access it easy. thanks for the info. texas mesothelioma attorneys
Posted 18:47, 28 Dec 2009
free energy
eczema cure This driver, see attached file "hrtimer.c", implements mainly the mmap() API. The "userland" application has just to perform an 'open("/dev/hrtimer")' to get a handdle on the device. The mmap() system call then permits to get the physical address to use to access the device registers. Some other Wiki give explanations (see ModuleProgramming) on how to add a driver to the kernel, but one have mainly to modify Makefile and Kconfig in the directory where the driver will be placed, for e.g. in linux-2.6/arch/nios2/drivers. The following lines are added in Kconfig : config NIOS2_HRTIMER tristate "32 bits high-res timer" default N help Say Y here if you use high-res timer and this line in Makefile : obj-$(CONFIG_NIOS2_HRTIMER) += hrtimer.o Don't forget to set a device node in uClinux-dist/vendors/Altera/nios2/romfs_list. Once the kernel has been completed with the driver, and regenerate, the userland program can access the hrtimer without read() nor ioctl(), using the simple code like: #include #include #include static volatile unsigned long* pHrTimer = 0; static int hrtimerFd = -1; /* init */ int HrTimerInit() { if ((hrtimerFd = open("/dev/hrtimer", O_RDWR)) != -1) { pHrTimer = (unsigned long*)mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED, hrtimerFd, 0); if (pHrTimer == MAP_FAILED) return -1; } return hrtimerFd; } /* utilization */ int TickGet() { return *pHrTimer; } /* deInit */ int HrTimerEnd() { if (pHrTimer != MAP_FAILED) munmap((void*)pHrTimer, 4); if (hrtimerFd != -1) close(hrtimerFd); }
Posted 12:11, 26 Jan 2010
"As an example, we will use the small device "HR Timer" (see attached file) which increments a 32 bits counter at a given frequency. The parameter "COUNT_VALUE", with the default value 100000, gives a frequency of 1000 Hz for a system running at 100 MHz. The counter is reset to 0 at init time, but can be set later to a given value using a write access at the base address." I can't understand this, can you elaborate more on it? Maria, support @ Presentation Skills
Posted 17:19, 16 Feb 2010
WOW...this is my new love...great music...listening to the album on youtube now :) Bachelor Degree Public Administration AND Doctorate Political Science AND Doctorate Public Administration PhD Political Science AND bachelors degree english
Posted 11:43, 26 Feb 2010
nice to be here.... thanks for share

nowGoogle.com adalah Multiple Search Engine Popular|intermezo

Posted 05:26, 6 Mar 2010
Excellent post. This was actually what I was looking for, and I am glad that I finally came here! This for sharing and keep up the good work... thanks for this nice article Good post.....Valuable information for all. I will recommend my friends to read this for sure… Wedding Dresses Wedding Dresses 2010 Beach Wedding Dresses A-Line Wedding Dresses Strapless Wedding Dresses
Posted 03:05, 8 Mar 2010
This is a very informative, I like the topic. I just found this site while finding a website that works for pressurized walls and pressure walls. I really like the article edited 15:38, 8 Mar 2010
Posted 15:37, 8 Mar 2010
This article really helped me a lot! Thanks for posting this..
transfer to ipod | transfer ipod to computer
Posted 15:44, 8 Mar 2010
Aubry appears in louis vuitton Spring Collection 2010Your comment is touching but it sounds louis vuitton speedy, I'm so sick of you and your racist kind harping on about being previously disadvantaged, and that the whites had it so good, we had to work for what we had, the louis vuitton shop didn't give us anything back then, louis vuitton replica, and today we still have what we have because when we meet adversity or poverty we just work harder, replica louis vuitton and our children and we teach them of no work no pay. What are you looking for now a free ride, the ANC has had plenty of time to give you that! Or Super louis vuitton Malema to rescue you.
Posted 01:22, 9 Mar 2010
Viewing 9 of 9 comments: view all
You must login to post a comment.
SourceForge.net