openFPGALoader/main.cpp

186 lines
5.0 KiB
C++
Raw Normal View History

2019-09-26 18:29:20 +02:00
/*
* Copyright (C) 2019 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include <argp.h>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string.h>
#include <unistd.h>
#include <vector>
#include "altera.hpp"
2019-09-26 18:29:20 +02:00
#include "board.hpp"
#include "cable.hpp"
#include "device.hpp"
2019-12-06 07:29:28 +01:00
#include "gowin.hpp"
2019-11-18 16:04:50 +01:00
#include "lattice.hpp"
2019-09-26 18:29:20 +02:00
#include "ftdijtag.hpp"
#include "part.hpp"
2019-09-26 18:29:20 +02:00
#include "xilinx.hpp"
using namespace std;
struct arguments {
bool verbose, reset;
2019-09-26 18:29:20 +02:00
unsigned int offset;
string bit_file;
string device;
2019-09-26 18:29:20 +02:00
string cable;
string board;
};
2019-12-06 11:51:47 +01:00
const char *argp_program_version = "openFPGALoader 1.0";
2019-09-26 18:29:20 +02:00
const char *argp_program_bug_address = "<gwenhael.goavec-merou@trabucayre.com>";
2019-12-06 11:51:47 +01:00
static char doc[] = "openFPGALoader -- a program to flash FPGA";
2019-09-26 18:29:20 +02:00
static char args_doc[] = "BIT_FILE";
static error_t parse_opt(int key, char *arg, struct argp_state *state);
static struct argp_option options[] = {
{"cable", 'c', "CABLE", 0, "jtag interface"},
{"board", 'b', "BOARD", 0, "board name, may be used instead of cable"},
{"device", 'd', "DEVICE", 0, "device to use (/dev/ttyUSBx)"},
2019-09-26 18:29:20 +02:00
{"offset", 'o', "OFFSET", 0, "start offset in EEPROM"},
{"verbose", 'v', 0, 0, "Produce verbose output"},
{"reset", 'r', 0, 0, "reset FPGA after operations"},
{0}
};
static struct argp argp = { options, parse_opt, args_doc, doc };
int main(int argc, char **argv)
{
FTDIpp_MPSSE::mpsse_bit_config cable;
/* command line args. */
struct arguments args = {false, false, 0, "", "-", "-", "-"};
2019-09-26 18:29:20 +02:00
/* parse arguments */
argp_parse(&argp, argc, argv, 0, 0, &args);
/* if a board name is specified try to use this to determine cable */
if (args.board[0] != '-' && board_list.find(args.board) != board_list.end()) {
auto t = cable_list.find(board_list[args.board]);
if (t == cable_list.end()) {
cerr << "Error: interface "<< board_list[args.board];
cerr << " for board " << args.board << " is not supported" << endl;
return 1;
}
args.cable = (*t).first;
} else if (args.cable[0] == '-') { /* if no board and no cable */
if (args.verbose)
cout << "No cable or board specified: using direct ft2232 interface" << endl;
2019-09-26 18:29:20 +02:00
args.cable = "ft2232";
}
auto select_cable = cable_list.find(args.cable);
if (select_cable == cable_list.end()) {
cerr << "error : " << args.cable << " not found" << endl;
return EXIT_FAILURE;
}
cable = select_cable->second;
/* jtag base */
FtdiJtag *jtag;
if (args.device == "-")
jtag = new FtdiJtag(cable, 1, 6000000, args.verbose);
else
jtag = new FtdiJtag(cable, args.device, 1, 6000000, args.verbose);
2019-09-26 18:29:20 +02:00
/* chain detection */
2019-09-26 18:29:20 +02:00
vector<int> listDev;
int found = jtag->detectChain(listDev, 5);
if (args.verbose)
cout << "found " << std::to_string(found) << " devices" << endl;
2019-09-26 18:29:20 +02:00
if (found > 1) {
cerr << "Error: currently only one device is supported" << endl;
return EXIT_FAILURE;
} else if (found < 1) {
cerr << "Error: no device found" << endl;
2019-09-26 18:29:20 +02:00
return EXIT_FAILURE;
}
int idcode = listDev[0];
2019-09-26 18:29:20 +02:00
if (fpga_list.find(idcode) == fpga_list.end()) {
2019-12-06 07:29:28 +01:00
cerr << "Error: device " << hex << idcode << " not supported" << endl;
2019-09-26 18:29:20 +02:00
return 1;
} else if (args.verbose) {
2019-12-06 19:59:35 +01:00
printf("idcode 0x%x\nmanufacturer %s\nmodel %s\nfamily %s\n",
2019-09-26 18:29:20 +02:00
idcode,
2019-12-06 19:59:35 +01:00
fpga_list[idcode].manufacturer.c_str(),
2019-09-26 18:29:20 +02:00
fpga_list[idcode].model.c_str(),
fpga_list[idcode].family.c_str());
}
2019-12-06 19:59:35 +01:00
string fab = fpga_list[idcode].manufacturer;
2019-09-26 18:29:20 +02:00
Device *fpga;
if (fab == "xilinx") {
fpga = new Xilinx(jtag, args.bit_file, args.verbose);
2019-11-18 16:04:50 +01:00
} else if (fab == "altera") {
fpga = new Altera(jtag, args.bit_file, args.verbose);
2019-12-06 07:29:28 +01:00
} else if (fab == "gowin") {
fpga = new Gowin(jtag, args.bit_file, args.verbose);
2019-11-18 16:04:50 +01:00
} else {
fpga = new Lattice(jtag, args.bit_file, args.verbose);
2019-09-26 18:29:20 +02:00
}
fpga->program(args.offset);
if (args.reset)
fpga->reset();
delete(fpga);
2019-11-19 09:03:09 +01:00
delete(jtag);
2019-09-26 18:29:20 +02:00
}
/* arguments parser */
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *arguments = (struct arguments *)state->input;
switch (key) {
case 'r':
arguments->reset = true;
break;
case 'd':
arguments->device = arg;
2019-09-26 18:29:20 +02:00
break;
case 'v':
arguments->verbose = true;
break;
case 'o':
arguments->offset = strtoul(arg, NULL, 16);
break;
case 'c':
arguments->cable = arg;
break;
case 'b':
arguments->board = arg;
break;
case ARGP_KEY_ARG:
arguments->bit_file = arg;
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}