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

📄 dldbug.c

📁 motorola自己开发的针对coldfire 5272的Dbug bootloader程序
💻 C
字号:
/*
 * File:		dldbug.c
 * Purpose:		Routines to download new dBUG image from serial port
 *              and program it into Flash
 *
 * Notes:
 *
 */

#include "src/include/dbug.h"
#include "proj/common/relocate_io.h"

/* 
 * Compiler specific code relocation section
 */
#ifdef __DCC__
#pragma section CODE	".code_relocation"
#pragma section CONST	".code_relocation"
#endif

/********************************************************************/

#define putchar 		relocate_putchar
#define getchar 		relocate_getchar
#define getchar_present relocate_getchar_present

#ifdef __MWERKS__
__relocate_const__ char
#else
const char
#endif
erase_flash[] = "Erasing Flash\n";

#ifdef __MWERKS__
__relocate_const__ char
#else
const char
#endif
send_now[] = "Turn Xon/Xoff handshaking on and send new dBUG S-records now.\n";

#ifdef __MWERKS__
__relocate_const__ char
#else
const char
#endif
dldbug_done[] = "\bdBUG download complete.\n";

/********************************************************************/

#define CHAR_WAIT_TIMEOUT	2000

#ifdef __MWERKS__ 
__relocate_data__
#endif
char ovbuffer[60];

#ifdef __MWERKS__ 
__relocate_data__
#endif
int ovc;

#ifdef __MWERKS__ 
__relocate_data__
#endif
int ovi;

/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
static uint8
dldbug_getchar(void)
{	
	uint8 rval;
	
	if (ovc)
	{
		rval = ovbuffer[ovi++];
		if (!--ovc)
		{
			/* Send Xon character */
			putchar(XON);
			ovi = 0;
		}
		return rval;
	}
	else
	{
		return getchar();
	}
}

/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
static uint8
gethexvalue (uint8 ch)
{
	switch (ch)
	{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			return (ch - '0');
			break;
		case 'A':
		case 'a':
			return 10;
			break;
		case 'B':
		case 'b':
			return 11;
			break;
		case 'C':
		case 'c':
			return 12;
			break;
		case 'D':
		case 'd':
			return 13;
			break;
		case 'E':
		case 'e':
			return 14;
			break;
		case 'F':
		case 'f':
			return 15;
			break;
		default:
			return 0;
	}
}

/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
static uint8
getS (void)
{
	/*
	 * This routine skips all characters until an `S' is received.
	 * It then returns the next character.
	 * This routine is here in order to not echo characters back
	 * out as they are received during a download.
	 *
	 * This code ignores NL,CR-LF end of line issues by waiting
	 * for 'S'.
	 */
	uint8 c;

	while (TRUE)
	{
		c = dldbug_getchar();
		if (c == 'S')
			break;
	}

	/* Get type of S-record */
	return dldbug_getchar();
}

/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
static uint8
getSpair (void)
{
	uint8 ch;
	uint8 upper;

	ch = dldbug_getchar();
	upper = (uint8)(gethexvalue(ch) << 4);
	ch = dldbug_getchar();
	return (upper | gethexvalue(ch));
}

/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
void
download_dbug (void (*dbug)(void))
{
	ADDRESS address;
	uint8 length, type, data, buffer[200], hash[5];
	int i, timeout, hashi = 1, hashj = 0;
	int amd_flash_erase(ADDRESS, int, void (*)(char));
	int amd_flash_program(ADDRESS, ADDRESS, int, int, 
						void (*)(void), void (*)(char));
	
	hash[0]=8;	/* Backspace */
	hash[1]=124;/* "|" */
	hash[2]=47; /* "/" */
	hash[3]=45; /* "-" */
	hash[4]=92; /* "\" */
	
	/* Print "Erasing Flash" message */
	for (i = 0; erase_flash[i] != '\n'; i++)
		putchar(erase_flash[i]);
	
	/* Erase dBUG - no turning back now */
	amd_flash_erase(AMD_FLASH_ADDRESS, AMD_FLASH_SIZE, putchar);

	/* Print send s-record message */
	for (i = 0; send_now[i] != '\n'; i++)
		putchar(send_now[i]);
	putchar(LF);
	putchar(CR);
	
	/* Send Xon character */
	putchar(XON);
	
	ovc = 0;
	while (1)
	{
		/* Get start of S-record */
		type = getS();

		/* Get record length */
		length = getSpair();

		/* Take appropriate action */
		switch (type)
		{
			case '1':
			case '2':
			case '3':
				timeout = 0;
				address = NULL;
				type -= '0';
				for (i = 0; i <= type ; i++)
				{
					/* formulate address */
					data = getSpair();
					address = (address << 8) | data;
					length--;
				}
				
				/* Get data and put into buffer */
				for (i = 0; i < (length - 1); i++)
				{
					data = getSpair();
					buffer[i] = (uint8)data;
				}
				
				/* Send Xoff character */
				putchar(XOFF);

				/* Catch the overflow */
				while (++timeout < CHAR_WAIT_TIMEOUT)
				{
					if (getchar_present())
					{
						ovbuffer[ovc++] = getchar();
					}
				}
				
				/* Throw out non Flash addresses */
				if ((address >= AMD_FLASH_ADDRESS) &&
					(address < (AMD_FLASH_ADDRESS + AMD_FLASH_SIZE - 1)))
				{			
					/* Write buffered data to Flash */
					amd_flash_program(address,(ADDRESS)buffer, 
								i, FALSE, NULL, NULL);
				}
				
				/* 
				 * Hash marks to indicate progress 
				 */
				if (++hashj == 10)
				{
					putchar(hash[0]);
					putchar(hash[hashi]);
					if (++hashi == 5)	
						hashi=1;
					hashj = 0;
				}
				/* Send Xon if no overflow occured */
				if (!ovc)
					putchar(XON);
				
				break;
			case '7':
			case '8':
			case '9':
				/*
				 * Read in termination record and discard
				 */
				for (i = 0; i < length; i++)
					getSpair();
				/*
				 * Print done message
				 */
				for (i = 0; dldbug_done[i] != '\n'; i++)
					putchar(dldbug_done[i]);
					
				/* 
				 * Call new dBUG 
				 */
				dbug();
				
				break;
			case '0':
			case '4':
			case '5':
			case '6':
			default:
				break;
		}
	}
}
/********************************************************************/

⌨️ 快捷键说明

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