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

📄 bl78k0_kx2_control.c

📁 NEC upd78f0300的自编程序样例
💻 C
📖 第 1 页 / 共 4 页
字号:
            if( !rxError() )                        // error on receive interface?
            {
              exception_byte = RECEIVE_ERROR;       // write error(0x05) id for exception handler
              return ERROR_OCCURS;                  // return error occurs
            }

            sendString(HEX_FILE_TIMEOUT);           // send message hex file receive timeout
                                                    // timeout detection is in these case don't essential
            return APP_START;                       // return start application
          }
        }
        else
        {
          while(!byteReceived())                    // wait on receive
          {
            __no_operation();                       // NOP
          }

          if( !rxError() )                          // is error on receive interface ?
          {
            exception_byte = RECEIVE_ERROR; // write error(0x05) id for exception handler
            return ERROR_OCCURS;            // return error occurs
          }
        }

        rx_buffer[0] = getRxByte();         // fetch received byte
        rx_count++;                         // increment receive count


        update_state = RECEIVE_BYTES;       // enter receive state

        break;

      case RECEIVE_BYTES:                   // receive state

        setTimerIntervall_2ms();            // set timer on 2ms interval

        #if BYTES_AFTER_RX_STOP > 0

          temp_u16 = RX_BUFFER_SIZE - BYTES_AFTER_RX_STOP;  // set receive count without after XOFF bytes

          while(rx_count < temp_u16)                        // not all bytes received
          {
            if( !wait_for_rx(RX_BYTE_TIMEOUT_HEX_2MS) )     // wait for the first byte from hex-file
            {
              if( !rxError() )                              // is error on receive interface
              {
                exception_byte = RECEIVE_ERROR;             // write error(0x05) id for exception handler
                return ERROR_OCCURS;                        // return error occurs
              }
              timeout = 1;                                  // set timeout bit for EOF check
              break;
            }
            else
            {
              rx_buffer[rx_count] = getRxByte();            // fetch received byte
              rx_count++;                                   // increment receive count
            }
          }


          #ifndef STOP_RX_WITH_TIMEOUT
              stopRx();                             // hello interface, stop receive please
          #else
            if(stopRxWithTimeout())                           // XOFF is sent?
            {
              exception_byte = STOP_RX_IFACE_ERROR;           // bit set, interface not ready
              return ERROR_OCCURS;
            }
          #endif



          if( timeout == 1 )                                // is EOF timeout bit set?
          {
             update_state = ANALYSE_BYTES;                  // switch update state to anylse received bytes
             break;
          }

          temp_u08 = 0;                                     // reset temporary variable

          while( temp_u08 < BYTES_AFTER_RX_STOP )           // receive bytes after XOFF
          {
            if( !wait_for_rx(RX_BYTE_TIMEOUT_HEX_2MS) )     // wait for the first byte from hex-file
            {
              // timeout
              if( !rxError() )                              // is error on receive interface?
              {
                exception_byte = RECEIVE_ERROR;             // write error(0x05) id for exception handler
                return ERROR_OCCURS;                        // return error occurs
              }
              break;
            }


            rx_buffer[rx_count] = getRxByte();              // fetch received byte
            rx_count++;                                     // increment receive count
            temp_u08++;                                     // increment
          }

          setTimerIntervall_50ms();                         // set intervall timer on 50ms

        #else

          while(rx_count < RX_BUFFER_SIZE)                  // not all bytes received?
          {
            if( !wait_for_rx(RX_BYTE_TIMEOUT_HEX_2MS) )     // wait for the first byte from hex-file
            {
              if( !rxError() )                              // is error on receive interface?
              {
                exception_byte = RECEIVE_ERROR;             // write error(0x05) id for exception handler
                return ERROR_OCCURS;                        // return error occurs
              }
              timeout = 1;                                  // set EOF timeout bit
              break;
            }

          rx_buffer[rx_count] = getRxByte();                // fetch received byte
          rx_count++;                                       // increment received count
          }

          setTimerIntervall_50ms();                         // set timer on 50ms

          if(stopRxWithTimeout())                           // is interface receive ready
          {
            exception_byte = STOP_RX_IFACE_ERROR;           // write error(0x02) id for exception handler
            return ERROR_OCCURS;                            // return error occurs
          }
        #endif


        update_state = ANALYSE_BYTES;                       // switch update state to anylse received bytes
        break;



      case ANALYSE_BYTES:       // here we can assume that receive_count bytes are written

       for(u16 z = 0; z < rx_count; z++ )                   // are all bytes analysed?
       {
          temp_u08 = decodeReceivedBytes( rx_buffer[z] );  // analyse next byte

          if( temp_u08 == 1 )                               // is error on hex file?
          {
            exception_byte = HEX_FILE_ERROR;                // write error(0x03) id for exception handler
            return ERROR_OCCURS;                            // return error occurs
          }
          else if(temp_u08 == 3)                            // write in data buffer?
          {
            if( !is_not_first_data )                        // is first data to write?
            {
              is_not_first_data = 1;                        // first data processed

              if( !eraseAppArea() )                         // application area erased complete without error?
              {
                exception_byte = APP_ERASE_ERROR;           // write error(0x13) id for exception handler
                return ERROR_OCCURS ;                       // return error occurs
              }

              temp_u32 = getAddress();                      // get the first write Address

              #ifdef BOOTL_UPDATE_ALLOWED

                if( temp_u32 < (u32)FIRST_ADDR_BCLUSTER1 )  // is first address in the bootloader area?
                {
                  if( user_pressed_update == APPLICATION_UPDATE)// user want to update appl.?
                  {
                    exception_byte = HEX_FILE_IS_IN_BOOTL_AREA; // set exception id
                    return ERROR_OCCURS;                        // return error occurs
                  }

                  update_soft_or_bootl = 1;                     // bootloader update is set
                }
                else  // first address is in the application area
                {

                  if( user_pressed_update == BOOTLOADER_UPDATE) // user want to update bootloader?
                  {
                    exception_byte = HEX_FILE_IS_IN_APP_AREA;   // set exception id
                    return ERROR_OCCURS;                        // return error occurs
                  }

                  update_soft_or_bootl = 0;                     // application update
                }

              #else
                if( temp_u32 < (u32)FIRST_ADDR_BCLUSTER1 )    // is first address in the bootloader area?
                {
                  // bootloader update don't allowed
                  exception_byte = BOOTL_UPDATE_NOT_ALLOWED;  // write error(0x19) id for exception handler
                  return ERROR_OCCURS;                        // return error occurs
                }
              #endif


            }

            while(1)                                          // wait until all bytes are written into the dbuffer
            {
                setByteAddr(getAddress());                    // set flash write address for the first byte in data buffer

                temp_u08 = dB_write(getWriteByte());          // write byte in data buffer


                if( temp_u08 == 1 )                           // is error occured on data buffer write?
                {
                  exception_byte = DATA_BUFFER_WRITE_ERROR;   // write error(0x07) id for exception handler
                  return ERROR_OCCURS;                        // return error occurs

                }
                else if( temp_u08 == 2 )                      // data to write into the flash?
                {
                   if( !writeIntoFlash() )                    // is error occured on flash write?
                    {
                      exception_byte = FLASH_WRITE_ERROR;     // write error(0x12) id for exception handler
                      return ERROR_OCCURS;                    // return error occurs
                    }
                }
                else
                {
                  break;
                }

              } // while

          }
          else                            // get next byte to analyse
          {
            __no_operation();             // NOP
          }

        }   // end of the for loop

        rx_count = 0;                     // reset rx_count, all bytes analysed and written

          if( timeout == 1 )              // is timeout occured?
          {
            if( !isEOF() )                // isn't EOF?
            {
              exception_byte = FILE_TRANSFER_TIMEOUT;   // write error(0x04) id for exception handler
              return ERROR_OCCURS ;                     // return error occurs
            }

            if( isLastDataToWrite() )                   // is any data to write into the flash?
            {
              if( !writeIntoFlash() )                   // write data into flash, is error on write?
              {
                exception_byte = FLASH_WRITE_ERROR;     // write error(0x12) id for exception handler
                return ERROR_OCCURS;                    // return error occurs
              }
            }
            update_state = AFTER_FLASH_WRITE;           // enter after flash write state
            break;

          }
          else                                          // no timeout on receive!
          {

            if(readyToRxWithTimeout())                  // is interface ready to rx?
            {
              exception_byte = STOP_RX_IFACE_ERROR;     // interface don't ready
              return ERROR_OCCURS;                      // return error occurs
            }

            update_state = RECEIVE_BYTES;               // set update state on receive bytes
            break;
          }

          // state: do something after flash write
      case AFTER_FLASH_WRITE:

        sendString( HEX_FILE_RECEIVED );                // send message " File OK!"

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

        #ifdef BOOTL_UPDATE_ALLOWED
        if( update_soft_or_bootl )                      // is bootloader update?
        {
          calcBootlSec1CRC();                           // chalculate CRC over bootsector 1
          temp_u16 = BL_CRC_ADDRESS + FIRST_ADDR_BCLUSTER1; // set pointer on CRC within the ROM area
          crc_pt = (u16 *)temp_u16;                     // read CRC

          if( *crc_pt != getCRC())                      // is calculated CRC equal to the read CRC
          {
            exception_byte = BOOTL_UPDATE_CRC_ERROR;    // set exception(0x09) error
            return ERROR_OCCURS;                        // return error occurs
          }

          temp_u08 = SelfLib_SetInfo_SwapBootCluster(); // execute swap with the SelfLib

          if (temp_u08 != STS_NO_ERROR)                 // is error on swapping?
          {
            exception_byte = SWAP_SET_INFO_ERROR;       // set exception(0x10) error
            return ERROR_OCCURS;                        // return error occurs
          }

          START_WATCHDOG;                               // start watchdog

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

        }
        else  // Software update
        {
          if( !checkAppCRC() )                          // calc. Application CRC
          {
            exception_byte = APP_UPDATE_CRC_ERROR;      // set exception(0x08) error
            return ERROR_OCCURS;                        // return error occurs
          }

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

          data_buffer[0] = APP_VALID_PATTERN;           // write appl. is valid
          data_buffer[1] = 0;
          data_buffer[2] = 0;
          data_buffer[3] = 0;

          // write one word into the flash
          temp_u08 =  SelfLib_EepWrite( FIRST_ADDR_BCLUSTER1, 1);

          if (temp_u08 != STS_NO_ERROR)                 // is error occured on flash write?
          {
            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;                             // return -> application start
        }

        #else
          if( !checkAppCRC() )                          // is appl. CRC ok?
          {
            exception_byte = APP_UPDATE_CRC_ERROR;      // set exception(0x08) error
            return ERROR_OCCURS;                        // return error occurs
          }

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

          data_buffer[0] = APP_VALID_PATTERN;           // write appl. is valid pattern
          data_buffer[1] = 0;
          data_buffer[2] = 0;
          data_buffer[3] = 0;

          // write one word into the flash
          temp_u08 =  SelfLib_EepWrite( FIRST_ADDR_BCLUSTER1, 1);

⌨️ 快捷键说明

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