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

📄 flash_uploader.c

📁 使用JTAG口对AT91R40008芯片进行FLASH编程的程序
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : Flash_uploader.c
//* Object              : Allows loading program and data on the target
//*
//* 1.0 27/02/02 NL    : Creation
//*----------------------------------------------------------------------------

#ifdef SEMIHOSTING
#include <stdio.h>
#endif

#include    "parts/r40008/lib_r40008.h"
#include    "targets/eb40a/eb40a.h"
#include    "parts/r40008/reg_r40008.h"

#include	"jtag.h"
#include 	"Flash_uploader_appl.h"
#include	"Flash_uploader.h"

#include 	"global.h"

static const char Test_menu_msg[]=
{
"\n\r  COMMAND MENU: JTAG\n\r"
" 0) Reset				1) Stop\n\r"
" 2) Read ID			3) Read register\n\r"
" 4) Write Register		5) Read Memory\n\r"
" 6) Write Memory		7) Store Multiple\n\r"
" 8) Load Multiple		9) Read CPSR\n\r"
"10) Write CPSR         11) Go\n\r"
"12) Print Context		13) Flash uploader\n\r"
"14) Erase Target       15) Mirror\n\r"

"Enter command to execute: "
};

static const char value_msg[]={"\n\r  VALUE: 0x"};
static const char reg_msg[]={"\n\r  REGISTER: 0x"};
const char addr_msg[]={"\n\r  ADDRESS: 0x"};
static const char nb_data_msg[]={"\n\r  NB OF DATA: "};

//* Global Variables
extern u_int	Current_Scan_Chain;
extern u_int	context[16];

//*----------------------------------------------------------------------------
//* Function Name       : Read_ID_Code
//* Object              : read the ID Code of the core
//* Input Parameters    : none
//* Output Parameters   : TRUE
//* Functions called    : at91_pio_open, at91_pio_write
//*----------------------------------------------------------------------------
void Read_ID_Code(){

	u_int code;
	
	at91_print(&COM0,"ID CODE\n\r");
   	//* Reset the TAP controller
   	JTAG_Reset();
   	//* Shift IDCode instruction
   	JTAG_Shift_ir(IDCODE, JTAG_NO_IDLE);
   	//* Get the ID code of the target
   	JTAG_IDCode(&code);
	sprintf(message,"\n\rThe target's ID code is : 0x%08X\n\r", code);
   	at91_print(&COM0,message);
   	
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Read_Memory
//* Object              : read the data at the desired address
//* Input Parameters    : address
//* Output Parameters   : value
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Read_Memory(){
	
	u_int addr, data;

	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"\n\rRead a value in memory\n\r");
	addr = Get_Value(addr_msg);
	
	JTAG_Read_Memory(addr, &data, WORD, WAIT_STATE);	
	
	sprintf(message,"\n\rThe data stored at address 0x%08X is 0x%08X\n\r", addr, data);
   	at91_print(&COM0,message);
	
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Write_Memory
//* Object              : Write the data at the desired address
//* Input Parameters    : address, value
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Write_Memory(){
	
	u_int addr, data;

	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"\n\rWrite a value in memory\n\r");
	
	addr = Get_Value(addr_msg);
	data = Get_Value(value_msg);
	
	JTAG_Write_Memory(addr, data, WORD, WAIT_STATE);	
	
   	at91_print(&COM0,"\n\rThe data is written\n\r");
	
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Read_Reg
//* Object              : Read a register
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Read_Reg(){
	
	u_int reg, data;
	
	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"\n\rRead a register\n\r");
	reg = Get_Value(reg_msg);
	
	at91_print(&COM0,"Starting reading the data\n\r");
	JTAG_Read_Register(reg, &data);
	
	sprintf(message,"The data read in register R%d is 0x%08X\n\r", reg, data);
   	at91_print(&COM0,message);
   
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Write_Reg
//* Object              : Store a data in a register and read it
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Write_Reg(){
	
	u_int value, reg;
	
	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"\n\rWrite a data in a register\n\r");
	reg = Get_Value(reg_msg);
	value = Get_Value(value_msg);
	
	sprintf(message,"\n\rWrite the value 0x%08X in register R%d\n\r", value, reg);
   	at91_print(&COM0,message);
	JTAG_Write_Register(reg, value);	
	at91_print(&COM0,"The value is written\n\r");
   
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Store_Multiple
//* Object              : Store registers in memory
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Store_Multiple(){

	int i;	
	u_int data;
	
	u_int addr, value;
	
	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"\n\rStore registers in memory\n\r");
	addr = Get_Value(addr_msg);
	value = Get_Value(value_msg);
	
	//* Read all the registers
	for (i=0; i<14; i++){
		JTAG_Read_Register(i, &data);
		sprintf(message,"The data read in register R%d is 0x%08X\n\r", i, data);
   		at91_print(&COM0,message);	
	}
	
	sprintf(message,"\n\rStore the specified registers at address 0x%08X\n\r", addr);
   	at91_print(&COM0,message);
	JTAG_Store_Multiple(addr, value);	
	at91_print(&COM0,"The registers are stored\n\r");
   
   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Load_Multiple
//* Object              : Load data in registers from R0
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Load_Multiple(){
	
	int i;
	u_int value;
	
	u_int number;
	u_int tab_data[14];
	
	Current_Scan_Chain = -1;
	
	//* Init of the table
	for (i=0; i<14; i++){
		tab_data[i] = 0x55AA55A0 + i;
	}
	
	at91_print(&COM0,"\n\rLoad data in registers\n\r");
	
	//* Get the number of data desired
	number = Get_Value(nb_data_msg);
	
	JTAG_Load_Multiple(number, tab_data);	
	at91_print(&COM0,"Data are loaded\n\r");
   
	//* Read all the registers to verify if the function works
	for (i=0; i<14; i++){
		JTAG_Read_Register(i, &value);
		sprintf(message,"The data read in register R%d is 0x%08X\n\r", i, value);
   		at91_print(&COM0,message);	
	}   

   	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Stop
//* Object              : Put the ARM in debug Mode
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : JTAG_Stop
//*----------------------------------------------------------------------------
void Stop(){

	Current_Scan_Chain = -1;
	
	JTAG_Stop();

	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Reset
//* Object              : Reset the JTAG
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : JTAG_Reset
//*----------------------------------------------------------------------------
void Reset(){

	Current_Scan_Chain = -1;
	
	JTAG_Reset();

	sprintf(message,"\n\rThe value  in the debug status register is 0x%08X \n\r", JTAG_Read_Debug_Status());
   	at91_print(&COM0,message);

	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Read_CPSR
//* Object              : Read CPSR register
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Read_CPSR(){

	u_int cpsr_value;

	Current_Scan_Chain = -1;

	at91_print(&COM0,"\n\rRead the CPSR\n\r\n\r");

	JTAG_Read_CPSR(&cpsr_value);
	sprintf(message,"The value in CPSR is 0x%08X \n\r", cpsr_value);
	at91_print(&COM0,message);	
	
	JTAG_Analyse_CPSR(cpsr_value);
	
	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Write_CPSR
//* Object              : Write CPSR register
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Write_CPSR(){

	u_short mode;
	u_short irq, fiq, thumb;
	
	Current_Scan_Chain = -1;

	at91_print(&COM0,"\n\rWrite the CPSR\n\r\n\r");

	at91_print(&COM0,"Mode wanted : 10 = USER       11 = FIQ\n\r");
	at91_print(&COM0,"Mode wanted : 12 = IRQ        13 = SUPERVISOR\n\r");
	at91_print(&COM0,"Mode wanted : 17 = ABORT      1B = UNDEF\n\r");
	at91_print(&COM0,"Mode wanted : 1F = SYSTEM\n\r");
	mode = (u_short)Get_Value(value_msg);
	
	at91_print(&COM0,"\n\rFIQ : 1 = disabled and 0 = enabled");
	fiq = Get_Value(value_msg);
	
	at91_print(&COM0,"\n\rIRQ : 1 = disabled and 0 = enabled");
	irq = Get_Value(value_msg);
	
	at91_print(&COM0,"\n\rTHUMB : 1 = THUMB and 0 = ARM");
	thumb = Get_Value(value_msg);
	
	JTAG_Write_CPSR(mode, (IRQ_BIT)irq, (FIQ_BIT)fiq, (THUMB_BIT)thumb);
	
	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Go
//* Object              : Start at an address
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Go(){
	
	u_int addr;
	
	Current_Scan_Chain = -1;
	
	at91_print(&COM0,"Start at the specified address\n\r");
	addr = Get_Value(addr_msg);

	//* Restart to the desired address 
	JTAG_Go(addr);

	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : Test_Print_Context
//* Object              : Print all registers
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : 
//*----------------------------------------------------------------------------
void Test_Print_Context(){
	
	int i;
	u_int tab[16];

	//* Backup context	
	JTAG_Save_Context(tab);
	
	//* Print Context
	for (i=0; i<16; i++){
		sprintf(message,"The value in R%d is 0x%08X \n\r", i, tab[i]);
   		at91_print(&COM0,message);		
	}
	
	return;
}

//*----------------------------------------------------------------------------
//* Function Name       : test_menu
//* Object              : Main menu function
//* Input Parameters    : none
//* Output Parameters   : TRUE
//* Functions called    : at91_set_protect_mode, at91_pio_open
//*----------------------------------------------------------------------------
void test_menu( void )
//* Begin
{
    //* Protect Mode for Debug
	at91_set_protect_mode(TRUE);

    //* Define ICE signals as inputs or outputs
    at91_pio_open(&PIO_DESC, TMS|TDI|TCK, PIO_OUTPUT);
    at91_pio_open(&PIO_DESC, TDO, PIO_INPUT);

    //init global data
    while(command!=99)
  	{
        command = Get_Command(Test_menu_msg);
    	switch (command)
    	{
        	case 0 : //* Reset
				Reset();
        	break;
        
        	case 1 : //* Stop
				Stop();
        	break;
        	
        	case 2 : //* ID Code
				Read_ID_Code();
        	break;

			case 3 : //* Read a register
				Test_Read_Reg();
        	break;

			case 4 : //* Write a register
				Test_Write_Reg();
        	break;

			case 5 : //* Read a value in memory
				Test_Read_Memory();
        	break;

			case 6 : //* Write a value in memory
				Test_Write_Memory();
			break;

			case 7 : //* Store registers in memory
				Test_Store_Multiple();
			break;

			case 8 : //* Load data in registers
				Test_Load_Multiple();
			break;

			case 9 : //* Read CPSR
				Test_Read_CPSR();
			break;

			case 10 : //* Write CPSR
				Test_Write_CPSR();
			break;

			case 11 : //* Go
				Test_Go();
			break;

			case 12 : //* Print Context
				Test_Print_Context();
			break;

			case 13 : //* Flash uploader
				JTAG_Flash_uploader(PROGRAM, NO_MIRROR);
			break;

			case 14 : //* Erase Target
				JTAG_Flash_uploader(ERASE, NO_MIRROR);
			break;
			
			case 15 : //* Duplicate the flash uploader application
				JTAG_Flash_uploader(PROGRAM, MIRROR);
			break;
			
			default:
				print_default();
        	break;
        	
        }//* End switch
    }//* End while
//* End
}

⌨️ 快捷键说明

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