Hatem Mohamed wrote:
> Hi every body
> I have Ts-7300 , I wrote this C code on it and it is compiled
> successfully but when I tried to execute the file I get this message "
> segmentation fault "
> all I wanted from this code was to make pins from Dio0 ~ Dio7 all zeros.
>
> #include<unistd.h>
> #include<sys/types.h>
> #include<sys/mman.h>
> #include<stdio.h>
> #include<fcntl.h>
> #include<string.h>
> int main()
> {
> volatile unsigned char *P1DR;
> unsigned char *start;
> int fd = open("/dev/mem", O_RDWR);
> start = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
> 0x80840004);
> P1DR = (unsigned char *)(start + 0x00);
> *P1DR=0x00;
> close(fd);
> return 0;
> }
>
> that was my first question
As Peter Elliot already pointed out, you must pass a page aligned
address to mmap.
Also, always check the return codes from system calls so that you have
better debug information when things go wrong (something better than "I
had a segmentation fault somewhere");
I would probably write the code more like this:
#define PORTB 0x04
...
volatile unsigned char *port_map;
int fd = open("/dev/mem", O_RDWR);
port_map = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0x80840000);
if (port_map == MAP_FAILED) {
perror("mmap ports");
return 1;
}
port_map[PORTB] = 0x00;
close(fd);
return 0;
I think arm handles 32 bit operations better than 8 bit operations, so
the "port_map[PORTB] = 0x00" is probably more efficient written as
"*((unsigned long *)(&port_map[PORTB])) = 0;". Maybe with a macro or a
static inline function could be used to make it look better.
> the second question
>
> can I make any pin in the 55 Dio zero or one alone I mean is there any
> function can do that directly ??
Looking at the datasheet:
http://www.cirrus.com/en/pubs/manual/EP9301_User_Guide.pdf
there doesn't seem to be a good way to do this apart from the generic:
port_map[PORTB] |= (1 << pin_number);
or
port_map[PORTB] &= ~(1 << pin_number);
But since this requires a read / modify / write, there might be races
with other users of the same port.
I hope this helps,
--
Paulo Marques
Software Development Department - Grupo PIE, S.A.
Phone: +351 252 290600, Fax: +351 252 290601
Web: www.grupopie.com
"You're just jealous because the voices only talk to me."
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/
|