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

📄 bl78k0_kx2_control.c

📁 NEC upd78f0300的自编程序样例
💻 C
📖 第 1 页 / 共 4 页
字号:

          if (temp_u08 != STS_NO_ERROR)                 // is error on sweapping?
          {
            exception_byte = FLASH_WRITE_ERROR;         // set exception(0x12) error
            return ERROR_OCCURS;                        // return error occurs
          }

          if( !verifyAppArea() )                        // is an error occured on appl. area verify?
          {
            exception_byte = VERIFY_BYTES_ERROR;        // set exception(0x11) error
            return ERROR_OCCURS;                        // return error occurs
          }

          sendString( UPDATE_READY );                   // send message "Update is Ready"
          return APP_START;                             // Start application

        #endif

      default:
        break;

    }
  }

}



//*****************************************************************************
// Function:    writeIntoFlash
// Parameter:   None
// Return:      u08
//      ERROR:  error occurs
//      OK:     all bytes ar written into the flash
//
// Description: This function communicate with the DataBuffer module.
//              Written bytes in the data buffer will be written into the flash.
//*****************************************************************************

u08 writeIntoFlash(void)
{
  union_u32.u32_access = getFirstWriteAddress();    // get first flash write addr.
  temp_u08 = getWordCount();                        // get word count to write

  #ifdef LAST_APP_BLOCK
    if(getBlockNr() > LAST_APP_BLOCK )              // is block nr. over the boundary?
    {
      exception_byte = DEF_BLOCK_END_EXCEEDED;      // set exception(0x17) error
      return ERROR ;                                // return -> error occurs
    }
  #endif

  #ifdef BOOTL_UPDATE_ALLOWED
  if( update_soft_or_bootl )                        // is bootloader update?
    {
      // is addr. over bootcluster boundary?
      if( union_u32.u16_access.U16_1 > FIRST_ADDR_BCLUSTER1 )
      {
        exception_byte = BOOTL_UPDATE_ADDR_ERROR;   // set exception(0x18) error
        return ERROR;                               // return error
      }

      // add offset of 4K for bootl. update -> bootl. must be in the bootsector 1
      union_u32.u16_access.U16_1 += FIRST_ADDR_BCLUSTER1;
    }
  #endif

  // write into the flash!
  temp_u08 =  SelfLib_EepWrite( union_u32.u32_access, temp_u08);
  if (temp_u08 != STS_NO_ERROR)                     // is error on flash write?
  {
    exception_byte = FLASH_WRITE_ERROR;             // set exception(0x12) error
    return ERROR;                                   // return error occurs
  }

  return OK;                                        // return -> all bytes written

}





//*****************************************************************************
// Function:    afterFirstSwap
// Parameter:   None
// Return:      None
//
// Description: This function will be used, if the first swap is processed.
//
//*****************************************************************************
void afterFirstSwap(void)
{
    u08 self_lib_result = 0;
    u16 crc_temp = 0;
    u16 * crc_pt;

  if( !eraseAppArea())                              // error on erase?
  {
    exception_byte = APP_ERASE_ERROR;               // set exception(0x13) error
    exceptionHandler();                             // perform exception handler

    while(1)                                        // infinity loop!
    {
      __no_operation();                             // NOP
    }
  }
  else                                              // no error on app. erase!
  {
    if( !copyBootCluster() )                        // error on copy bootcluster 0 to 1
    {
      exception_byte = COPY_BCLUSTER_ERROR;         // set exception(0x15) error
      exceptionHandler();                           // perform exception handler
      while(1)                                      // infinity loop
      {
        __no_operation();                           // NOP
      }
    }
    else                                            // no error on bootcluster copy
    {
      if( !verifyAppArea())                         // error on app. area verify?
      {
        exception_byte = VERIFY_BYTES_ERROR;        // set exception(0x11) error
        exceptionHandler();                         // perform exception handler

        while(1)                                    // infinity loop
        {
          __no_operation();                         // NOP
        }
    }

    calcBootlSec1CRC();                             // calculate CRC of bootcluster(bootl. 1) 1
    crc_pt = (u16 *)BL_CRC_ADDRESS;                 // read CRC of the bootcluster 0
    crc_temp = getCRC();

    if( *crc_pt != crc_temp )                       // is CRC of bootcluster 0 and 1 the same?
    {
      exception_byte = BOOTL_UPDATE_CRC_ERROR;      // set exception(0x09) error
      exceptionHandler();                           // perform exception handler

      while(1)                                      // infinity loop
      {
        __no_operation();                           // NOP
      }
    }


    #if DATA_BUFFER_SIZE < 4
      #error DATA_BUFFER_SIZE is smaller than 4
    #endif


    data_buffer[0] = 0;
    data_buffer[1] = 0;
    data_buffer[2] = (u08)(crc_temp & 0x00FF);      // write CRC 8 Bit LSB
    data_buffer[3] = (u08)(crc_temp >> 8);          // write CRC 8 Bit MSB

    // write bootloader CRC into the flash of the bootcluster 1
    self_lib_result =  SelfLib_EepWrite( CRC_BL_HEADER + FIRST_ADDR_BCLUSTER1, 1);

    if ( self_lib_result != STS_NO_ERROR )          // is error on write?
    {
      exception_byte = FLASH_WRITE_ERROR;           // set exception(0x12) error
      exceptionHandler();                           // perform exception handler

      START_WATCHDOG;                               // start watchdog

      while(1)                                      // infinity loop
      {
        __no_operation();                           // NOP
      }
    }

    // set swap flag in the hidden rom area
    self_lib_result = SelfLib_SetInfo_SwapBootCluster();

    if (self_lib_result != STS_NO_ERROR)            // is error on swapflag set?
    {
      exception_byte = SWAP_SET_INFO_ERROR;         // set exception(0x10) error
      exceptionHandler();                           // perform exception handler

      while(1)                                      // infinity loop
      {
        __no_operation();                           // NOP
      }
    }

    sendString( UPDATE_READY );                     // send message "Update ready"
    START_WATCHDOG;                                 // start watchdog

    while(1)                                        // infinity loop <- reset generating
    {
      __no_operation();                             // NOP
    }
  }

 }
}




//*****************************************************************************
// Function:    calcBootlSec1CRC
// Parameter:   None
// Return:      None
//
// Description: Calculate CRC over the bootcluster 1 area
//              Start and End of the area to calculate must be always the same
//*****************************************************************************
__callt void calcBootlSec1CRC(void)
{
  u16 end_addr;
  u08 rom_read_byte;

  resetCRC();                                       // reset old CRC

  end_addr = LAST_BOOTL_CODE_ADDR + FIRST_ADDR_BCLUSTER1; // set calc. end addr.

  for( u16 i = FIRST_ADDR_BCLUSTER1; i <= end_addr; i++ ) // all bytes included in calc.
  {
    rom_read_byte = rom_Read(i);                    // read byte from bootcl. 1 area
    calcCRC( rom_read_byte );                       // calc CRC
  }

  // The calcCRC must be called with 2 zeros after calculation!
  calcCRC(0x00);
  calcCRC(0x00);
}



//*****************************************************************************
// Function:    checkAppCRC
// Parameter:   None
// Return:      u08
//              OK :    calc. and read CRC are equal
//              ERROR:  calc. CRC isn't equal to read CRC or addr. error
//
// Description: Calculate CRC over the application area
//              The start and end addresses are defined in the application area
//              on addresses APP_START_ADR and APP_END_ADR
//*****************************************************************************
__callt u08 checkAppCRC(void)
{

  u16 * crc_pt;
  u16 read_crc;
  u08 rom_read_byte;

  resetCRC();                             // reset old CRC

  pAddr = (u32 *)APP_START_ADR;           // set pointer on the start addr. location of the appl.
  union_u32.u32_access = *pAddr;          // read and save start address of the application

  pAddr = (u32 *)APP_END_ADR;             // set pointer on the end addr. location of the appl.
  union_second_u32.u32_access = *pAddr;   // read and save end address of the application

  #ifdef CODEBANK_BANKS_USER

  if( union_u32.u32_access > union_second_u32.u32_access )  // is end address smaller than start addr.
  {
    return ERROR;                             // return error occurs!
  }

  if( union_second_u32.u08_access.U08_3 > 0 ) // is end address outside the common area?
  {
    while( union_u32.u32_access <= union_second_u32.u32_access )  // all addresses read?
    {
      if( union_u32.u08_access.U08_3 >= CODEBANK_BANKS_USER )     // isn't valid bank nr.?
      {
        return ERROR;                                             // error!
      }

      BANK = union_u32.u08_access.U08_3;                          // select bank

      rom_read_byte = rom_Read(union_u32.u16_access.U16_1);       // read byte from flash addr.
      calcCRC( rom_read_byte );                                   // calc. CRC for read byte


      if( (u16)union_u32.u16_access.U16_1 < (u16)CODEBANK_END_USER )  // isn't boundary reached?
      {
        union_u32.u16_access.U16_1++;                                 // increment address
      }
      else                                                            // boundary reached!
      {
        // set addr. pointer on the first address in the bank
        union_u32.u16_access.U16_1 = CODEBANK_START_USER;
        union_u32.u08_access.U08_3++;                             // inc. address
      }

    }
  }
  else      // last address is in the common area!
  {
    // isn't last addr. valid( exceeds BANK0 )
    if( union_second_u32.u16_access.U16_1 > CODEBANK_END_USER )
    {
      return ERROR; // ERROR: Address exceeds BANK 0
    }

    // all bytes read?
    while( union_u32.u16_access.U16_1 <= union_second_u32.u16_access.U16_1 )
    {

      rom_read_byte = rom_Read(union_u32.u16_access.U16_1);       // read byte from flash!
      calcCRC( rom_read_byte );                                   // calc CRC for read byte!
      union_u32.u16_access.U16_1++;                               // set pointer on the next address
    }
  }

  #else

    #ifndef FLASH_END_ADDR
      #error FLASH_END_ADDR is not defined! Please define the end address of the micro.
    #endif

    if( union_second_u32.u08_access.U08_3 > 0 ) // is end address outside the common area?
    {
      return ERROR;                             // return error occurs!
    }

    if( union_u32.u16_access.U16_1 > union_second_u32.u16_access.U16_1 )  // is end address smaller than start addr.
    {
      return ERROR;                             // return error occurs!
    }

    if( union_second_u32.u16_access.U16_1 > FLASH_END_ADDR )  // is end address bigger than mem. area
    {
      return ERROR;                             // return error occurs!
    }

    while( union_u32.u16_access.U16_1 <= union_second_u32.u16_access.U16_1 )
    {

      rom_read_byte = rom_Read(union_u32.u16_access.U16_1);       // read byte from flash!
      calcCRC( rom_read_byte );                                   // calc CRC for read byte!
      union_u32.u16_access.U16_1++;                               // set pointer on the next address
    }

  #endif

  // The calcCRC must be called with 2 zeros after calculation!
  calcCRC(0x00);
  calcCRC(0x00);

  crc_pt = (u16 *)CRC_APP_LOCATION;                     // set CRC-Read-Pointer on CRC-Location

  read_crc = *crc_pt;                                   // read and save CRC

  if( read_crc == getCRC() )                            // calc. and read CRC are same?
  {
   return OK;                                           // appl. is OK!
  }

  return ERROR;                                         // error on CRC check
}


//*****************************************************************************
// Function:    verifyAppArea
// Parameter:   None

⌨️ 快捷键说明

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