Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5009

C/C++ • Re: Allocate memory for DMA on Raspberry Pi 5

$
0
0
mmap uses virtual addresses, right? If I follow DOMs example with total_mem = 3096. Then the free area would be
at base=0xC1800000.
But if I execute the code as follows, the program crashes immediately.

Am I missing something? It's not clear to me.

Code:

#define PAGE_SIZE (4*1024)void *mapmem(unsigned base, unsigned size){   int mem_fd;   unsigned offset = base % PAGE_SIZE;   base = base - offset;   /* open /dev/mem */   if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {      printf("can't open /dev/mem\nThis program should be run as root\n");      exit (-1);   }   void *mem = mmap(      0,      size,      PROT_READ|PROT_WRITE,      MAP_SHARED,      mem_fd,      base);   printf("base=0x%x, mem=%p\n", base, mem);   if (mem == MAP_FAILED) {      printf("mmap error %d\n", (int)mem);      exit (-1);   }   close(mem_fd);   return (char *)mem + offset;}

Statistics: Posted by drLaplace — Mon Nov 18, 2024 12:54 pm



Viewing all articles
Browse latest Browse all 5009

Trending Articles