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

📄 icmp.s

📁 AVR单片机系统开发经典实例部分源程序
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module icmp.c
	.area lit(rom, con, rel)
_icmp_str1::
	.byte 32,32,32,32,'I,'C,'M,'P,'H,'d,'r,58,32,'t,'y,'p
	.byte 'e,58,0
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\icmp.c
	.dbsym e icmp_str1 _icmp_str1 A[19:19]kc
_icmp_str2::
	.byte 32,'C,'o,'d,'e,58,0
	.dbsym e icmp_str2 _icmp_str2 A[7:7]kc
_icmp_str3::
	.byte 32,'C,'h,'k,'s,'u,'m,58,0
	.dbsym e icmp_str3 _icmp_str3 A[9:9]kc
_icmp_str4::
	.byte 32,'I,'D,58,0
	.dbsym e icmp_str4 _icmp_str4 A[5:5]kc
_icmp_str5::
	.byte 32,'S,'e,'q,'N,'u,'m,58,0
	.dbsym e icmp_str5 _icmp_str5 A[9:9]kc
_icmp_str6::
	.byte 32,'D,'a,'t,'a,'B,'y,'t,'e,'s,58,0
	.dbsym e icmp_str6 _icmp_str6 A[12:12]kc
_icmp_str7::
	.byte 32,45,32,'B,'L,'O,'C,'K,'E,'D,10,0
	.dbsym e icmp_str7 _icmp_str7 A[12:12]kc
_icmp_str8::
	.byte 42,42,42,32,'I,'C,'M,'P,32,'c,'h,'k,'s,'u,'m,32
	.byte 'e,'r,'r,10,0
	.dbsym e icmp_str8 _icmp_str8 A[21:21]kc
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\icmp.c
_ICMP_Header::
	.blkb 2
	.area idata
	.word 0
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\icmp.c
	.dbstruct 0 8 TICMPHeader
	.dbfield 0 Type c
	.dbfield 1 Code c
	.dbfield 2 Checksum s
	.dbfield 4 ID s
	.dbfield 6 SeqNum s
	.dbend
	.dbsym e ICMP_Header _ICMP_Header pS[TICMPHeader]
	.area text(rom, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\icmp.c
	.dbfunc e ICMP_DisplayHeader _ICMP_DisplayHeader fV
;     TotalBytes -> R20,R21
;      HeaderIdx -> R22,R23
	.even
_ICMP_DisplayHeader::
	xcall push_gset2
	movw R20,R18
	movw R22,R16
	sbiw R28,2
	.dbline -1
	.dbline 76
; 
; /*
;  * Copyright (C) 2003-2004 by Clive Moss All rights reserved.
;  *
;  * Help & Contributions from D.J.Armstrong
; 
;  * Redistribution and use in source and binary forms, with or without
;  * modification, are permitted provided that the following conditions
;  * are met:
;  *
;  * 1. Redistributions of source code must retain the above copyright
;  *    notice, this list of conditions and the following disclaimer.
;  * 2. Redistributions in binary form must reproduce the above copyright
;  *    notice, this list of conditions and the following disclaimer in the
;  *    documentation and/or other materials provided with the distribution.
;  * 3. Neither the name of the copyright holders nor the names of
;  *    contributors may be used to endorse or promote products derived
;  *    from this software without specific prior written permission.
;  *
;  * THIS SOFTWARE IS PROVIDED BY CLIVE MOSS 'AS IS' AND ANY EXPRESS OR
;  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;  * ARE DISCLAIMED.
;  * IN NO EVENT SHALL CLIVE MOSS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
;  * INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
;  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
;  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
;  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
;  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
;  * SUCH DAMAGE.
;  */
; 
; //#ifdef CPU_eZ8
; //	#pragma stkck									// enable stack checking
; //#endif
; 
; #include <string.h>
; #include <stdlib.h>
; #include <stdio.h>
; 
; #include "common.h"
; #include "ppp.h"
; #include "ip.h"
; #include "icmp.h"
; 
; #ifdef Debug
; flash char	icmp_str1[]	=	"    ICMPHdr: type:";
; flash char	icmp_str2[]	=	" Code:";
; flash char	icmp_str3[]	=	" Chksum:";
; flash char	icmp_str4[]	=	" ID:";
; flash char	icmp_str5[]	=	" SeqNum:";
; flash char	icmp_str6[]	=	" DataBytes:";
; flash char	icmp_str7[]	=	" - BLOCKED\n";
; flash char	icmp_str8[]	=	"*** ICMP chksum err\n";
; #endif
; 
; #ifdef CPU_eZ8
; volatile near u16		ICMP_Timer;
; volatile near u16		ICMP_SeqNum;
; volatile near T_IP_Addr	ICMP_IP;
; #endif
; #ifdef CPU_ATmega128
; volatile u16			ICMP_Timer;
; volatile u16			ICMP_SeqNum;
; volatile T_IP_Addr		ICMP_IP;
; #endif
; 
; T_ICMP_Header	*ICMP_Header = NULL;
; 
; // **************************************************************************
; 
; #ifdef Debug
; 
; void ICMP_DisplayHeader(int HeaderIdx, int TotalBytes)
; {
	.dbline 90
; 
; /*
; 	 0                   1                   2                   3
; 	 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
; 	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
; 	|    Type       |    Code       |         Checksum              |
; 	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
; 	|             ID                |      Sequence Number          |
; 	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
; */
; 
; 	// show the header
; 
; 	if (!SendDebugRStr(icmp_str1)) return;
	ldi R16,<_icmp_str1
	ldi R17,>_icmp_str1
	xcall _SendDebugRStr
	tst R16
	brne L2
	.dbline 90
	xjmp L1
L2:
	.dbline 91
; 	sprintf((char*)ScratchPad, "%u", ICMP_Header->Type);
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R2,z+0
	clr R3
	std y+1,R3
	std y+0,R2
	ldi R18,<L4
	ldi R19,>L4
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 92
; 	if (!SendDebugStr((char*)ScratchPad)) return;
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L5
	.dbline 92
	xjmp L1
L5:
	.dbline 94
; 
; 	if (!SendDebugRStr(icmp_str2)) return;
	ldi R16,<_icmp_str2
	ldi R17,>_icmp_str2
	xcall _SendDebugRStr
	tst R16
	brne L7
	.dbline 94
	xjmp L1
L7:
	.dbline 95
; 	sprintf((char*)ScratchPad, "%u", ICMP_Header->Code);
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R2,z+1
	clr R3
	std y+1,R3
	std y+0,R2
	ldi R18,<L4
	ldi R19,>L4
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 96
; 	if (!SendDebugStr((char*)ScratchPad)) return;
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L9
	.dbline 96
	xjmp L1
L9:
	.dbline 98
; 
; 	if (!SendDebugRStr(icmp_str3)) return;
	ldi R16,<_icmp_str3
	ldi R17,>_icmp_str3
	xcall _SendDebugRStr
	tst R16
	brne L11
	.dbline 98
	xjmp L1
L11:
	.dbline 99
; 	sprintf((char*)ScratchPad, "%04X", ntohs(ICMP_Header->Checksum));
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R16,z+2
	ldd R17,z+3
	xcall _ntohs
	std y+1,R17
	std y+0,R16
	ldi R18,<L13
	ldi R19,>L13
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 100
; 	if (!SendDebugStr((char*)ScratchPad)) return;
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L14
	.dbline 100
	xjmp L1
L14:
	.dbline 102
; 
; 	if (!SendDebugRStr(icmp_str4)) return;
	ldi R16,<_icmp_str4
	ldi R17,>_icmp_str4
	xcall _SendDebugRStr
	tst R16
	brne L16
	.dbline 102
	xjmp L1
L16:
	.dbline 103
; 	sprintf((char*)ScratchPad, "%u", ntohs(ICMP_Header->ID));
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R16,z+4
	ldd R17,z+5
	xcall _ntohs
	std y+1,R17
	std y+0,R16
	ldi R18,<L4
	ldi R19,>L4
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 104
; 	if (!SendDebugStr((char*)ScratchPad)) return;
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L18
	.dbline 104
	xjmp L1
L18:
	.dbline 106
; 
; 	if (!SendDebugRStr(icmp_str5)) return;
	ldi R16,<_icmp_str5
	ldi R17,>_icmp_str5
	xcall _SendDebugRStr
	tst R16
	brne L20
	.dbline 106
	xjmp L1
L20:
	.dbline 107
; 	sprintf((char*)ScratchPad, "%u", ntohs(ICMP_Header->SeqNum));
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R16,z+6
	ldd R17,z+7
	xcall _ntohs
	std y+1,R17
	std y+0,R16
	ldi R18,<L4
	ldi R19,>L4
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 108
; 	if (!SendDebugStr((char*)ScratchPad)) return;
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L22
	.dbline 108
	xjmp L1
L22:
	.dbline 112
; 
; 	// done
; 
; 	if (!SendDebugRStr(icmp_str6)) return;
	ldi R16,<_icmp_str6
	ldi R17,>_icmp_str6
	xcall _SendDebugRStr
	tst R16
	brne L24
	.dbline 112
	xjmp L1
L24:
	.dbline 113
	movw R24,R22
	adiw R24,8
	movw R2,R20
	sub R2,R24
	sbc R3,R25
	std y+1,R3
	std y+0,R2
	ldi R18,<L26
	ldi R19,>L26
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 114
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	tst R16
	brne L27
	.dbline 114
L27:
	.dbline -2
L1:
	adiw R28,2
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r TotalBytes 20 I
	.dbsym r HeaderIdx 22 I
	.dbend
	.dbfunc e ICMP_In _ICMP_In fV
;             dw -> y+2
;            len -> R10,R11
	.even
_ICMP_In::
	xcall push_gset3
	sbiw R28,6
	.dbline -1
	.dbline 122
; 	sprintf((char*)ScratchPad, "%u\n", TotalBytes - (HeaderIdx + sizeof(T_ICMP_Header)));
; 	if (!SendDebugStr((char*)ScratchPad)) return;
; }
; 
; #endif
; 
; // **************************************************************************
; 
; void ICMP_In(void)
; {
	.dbline 128
; 	u16 len;
; 	u32	dw;
; 
; 	// **********************
; 
; 	len = MainBufferWr_Rx - MainBufferRd_Rx;							// length of data left
	lds R2,_MainBufferRd_Rx
	lds R3,_MainBufferRd_Rx+1
	lds R10,_MainBufferWr_Rx
	lds R11,_MainBufferWr_Rx+1
	sub R10,R2
	sbc R11,R3
	.dbline 130
; 																		//
; 	ICMP_Header = (T_ICMP_Header*)(MainBuffer + MainBufferRd_Rx);		// point to the ICMP header
	ldi R24,<_MainBuffer
	ldi R25,>_MainBuffer
	add R2,R24
	adc R3,R25
	sts _ICMP_Header+1,R3
	sts _ICMP_Header,R2
	.dbline 132
; 																		//
; 	if (!ICMP_Header->Checksum) return;									// invalid checksum
	movw R30,R2
	ldd R2,z+2
	ldd R3,z+3
	tst R2
	brne L30
	tst R3
	brne L30
X0:
	.dbline 132
	xjmp L29
L30:
	.dbline 133
; 	if (IP_Checksum1((char*)ICMP_Header, len))							//
	movw R18,R10
	lds R16,_ICMP_Header
	lds R17,_ICMP_Header+1
	xcall _IP_Checksum1
	cpi R16,0
	cpc R16,R17
	breq L32
X1:
	.dbline 134
; 	{																	// invalid checksum
	.dbline 136
; 		#ifdef Debug
; 			SendDebugRStr(icmp_str8);									//
	ldi R16,<_icmp_str8
	ldi R17,>_icmp_str8
	xcall _SendDebugRStr
	.dbline 138
; 		#endif
; 		return;															//
	xjmp L29
L32:
	.dbline 142
; 	}
; 
; 	#ifdef Debug
; 		ICMP_DisplayHeader(MainBufferRd_Rx, MainBufferWr_Rx);			//
	lds R18,_MainBufferWr_Rx
	lds R19,_MainBufferWr_Rx+1
	lds R16,_MainBufferRd_Rx
	lds R17,_MainBufferRd_Rx+1
	xcall _ICMP_DisplayHeader
	.dbline 145
; 	#endif
; 
; 	MainBufferRd_Rx += sizeof(T_ICMP_Header);							//
	lds R24,_MainBufferRd_Rx
	lds R25,_MainBufferRd_Rx+1
	adiw R24,8
	sts _MainBufferRd_Rx+1,R25
	sts _MainBufferRd_Rx,R24
	.dbline 149
; 
; 	// **********************
; 
; 	if (ICMP_Header->Type == ICMP_ECHO_REQUEST)
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	ldd R24,z+0
	cpi R24,8
	breq X3
	xjmp L34
X3:
	.dbline 150
; 	{	// PING
	.dbline 151
; 		if (IP_FireWalled())
	xcall _IP_FireWalled
	tst R16
	breq L36
	.dbline 152
; 		{
	.dbline 154
; 			#ifdef Debug
; 				SendDebugRStr(icmp_str7);
	ldi R16,<_icmp_str7
	ldi R17,>_icmp_str7
	xcall _SendDebugRStr
	.dbline 156
; 			#endif
; 			return;									// firewalled
	xjmp L29
L36:
	.dbline 161
; 		}
; 
; 		// send a PONG - eeeewwwww
; 
; 		IP_Header->ID = htons(IP_ID++);
	lds R2,_IP_ID
	lds R3,_IP_ID+1
	movw R24,R2
	adiw R24,1
	sts _IP_ID+1,R25
	sts _IP_ID,R24
	movw R16,R2
	xcall _htons
	lds R30,_IP_Header
	lds R31,_IP_Header+1
	std z+5,R17
	std z+4,R16
	.dbline 164
; 
; 		// first swap the ip addresses
; 		dw = IP_Header->SourceIP.ip32;
	ldd R2,z+12
	ldd R3,z+13
	ldd R4,z+14
	ldd R5,z+15
	movw R30,R28
	std z+2,R2
	std z+3,R3
	std z+4,R4
	std z+5,R5
	.dbline 165
; 		IP_Header->SourceIP.ip32 = IP_Header->DestIP.ip32;
	lds R30,_IP_Header
	lds R31,_IP_Header+1
	ldd R2,z+16
	ldd R3,z+17
	ldd R4,z+18
	ldd R5,z+19
	std z+12,R2
	std z+13,R3
	std z+14,R4
	std z+15,R5
	.dbline 166
; 		IP_Header->DestIP.ip32 = dw;
	movw R30,R28
	ldd R2,z+2
	ldd R3,z+3
	ldd R4,z+4
	ldd R5,z+5
	lds R30,_IP_Header
	lds R31,_IP_Header+1
	std z+16,R2
	std z+17,R3
	std z+18,R4
	std z+19,R5
	.dbline 169
; 
; 		// now the packet type
; 		ICMP_Header->Type = ICMP_ECHO_REPLY;
	clr R2
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	std z+0,R2
	.dbline 172
; 
; 		// update checksums
; 		ICMP_Header->Checksum = 0;
	clr R3
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	std z+3,R3
	std z+2,R2
	.dbline 173
; 		ICMP_Header->Checksum = htons(IP_Checksum1((char*)ICMP_Header, len));
	movw R18,R10
	movw R16,R30
	xcall _IP_Checksum1
	xcall _htons
	lds R30,_ICMP_Header
	lds R31,_ICMP_Header+1
	std z+3,R17
	std z+2,R16
	.dbline 174
; 		IP_Header->Checksum = 0;
	clr R2
	clr R3
	lds R30,_IP_Header
	lds R31,_IP_Header+1
	std z+11,R3
	std z+10,R2
	.dbline 175
; 		IP_Header->Checksum = htons(IP_Checksum1((char*)IP_Header, (IP_Header->VerLen & 0x0f) << 2));
	ldd R18,z+0
	clr R19
	andi R18,15
	andi R19,0
	lsl R18
	rol R19
	lsl R18

⌨️ 快捷键说明

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