⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testjtagdll.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的jtag接口程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  uchar		tmpbuf[4];
  time_t        start_time;
  time_t        end_time;
  time_t        elapsed_time;
  double        usecs_per_byte;

  /*
   * Download protocol:
   *  first:  load address
   *  next:   code size (in bytes)
   *  last:   actual code bytes
   */

  /* Load address (was set by tdd_init_jtag_driver) */

  tmpbuf[0] = (uchar)(targetInfo[pindx].LoadAddr & 0xFF);
  tmpbuf[1] = (uchar)((targetInfo[pindx].LoadAddr >>  8) & 0xFF);
  tmpbuf[2] = (uchar)((targetInfo[pindx].LoadAddr >> 16) & 0xFF);
  tmpbuf[3] = (uchar)((targetInfo[pindx].LoadAddr >> 24) & 0xFF);
  jtag_fast_write_n_bytes(pindx, tmpbuf, 4);
  printf("\nSent Load address     = 0x%08x\n", targetInfo[pindx].LoadAddr);

  /* Code Size */

  tmpbuf[0] = (uchar) (loadSize & 0xFF);
  tmpbuf[1] = (uchar)((loadSize >>  8) & 0xFF);
  tmpbuf[2] = (uchar)((loadSize >> 16) & 0xFF);
  tmpbuf[3] = (uchar)((loadSize >> 24) & 0xFF);
  jtag_fast_write_n_bytes(pindx, tmpbuf, 4);
  printf("Sent Code Size        = 0x%08x\n", loadSize);

  /* actual code download */

  loadsize_save = loadSize;
  file_buffer = (uchar *)imageHandle;
  cksum = 0;
  
  start_time = time(NULL);

  while (loadSize > 0) {
    long m, n;

    m = (loadSize > Chunk_Size) ? Chunk_Size : loadSize;
    n = m;
    m = jtag_fast_write_n_bytes(pindx, (uchar *)file_buffer, n);

    if (m != n) {
      printf("Down Load failed\n");
      free(image_buf);
      exit(1);
    }

    loadSize -= m;
    for (n = 0; n < m; n++) {
      cksum += file_buffer[n];
    }
    /* advance file_buffer to point to the next m bytes of image_buf */
    file_buffer += m;

  }
  
  end_time = time(NULL);

  free(imageHandle);

  /*
   * read the cksum from target and compare with ours.
   * time out after a while
   */
  cksum = cksum | 0x1;
  target_cksum = 0;
  time_out = 200;

  while (time_out-- > 0) {
    jtag_read_n_bytes(pindx, (uchar *)&target_cksum, 4, true);
    if (target_cksum == cksum)
      break;
	Sleep(1);
  }

  if (cksum != target_cksum) {
    if (time_out <= 0)
      printf("Time out waiting for target checksum\n");
    else
      printf("Check sum err, target 0x%08x Host 0x%08x\n", target_cksum, cksum);
    exit(1);
  }
  
  jtdsp_loaded(pindx) = true;

  elapsed_time = (end_time - start_time);
  usecs_per_byte = 1000000.0 * (((double)elapsed_time) / ((double)loadsize_save));
  printf("\nDownLoad stats: \nElapsed Time        = 0x%08x seconds\nLoad Size           = 0x%08x bytes\nSpeed               = %f bytes/micro seconds\n", elapsed_time, loadsize_save, usecs_per_byte);
  puts("Target Downloaded");
  puts("Press Enter to start the target");
  getchar();
  return 0;
};
  
void StartTalkingToTarget(long pindx)
{
	int data = 0;
	int count = 0;
	int runTarget = JUMP_TO_L2_CODE;
	int magic = MAGIC_NUMBER;
	int swapped = SWAPPED_MAGIC;

	/* signalling L1 boot code to start target */
	jtag_fast_write_n_bytes(pindx, (uchar *)&runTarget, 4);
	while (count++ < TALK_TIME) {
		
		jtag_read_n_bytes(pindx, (uchar *)&data, 4, false);
		if (data == MAGIC_NUMBER ) {
			printf("Magic Number Received\n");
			jtag_fast_write_n_bytes(pindx, (uchar *)&swapped, 4);
			
		}
		else if (data == SWAPPED_MAGIC) {
			printf("Swapped Magic Number Received\n");
			jtag_fast_write_n_bytes(pindx, (uchar *)&magic, 4);
			
		}
		else {
			puts("Nothing Received or Sent");
		}
		Sleep(1);
	    
	}
	if (data == 0 )
		printf("Couldn't establish handshake with the target\n");
	else {
		printf("\n****************************************************\n");
		printf("*** Congrats your JTAG wrapper DLL works fine !! ***");
		printf("\n****************************************************\n");
	}
  	
};		

int
jtag_fast_write_n_bytes(long pindx, uchar *buf, int buflen)
{
	int m;
	uchar sel_ifull_in = JTAG_SEL_IFULL_IN;
 
	while (1) {
		uchar old_ctl;

		scanInstructionReg(pindx, JT_TEST_BUS, &sel_ifull_in,
		       JTAG_INS_LEN_BITS, &old_ctl);

    /*
     * Least 2 bits of "in" contain fixed values 1 and 0 as per
     * IEEE 1149.1 standard. The next 3 bits are the ofull, ifull and
     * sleepless bits as per TriMedia spec.
     * So, ifull bit is bit 3 of "old_ctl" in this case,
     * instead of bit 1 when we scan ctrl register
     */
	    if (is_ifull(old_ctl >> 2) == 0) {
		  /* ifull is clr */
			break;
		}	
	}

	for (m = buflen; m > 0; m -= 4, buf += 4) {

		uchar oneBit = 1;
		uchar inBuf[20], localBuf[5];
		int i;
		for (i = 0; i< 4; i++ ) {
		
			localBuf[i] = oneBit; 
			localBuf[i] |= (buf[i] << 1); 
			oneBit = (buf[i] >> 7);
		}  
		localBuf[4] = oneBit;
		scanDataReg(pindx, JT_TEST_BUS, localBuf,
			   JTAG_DATA_LEN_BITS + 1 , inBuf);

	}

	return buflen;
}

int
jtag_read_n_bytes(long pindx, uchar *buf, int buflen, int nonblocking)
{
	int   m = buflen;
	ulong dummy = 0;

	do {
		uchar ctl;
		ctl = read_jtag_control(pindx, JT_TEST_BUS);
		if (is_ofull(ctl)) {
			scan_jtag_data_out(pindx, JT_TEST_BUS, (uchar *)&dummy, buf);
			/* clear ofull bit */
			m   -= 4;
			buf += 4;
		}
		else {
			Sleep(1);
	    }
		if (nonblocking) {
			return buflen - m;
		}
	} while (m > 0);

	return buflen;
}

int
jtag_reset(long pindx)
{
	ulong i, j;

	jtdsp_loaded(pindx)		= false;
	jtdsp_running(pindx)	= false;
	jtdsp_l1_ready(pindx)	= false;
	jtdsp_connected(pindx)	= false;

	/*
	 * This is to reset the jtag board and not TM1 board
	 */
	resetBoard(pindx);
	
	/*
	 * Very first scan seems to transfer out junk. Do a couple of scans
	 */
	i = 0;
	scan_jtag_data_out(pindx, JT_TEST_BUS, (uchar *)(&i), (uchar *)(&j));
	scan_jtag_data_out(pindx, JT_TEST_BUS, (uchar *)(&i), (uchar *)(&j));

	return 0;
}

/*
 *-------------------------------------------------------------------------
 * Low level JTAG functions
 *-------------------------------------------------------------------------
 */

/* Assumption:
 *    Bits are scanned out starting with
 *    the first byte in the buffer, then the next byte etc.
 *    Note: Within a byte, bits are shifted out from bit 0 thru 7
 */

/* ------------------------------------------------------------- */

void
scan_jtag_control(long pindx, ulong test_bus, uchar *out_buf, uchar *in_buf)
{
	uchar out, in = 0;
	/*
	 * scan jtag control register
	 */
	out = JTAG_SEL_JTAG_CTRL;

	scanInstructionReg(pindx, test_bus, &out,    JTAG_INS_LEN_BITS, &in);
	scanDataReg(pindx, test_bus, out_buf, JTAG_CTL_LEN_BITS, in_buf);
}

/* ------------------------------------------------------------- */


uchar
read_jtag_control(long pindx, ulong test_bus)
{
	uchar out, in = 0;
	/*
	 * read jtag control register
	 *
	 * This uses the fact that scanning in an instruction automatically
	 * scans out JTAG CTRL register values. Thus there is no update of
	 * JTAG_CTRL register. Works around tm1.0 and tm1.1 bug (simultaneous
	 * update of JTAG_CTRL from jtag and from tm1 causes tm1 update to be lost.)
	 */
	out = JTAG_SEL_JTAG_CTRL;

	scanInstructionReg(pindx, test_bus, &out, JTAG_INS_LEN_BITS, &in);

	/* Least 2 bits of "in" contain fixed values 1 and 0 as per
	 * IEEE 1149.1 standard. The next 3 bits are the ofull, ifull and
	 * sleepless bits as per TriMedia spec
	 */
	return (in >> 2);
}

/* ------------------------------------------------------------- */

void
scan_jtag_data_in(long pindx, ulong test_bus, uchar *out_buf, uchar *in_buf)
{
    uchar out, in;
    /*
     * scan jtag data_in register
     */
    out = JTAG_SEL_DATA_IN;

    scanInstructionReg(pindx, test_bus, &out,    JTAG_INS_LEN_BITS, &in);
    scanDataReg(pindx, test_bus, out_buf, JTAG_DATA_LEN_BITS, in_buf);
}

/* ------------------------------------------------------------- */

void
scan_jtag_data_out(long pindx, ulong test_bus, uchar *out_buf, uchar *in_buf)
{
    uchar out, in;
    /*
     * scan jtag data_out register
     */
    out = JTAG_SEL_DATA_OUT;


    scanInstructionReg(pindx, test_bus, &out,    JTAG_INS_LEN_BITS, &in);
    scanDataReg(pindx, test_bus, out_buf, JTAG_DATA_LEN_BITS, in_buf);
}

/* ------------------------------------------------------------- */

void
scan_jtag_ifull_in(long pindx, ulong test_bus, uchar *out_buf, uchar ifull)
{
    uchar out, in;
    int i;
    uchar inBuf[20], localBuf[5];
    uchar oneBit = ifull;
    /*
     * scan jtag data_ifull_in register
     */
    out = JTAG_SEL_IFULL_IN;

    scanInstructionReg(pindx, test_bus, &out,    JTAG_INS_LEN_BITS, &in);
 
   	for (i = 0; i< 4; i++ ) {
    	localBuf[i] = oneBit; 
     	localBuf[i] |= (out_buf[i] << 1); 
     	oneBit = (out_buf[i] >> 7);
   	}  
   	localBuf[4] = oneBit;
   	scanDataReg(pindx, test_bus, localBuf, JTAG_DATA_LEN_BITS + 1,inBuf);

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -