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

📄 loader_f30x.c

📁 C8051F0XX引导装入程序考虑及举例
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// loader_F30x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: FB
// DATE: 28 JUN 02
//
// This program shows an example 'selective code loader' using the 'F30x. 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 VDD monitor is enabled in
// in the initialization routine.
//
// Target: C8051F30x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f300.h>                 // SFR declarations
#include <stdio.h>                     // printf() and getchar()
#include <ctype.h>                     // tolower() and toint()

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR2RL   = 0xca;                 // Timer2 reload value
sfr16 TMR2     = 0xcc;                 // Timer2 counter
sfr16 PCA0CP1  = 0xe9;                 // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2  = 0xeb;                 // PCA0 Module 2 Capture/Compare
sfr16 PCA0     = 0xf9;                 // PCA0 counter
sfr16 PCA0CP0  = 0xfb;                 // PCA0 Module 0 Capture/Compare

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define TRUE         1
#define FALSE        0

#define SYSCLK       24500000          // SYSCLK frequency in Hz
#define BAUDRATE     115200            // Baud rate of UART in bps

sbit LED = P0^2;                       // LED='1' means ON
sbit SW2 = P0^3;                       // SW2='0' means switch pressed
sbit TX0 = P0^4;                       // UART0 TX pin
sbit RX0 = P0^5;                       // UART0 RX pin


//-----------------------------------------------------------------------------
// 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.

//-----------------------------------------------------------------------------
// 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

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) 
{

   // Disable Watchdog timer
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer 
                                       // enable)

   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);
      
      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\nC8051F30x 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();
   byteL = _getkey();
   
   // convert to a single 8 bit result
   retval = (char) toint(byteH) * 16;
   retval += (char) toint(byteL);
   return retval;
}

//-----------------------------------------------------------------------------
// erase_flash_page
//-----------------------------------------------------------------------------
//
// This routine erases the FLASH page located at 0x1000
//
void erase_flash_page(void)
{
   char xdata* data pagePointer = 0x1000; // pointer to xdata space located 
                                          // in data space
   bit EA_state;                    // holds interrupt state

   PSCTL = 0x03;                    // MOVX erases FLASH
   
   FLKEY = 0xA5;                    // FLASH lock and key sequence 1

⌨️ 快捷键说明

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