September 28, 2010

filip filip
Lab Rat
63 posts

/dev/mem

 

I have to address gpio’s on an embedded device (with MX27 processor) from my Qt application. I think I can do it via memory mapping, i.e. using /dev/mem. I found some examples in C code using mmap. However, I don’t know how to do it in Qt. Is it possible to use QFile for opening the device (and then use map() function to do the mapping) ? Or do I have to subclass QIODevice and implement things myself ? Are there better alternatives to address gpio ? Maybe an example ?

Thanks !

4 replies

September 29, 2010

jbarron jbarron
Lab Rat
48 posts

There is an abstraction for mmap in QFile:

http://doc.trolltech.com/4.6/qfile.html#map [doc.trolltech.com]

September 30, 2010

filip filip
Lab Rat
63 posts

This is the way I do it :

  1.     uchar *gpio;
  2.     QFile f("/dev/mem");
  3.     if (f.open(QIODevice::ReadWrite) == false)
  4.     {
  5.       std::cerr << "Could not open /dev/mem : " << strerror(errno) << std::endl;
  6.       exit (1);
  7.     }
  8.  
  9.     f.flush();
  10.     gpio = f.map(0x10015000UL, 4096);
  11.  
  12.     if (gpio == 0)
  13.     {
  14.       std::cerr << "Mapping failed : " << strerror(errno) << std::endl;
  15.     }

However, when running the application I get the following message on the console output :

QFSFileEngine::map: Mapping a file beyond its size is not portable

And I get the “Mapping failed” message indicating the map function returned a 0.
Did I make a mistake in using QFile and map ?

September 30, 2010

filip filip
Lab Rat
63 posts

As a first experiment, I build my application for my host (Ubuntu 10.04). Can it be a problem when I use /dev/mem of the host pc to test mapping functionality ?

I also tested the mapping in its ‘basic’ way :

  1.     int fd;
  2.     void* map_base;
  3.  
  4.     if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
  5.     {
  6.       std::cerr << "Failed to open /dev/mem : " << strerror(errno) << std::endl;
  7.       exit (1);
  8.     }
  9.  
  10.     std::cerr << "Open successful" << std::endl;
  11.  
  12.     map_base = mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x10015000UL);
  13.  
  14.     if (map_base == MAP_FAILED)
  15.     {
  16.       std::cerr << "Mapping failed : " << strerror(errno) << std::endl;
  17.       exit (2);
  18.     }
  19.  
  20.     std::cerr << "mapped on :" << map_base << std::endl;

But this also fails. The opening of the device is successful, but the mapping fails with :

Mapping failed : Operation not permitted

Any ideas ?

September 30, 2010

filip filip
Lab Rat
63 posts

The second way of working, using mmap (see previous message), seems to work on my embedded device. Mmap is returning a valid address. So, it looks like it will work that way. Still have to test if I can really address the hardware on these addresses…

 
  ‹‹ problem using qtSoap      QT simulator symbian vs N900 ››

You must log in to post a reply. Not a member yet? Register here!