c - Issues mmaping the same file twice -
i'm using raspberry pi b+, , i'm trying mmap 2 different sections of /dev/mem - first able set 2 pins' functions location 0x2020 0004 (0x04 bytes long), other manipulate bsc slave functions on bcm2835 chip on pi location 0x2021 4000 (0x1c bytes long).
static uint32_t * initmapmem(int fd, uint32_t addr, uint32_t len) { return (uint32_t *) mmap((void*)0x0, len, prot_read|prot_write|prot_exec, map_shared|map_locked, fd, addr); } int initialise(void) { int fd; fd = open("/dev/mem", o_rdwr | o_sync) ; if (fd < 0) { fprintf(stderr, "this program needs root privileges. try using sudo.\n"); return 1; } pinreg = initmapmem(fd, 0x20200004, 0x4); bscreg = initmapmem(fd, 0x20214000, 0x1c); close(fd); if (bscreg == map_failed) { fprintf(stderr, "bad, mmap failed.\n"); return 1; } if (pinreg == map_failed) { fprintf(stderr, "bad, mmap failed.\n"); return 1; } return 0; } initialise() called out of main(). stepping through program gdb find bscreg gets positioned right, pinreg returns map_failed (aka 0xffffffff) errno set einval. doesn't matter way it's done, either - pinreg finds map_failed when mmaped first or second.
how pinreg valid value?
the first mmap() failing because offset you're trying map (0x20200004) isn't page-aligned. create mapping @ 0x20200000 size of @ least 8, write @ offset of 0x4.
Comments
Post a Comment