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

📄 updater_f02x.c

📁 C8051F02x Firmware Updating Code
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// updater_F02x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: FB
// DATE: 28 JUN 02
//
// This program shows an example Firmware Updater using the 'F02x. 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 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 <stdlib.h>
#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 = P1^6;                       // LED='1' means ON
sbit SW2 = P3^7;                       // SW2='0' means switch pressed


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

   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;

   EA = 0;                             // disable interrupts (this statement
                                       // is needed because the device is not
                                       // neccesarily in a reset state prior 
                                       // to executing this code) 

   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(); 
                                               
         case '2': printf("\n** RESETTING **\n\n");
                   RSTSRC = 0x10;      // reset the device                   
         
         case '?': print_menu(); 
                   break;
                       
         default:  print_menu(); 
                   printf("\n*** Unknown Command\n");
                   break;
      }   

   } // end while

} // end main


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

//-----------------------------------------------------------------------------
// print_menu
//-----------------------------------------------------------------------------
//
// This routine prints the command menu to the UART.
//
void print_menu(void)
{
  
   printf("\n\nC8051F02x 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

⌨️ 快捷键说明

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