On Sat, 29 Dec 2007, mrjbradski wrote:
> Im just experimenting with DIO before I actually need to use it, so I
> wrote a simple program to toggle the DIO0..7 pins as follows:
>
> fd = open("/dev/mem", O_RDWR|O_SYNC);
> gpioregs = (char *)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED,fd,
> 0x12c00000);
> gpioregs[0] = 0xff; /* data direction register set to all outputs */
>
> for (;;) {
> gpioregs[2] = 0xff; /* output high to DIO_01 and DIO_4, all
> else low */
> gpioregs[2] = 0x0; /* output high to DIO_01 and DIO_4, all else
> low */
> }
> close(fd);
> exit(0);
>
> For some reason, the pins never toggle. If I remove the for loop, and
> execute gpioregs[2] = 0xff; OR gpioregs[2] = 0x0; the pins will change
> state. What am I doing wrong?
I would say that since gpioregs isn't volatile, gcc is optimising away the
writes in the loop. The volatile keyword tells the compiler that it's not
a normal memory location, and that every single access must be preserved
even if the compiler thinks it can predict the end result.
Also, you may want some delays in there or the pin could toggle at a few
megahertz... usleep() is good for that.
See ftp://ftp.embeddedarm.com/ts-arm-linux-cd/samples/tempSensor.c for an
example of both DIO and SPI. Note that both pointers are declared
volatile.
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/
|