Rev 2078 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1393 | Kulich | 1 | #include <stdio.h> |
2 | #include <string.h> |
||
3 | #include <oscalls.h> |
||
4 | #include <socket.h> |
||
5 | #include <intrz80.h> |
||
2068 | kulich | 6 | #include <osfs.h> |
7 | #include <stdlib.h> |
||
8 | unsigned int RBR_THR = 0xf8ef; |
||
9 | unsigned int IER = 0xf9ef; |
||
10 | unsigned int IIR_FCR = 0xfaef; |
||
11 | unsigned int LCR = 0xfbef; |
||
12 | unsigned int MCR = 0xfcef; |
||
13 | unsigned int LSR = 0xfdef; |
||
14 | unsigned int MSR = 0xfeef; |
||
15 | unsigned int SR = 0xffef; |
||
16 | unsigned int divider = 1; |
||
17 | unsigned char comType = 0; |
||
18 | unsigned int espType = 32; |
||
1651 | kulich | 19 | |
2068 | kulich | 20 | unsigned char cmd[512]; |
21 | const unsigned char sendOk[] = "SEND OK"; |
||
22 | const unsigned char gotWiFi[] = "WIFI GOT IP"; |
||
2078 | kulich | 23 | const unsigned char timeUpdated[] = "+CIPSNTPTIME:"; |
1668 | kulich | 24 | int GMT = 3; |
2068 | kulich | 25 | unsigned char is_atm; |
26 | unsigned char netbuf[4 * 1024]; |
||
27 | struct sockaddr_in ntp_ia; |
||
1668 | kulich | 28 | union |
29 | { |
||
30 | unsigned long ul; |
||
31 | unsigned char b[4]; |
||
32 | } secsUnix; |
||
1393 | Kulich | 33 | unsigned int hour, minute, second, day, month, year, weekday; |
1668 | kulich | 34 | SOCKET s = 0; |
2068 | kulich | 35 | unsigned char inet = 0, espInet = 0; |
1668 | kulich | 36 | const unsigned char monthDays[12] = |
37 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
||
1393 | Kulich | 38 | const unsigned char ntpnead[48] = |
1668 | kulich | 39 | { |
40 | 0xdb, |
||
41 | 0x00, |
||
42 | 0x11, |
||
43 | 0xfa, |
||
44 | 0x00, |
||
45 | 0x00, |
||
46 | 0x00, |
||
47 | 0x00, |
||
48 | 0x00, |
||
49 | 0x01, |
||
50 | 0x03, |
||
51 | 0xfe, |
||
52 | }; |
||
53 | unsigned char *defntp = "2.ru.pool.ntp.org"; |
||
54 | const unsigned char regaddr_ve[16] = {0x10, 0, 0x50, 0, 0x90, 0, 0, 0x12, 0x52, 0x92, 0, 0, 0, 0, 0, 0}; |
||
1394 | Kulich | 55 | |
1393 | Kulich | 56 | const unsigned char help[] = "\ |
57 | -H help\r\n\ |
||
58 | -T set time(-T17:59:38)\r\n\ |
||
59 | -D set date(-D21-06-2019)\r\n\ |
||
60 | -N ntp-server default: -N2.ru.pool.ntp.org\r\n\ |
||
61 | -Z time-zone default: -Z3\r\n\ |
||
2068 | kulich | 62 | -i get datetime from internet\r\n\ |
63 | -e get datetime from ESP-COM"; |
||
1393 | Kulich | 64 | |
2068 | kulich | 65 | extern void |
66 | dns_resolve(void); |
||
1393 | Kulich | 67 | |
1668 | kulich | 68 | void exit(int e) |
69 | { |
||
70 | if (s) |
||
71 | closesocket(s, 0); |
||
72 | if (e != 0) |
||
73 | { |
||
74 | puts((char *)e); |
||
1393 | Kulich | 75 | } |
1668 | kulich | 76 | ((void (*)(int))0x0000)(e); |
1393 | Kulich | 77 | } |
78 | |||
79 | extern void dns_resolve(void); |
||
1394 | Kulich | 80 | |
1668 | kulich | 81 | unsigned char readcmos(unsigned char r) |
82 | { |
||
83 | disable_interrupt(); |
||
84 | if (is_atm == 2 || is_atm == 3) |
||
85 | { |
||
86 | r = regaddr_ve[r]; |
||
87 | if (r != 0) |
||
88 | { |
||
89 | input(0x55FE); |
||
90 | r = input((r << 8) | 0x00fe); |
||
91 | } |
||
92 | } |
||
93 | else |
||
94 | { |
||
95 | output(0xdef7, r); |
||
96 | r = input(0xbef7); |
||
97 | } |
||
98 | enable_interrupt(); |
||
99 | return r; |
||
1394 | Kulich | 100 | } |
101 | |||
1668 | kulich | 102 | void writecmos(unsigned char r, unsigned char v) |
103 | { |
||
104 | disable_interrupt(); |
||
105 | if (is_atm == 2 || is_atm == 3) |
||
106 | { |
||
107 | r = regaddr_ve[r] + 1; // + 1 |
||
108 | if (r != 0) |
||
109 | { |
||
110 | input(0x55FE); |
||
111 | input((r << 8) | 0x00fe); |
||
112 | input((v << 8) | 0x00fe); |
||
1394 | Kulich | 113 | } |
1668 | kulich | 114 | } |
115 | else |
||
116 | { |
||
117 | output(0xdef7, r); |
||
118 | output(0xbef7, v); |
||
119 | } |
||
120 | enable_interrupt(); |
||
1394 | Kulich | 121 | } |
122 | |||
1393 | Kulich | 123 | void Unix_to_GMT(void) |
124 | { |
||
1668 | kulich | 125 | unsigned char monthLength = 0; |
126 | // ४ ᮢ ᨭ |
||
127 | int days = 0; |
||
128 | secsUnix.ul = secsUnix.ul + GMT * 3600; |
||
1393 | Kulich | 129 | |
1668 | kulich | 130 | second = secsUnix.ul % 60; |
131 | secsUnix.ul /= 60; // now it is minutes |
||
132 | minute = secsUnix.ul % 60; |
||
133 | secsUnix.ul /= 60; // now it is hours |
||
134 | hour = secsUnix.ul % 24; |
||
135 | secsUnix.ul /= 24; // now it is days |
||
136 | weekday = (secsUnix.ul + 4) % 7; // day week, 0-sunday |
||
137 | year = 70; |
||
138 | while (days + ((year % 4) ? 365 : 366) <= secsUnix.ul) |
||
139 | { |
||
140 | days += (year % 4) ? 365 : 366; |
||
141 | year++; |
||
142 | } |
||
143 | secsUnix.ul -= days; // now it is days in this year, starting at 0 |
||
144 | |||
145 | days = 0; |
||
146 | month = 0; |
||
147 | for (month = 0; month < 12; month++) |
||
148 | { |
||
149 | if (month == 1) |
||
150 | { // february |
||
151 | if (year % 4) |
||
152 | monthLength = 28; |
||
153 | else |
||
154 | monthLength = 29; |
||
155 | } |
||
156 | else |
||
157 | monthLength = monthDays[month]; |
||
158 | if (secsUnix.ul >= monthLength) |
||
159 | secsUnix.ul -= monthLength; |
||
160 | else |
||
161 | break; |
||
162 | } |
||
163 | month++; // jan is month 1 |
||
164 | day = secsUnix.ul + 1; // day of month |
||
165 | } |
||
166 | void ntp_resolver(void) |
||
167 | { |
||
168 | unsigned char i, j; |
||
1393 | Kulich | 169 | signed char res; |
170 | int len; |
||
1668 | kulich | 171 | ntp_ia.sin_port = 123 << 8; |
172 | ntp_ia.sin_addr = *dns_resolver((void *)defntp); |
||
173 | if (!ntp_ia.sin_addr.S_un.S_addr) |
||
174 | exit((int)"error: domain name not resolved"); |
||
175 | i = 200; |
||
1393 | Kulich | 176 | inetloop: |
177 | YIELD(); |
||
178 | i--; |
||
1654 | kulich | 179 | YIELD(); |
1668 | kulich | 180 | if (i == 0) |
181 | { |
||
1393 | Kulich | 182 | exit((int)"inet error"); |
183 | } |
||
1668 | kulich | 184 | s = socket(AF_INET, SOCK_DGRAM, 0); |
185 | if (s < 0) |
||
186 | { |
||
187 | s = 0; |
||
1393 | Kulich | 188 | goto inetloop; |
189 | } |
||
1668 | kulich | 190 | memcpy(netbuf, ntpnead, sizeof(ntpnead)); |
1660 | kulich | 191 | |
1668 | kulich | 192 | len = sendto(s, netbuf, 48, 0, &ntp_ia, sizeof(ntp_ia)); |
193 | if (res < 0) |
||
194 | { |
||
195 | closesocket(s, 0); |
||
196 | s = 0; |
||
1393 | Kulich | 197 | goto inetloop; |
198 | } |
||
1668 | kulich | 199 | j = 50; |
200 | while (j) |
||
201 | { |
||
1393 | Kulich | 202 | j--; |
1668 | kulich | 203 | len = recvfrom(s, netbuf, sizeof(netbuf), 0, &ntp_ia, sizeof(ntp_ia)); |
204 | if (len < 0) |
||
205 | { |
||
1393 | Kulich | 206 | YIELD(); |
1660 | kulich | 207 | YIELD(); |
1393 | Kulich | 208 | continue; |
209 | } |
||
210 | break; |
||
211 | } |
||
1668 | kulich | 212 | |
213 | closesocket(s, 0); |
||
214 | s = 0; |
||
215 | if (len <= 0) |
||
216 | { |
||
217 | exit((int)"server error"); |
||
1393 | Kulich | 218 | } |
219 | secsUnix.b[3] = netbuf[40]; |
||
220 | secsUnix.b[2] = netbuf[41]; |
||
221 | secsUnix.b[1] = netbuf[42]; |
||
222 | secsUnix.b[0] = netbuf[43]; |
||
1668 | kulich | 223 | secsUnix.ul -= 2208988800UL; |
1393 | Kulich | 224 | Unix_to_GMT(); |
225 | } |
||
226 | |||
2068 | kulich | 227 | ///////////////////////////////////ESP-COM///////////////////////////////////////// |
2070 | kulich | 228 | void delay(unsigned long counter) |
229 | { |
||
230 | unsigned long start, finish; |
||
231 | counter = counter / 20; |
||
232 | if (counter < 1) |
||
233 | { |
||
234 | counter = 1; |
||
235 | } |
||
236 | start = time(); |
||
237 | finish = start + counter; |
||
238 | |||
239 | while (start < finish) |
||
240 | { |
||
241 | start = time(); |
||
242 | } |
||
243 | } |
||
244 | |||
2068 | kulich | 245 | void uart_setrts(unsigned char mode) |
246 | { |
||
247 | switch (comType) |
||
248 | { |
||
249 | case 0: |
||
250 | switch (mode) |
||
251 | { |
||
252 | case 1: |
||
253 | output(MCR, 2); |
||
254 | break; |
||
255 | case 0: |
||
256 | output(MCR, 0); |
||
257 | break; |
||
258 | default: |
||
259 | disable_interrupt(); |
||
260 | output(MCR, 2); |
||
261 | output(MCR, 0); |
||
262 | enable_interrupt(); |
||
263 | break; |
||
264 | } |
||
265 | case 1: |
||
266 | switch (mode) |
||
267 | { |
||
268 | case 1: |
||
269 | disable_interrupt(); |
||
270 | input(0x55fe); // 室 ० |
||
271 | input(0x43fe); // ⠭ |
||
272 | input(0x03fe); // ⠭ ⮢ DTR RTS |
||
273 | enable_interrupt(); |
||
274 | break; |
||
275 | case 0: |
||
276 | disable_interrupt(); |
||
277 | input(0x55fe); // 室 ० |
||
278 | input(0x43fe); // ⠭ |
||
279 | input(0x00fe); // ⮢ DTR RTS |
||
280 | enable_interrupt(); |
||
281 | break; |
||
282 | default: |
||
283 | disable_interrupt(); |
||
284 | input(0x55fe); // 室 ० |
||
285 | input(0x43fe); // ⠭ |
||
286 | input(0x03fe); // ⠭ ⮢ DTR RTS |
||
287 | |||
288 | input(0x55fe); // 室 ० |
||
289 | input(0x43fe); // ⠭ |
||
290 | input(0x00fe); // ⮢ DTR RTS |
||
291 | enable_interrupt(); |
||
292 | break; |
||
293 | } |
||
294 | case 2: |
||
295 | break; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | unsigned char uart_hasByte(void) |
||
300 | { |
||
301 | unsigned char queue; |
||
302 | switch (comType) |
||
303 | { |
||
304 | case 0: |
||
305 | case 2: |
||
306 | return (1 & input(LSR)); |
||
307 | case 1: |
||
308 | disable_interrupt(); |
||
309 | input(0x55fe); // 室 ० |
||
310 | queue = input(0xc2fe); // 砥 ⢮ ਥ |
||
311 | enable_interrupt(); |
||
312 | return queue; |
||
313 | } |
||
314 | return 255; |
||
315 | } |
||
316 | |||
317 | unsigned char uart_read(void) |
||
318 | { |
||
319 | unsigned char data; |
||
320 | switch (comType) |
||
321 | { |
||
322 | case 0: |
||
323 | case 2: |
||
324 | return input(RBR_THR); |
||
325 | case 1: |
||
326 | disable_interrupt(); |
||
327 | input(0x55fe); // 室 ० |
||
328 | data = input(0x02fe); // |
||
329 | enable_interrupt(); |
||
330 | return data; |
||
331 | } |
||
332 | return 255; |
||
333 | } |
||
334 | |||
335 | unsigned char uart_readBlock(void) |
||
336 | { |
||
337 | unsigned char data; |
||
338 | switch (comType) |
||
339 | { |
||
340 | case 0: |
||
341 | while (uart_hasByte() == 0) |
||
342 | { |
||
343 | uart_setrts(2); |
||
344 | } |
||
345 | return input(RBR_THR); |
||
346 | case 1: |
||
347 | while (uart_hasByte() == 0) |
||
348 | { |
||
349 | uart_setrts(2); |
||
350 | } |
||
351 | disable_interrupt(); |
||
352 | input(0x55fe); // 室 ० |
||
353 | data = input(0x02fe); // |
||
354 | enable_interrupt(); |
||
355 | return data; |
||
356 | case 2: |
||
357 | while (uart_hasByte() == 0) |
||
358 | { |
||
359 | } |
||
360 | return input(RBR_THR); |
||
361 | } |
||
362 | return 255; |
||
363 | } |
||
364 | |||
365 | void uart_write(unsigned char data) |
||
366 | { |
||
367 | unsigned char status; |
||
368 | switch (comType) |
||
369 | { |
||
370 | case 0: |
||
371 | case 2: |
||
372 | while ((input(LSR) & 64) == 0) |
||
373 | { |
||
374 | } |
||
375 | output(RBR_THR, data); |
||
376 | break; |
||
377 | case 1: |
||
378 | disable_interrupt(); |
||
379 | do |
||
380 | { |
||
381 | input(0x55fe); // 室 ० |
||
382 | status = input(0x42fe); // |
||
383 | } while ((status & 64) == 0); // 塞 6 |
||
384 | |||
385 | input(0x55fe); // 室 ० |
||
386 | input(0x03fe); // |
||
387 | input((data << 8) | 0x00fe); // 뢠 data |
||
388 | enable_interrupt(); |
||
389 | break; |
||
390 | } |
||
391 | } |
||
392 | |||
393 | void uart_flush(void) |
||
394 | { |
||
395 | unsigned int count; |
||
2070 | kulich | 396 | for (count = 0; count < 5000; count++) |
2068 | kulich | 397 | { |
398 | uart_setrts(1); |
||
399 | uart_read(); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | void uart_init(unsigned char divisor) |
||
404 | { |
||
405 | switch (comType) |
||
406 | { |
||
407 | case 0: |
||
408 | case 2: |
||
409 | output(MCR, 0x00); // Disable input |
||
410 | output(IIR_FCR, 0x87); // Enable fifo 8 level, and clear it |
||
411 | output(LCR, 0x83); // 8n1, DLAB=1 |
||
412 | output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400) |
||
413 | output(IER, 0x00); // (divider 0). Divider is 16 bit, so we get (#0002 divider) |
||
414 | output(LCR, 0x03); // 8n1, DLAB=0 |
||
415 | output(IER, 0x00); // Disable int |
||
416 | output(MCR, 0x2f); // Enable AFE |
||
417 | break; |
||
418 | case 1: |
||
419 | disable_interrupt(); |
||
420 | input(0x55fe); |
||
421 | input(0xc3fe); |
||
422 | input((divisor << 8) | 0x00fe); |
||
423 | enable_interrupt(); |
||
424 | break; |
||
425 | } |
||
426 | } |
||
427 | |||
428 | void loadEspConfig(void) |
||
429 | { |
||
430 | unsigned char curParam[256]; |
||
431 | unsigned char res; |
||
432 | FILE *espcom; |
||
433 | OS_SETSYSDRV(); |
||
434 | OS_CHDIR("browser"); |
||
435 | espcom = OS_OPENHANDLE("espcom.ini", 0x80); |
||
436 | if (((int)espcom) & 0xff) |
||
437 | { |
||
438 | printf("mrfesp.ini opening error\r\n"); |
||
439 | return; |
||
440 | } |
||
441 | |||
442 | OS_READHANDLE(curParam, espcom, 256); |
||
443 | |||
444 | res = sscanf(curParam, "%x %x %x %x %x %x %x %x %u %u %u", &RBR_THR, &IER, &IIR_FCR, &LCR, &MCR, &LSR, &MSR, &SR, ÷r, &comType, &espType); |
||
445 | puts("Config loaded:"); |
||
446 | if (comType == 1) |
||
447 | { |
||
448 | puts("ATM Turbo 2+ Controller base port: 0x55fe"); |
||
449 | } |
||
450 | else |
||
451 | { |
||
452 | printf(" RBR_THR:0x%4x\r\n IER :0x%4x\r\n IIR_FCR:0x%4x\r\n LCR :0x%4x\r\n", RBR_THR, IER, IIR_FCR, LCR); |
||
453 | printf(" MCR :0x%4x\r\n LSR :0x%4x\r\n MSR :0x%4x\r\n SR :0x%4x\r\n", MCR, LSR, MSR, SR); |
||
454 | } |
||
2070 | kulich | 455 | printf(" DIVIDER:%u TYPE:%u ESP:%u\r\n", divider, comType, espType); |
2068 | kulich | 456 | } |
457 | |||
458 | void sendcommand(char *commandline) |
||
459 | { |
||
460 | unsigned int count, cmdLen; |
||
461 | cmdLen = strlen(commandline); |
||
462 | for (count = 0; count < cmdLen; count++) |
||
463 | { |
||
464 | uart_write(commandline[count]); |
||
465 | } |
||
466 | uart_write('\r'); |
||
467 | uart_write('\n'); |
||
2079 | kulich | 468 | // printf("Sended:[%s] \r\n", commandline); |
2068 | kulich | 469 | YIELD(); |
470 | } |
||
471 | |||
472 | unsigned char getAnswer2(void) |
||
473 | { |
||
474 | unsigned char readbyte; |
||
475 | unsigned int curPos = 0; |
||
476 | do |
||
477 | { |
||
478 | readbyte = uart_readBlock(); |
||
2078 | kulich | 479 | |
2068 | kulich | 480 | } while (((readbyte == 0x0a) || (readbyte == 0x0d))); |
481 | |||
482 | netbuf[curPos] = readbyte; |
||
2078 | kulich | 483 | |
2068 | kulich | 484 | do |
485 | { |
||
2078 | kulich | 486 | curPos++; |
2068 | kulich | 487 | readbyte = uart_readBlock(); |
488 | netbuf[curPos] = readbyte; |
||
489 | } while (readbyte != 0x0d); |
||
2078 | kulich | 490 | netbuf[curPos] = 0; |
491 | uart_readBlock(); // 0x0a |
||
2079 | kulich | 492 | // printf("Answer:[%s]\r\n", netbuf); |
493 | // getchar(); |
||
2068 | kulich | 494 | return curPos; |
495 | } |
||
496 | |||
497 | void espReBoot(void) |
||
498 | { |
||
499 | unsigned char byte, count; |
||
2079 | kulich | 500 | // uart_flush(); |
2068 | kulich | 501 | sendcommand("AT+RST"); |
502 | printf("Resetting ESP..."); |
||
2078 | kulich | 503 | count = 0; |
2068 | kulich | 504 | do |
505 | { |
||
506 | byte = uart_readBlock(); |
||
507 | if (byte == gotWiFi[count]) |
||
508 | { |
||
509 | count++; |
||
510 | } |
||
511 | else |
||
512 | { |
||
513 | count = 0; |
||
514 | } |
||
515 | } while (count < strlen(gotWiFi)); |
||
516 | uart_readBlock(); // CR |
||
517 | uart_readBlock(); // LF |
||
518 | puts("Reset complete."); |
||
2070 | kulich | 519 | |
2068 | kulich | 520 | sendcommand("ATE0"); |
521 | do |
||
522 | { |
||
523 | byte = uart_readBlock(); |
||
524 | } while (byte != 'K'); // OK |
||
525 | // puts("Answer:[OK]"); |
||
526 | uart_readBlock(); // CR |
||
527 | uart_readBlock(); // LN |
||
2079 | kulich | 528 | |
529 | sendcommand("AT+CIPCLOSE"); |
||
530 | getAnswer2(); |
||
531 | sendcommand("AT+CIPDINFO=0"); |
||
532 | getAnswer2(); |
||
533 | sendcommand("AT+CIPMUX=0"); |
||
534 | getAnswer2(); |
||
535 | sendcommand("AT+CIPSERVER=0"); |
||
536 | getAnswer2(); |
||
537 | sendcommand("AT+CIPRECVMODE=0"); |
||
538 | getAnswer2(); |
||
2068 | kulich | 539 | } |
540 | |||
541 | void espntp_resolver(void) |
||
542 | { |
||
2078 | kulich | 543 | unsigned char retry = 10; |
544 | unsigned char byte, count = 0; |
||
2068 | kulich | 545 | loadEspConfig(); |
546 | uart_init(divider); |
||
547 | espReBoot(); |
||
548 | |||
2070 | kulich | 549 | // AT+CIPSNTPCFG=1,8,"cn.ntp.org.cn","ntp.sjtu.edu.cn" |
550 | weekday = 0; |
||
551 | month = 0; |
||
552 | day = 0; |
||
553 | hour = 0; |
||
554 | second = 0; |
||
555 | year = 170; |
||
2068 | kulich | 556 | strcpy(cmd, "AT+CIPSNTPCFG=1,"); |
557 | sprintf(netbuf, "%u,\"%s\",\"time.google.com\"", GMT, defntp); |
||
558 | strcat(cmd, netbuf); |
||
559 | sendcommand(cmd); |
||
560 | getAnswer2(); // OK |
||
2078 | kulich | 561 | retryTime: |
562 | count = 0; |
||
2070 | kulich | 563 | delay(250); |
2068 | kulich | 564 | sendcommand("AT+CIPSNTPTIME?"); |
2075 | kulich | 565 | do |
566 | { |
||
567 | byte = uart_readBlock(); |
||
2079 | kulich | 568 | // printf("[%c]", byte); |
2075 | kulich | 569 | if (byte == timeUpdated[count]) |
570 | { |
||
571 | count++; |
||
572 | } |
||
573 | else |
||
574 | { |
||
575 | count = 0; |
||
576 | } |
||
577 | } while (count < strlen(timeUpdated)); |
||
578 | getAnswer2(); // TIME |
||
2068 | kulich | 579 | |
2078 | kulich | 580 | strncpy(cmd, netbuf, 3); |
2068 | kulich | 581 | cmd[3] = 0; |
582 | |||
583 | if (cmd[0] == 'S' && cmd[1] == 'u') |
||
584 | { |
||
585 | weekday = 1; |
||
586 | } |
||
587 | else if (cmd[0] == 'M' && cmd[1] == 'o') |
||
588 | { |
||
589 | weekday = 2; |
||
590 | } |
||
591 | else if (cmd[0] == 'T' && cmd[1] == 'u') |
||
592 | { |
||
593 | weekday = 3; |
||
594 | } |
||
595 | else if (cmd[0] == 'W' && cmd[1] == 'e') |
||
596 | { |
||
597 | weekday = 4; |
||
598 | } |
||
599 | else if (cmd[0] == 'T' && cmd[1] == 'h') |
||
600 | { |
||
601 | weekday = 5; |
||
602 | } |
||
603 | else if (cmd[0] == 'F' && cmd[1] == 'r') |
||
604 | { |
||
605 | weekday = 6; |
||
606 | } |
||
607 | else if (cmd[0] == 'S' && cmd[1] == 'a') |
||
608 | { |
||
609 | weekday = 7; |
||
610 | } |
||
611 | |||
2078 | kulich | 612 | strncpy(cmd, netbuf + 4, 3); |
2068 | kulich | 613 | cmd[3] = 0; |
614 | |||
615 | if (cmd[0] == 'J' && cmd[1] == 'a') |
||
616 | { |
||
617 | month = 1; |
||
618 | } |
||
619 | else if (cmd[0] == 'F' && cmd[1] == 'e') |
||
620 | { |
||
621 | month = 2; |
||
622 | } |
||
623 | else if (cmd[0] == 'M' && cmd[2] == 'r') |
||
624 | { |
||
625 | month = 3; |
||
626 | } |
||
627 | else if (cmd[0] == 'A' && cmd[1] == 'p') |
||
628 | { |
||
629 | month = 4; |
||
630 | } |
||
631 | else if (cmd[0] == 'M' && cmd[2] == 'y') |
||
632 | { |
||
633 | month = 5; |
||
634 | } |
||
635 | else if (cmd[0] == 'J' && cmd[2] == 'n') |
||
636 | { |
||
637 | month = 6; |
||
638 | } |
||
639 | else if (cmd[0] == 'J' && cmd[2] == 'l') |
||
640 | { |
||
641 | month = 7; |
||
642 | } |
||
643 | else if (cmd[0] == 'A' && cmd[1] == 'u') |
||
644 | { |
||
645 | month = 8; |
||
646 | } |
||
647 | else if (cmd[0] == 'S' && cmd[1] == 'e') |
||
648 | { |
||
649 | month = 9; |
||
650 | } |
||
651 | else if (cmd[0] == 'O' && cmd[1] == 'c') |
||
652 | { |
||
653 | month = 10; |
||
654 | } |
||
655 | else if (cmd[0] == 'N' && cmd[1] == 'o') |
||
656 | { |
||
657 | month = 11; |
||
658 | } |
||
659 | else if (cmd[0] == 'D' && cmd[1] == 'e') |
||
660 | { |
||
661 | month = 12; |
||
662 | } |
||
663 | |||
2078 | kulich | 664 | strncpy(cmd, netbuf + 8, 2); |
2068 | kulich | 665 | cmd[2] = 0; |
666 | day = atoi(cmd); |
||
667 | |||
2078 | kulich | 668 | strncpy(cmd, netbuf + 11, 2); |
2068 | kulich | 669 | hour = atoi(cmd); |
670 | |||
2078 | kulich | 671 | strncpy(cmd, netbuf + 14, 2); |
2068 | kulich | 672 | minute = atoi(cmd); |
673 | |||
2078 | kulich | 674 | strncpy(cmd, netbuf + 17, 2); |
2068 | kulich | 675 | second = atoi(cmd); |
676 | |||
2078 | kulich | 677 | strncpy(cmd, netbuf + 22, 2); |
2068 | kulich | 678 | cmd[4] = 0; |
679 | year = atoi(cmd) + 100; |
||
2070 | kulich | 680 | |
681 | getAnswer2(); // OK |
||
2075 | kulich | 682 | |
2079 | kulich | 683 | // printf("day of week:%u Month:%u day:%u hours:%u minutes:%u seconds:%u year:%u\r\n", weekday, month, day, hour, minute, second, year); |
2075 | kulich | 684 | |
2070 | kulich | 685 | if (year == 170) |
2068 | kulich | 686 | { |
2070 | kulich | 687 | YIELD(); |
688 | if (retry != 0) |
||
689 | { |
||
690 | retry--; |
||
691 | printf("Retry [%u]\r\n", retry); |
||
2079 | kulich | 692 | delay(250); |
2070 | kulich | 693 | goto retryTime; |
694 | } |
||
695 | puts("error getting time..."); |
||
696 | exit(255); |
||
2068 | kulich | 697 | } |
698 | } |
||
699 | |||
1668 | kulich | 700 | void set_datetime(void) |
701 | { |
||
702 | writecmos(0x0b, readcmos(0x0b) | 6); |
||
703 | writecmos(0x07, day); |
||
704 | writecmos(0x08, month); |
||
705 | if (is_atm == 2 || is_atm == 3) |
||
706 | { |
||
707 | writecmos(0x09, year - 80); |
||
708 | } |
||
709 | else |
||
710 | { |
||
711 | writecmos(0x09, year - 100); |
||
712 | } |
||
1393 | Kulich | 713 | |
1668 | kulich | 714 | writecmos(0x00, second); |
715 | writecmos(0x02, minute); |
||
716 | writecmos(0x04, hour); |
||
1393 | Kulich | 717 | } |
718 | void get_datetime(void) |
||
719 | { |
||
1668 | kulich | 720 | writecmos(0x0b, readcmos(0x0b) | 6); |
721 | second = readcmos(0x00); |
||
722 | minute = readcmos(0x02); |
||
723 | hour = readcmos(0x04); |
||
724 | weekday = readcmos(0x06) - 1; |
||
725 | day = readcmos(0x07); |
||
726 | month = readcmos(0x08); |
||
727 | if (is_atm == 2 || is_atm == 3) |
||
728 | { |
||
2068 | kulich | 729 | year = readcmos(0x09) + 80; |
1668 | kulich | 730 | } |
2068 | kulich | 731 | else |
732 | { |
||
733 | year = readcmos(0x09) + 100; |
||
734 | } |
||
1393 | Kulich | 735 | } |
736 | |||
1668 | kulich | 737 | C_task main(int argc, char *argv[]) |
1393 | Kulich | 738 | { |
1668 | kulich | 739 | unsigned char i = 1; |
1393 | Kulich | 740 | os_initstdio(); |
741 | is_atm = (unsigned char)OS_GETCONFIG(); |
||
742 | |||
1668 | kulich | 743 | if (argc == 1) |
744 | { |
||
1393 | Kulich | 745 | get_datetime(); |
746 | puts(help); |
||
747 | } |
||
1668 | kulich | 748 | while (i != argc) |
749 | { |
||
750 | char *p = argv[i]; |
||
751 | if (p[0] != '-') |
||
752 | exit((int)"Wrong parameter. Use -H for help"); |
||
753 | switch (p[1] & 0xdf) |
||
754 | { |
||
755 | case 'T': |
||
756 | get_datetime(); |
||
757 | if (sscanf(p + 2, "%d:%d:%d", &hour, &minute, &second) == 3) |
||
758 | { |
||
759 | disable_interrupt(); |
||
760 | set_datetime(); |
||
761 | enable_interrupt(); |
||
762 | } |
||
763 | break; |
||
764 | case 'D': |
||
765 | get_datetime(); |
||
766 | if (sscanf(p + 2, "%d-%d-%d", &day, &month, &year) == 3) |
||
767 | { |
||
768 | disable_interrupt(); |
||
769 | year -= 1900; |
||
770 | set_datetime(); |
||
771 | enable_interrupt(); |
||
772 | } |
||
773 | break; |
||
774 | case 'N': |
||
775 | defntp = p + 2; |
||
776 | break; |
||
777 | case 'Z': |
||
778 | if (sscanf(p + 2, "%d", &GMT) != 1) |
||
779 | { |
||
780 | GMT = 3; |
||
781 | } |
||
782 | break; |
||
783 | case 'H': |
||
784 | exit((int)help); |
||
785 | break; |
||
786 | case 'I': |
||
787 | inet = 1; |
||
788 | break; |
||
2068 | kulich | 789 | case 'E': |
790 | espInet = 1; |
||
791 | break; |
||
792 | |||
1668 | kulich | 793 | default: |
794 | exit((int)"Wrong parameter. Use -H for help"); |
||
1393 | Kulich | 795 | } |
796 | i++; |
||
797 | } |
||
1668 | kulich | 798 | if (inet) |
799 | { |
||
1393 | Kulich | 800 | ntp_resolver(); |
801 | set_datetime(); |
||
1668 | kulich | 802 | writecmos(0x06, weekday + 1); |
1393 | Kulich | 803 | } |
2068 | kulich | 804 | |
805 | if (espInet) |
||
806 | { |
||
807 | espntp_resolver(); |
||
808 | set_datetime(); |
||
809 | writecmos(0x06, weekday + 1); |
||
810 | } |
||
1393 | Kulich | 811 | puts("Now time:"); |
1668 | kulich | 812 | printf("%02u-%02u-%04u ", day, month, year + 1900); |
813 | printf("%02u:%02u:%02u\r\n", hour, minute, second); |
||
2079 | kulich | 814 | uart_setrts(1); |
1393 | Kulich | 815 | exit(0); |
816 | return 0; |
||
1668 | kulich | 817 | } |