📄 ezusb_jtag.cpp
字号:
/* * This file is part of Jelie, * (c) 2002 Julien Pilet <julien.pilet@epfl.ch> and * Stephane Magnenat <stephane.magnenat@epfl.ch> * * Jelie 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. * * Jelie 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 Foobar; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//*! \file * \brief EzUSB controller implementation * * This is the EzUSB implementation of the JTAG controller. * * \author Julien Pilet <julien.pilet@epfl.ch> * \author Stephane Magnenat <stephane.magnenat@epfl.ch> */#include "ezusb_jtag.h"#include "jtag_order.h"#include <string.h>#include <assert.h>#include <unistd.h>#include <signal.h>#ifdef HAVE_CONFIG_H#include <config.h>#else#define RETSIGTYPE void#endif//#define VERBOSE_JTAG_CMDEzUSBJTAG *ezusb_jtag_ptr=NULL;//! Interrupt signal handler to close correctly the USB link in case of unexpected exit.RETSIGTYPE criticalStop(int i){ fprintf(stderr, "Received unexpected critical stop, resetting USB and quitting\n"); if (ezusb_jtag_ptr) ezusb_jtag_ptr->forceClose(); exit(-1);}EzUSBJTAG::EzUSBJTAG():EzUSB(0) { queuePos=0; // install signals handler signal(SIGTERM, criticalStop); signal(SIGINT, criticalStop); signal(SIGQUIT, criticalStop); signal(SIGSEGV, criticalStop); signal(SIGPIPE, criticalStop);}EzUSBJTAG::~EzUSBJTAG(){ // remove signal handler signal(SIGTERM, SIG_DFL); signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL); signal(SIGSEGV, SIG_DFL); signal(SIGPIPE, SIG_DFL);}void EzUSBJTAG::writeAndReadBuffer(unsigned char *buffer, int toWrite, int toRead, unsigned long delay){ if (toWrite) { int wReturn=usb_bulk_write(devHandle, 2, (char *)buffer, toWrite, 50); if (wReturn<0) fprintf(stderr, "USB : WRITE ERROR : %s\n", usb_strerror()); } if (delay) usleep(delay); if (toRead) { int rReturn=usb_bulk_read(devHandle, 2, (char *)buffer, toRead, 50); if (rReturn<0) fprintf(stderr, "USB : READ ERROR : %s\n", usb_strerror()); }}bool EzUSBJTAG::init(int argc, char **argv){ parseCmdLine(argc, argv); if (!findDevice()) return false; ezusb_jtag_ptr = this; if (!downloadFirmware("jtag_usb.ihx")) { return false; } if (usb_set_altinterface(devHandle, 1)<0) { fprintf(stderr, "USB : SET ALT ERROR : %s\n", usb_strerror()); return false; } unsigned char c=JO_SELF_TEST;#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_SELF_TEST\n");#endif writeAndReadBuffer(&c, 1, 1, 2000000); if (c!=0xBC) { fprintf(stderr, "SELF TEST FAILURE (received 0x%02x instead of 0xBC) !!!\n", c); return false; } return true;}void EzUSBJTAG::ireg(unsigned char *data){ unsigned char buffer[2]; buffer[0]=JO_IREG; buffer[1]=data[0];#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_IREG\n");#endif flushQueue(); writeAndReadBuffer(buffer, 2, 1); data[0]=buffer[0];}void EzUSBJTAG::dreg(unsigned char *data, unsigned char length, bool writeOnly=false){ unsigned char buffer[10]; unsigned char dataLength=(length>>3)+(length & 7 ? 1 : 0); assert(length<=64);#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_DREG\n");#endif if (writeOnly) { // if write only, queuing is used to provide bigger packet transfer int dregSize=2+dataLength; if (queuePos+dregSize>64) flushQueue(); queue[queuePos]=JO_DREG_WRITE_ONLY; queue[queuePos+1]=length; memcpy(queue+queuePos+2, data, dataLength); queuePos+=dregSize; } else { buffer[0]=JO_DREG; buffer[1]=length; memcpy(buffer+2, data, dataLength); flushQueue(); writeAndReadBuffer(buffer, dataLength+2, (writeOnly ? 0 : dataLength)); memcpy(data, buffer, dataLength); }}void EzUSBJTAG::jtagReset(void){ unsigned char buffer[1]; buffer[0]=JO_JTAG_RESET;#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_JTAG_RESET\n");#endif flushQueue(); writeAndReadBuffer(buffer, 1, 0);}void EzUSBJTAG::cpuReset(bool state){ unsigned char buffer[2]; buffer[0]=JO_CPU_RESET; buffer[1]=state ? 1 : 0;#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_CPU_RESET\n");#endif flushQueue(); writeAndReadBuffer(buffer, 2, 0);}void EzUSBJTAG::trst(bool state){ unsigned char buffer[2]; buffer[0]=JO_TRST; buffer[1]=state ? 1 : 0;#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_TRST\n");#endif flushQueue(); writeAndReadBuffer(buffer, 2, 0);}void EzUSBJTAG::idle(unsigned int nbTCK){ unsigned char buffer[3]; buffer[0]=JO_IDLE; buffer[1]=nbTCK&0xFF; buffer[2]=(nbTCK>>8)&0xFF;#ifdef VERBOSE_JTAG_CMD printf("JTAG : Sending JO_IDLE\n");#endif flushQueue(); writeAndReadBuffer(buffer, 3, 0);}void EzUSBJTAG::flushQueue(void){ assert(queuePos<=64); if (queuePos>0) { writeAndReadBuffer(queue, queuePos, 0); queuePos=0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -