Welcome! Log In Create A New Profile

Advanced

Why must I use mmap() when accessing /dev/mem?

Posted by franksmicro 
Why must I use mmap() when accessing /dev/mem?
January 06, 2011 07:11PM
I'm using a ts-7200 Arm board (embedded). However, I believe this is a general question.

LEDs on that board are memory mapped. I can successfully make them blink using code like the following:


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

#define LED_ADDR 0x80840020

extern int errno;

int main()
{
        int i;
        unsigned char *leds;
        unsigned char val;

        int fd = open("/dev/mem",O_RDWR|O_SYNC);
        if(fd < 0)
        {
                printf("Can't open /dev/mem\n"winking smiley;
                return 1;
        }
        leds = (unsigned char *) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x80840000);
        if(leds == NULL)
        {
                printf("Can't mmap\n"winking smiley;
                return 1;
        }
        else
                printf("leds=%x\n",leds);

        for(i = 0; i < 256; i++)
        {
                val = i % 4;
                leds[0x20] = val;

                sleep(1);
        }

        return 0;
}

However, the following code fails with errno=22 (invalid argument):


#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#define LED_ADDR 0x80840020

extern int errno;

int main()
{
        int i;
        off_t cs;
        unsigned char val;

        int fd = open("/dev/mem",O_RDWR|O_SYNC);
        if(fd < 0)
        {
                printf("Can't open /dev/mem\n"winking smiley;
                return 1;
        }

        for(i = 0; i < 256; i++)
        {

                cs=lseek(fd,LED_ADDR,SEEK_SET);
                if(cs != LED_ADDR)
                {
                        printf("Can't lseek\n"winking smiley;
                        return 1;
                }
                val = i % 4;
                cs=write(fd,&val,1);
                if(cs != 1)
                {
                        printf("Write failed: %d\n",errno);
                        return 1;
                }

                sleep(1);
        }

        return 0;
}


Anyone know why I have to use mmap()? Seems to me I should be able to seek in /dev/mem just as well.

Thanks,
Frank
Re: Why must I use mmap() when accessing /dev/mem?
April 23, 2012 06:47PM
lseek() can't access the address larger than 2G.
you can rebuild your seek function by enable _FILE_OFFSET_BITS micro, or abandon this method.
Re: Why must I use mmap() when accessing /dev/mem?
March 10, 2014 02:45AM
It's better to lose your pride to the one you love,
than to lose the one you love because of pride.
——————————————————————————
fifa 14 ultimate team coins or buy fifa 14 coins
Re: Why must I use mmap() when accessing /dev/mem?
June 13, 2014 01:50AM
wow, you are all in here, it is great!
Fifa Coins | Fut 14 Coins
“The Iraqi Army was tough on the people, not on ISIS,” said Abu Mohammed, referring to the extremist fifa 14 coins group.The events over the last several days in Mosul — which is majority Sunni, although it has a sizable cheap fifa 14 coins population of Kurds and some Shiites, too — highlight what critics have said for years: that Sunnis see the army not as fifa 14 coins a national force but as the protector of the Shiite population.
Re: Why must I use mmap() when accessing /dev/mem?
July 18, 2014 05:20AM
On Capitol Hill, however, the images of Baghdad under threat from Islamic militants fanned fifa 14 coins a political firestorm. Speaker John A. Boehner warned that the progress in Iraq was “clearly fifa 14 coins in jeopardy,” and said Mr. Obama had been caught “taking a nap.”Democrats said the strife cheap fifa 14 coins was the result of former President George W.
Re: Why must I use mmap() when accessing /dev/mem?
July 18, 2014 05:20AM
Republicans and some Democrats sharply fifa 14 coins criticized the Obama administration for not having a credible response to help the Iraqi fifa 14 coins government.“There is no strategy,” said Senator John McCain, Republican of Arizona, in an cheap fifa 14 coins interview. “The president said he would not rule out anything.
Re: Why must I use mmap() when accessing /dev/mem?
August 24, 2014 05:11AM
how about fifa 15 game online?
maybe you can give me more advise!
_________________________
fifa 15 coins
cheap fifa 15 coins
I have the same question with you, hope I find the answer soon
super smash flash 2
Author:

Your Email:


Subject:


Spam prevention:
Please, solve the mathematical question and enter the answer in the input field below. This is for blocking bots that try to post this form automatically.
Question: how much is 19 plus 10?
Message: