Exemplo n.º 1
0
/********************************************************************
* Function:   WriteHexRecord2Flash()
*
* Precondition: 
*
* Input:    None.
*
* Output:   
*
* Side Effects: No return from here.
*
* Overview:   Writes Hex Records to Flash.
*
*     
* Note:     None.
********************************************************************/
void WriteHexRecord2Flash(UINT8* HexRecord)
{
  static T_HEX_RECORD HexRecordSt;
  UINT8 Checksum = 0;
  UINT8 i;
  UINT WrData;
  UINT RdData;
  void* ProgAddress;
  UINT result;
    
  HexRecordSt.RecDataLen = HexRecord[0];
  HexRecordSt.RecType = HexRecord[3]; 
  HexRecordSt.Data = &HexRecord[4]; 
  
  // Hex Record checksum check.
  for(i = 0; i < HexRecordSt.RecDataLen + 5; i++)
  {
    Checksum += HexRecord[i];
  } 
  
    if(Checksum != 0)
    {
      //Error. Hex record Checksum mismatch.
      //Indicate Error by switching ON all LEDs.
      //Error();
      // Do not proceed further.
      ErrorFunction();
  } 
  else
  {
    // Hex record checksum OK.
    switch(HexRecordSt.RecType)
    {
      case DATA_RECORD:  //Record Type 00, data record.
        HexRecordSt.Address.byte.MB = 0;
          HexRecordSt.Address.byte.UB = 0;
          HexRecordSt.Address.byte.HB = HexRecord[1];
          HexRecordSt.Address.byte.LB = HexRecord[2];
          
          // Derive the address.
          HexRecordSt.Address.Val = HexRecordSt.Address.Val + HexRecordSt.ExtLinAddress.Val + HexRecordSt.ExtSegAddress.Val;
              
          while(HexRecordSt.RecDataLen) // Loop till all bytes are done.
          {
                      
            // Convert the Physical address to Virtual address. 
            ProgAddress = (void *)PA_TO_KVA0(HexRecordSt.Address.Val);
            
            // Make sure we are not writing boot area and device configuration bits.
            if(((ProgAddress >= (void *)APP_FLASH_BASE_ADDRESS) && (ProgAddress <= (void *)APP_FLASH_END_ADDRESS))
               && ((ProgAddress < (void*)DEV_CONFIG_REG_BASE_ADDRESS) || (ProgAddress > (void*)DEV_CONFIG_REG_END_ADDRESS)))
            {
              if(HexRecordSt.RecDataLen < 4)
              {
                
                // Sometimes record data length will not be in multiples of 4. Appending 0xFF will make sure that..
                // we don't write junk data in such cases.
                WrData = 0xFFFFFFFF;
                memcpy(&WrData, HexRecordSt.Data, HexRecordSt.RecDataLen);  
              }
              else
              { 
                memcpy(&WrData, HexRecordSt.Data, 4);
              }   
              // Write the data into flash. 
              result = NVMemWriteWord(ProgAddress, WrData); 
              // Assert on error. This must be caught during debug phase.   
              if(result != 0)
              {
                  while(1);
                }                 
            } 
            
            // Increment the address.
            HexRecordSt.Address.Val += 4;
            // Increment the data pointer.
            HexRecordSt.Data += 4;
            // Decrement data len.
            if(HexRecordSt.RecDataLen > 3)
            {
              HexRecordSt.RecDataLen -= 4;
            } 
            else
            {
              HexRecordSt.RecDataLen = 0;
            } 
          }
          break;
      
      case EXT_SEG_ADRS_RECORD:  // Record Type 02, defines 4th to 19th bits of the data address.
          HexRecordSt.ExtSegAddress.byte.MB = 0;
        HexRecordSt.ExtSegAddress.byte.UB = HexRecordSt.Data[0];
        HexRecordSt.ExtSegAddress.byte.HB = HexRecordSt.Data[1];
        HexRecordSt.ExtSegAddress.byte.LB = 0;
        // Reset linear address.
        HexRecordSt.ExtLinAddress.Val = 0;
        break;
        
      case EXT_LIN_ADRS_RECORD:   // Record Type 04, defines 16th to 31st bits of the data address. 
        HexRecordSt.ExtLinAddress.byte.MB = HexRecordSt.Data[0];
        HexRecordSt.ExtLinAddress.byte.UB = HexRecordSt.Data[1];
        HexRecordSt.ExtLinAddress.byte.HB = 0;
        HexRecordSt.ExtLinAddress.byte.LB = 0;
        // Reset segment address.
        HexRecordSt.ExtSegAddress.Val = 0;
        break;
        
      case END_OF_FILE_RECORD:  //Record Type 01, defines the end of file record.
        HexRecordSt.ExtSegAddress.Val = 0;
        HexRecordSt.ExtLinAddress.Val = 0;
        // Disable any interrupts here before jumping to the application.
        JumpToApp();
        break;
        
      default: 
        HexRecordSt.ExtSegAddress.Val = 0;
        HexRecordSt.ExtLinAddress.Val = 0;
        break;
    }   
  } 
    
} 
Exemplo n.º 2
0
static void WriteHexRecord2Flash(UINT8* HexRecord)
{
	static T_HEX_RECORD HexRecordSt;
	UINT8 Checksum = 0;
	UINT i;
	UINT32 WrData;
	UINT32 ProgAddress;
	UINT Result;

	HexRecord = &HexRecord[0];
	HexRecordSt.RecDataLen = HexRecord[0];
	HexRecordSt.RecType = HexRecord[3];
	HexRecordSt.Data = &HexRecord[4];

	Checksum = 0;       // Hex Record checksum check.
	for (i = 0; i < HexRecordSt.RecDataLen + 5; i++)
	{
		Checksum += HexRecord[i];
	}

	if (Checksum != 0)
	{
		// Error. Hex record Checksum mismatch.
		error(ERR_HEX_CSUM);
	}
	else
	{
		// Hex record checksum OK.
		switch (HexRecordSt.RecType)
		{
			case DATA_RECORD:   // Record Type 00, data record.
				HexRecordSt.Address.byte.MB = 0;
				HexRecordSt.Address.byte.UB = 0;
				HexRecordSt.Address.byte.HB = HexRecord[1];
				HexRecordSt.Address.byte.LB = HexRecord[2];

				// Derive the address.
				HexRecordSt.Address.Val = HexRecordSt.Address.Val + HexRecordSt.ExtLinAddress.Val + HexRecordSt.ExtSegAddress.Val;

				while (HexRecordSt.RecDataLen) // Loop till all bytes are done.
				{
					// Convert the Physical address to Virtual address.
					ProgAddress = (HexRecordSt.Address.Val / 2);

					// Make sure we are not writing boot area and device configuration bits.
					if (((ProgAddress < AUX_FLASH_BASE_ADRS) ||
					     (ProgAddress > AUX_FLASH_END_ADRS)) &&
					    ((ProgAddress < DEV_CONFIG_REG_BASE_ADDRESS) ||
					     (ProgAddress > DEV_CONFIG_REG_END_ADDRESS)))
					{
						if (HexRecordSt.RecDataLen < 4)
						{
							// Sometimes record data length will not be in multiples of 4. Appending 0xFF will make sure that..
							// we don't write junk data in such cases.
							WrData = 0xFFFFFFFF;
							memcpy(&WrData, HexRecordSt.Data, HexRecordSt.RecDataLen);
						}
						else
						{
							memcpy(&WrData, HexRecordSt.Data, 4);
						}
						// Write the data into flash.
						Result = NVMemWriteWord(ProgAddress, WrData);
						// Assert on error. This must be caught during debug phase.
						while (Result!=0);
					}

					// Increment the address.
					HexRecordSt.Address.Val += 4;
					// Increment the data pointer.
					HexRecordSt.Data += 4;
					// Decrement data len.
					if (HexRecordSt.RecDataLen > 3)
					{
						HexRecordSt.RecDataLen -= 4;
					}
					else
					{
						HexRecordSt.RecDataLen = 0;
					}
				}
				break;

			case EXT_SEG_ADRS_RECORD:   // Record Type 02, defines 4th to 19th bits of the data address.
				HexRecordSt.ExtSegAddress.byte.MB = 0;
				HexRecordSt.ExtSegAddress.byte.UB = HexRecordSt.Data[0];
				HexRecordSt.ExtSegAddress.byte.HB = HexRecordSt.Data[1];
				HexRecordSt.ExtSegAddress.byte.LB = 0;
				// Reset linear address.
				HexRecordSt.ExtLinAddress.Val = 0;
				break;

			case EXT_LIN_ADRS_RECORD:   // Record Type 04, defines 16th to 31st bits of the data address. 
				HexRecordSt.ExtLinAddress.byte.MB = HexRecordSt.Data[0];
				HexRecordSt.ExtLinAddress.byte.UB = HexRecordSt.Data[1];
				HexRecordSt.ExtLinAddress.byte.HB = 0;
				HexRecordSt.ExtLinAddress.byte.LB = 0;
				// Reset segment address.
				HexRecordSt.ExtSegAddress.Val = 0;
				break;

			case END_OF_FILE_RECORD:    // Record Type 01, defines the end of file record.
			default:
				HexRecordSt.ExtSegAddress.Val = 0;
				HexRecordSt.ExtLinAddress.Val = 0;
				break;
		}
	}
}