Guest aleale97 Posted July 4, 2014 Posted July 4, 2014 Hi guys, this is my first post here so, hi! :) I'm stuck in this piece of code found in a library, as far as i understood it should read a value from memory(RAM) and return it. I fired up google and got my self up and running with almost all the functions but there're some lines that i still don't understand, here is the C code: Code: uint32_t readl(uint32_t addr) { static volatile uint32_t *gpio_vaddr ; int fd = 0; uint32_t val = 0; fd = open("/dev/mem", O_RDWR | O_NDELAY); if(-1 == fd){ printf("open /dev/mem failed."); return -1; } uint32_t mmap_base = (addr & ~MAP_MASK); uint32_t mmap_seek = ((addr - mmap_base) >> 2); gpio_vaddr = (uint32_t *)mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mmap_base); if (0 == gpio_vaddr){ close(fd); printf("mmap failed:fd(%d), addr(0x%x),size(%d)\n", fd, addr, MAP_SIZE); } else { val = *(gpio_vaddr + mmap_seek); if (wiringPiDebug) printf("readl: vaddr = 0x%x,val:0x%x\n", (gpio_vaddr + mmap_seek),val); close(fd); munmap((uint32_t *)gpio_vaddr, MAP_SIZE); return val; } } Where the constants are: Code: MAP_SIZE (4096*2) MAP_MASK (MAP_SIZE - 1) and the addr parameter of the function is the address that has to be red(31 bit long binary number). In this line there is a bitwise not(the tilde) on the MAP_MASK value and an and of the result with the address given(correct me if i'm wrong): Code: uint32_t mmap_base = (addr & ~MAP_MASK); (MAP_SIZE-1)=111111111111(8191dec), if i invert that i get 000000000000 that will set, with every address given, the mmap_base to 0! Is just bad code or am i missing something? What is hte purpose of the function munmap? If anyone wants to help, thanks !!! Alessandro from Italy. Continue reading... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.