/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 !
-
There is an abstraction for mmap in QFile:
"http://doc.trolltech.com/4.6/qfile.html#map":http://doc.trolltech.com/4.6/qfile.html#map
-
This is the way I do it :
@ uchar *gpio;
QFile f("/dev/mem");
if (f.open(QIODevice::ReadWrite) == false)
{
std::cerr << "Could not open /dev/mem : " << strerror(errno) << std::endl;
exit (1);
}f.flush(); gpio = f.map(0x10015000UL, 4096); if (gpio == 0) { std::cerr << "Mapping failed : " << strerror(errno) << std::endl; }
@
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 ? -
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 :
@ int fd;
void* map_base;if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1) { std::cerr << "Failed to open /dev/mem : " << strerror(errno) << std::endl; exit (1); } std::cerr << "Open successful" << std::endl; map_base = mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x10015000UL); if (map_base == MAP_FAILED) { std::cerr << "Mapping failed : " << strerror(errno) << std::endl; exit (2); } 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 ?
-
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...