|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sys/time.h> |
| 5 | +#include <stdint.h> |
| 6 | +#include <endian.h> |
| 7 | +#include <errno.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include <stddef.h> |
| 10 | +#include <stdint.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <sys/mman.h> |
| 14 | +#include <sys/stat.h> |
| 15 | +#include <sys/types.h> |
| 16 | +#include <unistd.h> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +//#define MMIOADDR 0x601100000 |
| 21 | +//#define MMIOADDR 0x1f000f0000 |
| 22 | +#define MMIOADDR 0x1f00400000 |
| 23 | +#define MMIOLEN 0x10000 |
| 24 | +#define BASE 0x700 |
| 25 | +#define LEN 0x100 |
| 26 | + |
| 27 | +volatile unsigned int *mmio; |
| 28 | +#define REG32(m,x) ((volatile uint64_t *)((uint64_t)(m)+(uint64_t)(x))) |
| 29 | + |
| 30 | +static void setup_io() { |
| 31 | + int fd = open("/dev/mem", O_RDWR | O_SYNC); |
| 32 | + if (fd < 0) { |
| 33 | + printf("Unable to open /dev/mem. Run as root using sudo?\n"); |
| 34 | + exit(-1); |
| 35 | + } |
| 36 | + |
| 37 | + void *gpio_map = |
| 38 | + mmap(NULL, // Any adddress in our space will do |
| 39 | + MMIOLEN, // Map length |
| 40 | + PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory |
| 41 | + MAP_SHARED, // Shared with other processes |
| 42 | + fd, // File to map |
| 43 | + MMIOADDR // Offset to GPIO peripheral |
| 44 | + ); |
| 45 | + |
| 46 | + close(fd); |
| 47 | + |
| 48 | + if (gpio_map == MAP_FAILED) { |
| 49 | + perror("mmap failed, errno"); |
| 50 | + exit(-1); |
| 51 | + } |
| 52 | + |
| 53 | + mmio = ((volatile unsigned *)gpio_map) + BASE/4; |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +volatile unsigned int test; |
| 59 | +int main(void) { |
| 60 | + |
| 61 | + |
| 62 | +setup_io(); |
| 63 | + |
| 64 | +printf("Read mmio address: %10lx \n", MMIOADDR + BASE); |
| 65 | + |
| 66 | +unsigned int a=0; |
| 67 | +volatile unsigned int b=0; |
| 68 | +unsigned int i = 0; |
| 69 | + |
| 70 | +for(a=0; a<LEN/4; a++) { |
| 71 | + if (!(a & 0x7)) printf("\n%04x: ", a*4); |
| 72 | + b= *(mmio + a); |
| 73 | + //*(mmio + a) = 0; |
| 74 | + printf("%08x ", b); |
| 75 | +} |
| 76 | +printf("\n"); |
| 77 | + |
| 78 | +// Jump from 0x200007c8 to 0x20007000 |
| 79 | +*(mmio + 0xc8/4) = 0xf04f281f; |
| 80 | +//*(mmio + 0xc8/4) = 0xbc1af006; |
| 81 | + |
| 82 | +#if 0 |
| 83 | +a = 0; |
| 84 | +//FILE* f = fopen("counter.bin", "rb"); |
| 85 | +FILE* f = fopen("core1.bin", "rb"); |
| 86 | +while (!feof(f)) { |
| 87 | + unsigned int val; |
| 88 | + fread(&val, sizeof(val), 1, f); |
| 89 | + *(mmio + a++) = val; |
| 90 | +} |
| 91 | +#endif |
| 92 | +} |
| 93 | + |
0 commit comments