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

📄 jtag_flash.c

📁 c8051F00x的pca的使用
💻 C
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------------
// JTAG_FLASH.c
//------------------------------------------------------------------------------------
// This program contains some primitive routines which read, write, and erase the FLASH
// through the JTAG port on a C8051Fxxx device under test (DUT).  The JTAG pins on the
// DUT are connected to port pins on the C8051F000 master device.
//
// Target device: C8051F000, C8051F010
//
// Tool chain: KEIL Eval 'c'
//

//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f000.h>                       // SFR declarations

//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
sbit   LED = P1^6;                           // green LED: '1' = ON; '0' = OFF

// GPIO pins connecting to JTAG pins on device to be programmed (DUT)
sbit   TCK = P3^7;                           // JTAG Test Clock
sbit   TMS = P3^6;                           // JTAG Mode Select
sbit   TDI = P3^5;                           // JTAG Data Input
sbit   TDO = P3^4;                           // JTAG Data Output

#define   TRUE 1
#define   FALSE 0
                        
// JTAG Instruction Register Addresses
#define   INST_LENGTH 16                     // number of bits in the 
                                             // Instruction Register
#define   BYPASS      0xffff
#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

//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------

void init (void);
void JTAG_StrobeTCK (void);
void JTAG_Reset (void);
unsigned int JTAG_IR_Scan (unsigned int instruction, int num_bits);
unsigned long JTAG_DR_Scan (unsigned long dat, int 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) {
   
   unsigned long id;
   unsigned char dest;
   int pass;

   id = 0x12345678L;
   
   init ();                                    // initialize ports

   JTAG_Reset ();                              // Reset the JTAG state machine on DUT
   
   JTAG_IR_Scan (RESET, INST_LENGTH);          // Reset the DUT

   JTAG_IR_Scan (IDCODE, INST_LENGTH);         // load IDCODE into IR and HALT the DUT
   id = JTAG_DR_Scan (0x0L, IDCODE_LEN);       // read the IDCODE
                                               // IDCODE should = 0x10000243 for
                                               // C8051F000 rev D device

   // here we erase the FLASH page 0x1000 - 0x11ff, read 0x1000 (it's an 0xff),
   // write a 0x66 to 0x1000, and read 0x1000 again (it's changed to an 0x66).
   while (1) {
      pass = FLASH_PageErase (0x7c00);         // erase page prior to writing...
      while (!pass);                           // handle Write Lock condition

      dest = 0x5a;                             // set test variable to non-0xff value

      pass = FLASH_ByteRead (0x7c00, &dest);   // dest should return 0xff
      while (!pass);                           // handle Read Lock condition

      dest = 0x66;
      pass = FLASH_ByteWrite (0x7c00, dest);   // store 0x66 at 0x1000
      while (!pass);                           // handle Read Lock condition

      pass = FLASH_ByteRead (0x7c00, &dest);   // dest should return 0x66
      while (!pass);                           // handle Read Lock condition

      pass = FLASH_PageErase (0x7c00);
      while (!pass);

      pass = FLASH_ByteRead (0x7c00, &dest);
      while (!pass);
   }
}

//------------------------------------------------------------------------------------
// Functions and Procedures
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
// 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 &= 0x1f;                            // TCK, TMS, and TDI all low

}

//------------------------------------------------------------------------------------
// 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_IR_Scan
//------------------------------------------------------------------------------------
// This routine loads the supplied <instruction> of <num_bits> length into the JTAG 
// Instruction Register on the target system.  Leaves in the Run_Test/Idle state.
// The return value is the n-bit value read from the IR.
// Assumes the JTAG state machine starts in the Run_Test/Idle state.
//
unsigned int JTAG_IR_Scan (unsigned int instruction, int num_bits) {

   unsigned int retval;                   // JTAG instruction read
   int   i;                               // JTAG IR bit counter

   retval = 0x0;
   
   TMS = 1;                     
   JTAG_StrobeTCK ();                     // move to SelectDR
   TMS = 1;
   JTAG_StrobeTCK ();                     // move to SelectIR
   TMS = 0;
   JTAG_StrobeTCK ();                     // move to Capture_IR
   TMS = 0;
   JTAG_StrobeTCK ();                     // move to Shift_IR state

   for (i=0; i < num_bits; i++) {

      TDI = (instruction & 0x01);         // shift IR, LSB-first
      instruction = instruction >> 1;

      retval = retval >> 1;
      if (TDO) {
         retval |= (0x01 << (num_bits - 1));
      }

      if (i == (num_bits - 1)) {
         TMS = 1;                         // move to Exit1_IR state
      }

      JTAG_StrobeTCK();
   }

   TMS = 1;
   JTAG_StrobeTCK ();                     // move to Update_IR
   TMS = 0;
   JTAG_StrobeTCK ();                     // move to RTI state

   return retval;
}

//------------------------------------------------------------------------------------
// JTAG_DR_Scan
//------------------------------------------------------------------------------------
// This routine shifts <num_bits> of <data> into the Data Register, and returns
// up to 32-bits of data read from the Data Register.
// Leaves in the Run_Test/Idle state.

⌨️ 快捷键说明

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