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

📄 flash.c

📁 DSP 5409 plc应用程序,调试通过,是用在电力线通讯上的演示程序.
💻 C
📖 第 1 页 / 共 4 页
字号:
u16 FlashErase(i16 Target)
{
	u16 i;

	if(FlashLogSize != 0)									// if area currently used by flash log
	{
		return FLASH_STATUS_IN_USE;							// tell the user
	}
  
  	DownLoadActive = DOWN_LOAD_TIMEOUT;						// indicate the lights can come on
															// if the boot code has not been loaded or appears corrupt
	if(ReadFlash(GetBaseAddress(FLASH_TARGET_BOOT) + FLASH_BOOT_CODE_TAG_OFFSET) != FLASH_BOOT_CODE_TAG)
	{
		// by indicating flash needs to be erased in all targets we lockout any possibility of other
		// commands being issued to other targets and halt any commands in progress
		for(i = 0; i < FLASH_TARGET_COUNT; i++)// loop through all targets
		{
			FlashSize[i] = -1;								// indicate the flash needs to be erased
		}
	}
	else													// if erasing a target image
	{
		switch(Target)										// select target
		{
        	case FLASH_TARGET_PROG:
				FlashLoadAddress[Target] = FLASH_PROG_CODE_LOWER_LIMIT;	// point to beginning of code space
                break;
        	case FLASH_TARGET_LOG:
				FlashLoadAddress[Target] = 0;				// point to beginning of log space
                break;
		}

		FlashSize[Target] = -2;								// indicate to target to erase old image
	}

	EnableFlashWriteAccess();								// enable the erase to take place
	return StateMachineStatus = FLASH_STATUS_PENDING;		// return change of status
}

//==========================================================================================
// Function:		ParseRecord()		
//
// Description:		Called to parse and write to flash a line of data from an intelHex
//					file.
//
//					If any errors are detected during parsing of the line, the failure mode
//					is returned and the write does not take place.
//
//					If the parse completes withouth error, the data is put in the
//					FlashBuffer for the target specified and the write is initiated for the
//					state machine.  A pending status is returned to indicate successful
//					parsing.
//
// Revision History:
//==========================================================================================
u16 ParseRecord(i16 Target)
{
	i16 i;
	u16 Size = FlashSize[Target];
	u16* Buffer = FlashBuffer[Target];
	u16 CheckSum;
	u16 Address;
	u16 Type;
	u16 DataValue;
    u32 tmp;
	i16 UartBuffPos = 0;									// index into buffer uart data buffer

	DownLoadActive = DOWN_LOAD_TIMEOUT;						// indicate we are actively downloading data

	Size = ReadHexNumber(2, &UartBuffPos);					// get record size
	CheckSum = Size;										// initialize the checksum
	Address = ReadHexNumber(4, &UartBuffPos);				// get address for this record
	CheckSum += (Address >> 8) & 0xFF;
	CheckSum += Address & 0xFF;								// add address into checksum 
	Type = ReadHexNumber(2, &UartBuffPos);					// get record type
	CheckSum += Type;										// add type into checksum

	for(i = 0; i < (Size >> 1); i++)						// loop through record data field
	{
		DataValue = ReadHexNumber(4, &UartBuffPos);			// get this data word
		CheckSum += DataValue & 0xFF;						// add low byte to checksum
		CheckSum += DataValue >> 8;							// add high byte to checksum
		Buffer[i] = DataValue;								// save the data word
	}

	if((Size & 1) != 0)										// if record size was an odd number of bytes
	{
		DataValue = ReadHexNumber(2, &UartBuffPos);			// read in last byte
		CheckSum += DataValue;								// add it to the checksum
		DataValue = DataValue << 8;							// move data to the high byte
		DataValue |= 0xFF;									// set unused bits
		Buffer[(Size >> 1) + 1] = DataValue;				// assign last byte of data
		Size++;												// adjust size to include unused bits
	}

	CheckSum = (~CheckSum + 1) & 0xFF;						// calculate final checksum

	if(CheckSum != ReadHexNumber(2, &UartBuffPos))			// if checksum fails
	{
		return StateMachineStatus = FLASH_STATUS_CHECKSUM;	// bail with error
	}

	if(Type == 4)											// if this is a high address field
	{
		if(Target > FLASH_TARGET_CODE_LIMIT)				// if working on general data
		{
			FlashLoadAddress[Target] = Buffer[0] * 0x10000ul;	// set high address bits
		}
		else												// if working in a code image
		{
			if(Buffer[0] != 0)								// and out of address range
			{
				return StateMachineStatus = FLASH_STATUS_ADDR_RANGE;	// bail with error
			}
		}
	}

	if(Type == 0 || Type == 1)								// if this is a data record
	{
		if(Type != 0 && Target <= FLASH_TARGET_CODE_LIMIT)	// check to see if the tag needs to be updated, only for code images
		{
			DownLoadActive = 5;								// allow the delay to be short
			WriteTag[Target] = 1;							// indicate the tag needs to be updated
		}
		else												// if this is not the end of the file
		{
			WriteTag[Target] = 0;							// don't modify the tag yet
		}
      
		FlashLoadAddress[Target] &= ~0xFFFFul;				// remove lower address bits
		FlashLoadAddress[Target] += Address;				// add in low address value

		if(Size > 0)										// if this record contains data
		{
           	tmp = FlashLoadAddress[Target];					// get working address
			if(Target == FLASH_TARGET_PROG)					// if working in DSP image
			{
				if(tmp < FLASH_PROG_CODE_LOWER_LIMIT
                   || tmp > FLASH_PROG_CODE_UPPER_LIMIT)  	// and address is out of range
				{
					return StateMachineStatus = FLASH_STATUS_ADDR_RANGE;	// bail on error
				}
			}
 		}
        
		FlashSize[Target] = Size >> 1;						// update the size of the buffer to write in words
		EnableFlashWriteAccess();							// allow flash to be written
		return StateMachineStatus = FLASH_STATUS_PENDING;	// tell user we have the data
	}

	return StateMachineStatus = FLASH_STATUS_INVALID_REC;	// indicate the error
}

//==========================================================================================
// Function:		ReadHexNumber()		
//
// Description:		Reads a hexadecimal number of Digits starting at nibble Position in the
//					input buffer and returns the number read.
//
// Revision History:
//==========================================================================================
u16 ReadHexNumber(i16 Digits, i16* Position)
{
	i16 i;
	i16 Character = -1;
	u16 Value = 0;
	for(i = 0; i < Digits; i++)								// loop through all digits
	{
		Character = GetBufferCharacter((*Position)++);		// get character, bump pointer
		Value <<= 4;										// shift by one hex digit
		Value += Character;									// add in this digit
	}
	return Value;											// return the value retrieved
}

//==========================================================================================
// Function:		GetTargetStatus()		
//
// Description:		Returns the status of a specific target from the view point of the
//					state machine.  If the target is busy a nonzero value is returned.
//
// Revision History:
//==========================================================================================
i16 GetTargetStatus(i16 Target)
{
	return TargetStatus[Target];							// return info requested
}

//==========================================================================================
// Function:		FlashLog()
//
// Description:		Writes records to the current log heap located in flash memory.
//
//					Records can be any size up to FLASH_LOG_RECORD_MAX_SIZE words.
//
//					Returns status codes indicate:
//					SUCCESS				The state machine has been handed the data.
//					FLASH_LOG_NO_MEM	The log heap in flash is full, data not stored.
//					FLASH_LOG_TOO_BIG   The data size passed is too large for a log record,
//										data not stored.
//					FLASH_LOG_BUSY      The state machine is currently busy with a previous
//										record write and cannot handle this request,
//										data not stored.
//                  FLASH_LOG_CORRUPT   The log appears to be corrupted and must be erased
//										before any more writes can occur.
//
//					uFlashLogStatus will contain the status of the state machine with respect
//					to the flash log.  If uFlashLogStatus >= FLASH_STATUS_TIMEOUT then a call
//					to FlashLogErase() must be made to clear the log file and uFlashLogStatus.
//					This is because an error occured at sometime during state machine
//					operation and the log file is now suspected to be corrupt.  Erasure is
//					the only way to guarantee its status.
//					
//
// Revision History:
//==========================================================================================
i16 FlashLog(void* Source, u16 Size)
{
	u16 i;

	if(uFlashLogStatus >= FLASH_STATUS_TIMEOUT)				// if the flash log appears corrupted
	{
		return FLASH_LOG_CORRUPT;
	}
    
	// if adding this record will cause the log buffer to grow larger than the end of flash memory
	if(FlashLogSize + Size + 2 >= FLASH_LOG_MAX_SIZE)
	{
		return FLASH_LOG_NO_MEM;							// let the user know about it
	}
  
	if(Size > FLASH_LOG_RECORD_MAX_SIZE)					// if data record and succeeding link too big
	{
		return FLASH_LOG_TOO_BIG;							// let the user know about it
	}
  
	if(GetTargetStatus(FLASH_TARGET_LOG))					// if the buffer is not available
	{
		return FLASH_LOG_BUSY;								// let the user know about it
	}

	DownLoadActive = DOWN_LOAD_TIMEOUT;						// indicate the flash log is active

	EnableFlashWriteAccess();								// enable access to flash
															// set address to write data at
	FlashLoadAddress[FLASH_TARGET_LOG] = FlashLogSize;

	FlashLogSize += Size + 2;								// update size of the flash log
															// write the new succeeding link to the buffer
	FlashBuffer[FLASH_TARGET_LOG][0] = (u16)((FlashLogSize >> 16) & 0xFFFFul);
	FlashBuffer[FLASH_TARGET_LOG][1] = (u16)(FlashLogSize & 0xFFFFul);
  
	for(i = 0; i < Size; i++)								// loop through all data to write
	{														// copy it to the code buffer
		FlashBuffer[FLASH_TARGET_LOG][i + 2] = *(((u16*)Source) + i);
	}
  
	FlashSize[FLASH_TARGET_LOG] = Size + 2;					// set size of data to write so state machine will do it
  
	return 0;												// return success
}

//==========================================================================================
// Function:		FlashLogErase()
//
// Description:		Erases the entire flash log
//
//					Returns status codes indicate:
//					SUCCESS				The state machine has been handed the data.
//					FLASH_LOG_BUSY      The state machine is currently busy with a previous
//										record write and cannot handle this request.
//                  FLASH_LOG_CORRUPT   The log appears to be corrupted and must be erased
//										before any more writes can occur.
//
//					FlashLogErase() will clear any error condition in uFlashLogStatus if
//					the flash erase completes without error itself.
//
// Revision History:
//==========================================================================================
i16 FlashLogErase(void)
{
	if(FlashLogSize == 0)									// if nothing to erase
	{
		return 0;											// return success
	}
	
	if(GetTargetStatus(FLASH_TARGET_LOG))					// if the is not available
	{
		return FLASH_LOG_BUSY;								// let the user know
	}

	DownLoadActive = DOWN_LOAD_TIMEOUT;						// indicate flash log active

	EnableFlashWriteAccess();								// enable access to the flash
	FlashLoadAddress[FLASH_TARGET_LOG] =0;					// set address to erase
	FlashSize[FLASH_TARGET_LOG] = -2;						// set erase sector command

	FlashLogSize = 0;										// reflect the new status
	uFlashLogStatus = FLASH_STATUS_COMPLETE;				// clear the log status
  
	return 0;												// return success
}

//==========================================================================================
// Function:		UpdateStatus()
//
// Description:		Updates the appropriate status flag depending on the target passed
//
// Revision History:
//==========================================================================================
void UpdateStatus(i16 Target)
{
	if(Target == FLASH_TARGET_LOG)							// if working on the flash log
	{
		uFlashLogStatus = StateMachineStatus;				// update logging status
	}
	else													// if working on a program update
	{
		uFlashWriteStatus = StateMachineStatus;				// update program update status
	}
	return;
}

⌨️ 快捷键说明

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