?? loader_f02x.c
字號(hào):
//-----------------------------------------------------------------------------
// loader_F02x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
// 交互選擇性固件刷新程序,針對(duì)BTF020開發(fā)板做了程序改動(dòng);針對(duì)一些keil c語言函數(shù)做了注釋說明
// AUTH: 鐘磊
// DATE: 04 sep 07
//
// This program shows an example 'selective code loader' using the 'F02x. It
// designates the FLASH page at 0x1000 for the code loaded through the UART.
//
// Control Function:
//
// The system is controlled via the hardware UART, operating at a baud rate
// determined by the constant <BAUDRATE>, using Timer1 overflows as the baud
// rate source.
//
// Received File Type:
//
// This example receives Intel HEX files which are OMF51 (linker output files)
// passed through the OH51 utility in the 'CYGNAL\IDEfiles\C51\Bin' folder.
//
// Note: Because this program writes to FLASH, the MONEN pin should be tied
// high.
//
// Target: C8051F02x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <stdio.h> // printf() and getchar()
#include <ctype.h> // tolower() and toint()
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define TRUE 1
#define FALSE 0
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
sbit LED = P2^4; // LED='1' means ON
sbit SW2 = P2^0; // SW2='0' 表示按鍵按下
//-----------------------------------------------------------------------------
// Reserved Memory Space
//-----------------------------------------------------------------------------
char reserved_memory_bank[2] _at_ 0x08;// This memory bank is used by the
// functions that will be loaded
// through the UART.
// The memory bank location and size
// are based on values from the M51 map
// file generated when the loaded code
// is linked.
//保留數(shù)據(jù)區(qū)的大小有待刷程序所使用的數(shù)據(jù)區(qū)
//大小決定,可以查看M51文件得到
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void main (void);
// Support Subroutines
void print_menu(void);
void erase_flash_page(void);
void receive_code(void);
unsigned char hex2char();
// Initialization Subroutines
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
#define input_str_len 4 // buffer to hold characters entered
char input_str[input_str_len]; // at the command prompt
void (*f)(); // function pointer declaration
bit code_erased = FALSE; // flag used to indicate that the FLASH
// erase operation is complete
bit f_valid = FALSE; // flag to indicate that the FLASH
// programming operation is complete
// 標(biāo)志先置零
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
PORT_Init (); // initialize crossbar and GPIO
SYSCLK_Init (); // initialize oscillator
UART0_Init (); // initialize UART0
print_menu(); // print the command menu
while (1){
printf("\nEnter a command > ");
gets(input_str, input_str_len); //gets()讀取的字符串,其長(zhǎng)度沒有限制,編程
//者要保證字符數(shù)組有足夠大的空間,存放輸入的字符串。
switch ( input_str[0] ){
case '1': erase_flash_page();
printf("\nFlash page 0x1000 has been erased.\n");
break;
case '2': printf("\nReady to receive HEX file...\n");
receive_code();
break;
case '3': if(f_valid){
f = (void code *) 0x1000;
f();
printf("\nFinished\n");
} else {
printf("\n*** No function exists at 0x1000.\n");
}
break;
case '?': print_menu();
break;
default: printf("\n*** Unknown Command.\n");
break;
}
} // end while
} // end main
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// print_menu
//-----------------------------------------------------------------------------
//
// This routine uses prints the command menu to the UART.
//
void print_menu(void)
{
printf("\n\nC8051F02x Selective Code Loader Example\n");
printf("------------------------------------------\n");
printf("1. Erase the flash page at 0x1000\n");
printf("2. Receive HEX file\n");
printf("3. Execute the function at 0x1000\n");
printf("?. Print Command List\n");
}
//-----------------------------------------------------------------------------
// hex2char
//-----------------------------------------------------------------------------
//
// This routine converts a two byte ascii representation of a char to an
// 8-bit variable;
//
unsigned char hex2char()
{
unsigned char retval;
char byteH, byteL;
// get a two-byte ASCII representation of a char from the UART
byteH = _getkey(); //等待一個(gè)字符(使用_getkey函數(shù))
//函數(shù)_getkey和putchar使用片內(nèi)串口來完成串行I/O
byteL = _getkey(); //這些子程序包含在C51庫(kù)中,每個(gè)函數(shù)的源文件位于\C51\LIB目錄下
// convert to a single 8 bit result
retval = (char) toint(byteH) * 16; //toint可重入轉(zhuǎn)換一個(gè)十六進(jìn)制數(shù)為一個(gè)十進(jìn)制數(shù)
retval += (char) toint(byteL);
return retval;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -