Because the memory map of your user space program is NOT the hardware
memory map of the hardware. mmap is a mechanism to map between them.
On Wed, 5 Jan 2011, frank_kienast wrote:
> Maybe I'm doing something stupid here, but it seems like I have to use mmap()
> to access the LEDs on the TS7200. The following works:
>
> #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");
> return 1;
> }
> leds = (unsigned char *) mmap(0, getpagesize(), PROT_READ|PROT_WRITE,
> MAP_SHARED, fd, 0x80840000);
> if(leds == NULL)
> {
> printf("Can't mmap\n");
> return 1;
> }
> else
> printf("leds=%x\n",leds);
>
> for(i = 0; i < 256; i++)
> {
> val = i % 4;
> leds[0x20] = val;
>
> sleep(1);
> }
>
> return 0;
> }
>
>
> But the following gets an error on the write
> System error: 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");
> return 1;
> }
>
> for(i = 0; i < 256; i++)
> {
>
> cs=lseek(fd,LED_ADDR,SEEK_SET);
> if(cs != LED_ADDR)
> {
> printf("Can't lseek\n");
> 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;
> }
>
>
> Shouldn't either method work?
>
> Thanks,
> Frank
>
>
>
>
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|