Commit 8aafb84c authored by Alessandro Rubini's avatar Alessandro Rubini

tools: use a library to build binaries (tom commands)

parent a0b2c31f
load-lm32
load-virtex
mapper
wmapper
spi-pll
......@@ -15,3 +15,18 @@ NM = $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
LIB = libtools.a
LIBOBJ = lm32-loader.o load-fpga.o
LDFLAGS = -L. -ltools
PROGS = load-lm32 load-virtex spi-pll mapper wmapper
all: $(LIB) $(PROGS)
$(PROGS): $(LIB)
$(LIB): $(LIBOBJ)
$(AR) r $@ $^
/*
* devmem2.c: Simple program to read/write from/to any location in memory.
*
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
* modified 2005 by Alessandro Rubini, to make it less verbose
*
*
* This software has been developed for the LART computing board
* (http://www.lart.tudelft.nl/). The development has been sponsored by
* the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
* and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
* projects.
*
* The author can be reached at:
*
* Jan-Derk Bakker
* Information and Communication Theory Group
* Faculty of Information Technology and Systems
* Delft University of Technology
* P.O. Box 5031
* 2600 GA Delft
* The Netherlands
*
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int verbose = 0;
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result, writeval;
int wid;
char format[16];
off_t target;
int access_type = 'w';
setuid(0); /* try */
if(argc < 2) {
fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
"\taddress : memory address to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
"\tdata : data to be written\n\n",
argv[0]);
exit(1);
}
target = strtoul(argv[1], 0, 0);
if(argc > 2)
access_type = tolower(argv[2][0]);
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
if (verbose) {
printf("/dev/mem opened.\n");
fflush(stdout);
}
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
if (verbose) {
printf("Memory mapped at address %p.\n", map_base);
fflush(stdout);
}
virt_addr = map_base + (target & MAP_MASK);
if (argc <= 3) {
switch(access_type) {
case 'b':
read_result = *((unsigned char *) virt_addr);
wid = 2;
break;
case 'h':
read_result = *((unsigned short *) virt_addr);
wid = 4;
break;
case 'w':
read_result = *((unsigned long *) virt_addr);
wid = 8;
break;
default:
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}
sprintf(format, "0x%%0%ix\n", wid);
printf(format, read_result);
fflush(stdout);
}
if(argc > 3) {
writeval = strtoul(argv[3], 0, 0);
switch(access_type) {
case 'b':
*((unsigned char *) virt_addr) = writeval;
break;
case 'h':
*((unsigned short *) virt_addr) = writeval;
break;
case 'w':
*((unsigned long *) virt_addr) = writeval;
break;
}
}
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}
extern int load_lm32_main(char *fname);
extern int load_fpga_main(char *fname);
......@@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
......@@ -17,6 +18,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define BASE_FPGA 0x10000000
#define SIZE_FPGA 0x20000
......@@ -82,22 +84,16 @@ void copy_lm32(uint32_t *buf, int buf_nwords, uint32_t base_addr)
printf("OK.\n");
}
int main(int argc, char **argv)
int load_lm32_child(char *fname)
{
uint32_t *buf;
FILE *f;
int fdmem;
if(argc<2)
{
fprintf(stderr, "No parameters specified !\n");
fprintf(stderr, "Usage:\n\t%s <binary file> \n", argv[0]);
return 0;
}
/* /dev/mem for mmap of both gpio and spi1 */
if ((fdmem = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "%s: /dev/mem: %s\n", argv[0], strerror(errno));
fprintf(stderr, "%s: /dev/mem: %s\n", __func__,
strerror(errno));
exit(1);
}
......@@ -108,12 +104,12 @@ int main(int argc, char **argv)
if (base_fpga == MAP_FAILED) {
fprintf(stderr, "%s: mmap(/dev/mem): %s\n",
argv[0], strerror(errno));
__func__, strerror(errno));
exit(1);
}
f=fopen(argv[1],"rb");
f=fopen(fname,"rb");
if(!f)
{
fprintf(stderr, "Input file not found.\n");
......@@ -137,4 +133,22 @@ int main(int argc, char **argv)
return 0;
}
int load_lm32_main(char *fname)
{
int pid = fork();
int status;
switch(pid) {
case -1:
fprintf(stderr, "fork(): %s\n", strerror(errno));
return -1;
case 0: /* child */
load_lm32_child(fname);
exit(0);
default: /* parent */
waitpid(pid, &status, 0);
if (!WEXITSTATUS(status))
return 0;
return -1;
}
}
......@@ -18,6 +18,7 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/wait.h>
/* -I$LINUX/arch/arm/mach-at91/include/ */
#include <mach/at91_pio.h>
......@@ -29,7 +30,7 @@
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
unsigned char *bstream;
static unsigned char *bstream;
/* The address and size of the entire AT91 I/O reg space */
#define BASE_IOREGS 0xfff78000
......@@ -43,7 +44,7 @@ enum {
PIOE = 4
};
void *ioregs;
static void *ioregs;
#define AT91_PIOx(port) (AT91_PIOA + AT91_BASE_SYS + 0x200 * port)
......@@ -85,7 +86,7 @@ static int pio_get(int port, int bit)
#define FPGA_RESET PIOA, 5 //out
void ud(int usecs) /* horrible udelay thing without scheduling */
static void ud(int usecs) /* horrible udelay thing without scheduling */
{
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
......@@ -96,23 +97,18 @@ void ud(int usecs) /* horrible udelay thing without scheduling */
}
int main(int argc, char **argv)
/* being a lazy bastard, I fork a process to avoid free, munmap etc */
static int load_fpga_child(char *fname)
{
int bs_size, i;
int fdmem;
if (argc != 2) {
fprintf(stderr, "%s: Use: \"%s <bitstream>\"\n", argv[0],
argv[0]);
exit(1);
}
{
/* read the bisstream file */
FILE *f = fopen(argv[1], "r");
FILE *f = fopen(fname, "r");
if (!f) {
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1],
fprintf(stderr, "%s: %s: %s\n", __func__, fname,
strerror(errno));
exit(1);
}
......@@ -126,14 +122,13 @@ int main(int argc, char **argv)
if (bstream == NULL)
{
fprintf(stderr, "malloc failed\n");
fclose(f);
exit(1);
}
if (fread(bstream, 1, bs_size, f) != bs_size) {
fprintf(stderr, "%s: read(%s): %s\n", argv[0],
argv[1], strerror(errno));
fprintf(stderr, "%s: read(%s): %s\n", __func__,
fname, strerror(errno));
exit(1);
}
......@@ -143,7 +138,8 @@ int main(int argc, char **argv)
/* /dev/mem for mmap of both gpio and spi1 */
if ((fdmem = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "%s: /dev/mem: %s\n", argv[0], strerror(errno));
fprintf(stderr, "%s: /dev/mem: %s\n", __func__,
strerror(errno));
exit(1);
}
......@@ -153,8 +149,8 @@ int main(int argc, char **argv)
BASE_IOREGS);
if (ioregs == MAP_FAILED) {
fprintf(stderr, "%s: mmap(/dev/mem): %s\n",
argv[0], strerror(errno));
fprintf(stderr, "%s: mmap(/dev/mem): %s\n", __func__,
strerror(errno));
exit(1);
}
......@@ -239,13 +235,13 @@ int main(int argc, char **argv)
/* check status: initb must be low and done must be low */
if (pio_get(INITB)) {
fprintf(stderr, "%s: INIT_B is still high after PROGRAM_B\n",
argv[0]);
__func__);
//exit(1);
}
if (!pio_get(DONE)) {
fprintf(stderr, "%s: DONE is already high after PROGRAM_B\n",
argv[0]);
__func__);
//exit(1);
}
......@@ -259,7 +255,7 @@ int main(int argc, char **argv)
if (pio_get(INITB)) {
fprintf(stderr, "%s: INIT_B is not going back high\n",
argv[0]);
__func__);
//exit(1);
}
......@@ -281,7 +277,7 @@ int main(int argc, char **argv)
if (0 && /* don't do this check */ !pio_get(DONE)) {
fprintf(stderr, "%s: DONE is already high after "
"%i bytes (missing %i)\n", argv[0],
"%i bytes (missing %i)\n", __func__,
i, bs_size - i);
exit(1);
}
......@@ -296,7 +292,7 @@ int main(int argc, char **argv)
}
if (pio_get(DONE)) {
fprintf(stderr, "%s: DONE is not going high after %i bytes\n",
argv[0], bs_size);
__func__, bs_size);
exit(1);
}
......@@ -304,6 +300,25 @@ int main(int argc, char **argv)
pio_set(PIO_CODR, FPGA_RESET);
ud(10);
pio_set(PIO_SODR, FPGA_RESET);
exit(0);
}
int load_fpga_main(char *fname)
{
int pid = fork();
int status;
switch(pid) {
case -1:
fprintf(stderr, "fork(): %s\n", strerror(errno));
return -1;
case 0: /* child */
load_fpga_child(fname);
exit(0);
default: /* parent */
waitpid(pid, &status, 0);
if (!WEXITSTATUS(status))
return 0;
return -1;
}
}
#include <stdio.h>
#include "libtools.h"
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Use: \"%s <filename>\"\n", argv[0]);
}
return load_lm32_main(argv[1]);
}
#include <stdio.h>
#include "libtools.h"
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Use: \"%s <filename>\"\n", argv[0]);
}
return load_fpga_main(argv[1]);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment