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

📄 ntp_client.lst

📁 ENC28J60 System HTTP
💻 LST
📖 第 1 页 / 共 3 页
字号:
   1               		.file	"ntp_client.c"
   2               	__SREG__ = 0x3f
   3               	__SP_H__ = 0x3e
   4               	__SP_L__ = 0x3d
   5               	__CCP__  = 0x34
   6               	__tmp_reg__ = 0
   7               	__zero_reg__ = 1
   8               		.global __do_copy_data
   9               		.global __do_clear_bss
  11               		.text
  12               	.Ltext0:
 120               	.global	ntp_client_init
 122               	ntp_client_init:
   1:net/ntp_client.c **** /*
   2:net/ntp_client.c **** ,-----------------------------------------------------------------------------------------.
   3:net/ntp_client.c **** | net/ntp_client
   4:net/ntp_client.c **** |-----------------------------------------------------------------------------------------
   5:net/ntp_client.c **** | this file describes a very simple ntp client.
   6:net/ntp_client.c **** | - it does not check the UDP checksum
   7:net/ntp_client.c **** | - it does not really do the ntp sequence, it justs sends ONE request
   8:net/ntp_client.c **** |   and uses the reply timestamp as valid clock (-> network delay -> wrong clock)
   9:net/ntp_client.c **** |   (i do not really need a very accurate clock...)
  10:net/ntp_client.c **** |
  11:net/ntp_client.c **** | Author   : {{removed according to contest rules}}
  12:net/ntp_client.c **** |            -> circuitcellar.com avr design contest 2006
  13:net/ntp_client.c **** |            -> Entry #AT2616
  14:net/ntp_client.c **** |
  15:net/ntp_client.c **** |-----------------------------------------------------------------------------------------
  16:net/ntp_client.c **** | License:
  17:net/ntp_client.c **** | This program is free software; you can redistribute it and/or modify it under
  18:net/ntp_client.c **** | the terms of the GNU General Public License as published by the Free Software
  19:net/ntp_client.c **** | Foundation; either version 2 of the License, or (at your option) any later
  20:net/ntp_client.c **** | version.
  21:net/ntp_client.c **** | This program is distributed in the hope that it will be useful, but
  22:net/ntp_client.c **** |
  23:net/ntp_client.c **** | WITHOUT ANY WARRANTY;
  24:net/ntp_client.c **** |
  25:net/ntp_client.c **** | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  26:net/ntp_client.c **** | PURPOSE. See the GNU General Public License for more details.
  27:net/ntp_client.c **** |
  28:net/ntp_client.c **** | You should have received a copy of the GNU General Public License along with
  29:net/ntp_client.c **** | this program; if not, write to the Free Software Foundation, Inc., 51
  30:net/ntp_client.c **** | Franklin St, Fifth Floor, Boston, MA 02110, USA
  31:net/ntp_client.c **** |
  32:net/ntp_client.c **** | http://www.gnu.de/gpl-ger.html
  33:net/ntp_client.c **** `-----------------------------------------------------------------------------------------*/
  34:net/ntp_client.c **** #include "ntp_client.h"
  35:net/ntp_client.c **** 
  36:net/ntp_client.c **** //ACTIVATE DEBUG by editing this file:
  37:net/ntp_client.c **** #include "debug.h"
  38:net/ntp_client.c **** 
  39:net/ntp_client.c **** uint32_t ntp_client_server_ip;
  40:net/ntp_client.c **** char ntp_client_call_again;
  41:net/ntp_client.c **** 
  42:net/ntp_client.c **** void ntp_client_init(){
 123               	.LM0:
 124               	.LFBB1:
 125               	/* prologue: function */
 126               	/* frame size = 0 */
  43:net/ntp_client.c **** ESS;
 128               	
 129               		ldi r24,lo8(1083055169)
 130 0000 81E4      		ldi r25,hi8(1083055169)
 131 0002 9CE1      		ldi r26,hlo8(1083055169)
 132 0004 AEE8      		ldi r27,hhi8(1083055169)
 133 0006 B0E4      		sts ntp_client_server_ip,r24
 134 0008 8093 0000 		sts (ntp_client_server_ip)+1,r25
 135 000c 9093 0000 		sts (ntp_client_server_ip)+2,r26
 136 0010 A093 0000 		sts (ntp_client_server_ip)+3,r27
  44:net/ntp_client.c **** ntp_client_call_again = 0;
 138               	s ntp_client_call_again,__zero_reg__
 139               	/* epilogue start */
 141               	.LM3:
  45:net/ntp_client.c **** 
 142               	
 144 001c 0895      	.Lscope1:
 148               	.global	ntp_client_timestamp_to_time
 150               	ntp_client_timestamp_to_time:
 152               	.LM4:
  46:net/ntp_client.c **** 
  47:net/ntp_client.c **** void ntp_client_send_request(unsigned char *buffer){
  48:net/ntp_client.c **** 	unsigned int i;
  49:net/ntp_client.c **** 
  50:net/ntp_client.c **** 	//set NTP flags (client)
  51:net/ntp_client.c **** 	buffer[NTP_POS_FLAGS] = 0xE3;
  52:net/ntp_client.c **** 
  53:net/ntp_client.c **** 	//set peer clock stratum to unspecified
  54:net/ntp_client.c **** 	buffer[NTP_POS_STRATUM] = 0x00;
  55:net/ntp_client.c **** 
  56:net/ntp_client.c **** 	//set poll intervall to 4 (=16sec)
  57:net/ntp_client.c **** 	buffer[NTP_POS_POLL_INTERVAL] = 0x04;
  58:net/ntp_client.c **** 
  59:net/ntp_client.c **** 	//set precision (??)
  60:net/ntp_client.c **** 	buffer[NTP_POS_PRECISION] = 0xFA;
  61:net/ntp_client.c **** 
  62:net/ntp_client.c **** 	//clear data buffer:
  63:net/ntp_client.c **** 	//-> we only need to set some values because many data is 0x00
  64:net/ntp_client.c **** 	for(i=NTP_POS_ROOT_DELAY; i<NTP_POS_LAST_DATA; i++)
  65:net/ntp_client.c **** 		buffer[i] = 0x00;
  66:net/ntp_client.c **** 
  67:net/ntp_client.c **** 	//set root delay to 1s -> 0x00 0x01 0x00 0x00
  68:net/ntp_client.c **** 	buffer[NTP_POS_ROOT_DELAY+1] = 0x01; //rest is zero, see above
  69:net/ntp_client.c **** 
  70:net/ntp_client.c **** 	//set clock dispersion to 1s -> 0x00 0x01 0x00 0x00
  71:net/ntp_client.c **** 	buffer[NTP_POS_CLOCK_DISP+1] = 0x01; //rest is zero, see above
  72:net/ntp_client.c **** 
  73:net/ntp_client.c **** 	//reference clock id = 0x00 0x00 0x00 0x00
  74:net/ntp_client.c **** 	//reference clock update time NULL = 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  75:net/ntp_client.c **** 	//originate timestamp         NULL= 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  76:net/ntp_client.c **** 	//receive timestamp           NULL= 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  77:net/ntp_client.c **** 	
  78:net/ntp_client.c **** 	//tx timestamp:
  79:net/ntp_client.c **** 	//send NULL ... MAYBE add timestamp calc from localclock here
  80:net/ntp_client.c **** 	//tx timestamp           NULL= 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  81:net/ntp_client.c **** 
  82:net/ntp_client.c **** 	//generate an udp packet:
  83:net/ntp_client.c **** 	unsigned char *dest_mac;
  84:net/ntp_client.c **** 	unsigned int arp_idx;
  85:net/ntp_client.c **** 	unsigned long *server_ip;
  86:net/ntp_client.c **** 
  87:net/ntp_client.c **** 	//target ip in the same network ?!
  88:net/ntp_client.c **** 	if ((ntp_client_server_ip & NIC_IP_NETMASK) != (nic_ip & NIC_IP_NETMASK)){
  89:net/ntp_client.c **** 		#if NTP_CLIENT_DEBUG
  90:net/ntp_client.c **** 		softuart_puts_progmem("NTP : ntp server ");
  91:net/ntp_client.c **** 		softuart_put_ip(ntp_client_server_ip);
  92:net/ntp_client.c **** 		softuart_puts_progmem(" outside home net -> use gateway\r\n");
  93:net/ntp_client.c **** 		#endif
  94:net/ntp_client.c **** 		server_ip = &nic_gateway_ip;
  95:net/ntp_client.c **** 	}else{
  96:net/ntp_client.c **** 		server_ip = &ntp_client_server_ip;
  97:net/ntp_client.c **** 	}
  98:net/ntp_client.c **** 	
  99:net/ntp_client.c **** 	arp_idx = arp_search_by_ip(*server_ip);
 100:net/ntp_client.c **** 
 101:net/ntp_client.c **** 	if (arp_idx == -1){
 102:net/ntp_client.c **** 		//target ip (gateway) unknown
 103:net/ntp_client.c **** 		//->send arp request and exit,
 104:net/ntp_client.c **** 		//this is called later again and we hopefully know the mac then
 105:net/ntp_client.c **** 		arp_send_request(buffer, server_ip);
 106:net/ntp_client.c **** 		
 107:net/ntp_client.c **** 		//ntp client must be called again after we finished the arp comm!
 108:net/ntp_client.c **** 		ntp_client_call_again = 1;
 109:net/ntp_client.c **** 		return;
 110:net/ntp_client.c **** 	}
 111:net/ntp_client.c **** 	//no need to be called again, we know the mac address of gateway
 112:net/ntp_client.c **** 	ntp_client_call_again = 0;
 113:net/ntp_client.c **** 
 114:net/ntp_client.c **** 	dest_mac = (arp_table[arp_idx].mac);
 115:net/ntp_client.c **** 
 116:net/ntp_client.c **** 	/*dest_mac[0] = 0x00;
 117:net/ntp_client.c **** 	dest_mac[1] = 0xE0;
 118:net/ntp_client.c **** 	dest_mac[2] = 0x4C;
 119:net/ntp_client.c **** 	dest_mac[3] = 0x6A;
 120:net/ntp_client.c **** 	dest_mac[4] = 0xBF;
 121:net/ntp_client.c **** 	dest_mac[5] = 0xDE;*/
 122:net/ntp_client.c **** 
 123:net/ntp_client.c **** 	udp_generate_packet(buffer, &ntp_client_server_ip, dest_mac , IP_PORT_NTP_CLIENT, IP_PORT_NTP, NTP
 124:net/ntp_client.c **** 
 125:net/ntp_client.c **** 	//send packet:
 126:net/ntp_client.c **** 	nic_send_packet(buffer, 14+(IP_HEADER_LENGTH<<2)+8+NTP_DATALENGTH);
 127:net/ntp_client.c **** }
 128:net/ntp_client.c **** 
 129:net/ntp_client.c **** //decode an ntp packet & extract time data
 130:net/ntp_client.c **** //this is called after ip+udp decidet this packet is valid & is for us.
 131:net/ntp_client.c **** //-> ip+udp checks are done before (!)
 132:net/ntp_client.c **** void ntp_client_decode_packet(unsigned char *buffer, unsigned int len){
 133:net/ntp_client.c **** 	unsigned long timestamp;
 134:net/ntp_client.c **** 	//check for valid flags, should be:
 135:net/ntp_client.c **** 	//- NTP Version 4
 136:net/ntp_client.c **** 	//- Server Mode
 137:net/ntp_client.c **** 	//-> ignore Leap indicator
 138:net/ntp_client.c **** 	if ((buffer[NTP_POS_FLAGS]&0x3F) != NTP_FLAG_NTP4_SERVER)
 139:net/ntp_client.c **** 		return;
 140:net/ntp_client.c **** 
 141:net/ntp_client.c **** 	//extract timestamp
 142:net/ntp_client.c **** 	//NOTE: because we do not really need the exact clock, we
 143:net/ntp_client.c **** 	//      only use the TX timestamp and hope that the transfer
 144:net/ntp_client.c **** 	//      did not take too long -> maybe fix this later
 145:net/ntp_client.c **** 	//      (and we dont use the microseconds...)
 146:net/ntp_client.c **** 	timestamp = buffer[NTP_POS_TX_TIMESTAMP+0]<<8;
 147:net/ntp_client.c **** 	timestamp = (timestamp + buffer[NTP_POS_TX_TIMESTAMP+1])<<8;
 148:net/ntp_client.c **** 	timestamp = (timestamp + buffer[NTP_POS_TX_TIMESTAMP+2])<<8;
 149:net/ntp_client.c **** 	timestamp = (timestamp + buffer[NTP_POS_TX_TIMESTAMP+3]);
 150:net/ntp_client.c **** 
 151:net/ntp_client.c **** 	#if NTP_CLIENT_DEBUG
 152:net/ntp_client.c **** 	softuart_puts_progmem("NTP : timestamp is ");
 153:net/ntp_client.c **** 	for(int i=0; i<4; i++){
 154:net/ntp_client.c **** 		softuart_put_uint8(buffer[0x52+i]);
 155:net/ntp_client.c **** 		softuart_putc(' ');
 156:net/ntp_client.c **** 	}
 157:net/ntp_client.c **** 	softuart_putnewline();
 158:net/ntp_client.c **** 	#endif
 159:net/ntp_client.c **** 	unsigned char date[6];
 160:net/ntp_client.c **** 	ntp_client_timestamp_to_time(timestamp, date);
 161:net/ntp_client.c **** 
 162:net/ntp_client.c **** 	#if NTP_CLIENT_DEBUG
 163:net/ntp_client.c **** 	//german format !
 164:net/ntp_client.c **** 	softuart_puts_progmem("DATE: ");
 165:net/ntp_client.c **** 	softuart_put_uint8(date[NTP_TIME_DAY]);
 166:net/ntp_client.c **** 	softuart_putc('.');
 167:net/ntp_client.c **** 	softuart_put_uint8(date[NTP_TIME_MONTH]);
 168:net/ntp_client.c **** 	softuart_putc('.');
 169:net/ntp_client.c **** 	softuart_put_uint16(1900 + date[NTP_TIME_YEAR]);
 170:net/ntp_client.c **** 	
 171:net/ntp_client.c **** 	softuart_puts_progmem(" TIME: ");
 172:net/ntp_client.c **** 	softuart_put_uint8(date[NTP_TIME_HOUR]);
 173:net/ntp_client.c **** 	softuart_putc(':');
 174:net/ntp_client.c **** 	softuart_put_uint8(date[NTP_TIME_MIN]);
 175:net/ntp_client.c **** 	softuart_putc(':');
 176:net/ntp_client.c **** 	softuart_put_uint8(date[NTP_TIME_SEC]);
 177:net/ntp_client.c **** 	softuart_puts_progmem(" (UTC!)");
 178:net/ntp_client.c **** 	softuart_putnewline();
 179:net/ntp_client.c **** 	#endif
 180:net/ntp_client.c **** 
 181:net/ntp_client.c **** 	clock_do();
 182:net/ntp_client.c **** 	clock[CLOCK_HOUR] = date[NTP_TIME_HOUR]+2;
 183:net/ntp_client.c **** 	clock[CLOCK_MIN] = date[NTP_TIME_MIN];
 184:net/ntp_client.c **** 	clock[CLOCK_SEC] = date[NTP_TIME_SEC];	
 185:net/ntp_client.c **** }
 186:net/ntp_client.c **** 
 187:net/ntp_client.c **** //lookup table
 188:net/ntp_client.c **** PROGMEM char NTP_CLIENT_MONTH_TO_DAYS[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 189:net/ntp_client.c **** 
 190:net/ntp_client.c **** //convert unix timestamp to date/time.
 191:net/ntp_client.c **** //returns: year,month,day,hour,min,sec (year ist realyear-1970)
 192:net/ntp_client.c **** //
 193:net/ntp_client.c **** //inspired by code posted on mikrocontroller.net by peter dannegger:
 194:net/ntp_client.c **** // see http://www.mikrocontroller.net/forum/read-4-140631.html#new
 195:net/ntp_client.c **** void ntp_client_timestamp_to_time(unsigned long timestamp, unsigned char *time){
 153               	ush r14
 154               		push r15
 155               		push r16
 156 001e EF92      		push r17
 157 0020 FF92      		push r28
 158 0022 0F93      		push r29
 159 0024 1F93      	/* prologue: function */
 160 0026 CF93      	/* frame size = 0 */
 161 0028 DF93      		movw r14,r22
 162               		movw r16,r24
 163               		movw r28,r20
 165 002c 8C01      	.LM5:
 166 002e EA01      		ldi r18,lo8(60)
 196:net/ntp_client.c **** lient_timestamp_to_time(unsigned long timestamp, unsigned char *time){
 197:net/ntp_client.c **** 	unsigned char tmp;
 198:net/ntp_client.c **** 	unsigned char tmp_year;
 199:net/ntp_client.c **** 	unsigned int tmp_day;
 200:net/ntp_client.c **** 	unsigned char leap400;
 201:net/ntp_client.c **** 
 202:net/ntp_client.c **** 	//calculate seconds:
 167               	0)
 168               		ldi r20,hlo8(60)
 169 0030 2CE3      		ldi r21,hhi8(60)
 170 0032 30E0      		call __udivmodsi4
 171 0034 40E0      		std Y+5,r22
 173 0038 0E94 0000 	.LM6:
 174 003c 6D83      		movw r24,r16
 203:net/ntp_client.c **** gned int tmp_day;
 175               	2,r14
 176               		ldi r18,lo8(60)
 177 003e C801      		ldi r19,hi8(60)
 178 0040 B701      		ldi r20,hlo8(60)
 179 0042 2CE3      		ldi r21,hhi8(60)
 180 0044 30E0      		call __udivmodsi4
 181 0046 40E0      		mov r17,r18
 182 0048 50E0      		mov r16,r19
 183 004a 0E94 0000 		mov r15,r20
 184 004e 122F      		mov r14,r21
 186 0052 F42E      	.LM7:
 187 0054 E52E      		mov r22,r18
 204:net/ntp_client.c **** 	unsigned char tmp;
 205:net/ntp_client.c **** 	unsigned char tmp_year;
 206:net/ntp_client.c **** 	unsigned int tmp_day;
 188               		mov r24,r20
 189               		mov r25,r21
 190 0056 622F      		ldi r18,lo8(60)
 191 0058 732F      		ldi r19,hi8(60)
 192 005a 842F      		ldi r20,hlo8(60)
 193 005c 952F      		ldi r21,hhi8(60)
 194 005e 2CE3      		call __udivmodsi4
 195 0060 30E0      		std Y+4,r22
 197 0064 50E0      	.LM8:
 198 0066 0E94 0000 		mov r22,r17
 199 006a 6C83      		mov r23,r16
 207:net/ntp_client.c **** 	unsigned char tmp;
 200               	
 201               		mov r25,r14
 202 006c 612F      		ldi r18,lo8(60)
 203 006e 702F      		ldi r19,hi8(60)
 204 0070 8F2D      		ldi r20,hlo8(60)
 205 0072 9E2D      		ldi r21,hhi8(60)
 206 0074 2CE3      		call __udivmodsi4
 207 0076 30E0      		mov r17,r18
 208 0078 40E0      		mov r16,r19
 209 007a 50E0      		mov r15,r20
 210 007c 0E94 0000 		mov r14,r21
 212 0082 032F      	.LM9:
 213 0084 F42E      		mov r22,r18
 214 0086 E52E      		mov r23,r19
 208:net/ntp_client.c **** me(unsigned long timestamp, unsigned char *time){
 209:net/ntp_client.c **** 	unsigned char tmp;
 210:net/ntp_client.c **** 	unsigned char tmp_year;
 215               	mov r25,r21
 216               		ldi r18,lo8(24)

⌨️ 快捷键说明

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