/********************************************************************** ** dump_raw.c ** ** Download RAW Image to Frame Buffer Memory ** Version 1.0 ** ** Copyright (C) 2018 H.Poetzl ** ** This program is free software: you can redistribute it and/or ** modify it under the terms of the GNU General Public License ** as published by the Free Software Foundation, either version ** 2 of the License, or (at your option) any later version. ** **********************************************************************/ #include #include #include #include #include #include #include #include #include #include #include static char *cmd_name = NULL; static uint32_t map_base = 0x18000000; static uint32_t map_size = 0x08000000; static uint32_t map_addr = 0x00000000; static char *dev_mem = "/dev/mem"; #define NUM_COLS 4096 #define NUM_ROWS 3072 #define ERR_WRITE 1 int write_data(int fd, uint8_t *buf, size_t cnt) { while (cnt) { size_t len = write(fd, buf, cnt); if (len < 0) return ERR_WRITE; cnt -= len; buf += len; } return 0; } void load_data(uint8_t *buf, uint64_t *ptr) { for (unsigned i = 0; i < NUM_COLS/2; i++) { unsigned ce = i * 3; unsigned co = (i + NUM_COLS/2) * 3; uint64_t val = ptr[i]; buf[ce + 0] = (val >> 56) & 0xFF; buf[ce + 1] = (val >> 48) & 0xFF; buf[ce + 2] = (val >> 40) & 0xFF; buf[co + 0] = (val >> 32) & 0xFF; buf[co + 1] = (val >> 24) & 0xFF; buf[co + 2] = (val >> 16) & 0xFF; } } int main(int argc, char **argv) { extern int optind; extern char *optarg; int c, err_flag = 0; #define OPTIONS "hB:S:" #define VERSION "1.0" cmd_name = argv[0]; while ((c = getopt(argc, argv, OPTIONS)) != EOF) { switch (c) { case 'h': fprintf(stderr, "This is %s " VERSION "\n" "options are:\n" "-h print this help message\n" "-B memory mapping base\n" "-S memory mapping size\n", cmd_name); exit(0); break; case 'B': map_base = strtoll(optarg, NULL, 0); break; case 'S': map_size = strtoll(optarg, NULL, 0); break; default: err_flag++; break; } } if (err_flag) { fprintf(stderr, "Usage: %s -[" OPTIONS "] [file]\n" "%s -h for help.\n", cmd_name, cmd_name); exit(1); } int fd = open(dev_mem, O_RDWR | O_SYNC); if (fd == -1) { fprintf(stderr, "error opening >%s<.\n%s\n", dev_mem, strerror(errno)); exit(2); } if (map_addr == 0) map_addr = map_base; void *base = mmap((void *)map_addr, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, map_base); if (base == (void *)-1) { fprintf(stderr, "error mapping 0x%08X+0x%08X @0x%08X.\n%s\n", map_base, map_size, map_addr, strerror(errno)); exit(2); } fprintf(stderr, "mapped 0x%08lX+0x%08lX to 0x%08lX.\n", (long unsigned)map_base, (long unsigned)map_size, (long unsigned)base); if (argc > optind) { close(1); int fd = open(argv[optind], O_CREAT|O_WRONLY, S_IRUSR); if (fd == -1) { fprintf(stderr, "error opening >%s< for writing.\n%s\n", argv[optind], strerror(errno)); exit(2); } } uint8_t buf[NUM_COLS*3]; uint64_t *ptr = base; for (unsigned j = 0; j < NUM_ROWS/2; j++) { load_data(buf, ptr); int res = write_data(1, buf, sizeof(buf)); if (res == ERR_WRITE) { fprintf(stderr, "error writing.\n%s\n", strerror(errno)); exit(4); } ptr += NUM_COLS/2; } exit(0); }