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

📄 updater_f30x.c

📁 C8051F0XX引导装入程序考虑及举例
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// updater_F30x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: FB
// DATE: 28 JUN 02
//
// This program shows an example Firmware Updater using the 'F300. It resides
// in FLASH at addresses above 0x1000 and is accessed through a function
// pointer casted as (void code*) 0x1000.   
// 
// Once the firmware update has taken place, the a software reset is issued 
// and the updated firmware takes control of the system. 
// 
// 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.
//
// 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 <stdlib.h>
#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


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

void main (void);

// Support Subroutines in Bootloader.c
void print_menu(void);
void erase_flash(void);
void receive_code(void);
unsigned char hex2char();

// Initialization Subroutines
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);


//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

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)
{
   
   char input;   

   
   EA = 0;                             // Disable interrupts (precautionary)
   
   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("Enter a command > ");
      input = getchar();
      
      switch ( input ){
         
         case '1': erase_flash();   
                   printf("\n*** Flash pages erased\n");
                   receive_code(); 
                   printf("\n** Firmware Update Complete **\n");
                            
         case '2': printf("\n** RESETTING **\n\n");
                   RSTSRC = 0x10;      // reset the device                   
         
         case '?': print_menu(); 
                   break;
                       
         default: print_menu(); 
                  printf("\n*** Unknown Command\n");
      }   

   } // end while

} // end main


//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// print_menu
//-----------------------------------------------------------------------------
//
// This routine prints the command menu to the UART.
//
void print_menu(void)
{
  
   printf("\n\nC8051F30x Firmware Updater\n");
   printf("---------------------------------\n");
   printf("1. Erase FLASH and Update Firmware\n");
   printf("2. Cancel Firmware Update\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
//-----------------------------------------------------------------------------
//
// This routine erases the first 8 pages of FLASH (0x0000 to 0x0FFF).
//
void erase_flash(void)
{
   char xdata* data pagePointer = 0;// a pointer to xdata located in data space
                                    // points to the first FLASH page that 
                                    // will be erased

   int i;                           // temporary int
   bit EA_state;                    // holds interrupt state

   printf("\n*** Erasing flash from 0x0000 to 0x0FFF");
   
   EA_state = EA;                   // save interrupt state
   
   PSCTL = 3;                       // MOVX erases FLASH
   
   // Erase the first 8 FLASH pages
   for (i = 0; i < 8; i++){
      FLKEY = 0xA5;                 // FLASH lock and key sequence 1
      FLKEY = 0xF1;                 // FLASH lock and key sequence 2

      *pagePointer = 0;             // initiate the erase
      
      pagePointer += 512;
   }   

   PSCTL = 0;                       // MOVX writes target XRAM

⌨️ 快捷键说明

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