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

📄 bl78k0_kx2_control.c

📁 NEC upd78f0300的自编程序样例
💻 C
📖 第 1 页 / 共 4 页
字号:
//==============================================================================
// PROJECT      = Bootloader
// MODULE       = bl78k0_kx2_control.c
// SHORT DESC.  = Bootloader control module
// DEVICE       = 78K0/Kx2
// VERSION      = 1.0
// DATE         = 05.12.2006
// LAST CHANGE  =
// =============================================================================
// By:          NEC Electronics (Europe) GmbH
//              Arcadiastrasse 10
//              D-40472 Duesseldorf
// =============================================================================



#include "io78f0547_80.h"
#include "spl78k0_kx2.h"                // NEC EE
#include "spl78k0_types.h"              // NEC-EE
#include "spl78k0_kx2_user.h"           // NEC EE
#include "spl78k0_kx2_config.h"         // NEC EE
#include "spl78k0_kx2_const.h"          // NEC EE
#include "bl78k0_kx2_user.h"
#include "bl78k0_kx2_help_fkt.h"
#include "bl78k0_kx2_control.h"
#include "bl78k0_kx2_dbuffer.h"
#include "bl78k0_kx2_const.h"
#include "bl78k0_kx2_exceptions.h"
#include <intrinsics.h>





//*****************************************************************************
// extern data buffer for self. lib
//*****************************************************************************
extern __no_init u08  data_buffer[DATA_BUFFER_SIZE];


//*****************************************************************************
// extern functions: interface
//*****************************************************************************
extern void ifaceInit(void);
extern __callt u08 getRxByte(void);
extern __callt u08 byteReceived(void);
extern void sendByte(u08 txData);
extern void stopRx(void);
extern void readyToRx(void);
extern u08 txStatus(void);
extern u08 stopRxEnabled(void);
extern u08 readyToRxEnabled(void);
extern __callt u08 rxError(void);
extern void resetRxErrorFlags(void);
extern void resetInterface(void);


//*****************************************************************************
// extern functions: file_decoder
//*****************************************************************************
extern u08 asciiDecode(u08 ascii);
extern __callt u08 decodeReceivedBytes(u08 rx_byte);
extern u08 isEOF(void);
extern u32 getAddress(void);
extern u08 getWriteByte(void);
extern __callt void resetFileDecoder(void);

//*****************************************************************************
// extern functions: self lib
//*****************************************************************************
extern void  SelfLib_Open(void);
extern void  SelfLib_Close(void);


//*****************************************************************************
// extern functions: time module
//*****************************************************************************
extern void init_timer_50ms(void);
extern __callt void initTimeoutDetect(u08 timer_factor);
extern __callt void resetTimeoutDetect(void);
extern __callt u08 isTimeout(void);
extern __callt void setTimerIntervall_50ms(void);
extern __callt void setTimerIntervall_2ms(void);

//*****************************************************************************
// global variables
//*****************************************************************************
__saddr u08 temp_u08;                     // temporary 8 Bit variable
__saddr u16 temp_u16;                     // temporary 16 Bit variable
__saddr u32 temp_u32;                     // temporary 32 Bit variable

__saddr union union_addr union_u32;       // temporary 32 Bit union variable
__saddr union union_addr union_second_u32;// temporary 32 Bit union variable


u08 rx_buffer[RX_BUFFER_SIZE];            // receive buffer
__saddr u08 is_not_first_data;            // signalled first write into flash
__saddr u08 update_soft_or_bootl;         // bootl. or appl. update flag
                                          // (1: bootl 0:appl.)

__saddr u32 * pAddr;                      // temp 32 Bit pointer
__saddr u08 app_valid;                    // appl. valid bit
__saddr u08 exception_byte;               // current exception holder
__saddr u08 user_pressed_update;          // store users selection (bootl. or. appl update)
__saddr u08 temp_string[3];               // temporary char array for exception message



//*****************************************************************************
// Function:    resetAll
// Parameter:   None
// Return:      None
//
// Description: This function reset. all modules and essential variables!
//*****************************************************************************
__callt void resetAll(void)
{
  is_not_first_data = 0;                  // reset first write into flash signal
  update_soft_or_bootl = 0;               // reset
  app_valid = 1;                          // set app. is valid(for code optimisation)
  exception_byte = 0;                     // reset exeption byte holder
  user_pressed_update = 0;                // reset user update selection
  resetDataBufferControl();               // reset data buffer
  resetFileDecoder();                     // reset filedecoder
  resetInterface();                       // reset interface
  setTimerIntervall_50ms();               // set timer intervall on 50ms
}




//*****************************************************************************
// Function:    bootloader_start
// Parameter:   None
// Return:      u08 -> constants see within bl78k0_kx2_control.h
//      UPDATE:            start update
//      ERROR_OCCURS:      error occurs
//      APP_START:         start application
//
// Description: bootloader start messages and user prompt
//*****************************************************************************
u08 bootloader_start(void)
{
#if 0
    #ifdef BOOTL_UPDATE_ALLOWED
    u08 temp = 0;                         // reset temporary variable
    u08 * flag_u08 = &temp_u08;           // set pointer for swap flag read
  #endif

  resetAll();                             // reset all modules of bootloader
  #ifdef BOOTL_UPDATE_ALLOWED

    SelfLib_Open();                       // open self. lib. environment

    if( SelfLib_ModeCheck() == STS_NO_ERROR ) // check voltage level on FLMD0
    {
      SelfLib_Init(data_buffer);          // initalization of data buffer

      temp = SelfLib_GetInfo_BootCluster( flag_u08 ); // is swap flag set?
      if( temp != STS_NO_ERROR )          // is error occured on swap flag read?
      {
        exception_byte = GET_INFO_ERROR;  // write error(0x16) id for exception handler
        exceptionHandler();               // performing exception handler
      }

      if( *flag_u08 == 1 )                // is swap flag set?
      {
        afterFirstSwap();                 // copy bootcluster 0 to bootcluster 1 and swap
      }
    }
    else                                  // low voltage level on FLMD0 pin
    {
      exception_byte = FLMD0_CHECK_ERROR; // write error(0x14) id for exception handler
      exceptionHandler();                 // performing exception handler

      while(1)                            // infinite loop
      {
        __no_operation();                 // NOP
      }
    }
  #endif

  u08 start_state = BOOTL_SET_RX_READY;   // set first bootloader state(interface receive-ready)

  while(1)
  {

    switch(start_state)                   // bootlaoder state?
    {

      case BOOTL_SET_RX_READY:            // set interface receive-ready

            if( readyToRxWithTimeout() )    // is interface don't  receive ready?
            {
              // timeout, interface don't ready
              exception_byte = STOP_RX_IFACE_ERROR;     // write error(0x02) id for exception handler
              return ERROR_OCCURS;                      // return error occurs
            }
            // receive a character

            if( !wait_for_rx(ILLEGAL_BYTE_TIMEOUT))     // wait on illegal byte
            {
                // timeout, no byte received
                if( !rxError() )                        // is error on receive interface?
                {
                  exception_byte = RECEIVE_ERROR;       // write error(0x05) id for exception handler
                  return ERROR_OCCURS;                  // return error occurs
                }

                start_state = BOOTL_START_MESSAGES;     // enter bootloader state -> show messages
                break;
            }
            else                                        // illegal byte recieved
            {
              sendString(ILLEGAL_CHARACTERS); // send message that the user stop to send on terminal
              start_state = BOOTL_ILLEGAL_CHARACTERS; // enter bootloader state -> rx other illegal bytes
              break;
            }

    case BOOTL_ILLEGAL_CHARACTERS:

      // if don't receive any byte within 700ms, then start bootloader (state: BOOTL_START_MESSAGES)
      // if interface receive one byte within 700ms, then restart timer and clear received byte.
      // wait again for 700ms!
        while(1)
        {

          if(!wait_for_rx(ILLEGAL_SEND_TIMEOUT))  // is timeout in receive?
          {
            resetRxErrorFlags();                  // reset error flags on receive interface

            start_state = BOOTL_START_MESSAGES;   // enter bootl. state -> send start messages
            break;
          }
        }

        break;

      case BOOTL_START_MESSAGES:                  // bootl. state -> send start messages

            sendString(VERSION_TEXT);             // is timeout on message send ("BV:")?
            sendString(BOOTLOADER_VERSION);       // is timeout on message send (version)?

            if( rom_Read(FIRST_ADDR_BCLUSTER1) != APP_VALID_PATTERN ) // is app. valid?
            {
              sendString( BAD_APP_CRC );          // send message application isn't valid
              app_valid = 0;                       // set bit, that application isn't valid
            }


            sendString(UPDATE_TEXT);              // send update message "Update?y or n" or "Update?a or b"
            start_state = BOOTL_RX_Y_OR_N;        // enter bootloader state -> user prompt "y or n"
        break;

      case BOOTL_RX_Y_OR_N:

        if(!wait_for_rx(RECEIVE_TIMEOUT_K_50MS))  // timeout, don't want to update??
        {
            // timeout
            if( !rxError() )                      // is error on rx interface?
            {
              exception_byte = RECEIVE_ERROR;     // write error(0x05) id for exception handler
              return ERROR_OCCURS;                // return error occurs
            }
            // user don't pressed any key, so the program can start
            if( app_valid )
              return APP_START;                     // start application
        }
        else                                      // user pressed any key within 2s
        {
          switch(getRxByte())                     // which key does the user press?
            {

              #ifdef BOOTL_UPDATE_ALLOWED
                case 'b':                           // user pressed 'b'
                  user_pressed_update = BOOTLOADER_UPDATE;  // bootloader update
                  return UPDATE;                    // start update
                case  'a':                          // user pressed 'n'
                  user_pressed_update = APPLICATION_UPDATE; // bootloader update
                  return UPDATE;                    // start application
              #else
                case 'y':                           // user pressed 'y'
                  return UPDATE;                    // start update
                case  'n':                          // user pressed 'n'
                  if( app_valid )
                  {
                    return APP_START;               // start application
                  }
                  else
                  {
                    start_state = BOOTL_SET_RX_READY; // retry user prompt
                    break;
                  }
              #endif

              default:                              // user pressed any key
                start_state = BOOTL_SET_RX_READY;   // retry user prompt
                break;
            }
        }

      default:
        break;
    }
  }
#endif
}



//*****************************************************************************
// Function:    executeUpdate
// Parameter:   None
// Return:      u08
//      ERROR_OCCURS:      error occurs
//      APP_START:         start application
//
// Description: This function perform control an update
//*****************************************************************************
u08 executeUpdate(void)
{

  u08 update_state = START_UPDATE;        // variable for update states
  u16 rx_count = 0;                       // byte receive count
  u08 timeout = 0;                        // timeout bit for EOF detection!
  #ifdef BOOTL_UPDATE_ALLOWED
    u16 * crc_pt;                         // pointer for crc read from flash
  #endif

  while(1)
  {
    switch(update_state)                  // which update state?
    {

      case START_UPDATE:                  // start update state

        #ifndef BOOTL_UPDATE_ALLOWED
          SelfLib_Open();                 // open self. lib. environment

          if( SelfLib_ModeCheck() == STS_NO_ERROR ) // check voltage level on FLMD0 pin
          {
            SelfLib_Init(data_buffer);    // init. data buffer
          }
          else                            // low voltage level on FLMD0 pin
          {
            exception_byte = FLMD0_CHECK_ERROR;     // write error(0x14) id for exception handler
            exceptionHandler();                     // performing exception handler

            while(1)                                // infinite loop
            {
              __no_operation();                     // NOP
            }
          }
        #endif

        sendString(SEND_FILE_TEXT);       // send message "send file"

        if( app_valid )                             // APP is valid?
        {
          if( !wait_for_rx(FIRST_BYTE_HEX_TIMEOUT) )  // wait for the first byte from hex-file
          {
            // timeout

⌨️ 快捷键说明

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