?login_element?

Subversion Repositories NedoOS

Rev

Rev 2078 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <oscalls.h>
  4. #include <socket.h>
  5. #include <intrz80.h>
  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;
  19.  
  20. unsigned char cmd[512];
  21. const unsigned char sendOk[] = "SEND OK";
  22. const unsigned char gotWiFi[] = "WIFI GOT IP";
  23. const unsigned char timeUpdated[] = "+CIPSNTPTIME:";
  24. int GMT = 3;
  25. unsigned char is_atm;
  26. unsigned char netbuf[4 * 1024];
  27. struct sockaddr_in ntp_ia;
  28. union
  29. {
  30.         unsigned long ul;
  31.         unsigned char b[4];
  32. } secsUnix;
  33. unsigned int hour, minute, second, day, month, year, weekday;
  34. SOCKET s = 0;
  35. unsigned char inet = 0, espInet = 0;
  36. const unsigned char monthDays[12] =
  37.         {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  38. const unsigned char ntpnead[48] =
  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};
  55.  
  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\
  62. -i get datetime from internet\r\n\
  63. -e get datetime from ESP-COM";
  64.  
  65. extern void
  66. dns_resolve(void);
  67.  
  68. void exit(int e)
  69. {
  70.         if (s)
  71.                 closesocket(s, 0);
  72.         if (e != 0)
  73.         {
  74.                 puts((char *)e);
  75.         }
  76.         ((void (*)(int))0x0000)(e);
  77. }
  78.  
  79. extern void dns_resolve(void);
  80.  
  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;
  100. }
  101.  
  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);
  113.                 }
  114.         }
  115.         else
  116.         {
  117.                 output(0xdef7, r);
  118.                 output(0xbef7, v);
  119.         }
  120.         enable_interrupt();
  121. }
  122.  
  123. void Unix_to_GMT(void)
  124. {
  125.         unsigned char monthLength = 0;
  126.         // корректировка часового пояса и синхронизация
  127.         int days = 0;
  128.         secsUnix.ul = secsUnix.ul + GMT * 3600;
  129.  
  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;
  169.         signed char res;
  170.         int len;
  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;
  176. inetloop:
  177.         YIELD();
  178.         i--;
  179.         YIELD();
  180.         if (i == 0)
  181.         {
  182.                 exit((int)"inet error");
  183.         }
  184.         s = socket(AF_INET, SOCK_DGRAM, 0);
  185.         if (s < 0)
  186.         {
  187.                 s = 0;
  188.                 goto inetloop;
  189.         }
  190.         memcpy(netbuf, ntpnead, sizeof(ntpnead));
  191.  
  192.         len = sendto(s, netbuf, 48, 0, &ntp_ia, sizeof(ntp_ia));
  193.         if (res < 0)
  194.         {
  195.                 closesocket(s, 0);
  196.                 s = 0;
  197.                 goto inetloop;
  198.         }
  199.         j = 50;
  200.         while (j)
  201.         {
  202.                 j--;
  203.                 len = recvfrom(s, netbuf, sizeof(netbuf), 0, &ntp_ia, sizeof(ntp_ia));
  204.                 if (len < 0)
  205.                 {
  206.                         YIELD();
  207.                         YIELD();
  208.                         continue;
  209.                 }
  210.                 break;
  211.         }
  212.  
  213.         closesocket(s, 0);
  214.         s = 0;
  215.         if (len <= 0)
  216.         {
  217.                 exit((int)"server error");
  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];
  223.         secsUnix.ul -= 2208988800UL;
  224.         Unix_to_GMT();
  225. }
  226.  
  227. ///////////////////////////////////ESP-COM/////////////////////////////////////////
  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.  
  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;
  396.         for (count = 0; count < 5000; count++)
  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, &divider, &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.         }
  455.         printf("    DIVIDER:%u TYPE:%u ESP:%u\r\n", divider, comType, espType);
  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');
  468.         // printf("Sended:[%s] \r\n", commandline);
  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();
  479.  
  480.         } while (((readbyte == 0x0a) || (readbyte == 0x0d)));
  481.  
  482.         netbuf[curPos] = readbyte;
  483.  
  484.         do
  485.         {
  486.                 curPos++;
  487.                 readbyte = uart_readBlock();
  488.                 netbuf[curPos] = readbyte;
  489.         } while (readbyte != 0x0d);
  490.         netbuf[curPos] = 0;
  491.         uart_readBlock(); // 0x0a
  492.         // printf("Answer:[%s]\r\n", netbuf);
  493.         //       getchar();
  494.         return curPos;
  495. }
  496.  
  497. void espReBoot(void)
  498. {
  499.         unsigned char byte, count;
  500.         // uart_flush();
  501.         sendcommand("AT+RST");
  502.         printf("Resetting ESP...");
  503.         count = 0;
  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.");
  519.  
  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
  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();
  539. }
  540.  
  541. void espntp_resolver(void)
  542. {
  543.         unsigned char retry = 10;
  544.         unsigned char byte, count = 0;
  545.         loadEspConfig();
  546.         uart_init(divider);
  547.         espReBoot();
  548.  
  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;
  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
  561. retryTime:
  562.         count = 0;
  563.         delay(250);
  564.         sendcommand("AT+CIPSNTPTIME?");
  565.         do
  566.         {
  567.                 byte = uart_readBlock();
  568.                 // printf("[%c]", byte);
  569.                 if (byte == timeUpdated[count])
  570.                 {
  571.                         count++;
  572.                 }
  573.                 else
  574.                 {
  575.                         count = 0;
  576.                 }
  577.         } while (count < strlen(timeUpdated));
  578.         getAnswer2(); // TIME
  579.  
  580.         strncpy(cmd, netbuf, 3);
  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.  
  612.         strncpy(cmd, netbuf + 4, 3);
  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.  
  664.         strncpy(cmd, netbuf + 8, 2);
  665.         cmd[2] = 0;
  666.         day = atoi(cmd);
  667.  
  668.         strncpy(cmd, netbuf + 11, 2);
  669.         hour = atoi(cmd);
  670.  
  671.         strncpy(cmd, netbuf + 14, 2);
  672.         minute = atoi(cmd);
  673.  
  674.         strncpy(cmd, netbuf + 17, 2);
  675.         second = atoi(cmd);
  676.  
  677.         strncpy(cmd, netbuf + 22, 2);
  678.         cmd[4] = 0;
  679.         year = atoi(cmd) + 100;
  680.  
  681.         getAnswer2(); // OK
  682.  
  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);
  684.  
  685.         if (year == 170)
  686.         {
  687.                 YIELD();
  688.                 if (retry != 0)
  689.                 {
  690.                         retry--;
  691.                         printf("Retry [%u]\r\n", retry);
  692.                         delay(250);
  693.                         goto retryTime;
  694.                 }
  695.                 puts("error getting time...");
  696.                 exit(255);
  697.         }
  698. }
  699.  
  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.         }
  713.  
  714.         writecmos(0x00, second);
  715.         writecmos(0x02, minute);
  716.         writecmos(0x04, hour);
  717. }
  718. void get_datetime(void)
  719. {
  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.         {
  729.                 year = readcmos(0x09) + 80;
  730.         }
  731.         else
  732.         {
  733.                 year = readcmos(0x09) + 100;
  734.         }
  735. }
  736.  
  737. C_task main(int argc, char *argv[])
  738. {
  739.         unsigned char i = 1;
  740.         os_initstdio();
  741.         is_atm = (unsigned char)OS_GETCONFIG();
  742.  
  743.         if (argc == 1)
  744.         {
  745.                 get_datetime();
  746.                 puts(help);
  747.         }
  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;
  789.                 case 'E':
  790.                         espInet = 1;
  791.                         break;
  792.  
  793.                 default:
  794.                         exit((int)"Wrong parameter. Use -H for help");
  795.                 }
  796.                 i++;
  797.         }
  798.         if (inet)
  799.         {
  800.                 ntp_resolver();
  801.                 set_datetime();
  802.                 writecmos(0x06, weekday + 1);
  803.         }
  804.  
  805.         if (espInet)
  806.         {
  807.                 espntp_resolver();
  808.                 set_datetime();
  809.                 writecmos(0x06, weekday + 1);
  810.         }
  811.         puts("Now time:");
  812.         printf("%02u-%02u-%04u ", day, month, year + 1900);
  813.         printf("%02u:%02u:%02u\r\n", hour, minute, second);
  814.         uart_setrts(1);
  815.         exit(0);
  816.         return 0;
  817. }
  818.