?? jtag_chain.c
字號:
//************************************************************************************
// JTAG_Chain.c
//------------------------------------------------------------------------------------
// This program contains some primitive routines which gather information through the
// JTAG port on multiple JTAG compatible devices under test (DUT) connected in a
// chain. The TCK & TMS JTAG pins on the DUT are connected in parallel to port pins on
// the C8051F00x, C8051F01x master device and the TDI & TDO pins are connected in
// series.
//
// **NOTE: The first device in the chain (device 0) is the one whose TDO pin is
// connected to the TDO pin of the master device.
//
// Target device: C8051F00x,C8051F01x
//
// Tool chain: KEIL Eval 'c'
//************************************************************************************
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f000.h> // SFR declarations
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define MAX_NUM_DEVICES_IN_CHAIN 10
#define SYSCLK 2000000 // SYSCLK frequency in Hz
sbit LED = P1^6; // green LED: '1' = ON; '0' = OFF
sbit TCK = P3^7; // JTAG Test Clock -- Connected to TCK pin on all devices.
sbit TMS = P3^6; // JTAG Mode Select -- Connected to TMS pin on all devices.
sbit TDI = P3^5; // JTAG Data Input(output of master) -- Connected to the
// TDI pin of device n.
sbit TDO = P3^4; // JTAG Data Output (input to master)-- Connected to the
// TDO pin of device 0.
#define TRUE 1
#define FALSE 0
// JTAG Instruction Register Addresses
#define INST_LENGTH 16 // number of bits in the C8051Fxxx
#define BYPASS 0xffff // Instruction Register
#define EXTEST 0x0000
#define SAMPLE 0x0002
#define RESET 0x2fff // System RESET Instruction
#define IDCODE 0x1004 // IDCODE Instruction address/HALT
#define IDCODE_LEN 32 // number of bits in the ID code
#define FLASHCON 0x4082 // FLASH Control Instruction address
#define FLCN_LEN 8 // number of bits in FLASHCON
#define FLASHDAT 0x4083 // FLASH Data Instruction address
#define FLD_RDLEN 10 // number of bits in an FLASHDAT read
#define FLD_WRLEN 8 // number of bits in an FLASHDAT write
#define FLASHADR 0x4084 // FLASH Address Instruction address
#define FLA_LEN 16 // number of bits in FLASHADR
#define FLASHSCL 0x4085 // FLASH Scale Instruction address
#define FLSC_LEN 8 // number of bits in FLASHSCL
//------------------------------------------------------------------------------------
// Global Variable DECLARATIONS
//------------------------------------------------------------------------------------
// The addresses of the following variables are explicitly defined for viewing
// purposes. If the width of the external memory window is 5 bytes, then each
// device will take up exactly one row starting from the second row.
char xdata num_devices _at_ 0x0000;
char xdata num_devices_before _at_ 0x0001; // #devices before and after the isolated
char xdata num_devices_after _at_ 0x0002; // device
char xdata num_IR_bits_before _at_ 0x0003; // #instruction register bits before and
char xdata num_IR_bits_after _at_ 0x0004; // after the isolated device
typedef struct JTAG_Information { // Discovery information
unsigned char IR_length; // Instruction register length
unsigned long id; // Identification code for each device
} JTAG_Information;
// Array: one entry per device in the
// JTAG chain
JTAG_Information xdata JTAG_info[MAX_NUM_DEVICES_IN_CHAIN];
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void init (void);
void JTAG_StrobeTCK (void);
void JTAG_Reset (void);
void Blink_Led(void);
void JTAG_Discover(void);
void JTAG_Discover_IR(void);
void JTAG_Discover_DR(void);
void JTAG_Isolate(char index);
unsigned long JTAG_IR_Scan (unsigned long instruction, char num_bits) ;
unsigned long JTAG_DR_Scan (unsigned long dat, char num_bits);
void JTAG_IWrite (unsigned int ireg, unsigned long dat, int num_bits);
unsigned long JTAG_IRead (unsigned int ireg, int num_bits);
int FLASH_ByteRead (unsigned int addr, unsigned char *pdat);
int FLASH_ByteWrite (unsigned int addr, unsigned char dat);
int FLASH_PageErase (unsigned int addr);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void)
{
long xdata id;
unsigned char dest;
int pass;
int address;
char device = 0;
init (); // initialize ports
LED = 1; // turn on the LED
JTAG_Discover(); // IDCODE should = 0x10000243 for
// C8051F000 rev D device
JTAG_Isolate(0); // isolate device 0
JTAG_IR_Scan (IDCODE, INST_LENGTH); // load IDCODE into IR and HALT the DUT
id = JTAG_DR_Scan (0x0L, IDCODE_LEN); // get the ID Code of the isolated device
JTAG_Isolate(1);
JTAG_IR_Scan (IDCODE, INST_LENGTH); // load IDCODE into IR and HALT the DUT
id = JTAG_DR_Scan (0x0L, IDCODE_LEN); // get the ID Code of the isolated device
JTAG_Isolate(2);
JTAG_IR_Scan (IDCODE, INST_LENGTH); // load IDCODE into IR and HALT the DUT
id = JTAG_DR_Scan (0x0L, IDCODE_LEN); // get the ID Code of the isolated device
// Here we perform 2 tests on each device. These 2 tests take approximately
// 43 seconds for each device with SYSCLK at 2 Mhz and approximatly 6 seconds
// running at 16 Mhz.
for(device = 0; device < num_devices; device++) {
JTAG_Isolate(device);
//TEST 1 -- ERASE A FLASH PAGE
pass = FLASH_PageErase (0x1000); // erase page prior to writing
while (!pass); // handle Write Lock condition
//Verify that locations 0x1000 - 0x11FF are 0xFF
for(address = 0x1000; address < 0x1200; address++){
pass = FLASH_ByteRead (address, &dest); // dest should return 0xff
if(!pass || dest != 0xFF) Blink_Led();
}
//TEST 2 -- WRITE A PATTERN TO FLASH PAGE
for(address = 0x1000; address < 0x1200; address++){
dest = address & 0x00FF; // strip away upper 8 bits
pass = FLASH_ByteWrite (address, dest);// store LSByte of address at address
while (!pass); // handle Read Lock condition
}
dest = 0x12; // set test variable to non-0xff value
//Verify that locations 0x1000 - 0x11FF are following the pattern
for(address = 0x1000; address < 0x1200; address++){
pass = FLASH_ByteRead (address, &dest);
if(!pass || dest != (address & 0x00FF)) Blink_Led();
}
}
LED = 0; // turn off the led,
// program executed correctly
while(1);
}
//************************************************************************************
// Function and Procedure DEFINITIONS
//************************************************************************************
//------------------------------------------------------------------------------------
// Blink_Led
//------------------------------------------------------------------------------------
// This routine blinks the Green LED forever to indicate an error.
//
void Blink_Led(void)
{
int i; // millisecond counter
int ms = 200; // stay in each state for ms milliseconds
TCON &= ~0x30; // STOP Timer0 and clear overflow flag
TMOD &= ~0x0F; // configure Timer0 to 16-bit mode
TMOD |= 0x01;
CKCON |= 0x08; // Timer0 counts SYSCLKs
while (1){
LED = ~LED;
for (i = 0; i < ms; i++) { // count milliseconds
TR0 = 0; // STOP Timer0
TH0 = (-SYSCLK/1000) >> 8; // SET Timer0 to overflow in 1ms
TL0 = -SYSCLK/1000;
TR0 = 1; // START Timer0
while(TF0 == 0); // wait for overflow
TF0 = 0; // clear overflow indicator
}
}
}
//------------------------------------------------------------------------------------
// init
//------------------------------------------------------------------------------------
// This routine disables the watchdog timer and initializes the GPIO pins
//
void init (void)
{
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
XBR2 |= 0x40; // enable crossbar
PRT1CF |= 0x40; // enable P1.6 (LED) as a push-pull output
PRT3CF |= 0xe0; // make P3.7-5 push-pull outputs
P3 &= ~0xE0; // set TCK, TMS, and TDI all low
num_devices = 1; // The default number of devices is one.
// JTAG_Discover() does not have to be
// called if only one device is connected.
num_devices_before = 0; // Initializing these variables to zero
num_devices_after = 0; // allows calling the JTAG_IR_Scan() and
num_IR_bits_before = 0; // the JTAG_DR_Scan() without first
num_IR_bits_after = 0; // calling JTAG_Isolate() when there is
// only one device in the chain.
}
//------------------------------------------------------------------------------------
// JTAG_StrobeTCK
//------------------------------------------------------------------------------------
// This routine strobes the TCK pin (brings high then back low again)
// on the target system.
//
void JTAG_StrobeTCK (void)
{
TCK = 1;
TCK = 0;
}
//------------------------------------------------------------------------------------
// JTAG_Reset
//------------------------------------------------------------------------------------
// This routine places the JTAG state machine on the target system in
// the Test Logic Reset state by strobing TCK 5 times while leaving
// TMS high. Leaves the JTAG state machine in the Run_Test/Idle state.
//
void JTAG_Reset (void)
{
TMS = 1;
JTAG_StrobeTCK (); // move to Test Logic Reset state
JTAG_StrobeTCK ();
JTAG_StrobeTCK ();
JTAG_StrobeTCK ();
JTAG_StrobeTCK ();
TMS = 0;
JTAG_StrobeTCK (); // move to Run_Test/Idle state
}
//------------------------------------------------------------------------------------
// JTAG_Discover
//------------------------------------------------------------------------------------
// This routine sequentially queries a chain of JTAG devices and accomplishes the
// following three tasks.
// For the global struct array <JTAG_info>
// -- fills in the length of each device's instruction register
// -- fills in each device's IDCODE.
// For the global variable <num_devices>
// -- updates it with the number of JTAG devices connected in the chain.
//
void JTAG_Discover(void)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -