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

📄 ntp.s

📁 AVR单片机系统开发经典实例部分源程序
💻 S
字号:
	.module ntp.c
	.area text(rom, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c
	.dbfunc e NTP_RequestSNTP _NTP_RequestSNTP fc
	.dbstruct 0 48 TNTP
	.dbfield 0 mode c
	.dbfield 1 stratum c
	.dbfield 2 poll c
	.dbfield 3 Precision c
	.dbfield 4 RootDelay l
	.dbfield 8 RootDisperson l
	.dbfield 12 RefID l
	.dbfield 16 Ref1 l
	.dbfield 20 Ref2 l
	.dbfield 24 Org1 l
	.dbfield 28 Org2 l
	.dbfield 32 Rcv1 l
	.dbfield 36 Rcv2 l
	.dbfield 40 Xmit1 l
	.dbfield 44 Xmit2 l
	.dbend
;            NTP -> R20,R21
;             IP -> y+6
	.even
_NTP_RequestSNTP::
	xcall push_arg4
	xcall push_gset1
	sbiw R28,4
	.dbline -1
	.dbline 66
; 
; /*
;  * 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 <stdio.h>
; //#include <math.h>
; 
; #include "common.h"
; #include "ppp.h"
; #include "ip.h"
; #include "udp.h"
; #include "ntp.h"
; 
; #define HoursPerDay		24
; #define MinsPerDay		(HoursPerDay * 60)
; #define SecsPerDay		(MinsPerDay * 60)
; #define MSecsPerDay		(SecsPerDay * 1000)
; 
; #ifdef CPU_eZ8
; volatile near u16			NTP_Timer;
; volatile near T_IP_Addr		NTP_IP;
; #endif
; #ifdef CPU_ATmega128
; volatile u16				NTP_Timer;
; volatile T_IP_Addr			NTP_IP;
; #endif
; 
; //*********************************************************************************************************************
; // request the date & time using SNTP
; 
; bool NTP_RequestSNTP(u32 IP)
; {
	.dbline 69
; 	T_NTP	*NTP;
; 
; 	if (!UDP_StartPacket(IP, 123, 123)) return false;									//
	ldi R24,123
	ldi R25,0
	std y+3,R25
	std y+2,R24
	std y+1,R25
	std y+0,R24
	movw R30,R28
	ldd R16,z+6
	ldd R17,z+7
	ldd R18,z+8
	ldd R19,z+9
	xcall _UDP_StartPacket
	tst R16
	brne L2
	.dbline 69
	clr R16
	xjmp L1
L2:
	.dbline 71
; 																						//
; 	NTP = (T_NTP*)(MainBuffer + MainBufferWr_Tx);										//
	ldi R24,<_MainBuffer
	ldi R25,>_MainBuffer
	lds R20,_MainBufferWr_Tx
	lds R21,_MainBufferWr_Tx+1
	add R20,R24
	adc R21,R25
	.dbline 72
; 	memset(NTP, 0, sizeof(T_NTP));														//
	ldi R24,48
	ldi R25,0
	std y+1,R25
	std y+0,R24
	clr R18
	clr R19
	movw R16,R20
	xcall _memset
	.dbline 73
; 	NTP->mode = 0x1b;																	//
	ldi R24,27
	movw R30,R20
	std z+0,R24
	.dbline 74
; 	MainBufferWr_Tx += sizeof(T_NTP);													// update index
	lds R24,_MainBufferWr_Tx
	lds R25,_MainBufferWr_Tx+1
	adiw R24,48
	sts _MainBufferWr_Tx+1,R25
	sts _MainBufferWr_Tx,R24
	.dbline 76
; 																						//
; 	u16_Put(&NTP_Timer, 0);																// reset the timer
	clr R18
	clr R19
	ldi R16,<_NTP_Timer
	ldi R17,>_NTP_Timer
	xcall _u16_Put
	.dbline 77
; 	u32_Put(&NTP_IP.ip32, IP);															// remember the ip we are getting the ntp from
	movw R30,R28
	ldd R2,z+6
	ldd R3,z+7
	ldd R4,z+8
	ldd R5,z+9
	std y+0,R4
	std y+1,R5
	movw R18,R2
	ldi R16,<_NTP_IP
	ldi R17,>_NTP_IP
	xcall _u32_Put
	.dbline 79
; 																						//
; 	return UDP_EndPacket();																//
	xcall _UDP_EndPacket
	.dbline -2
L1:
	adiw R28,4
	xcall pop_gset1
	adiw R28,4
	.dbline 0 ; func end
	ret
	.dbsym r NTP 20 pS[TNTP]
	.dbsym l IP 6 l
	.dbend
	.dbfunc e NTP_DecodeSNTP _NTP_DecodeSNTP fV
;            sec -> R14,R15
;            min -> R12,R13
;           hour -> R10,R11
;            lag -> y+12
;             dw -> y+8
;            NTP -> R10,R11
;             IP -> y+24
	.even
_NTP_DecodeSNTP::
	xcall push_arg4
	xcall push_gset5
	sbiw R28,14
	ldd R10,y+28
	ldd R11,y+29
	.dbline -1
	.dbline 86
; }
; 
; //*********************************************************************************************************************
; // decode an SNTP reply (UDP packet on port 123 normally)
; 
; void NTP_DecodeSNTP(u32 IP, T_NTP *NTP)
; {
	.dbline 92
; //	TDateTime		FNTPTime;
; //	double			Nsec, Nfrac;
; 	u32				dw;
; 	u16				hour, min, sec, lag;
; 
; 	if (!NTP) return;								//
	tst R10
	brne L5
	tst R11
	brne L5
X0:
	.dbline 92
	xjmp L4
L5:
	.dbline 94
; 													//
; 	NTP->Xmit1 = ntohl(NTP->Xmit1);					//
	movw R24,R10
	adiw R24,40
	movw R30,R24
	ldd R16,z+0
	ldd R17,z+1
	ldd R18,z+2
	ldd R19,z+3
	push R24
	push R25
	xcall _ntohl
	pop R31
	pop R30
	std z+0,R16
	std z+1,R17
	std z+2,R18
	std z+3,R19
	.dbline 95
; 	NTP->Xmit2 = ntohl(NTP->Xmit2);					//
	movw R24,R10
	adiw R24,44
	movw R30,R24
	ldd R16,z+0
	ldd R17,z+1
	ldd R18,z+2
	ldd R19,z+3
	push R24
	push R25
	xcall _ntohl
	pop R31
	pop R30
	std z+0,R16
	std z+1,R17
	std z+2,R18
	std z+3,R19
	.dbline 98
; 													//
; 	#ifdef Debug
; 		sprintf((char*)ScratchPad, "\nXmit1:%lu\nXmit2:%lu\n", NTP->Xmit1, NTP->Xmit2);
	movw R30,R10
	ldd R2,z+44
	ldd R3,z+45
	ldd R4,z+46
	ldd R5,z+47
	std y+4,R2
	std y+5,R3
	std y+6,R4
	std y+7,R5
	movw R30,R10
	ldd R2,z+40
	ldd R3,z+41
	ldd R4,z+42
	ldd R5,z+43
	std y+0,R2
	std y+1,R3
	std y+2,R4
	std y+3,R5
	ldi R18,<L7
	ldi R19,>L7
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 99
; 		SendDebugStr((char*)ScratchPad);
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	.dbline 110
; 	#endif
; 
; //	Nsec = (double)NTP->Xmit1;						// MS-Byte 1st ... shit ... the zilog s32 to double is inaccurate :( .. unless it's the sprintf function that is bad
; //	Nfrac = (double)NTP->Xmit2;						// as above
; //	Nfrac /= 4294967295.0;							//
; //	FNTPTime = Nsec;								//
; //	FNTPTime += Nfrac;								//
; //	FNTPTime /= SecsPerDay;							//
; //	FNTPTime += 2;									// add 2 days
; 													//
; 	lag = u16_Get(&NTP_Timer);						// how long it took to get the reply back (ms)
	ldi R16,<_NTP_Timer
	ldi R17,>_NTP_Timer
	xcall _u16_Get
	std y+13,R17
	std y+12,R16
	.dbline 111
; 	lag >>= 1;										//
	movw R2,R16
	lsr R3
	ror R2
	std y+13,R3
	std y+12,R2
	.dbline 112
; 	dw = (u32)(lag / 1000);							// now in seconds
	ldi R18,1000
	ldi R19,3
	movw R16,R2
	xcall div16u
	movw R2,R16
	clr R4
	clr R5
	movw R30,R28
	std z+8,R2
	std z+9,R3
	std z+10,R4
	std z+11,R5
	.dbline 113
; 	NTP->Xmit1 += dw;								// take the network lag/response time into account
	movw R24,R10
	adiw R24,40
	movw R30,R28
	ldd R4,z+8
	ldd R5,z+9
	ldd R6,z+10
	ldd R7,z+11
	movw R30,R24
	ldd R20,z+0
	ldd R21,z+1
	ldd R22,z+2
	ldd R23,z+3
	add R20,R4
	adc R21,R5
	adc R22,R6
	adc R23,R7
	std z+0,R20
	std z+1,R21
	std z+2,R22
	std z+3,R23
	.dbline 116
; 													//
; 													// NTP->Xmit1 is the number of seconds since "1st Jan 1900 00:00:00" .. NTP->Xmit2 is the fractional part
; 	dw = NTP->Xmit1 % SecsPerDay;					// dw = seconds thru the current day
	ldi R20,128
	ldi R21,81
	ldi R22,1
	ldi R23,0
	movw R30,R10
	ldd R2,z+40
	ldd R3,z+41
	ldd R4,z+42
	ldd R5,z+43
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R2
	movw R18,R4
	xcall mod32u
	movw R30,R28
	std z+8,R16
	std z+9,R17
	std z+10,R18
	std z+11,R19
	.dbline 117
; 	NTP->Xmit1 /= SecsPerDay;						// now left with the number of days since "1st Jan 1900 00:00:00"
	movw R24,R10
	adiw R24,40
	movw R2,R24
	ldi R20,128
	ldi R21,81
	ldi R22,1
	ldi R23,0
	movw R30,R24
	ldd R4,z+0
	ldd R5,z+1
	ldd R6,z+2
	ldd R7,z+3
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R4
	movw R18,R6
	xcall div32u
	movw R30,R2
	std z+0,R16
	std z+1,R17
	std z+2,R18
	std z+3,R19
	.dbline 119
; 													//
; 	sec = dw % 60;									//
	ldi R20,60
	ldi R21,0
	ldi R22,0
	ldi R23,0
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R2
	movw R18,R4
	xcall mod32u
	movw R14,R16
	.dbline 120
; 	dw /= 60;										//
	ldi R20,60
	ldi R21,0
	ldi R22,0
	ldi R23,0
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R2
	movw R18,R4
	xcall div32u
	movw R30,R28
	std z+8,R16
	std z+9,R17
	std z+10,R18
	std z+11,R19
	.dbline 121
; 	min = dw % 60;									//
	ldi R20,60
	ldi R21,0
	ldi R22,0
	ldi R23,0
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R2
	movw R18,R4
	xcall mod32u
	movw R12,R16
	.dbline 122
; 	dw /= 60;										//
	ldi R20,60
	ldi R21,0
	ldi R22,0
	ldi R23,0
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R23
	st -y,R22
	st -y,R21
	st -y,R20
	movw R16,R2
	movw R18,R4
	xcall div32u
	movw R30,R28
	std z+8,R16
	std z+9,R17
	std z+10,R18
	std z+11,R19
	.dbline 123
; 	hour = dw;										//
	movw R30,R28
	ldd R10,z+8
	ldd R11,z+9
	.dbline 126
; 
; 	#ifdef Debug
; 		sprintf((char*)ScratchPad, "%02u:%02u:%02u ... lag:%ums\n", hour, min, sec, lag);
	ldd R0,y+12
	ldd R1,y+13
	std y+7,R1
	std y+6,R0
	std y+5,R15
	std y+4,R14
	std y+3,R13
	std y+2,R12
	std y+1,R11
	std y+0,R10
	ldi R18,<L8
	ldi R19,>L8
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _sprintf
	.dbline 127
; 		SendDebugStr((char*)ScratchPad);
	ldi R16,<_ScratchPad
	ldi R17,>_ScratchPad
	xcall _SendDebugStr
	.dbline 130
; 	#endif
; 
; 	if (IP != u32_Get(&NTP_IP.ip32)) return;		//
	ldi R16,<_NTP_IP
	ldi R17,>_NTP_IP
	xcall _u32_Get
	movw R30,R28
	ldd R2,z+24
	ldd R3,z+25
	ldd R4,z+26
	ldd R5,z+27
	cp R2,R16
	cpc R3,R17
	cpc R4,R18
	cpc R5,R19
	breq L9
	.dbline 130
	xjmp L4
L9:
	.dbline 133
;    													//
; 	#ifdef Debug
; 		SendDebugStr("Time set\n");					//
	ldi R16,<L11
	ldi R17,>L11
	xcall _SendDebugStr
	.dbline 136
;    #endif
; 													//
; 	u32_Put(&NTP_IP.ip32, 0);						//
	ldi R20,0
	ldi R21,0
	ldi R22,0
	ldi R23,0
	std y+0,R22
	std y+1,R23
	movw R18,R20
	ldi R16,<_NTP_IP
	ldi R17,>_NTP_IP
	xcall _u32_Put
	.dbline -2
L4:
	adiw R28,14
	xcall pop_gset5
	adiw R28,4
	.dbline 0 ; func end
	ret
	.dbsym r sec 14 s
	.dbsym r min 12 s
	.dbsym r hour 10 s
	.dbsym l lag 12 s
	.dbsym l dw 8 l
	.dbsym r NTP 10 pS[TNTP]
	.dbsym l IP 24 l
	.dbend
	.dbfunc e NTP_10ms_Timer _NTP_10ms_Timer fV
	.even
_NTP_10ms_Timer::
	xcall push_gset2
	.dbline -1
	.dbline 143
; }
; 
; //*********************************************************************************************************************
; // this is called every 10ms from the UDP module
; 
; void NTP_10ms_Timer(void)
; {
	.dbline 144
; 	if (NTP_Timer < 5000)										//
	lds R24,_NTP_Timer
	lds R25,_NTP_Timer+1
	cpi R24,136
	ldi R30,19
	cpc R25,R30
	brsh L13
	.dbline 145
; 		NTP_Timer += 10;										// update the timer
	lds R24,_NTP_Timer
	lds R25,_NTP_Timer+1
	adiw R24,10
	sts _NTP_Timer+1,R25
	sts _NTP_Timer,R24
	xjmp L14
L13:
	.dbline 147
	ldi R20,0
	ldi R21,0
	ldi R22,0
	ldi R23,0
	sts _NTP_IP+1,R21
	sts _NTP_IP,R20
	sts _NTP_IP+2+1,R23
	sts _NTP_IP+2,R22
L14:
	.dbline -2
L12:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbend
	.area bss(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c
_NTP_IP::
	.blkb 4
	.dbunion 0 4 TIPAddr
	.dbfield 0 ip8 A[4:4]c
	.dbfield 0 ip32 l
	.dbend
	.dbsym e NTP_IP _NTP_IP S[TIPAddr]
_NTP_Timer::
	.blkb 2
	.dbsym e NTP_Timer _NTP_Timer s
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c
L11:
	.blkb 10
	.area idata
	.byte 'T,'i,'m,'e,32,'s,'e,'t,10,0
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c
L8:
	.blkb 29
	.area idata
	.byte 37,48,50,'u,58,37,48,50,'u,58,37,48,50,'u,32,46
	.byte 46,46,32,'l,'a,'g,58,37,'u,'m,'s,10,0
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c
L7:
	.blkb 22
	.area idata
	.byte 10,'X,'m,'i,'t,49,58,37,'l,'u,10,'X,'m,'i,'t,50
	.byte 58,37,'l,'u,10,0
	.area data(ram, con, rel)
	.dbfile C:\AVR\AT-Modem__PPP__UDP__IC\ntp.c

⌨️ 快捷键说明

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