Skip navigation.
Home
The QNX Community Portal

View topic - Memory fault in32()

Page 1 of 1

Memory fault in32()

PostPosted: Thu Jul 21, 2016 4:09 pm
by djidji
hi,

I'm using in32 and out32 function to read or write in register but I get au memory fault, in debug mode it indicate segmentation fault !

just a simple code :

Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <hw/inout.h>
#include <sys/neutrino.h>
#include <stdint.h>
#include <sys/mman.h>

int main(int argc, char *argv[]) {

int data=0;
ThreadCtl( _NTO_TCTL_IO, 0 );
   //out32(0x20ac004, 0 );
mmap_device_io( 4,0x20ac004 );
   data=in32(0x20ac004 );

   printf("data = %x",data);


   return EXIT_SUCCESS;
}

Re: Memory fault in32()

PostPosted: Thu Jul 21, 2016 6:17 pm
by maschoen
Are you running as root? To perform the indicated function with ThreadCtl() you need to be root. Other than that your code looks good.

Re: Memory fault in32()

PostPosted: Fri Jul 22, 2016 3:00 pm
by Tim
Actually isn't he supposed to use the return pointer from mmap_device_io like so:

Code: Select all
uintptr_t ptr;

ptr = mmap_device_io( 4,0x20ac004 );
data=in32( ptr );


Tim

Re: Memory fault in32()

PostPosted: Fri Jul 22, 2016 3:33 pm
by maschoen
Ideally yes. On some architecture this might be required. In general, no.

Re: Memory fault in32()

PostPosted: Fri Jul 22, 2016 7:03 pm
by denkelly
I agree with Tim. This is the correct code for any architecture.

#include <stdlib.h>
#include <stdio.h>
#include <hw/inout.h>
#include <sys/neutrino.h>
#include <stdint.h>
#include <sys/mman.h>

int main(int argc, char *argv[]) {
int data = 0;
uintptr_t hndl;

ThreadCtl( _NTO_TCTL_IO, 0 );
hndl = mmap_device_io( 4, (long long)0x20ac004 );
data = in32( hndl );
printf("data = %x",data);
return EXIT_SUCCESS;
]

Re: Memory fault in32()

PostPosted: Mon Jul 25, 2016 12:34 pm
by djidji
Thanks evreyone,

using a pointer returned from mmap_device_io and print it fixed the problem.