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

📄 xmodem.c

📁 一个比较好的三星s3c4510的启动代码
💻 C
字号:
#include "XMODEM.h"//#include "../inc/44blib.h"#include "uart.h"static char blockBuf[1024];/******************** WriteByte **********************/void WriteByte(char cc) { 	put_char(0,cc);}/******************** ReadByteWithTimeout **********************/int ReadByteWithTimeout(void) {	return Uart_Getch_Timeout(TIMEOUT,0);}/******************** XModemReceive **********************/  int XModemReceive(char *bufBase, int bufLen){	int errors = 0;	int wantBlockNo = 1;	int length = 0;	int crc = 1;		char nak = 'C';			int blockBegin = 0;	int blockNo = 0;	int blockNoOnesCompl = 0;	int blockLength = 0;	int cksum = 0;	int crcHi = 0;	int crcLo = 0;				//Uart_Flush(0);							/* Ask for CRC; if we get errors, we will go with checksum */	/* Use only one uart, so ignore all print info during xmodem transfer procedure */		/* Both sender and receiver use CRC */	WriteByte(nak);						for (;;)	{		blockBegin = 0;		blockNo = 0;		blockNoOnesCompl = 0;		blockLength = 0;		cksum = 0;		crcHi = 0;		crcLo = 0;				blockBegin = ReadByteWithTimeout();		if (blockBegin < 0)		{			goto timeout;        			}                                 nak = NAK;		switch (blockBegin) 		{		case SOH:		case STX:			break;		case EOT:			WriteByte(ACK);			goto done;		default:			errors--;			goto error;		}		/* block no */		blockNo = ReadByteWithTimeout();		if (blockNo < 0)		{			goto timeout;		}		/* block no one's compliment */		blockNoOnesCompl = ReadByteWithTimeout();		if (blockNoOnesCompl < 0)		{			goto timeout;		}		if (blockNo != (255 - blockNoOnesCompl))		{			goto error;		}		blockLength = (blockBegin == SOH) ? 128 : 1024;		{			int i;			for (i = 0; i < blockLength; i++)			{				int cc = ReadByteWithTimeout();				if (cc < 0)				{					goto timeout;				}				blockBuf[i] = cc;			}		}		if (crc) 		{			crcHi = ReadByteWithTimeout();			if (crcHi < 0)			{				goto timeout;			}			crcLo = ReadByteWithTimeout();			if (crcLo < 0)			{				goto timeout;			}		} 				else 		{			cksum = ReadByteWithTimeout();			if (cksum < 0)			{				goto timeout;			}		}		if (blockNo == ((wantBlockNo - 1) & 0xff))		{			/* a repeat of the last block is ok, just ignore it. */			/* this also ignores the initial block 0 which is */			/* meta data. */			goto next;		} 				else if (blockNo != (wantBlockNo & 0xff))			 {				goto error;			 }		if (crc) 		{			int crc = 0;			int i, j;			int expectedCrcHi;			int expectedCrcLo;			for (i = 0; i < blockLength; i++)			{				crc = crc ^ (int) blockBuf[i] << 8;				for (j = 0; j < 8; j++)					if (crc & 0x8000)						crc = crc << 1 ^ 0x1021;					else						crc = crc << 1;			}			expectedCrcHi = (crc >> 8) & 0xff;			expectedCrcLo = crc & 0xff;			if ((crcHi != expectedCrcHi) || (crcLo != expectedCrcLo))			{				goto error;			}		}				else		{			unsigned char expectedCksum = 0;			int i;			for (i = 0; i < blockLength; i++)				expectedCksum += blockBuf[i];			if (cksum != expectedCksum) 			{				goto error;			}		}		wantBlockNo++;		length += blockLength;		if (length > bufLen) 		{			goto error;		}		{			int i;			for (i = 0; i < blockLength; i++)				*bufBase++ = blockBuf[i];		}	next:		errors = 0;		WriteByte(ACK);		continue;	error:	timeout:		errors++;		if (errors == MAXERRORS) 		{			/* Abort */			int i;			/* if using crc, try no CRC */			if (nak == 'C') 			{				nak = NAK;				errors = 0;				crc = 0;				goto timeout;			}			for (i = 0; i < 5; i ++)				WriteByte(CAN);			for (i = 0; i < 5; i ++)				WriteByte(BS);			return -1;		}		Uart_Flush(0);								WriteByte(nak);	} done:	Uart_Flush(0);		return length;}/******************** XModemSend **********************/  int XModemSend(char *bufBase, int bufLen){	struct 	xmodemPacket	packet;	int 	i,j;	int 	crc;	int 	reply;	char 	c;	int 	y = bufLen / BUF_SIZE;	int 	s = bufLen % BUF_SIZE;	int 	a = (s == 0) ? 0 : 1;	Uart_Flush(0);		for (i = 0; i < MAXERRORS; i++)	{		c = (char)ReadByteWithTimeout();		if (c == 'C')			break;	}		if (c != 'C')	{		i_printf("\nXModem Time Out");		return -1;			}			packet.blockBegin = SOH;	packet.blockNo = 0;		for (i = 0;i < y + a; i++)	{		if (packet.blockNo == 255)			packet.blockNo = 0;		else			packet.blockNo++;						packet.blockNoOnesCompl = 255 - packet.blockNo;                if (i == y)        {        	for (j = 0;j < s;j++)        		*(packet.buffer + j) = *(bufBase + i * BUF_SIZE + j);        		        	for (j = s;j < BUF_SIZE;j++)        		*(packet.buffer + j) = SUB;        }                else        	for (j = 0;j < BUF_SIZE;j++)        		*(packet.buffer + j) = *(bufBase + i * BUF_SIZE + j);        		                       crc = calcrc(packet.buffer, BUF_SIZE, CRC_CCITT);            packet.crcHi = (crc >> 8) & 0xff;        packet.crcLo =  crc & 0xff;                       sendPacket((char *)&packet, sizeof(packet));                reply = ReadByteWithTimeout();                switch (reply)         {        	case -1:           	case NAK:                      i--;                packet.blockNo--;               	break;                        case ACK:               	break;                        case CAN:               	goto cancel;               	break;               	            default:            	break;        }	}    	WriteByte(EOT);	cancel:    for (j = 0; j < 10; j++)	    Uart_Flush(0);      if (reply == CAN)    	return -1;    else     	return 1;}/******************** calcrc **********************/  int calcrc(char *ptr, int count, int mask) {	int crc = 0;    int i,j;     for (i = 0; i < count; i++)	{		crc = crc ^ (int) ptr[i] << 8;		for (j = 0; j < 8; j++)			if (crc & 0x8000)				crc = crc << 1 ^ mask;			else				crc = crc << 1;	}    return crc;}/******************** sendPacket **********************/  void sendPacket(char *ptr, int ptrsize){		while(ptrsize-- > 0)	{		WriteByte(*ptr++);	}}/********************************************************/  

⌨️ 快捷键说明

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