Subversion Repositories NedoOS

Rev

Rev 2174 | Rev 2229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  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("../ini");
  435.         espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  436.         if (((int)espcom) & 0xff)
  437.         {
  438.                 printf("espcom.ini opening error\r\n");
  439.                 exit(0);
  440.                 return;
  441.         }
  442.  
  443.         OS_READHANDLE(curParam, espcom, 256);
  444.  
  445.         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);
  446.         puts("Config loaded:");
  447.         if (comType == 1)
  448.         {
  449.                 puts("ATM Turbo 2+ Controller base port: 0x55fe");
  450.         }
  451.         else
  452.         {
  453.                 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);
  454.                 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);
  455.         }
  456.         printf("    DIVIDER:%u TYPE:%u ESP:%u\r\n", divider, comType, espType);
  457. }
  458.  
  459. void sendcommand(char *commandline)
  460. {
  461.         unsigned int count, cmdLen;
  462.         cmdLen = strlen(commandline);
  463.         for (count = 0; count < cmdLen; count++)
  464.         {
  465.                 uart_write(commandline[count]);
  466.         }
  467.         uart_write('\r');
  468.         uart_write('\n');
  469.         // printf("Sended:[%s] \r\n", commandline);
  470.         YIELD();
  471. }
  472.  
  473. unsigned char getAnswer2(void)
  474. {
  475.         unsigned char readbyte;
  476.         unsigned int curPos = 0;
  477.         do
  478.         {
  479.                 readbyte = uart_readBlock();
  480.  
  481.         } while (((readbyte == 0x0a) || (readbyte == 0x0d)));
  482.  
  483.         netbuf[curPos] = readbyte;
  484.  
  485.         do
  486.         {
  487.                 curPos++;
  488.                 readbyte = uart_readBlock();
  489.                 netbuf[curPos] = readbyte;
  490.         } while (readbyte != 0x0d);
  491.         netbuf[curPos] = 0;
  492.         uart_readBlock(); // 0x0a
  493.         // printf("Answer:[%s]\r\n", netbuf);
  494.         //       getchar();
  495.         return curPos;
  496. }
  497.  
  498. void espReBoot(void)
  499. {
  500.         unsigned char byte, count;
  501.         // uart_flush();
  502.         sendcommand("AT+RST");
  503.         printf("Resetting ESP...");
  504.         count = 0;
  505.         do
  506.         {
  507.                 byte = uart_readBlock();
  508.                 if (byte == gotWiFi[count])
  509.                 {
  510.                         count++;
  511.                 }
  512.                 else
  513.                 {
  514.                         count = 0;
  515.                 }
  516.         } while (count < strlen(gotWiFi));
  517.         uart_readBlock(); // CR
  518.         uart_readBlock(); // LF
  519.         puts("Reset complete.");
  520.  
  521.         sendcommand("ATE0");
  522.         do
  523.         {
  524.                 byte = uart_readBlock();
  525.         } while (byte != 'K'); // OK
  526.         // puts("Answer:[OK]");
  527.         uart_readBlock(); // CR
  528.         uart_readBlock(); // LN
  529.  
  530.         sendcommand("AT+CIPCLOSE");
  531.         getAnswer2();
  532.         sendcommand("AT+CIPDINFO=0");
  533.         getAnswer2();
  534.         sendcommand("AT+CIPMUX=0");
  535.         getAnswer2();
  536.         sendcommand("AT+CIPSERVER=0");
  537.         getAnswer2();
  538.         sendcommand("AT+CIPRECVMODE=0");
  539.         getAnswer2();
  540. }
  541.  
  542. void espntp_resolver(void)
  543. {
  544.         unsigned char retry = 10;
  545.         unsigned char byte, count = 0;
  546.         loadEspConfig();
  547.         uart_init(divider);
  548.         espReBoot();
  549.  
  550.         // AT+CIPSNTPCFG=1,8,"cn.ntp.org.cn","ntp.sjtu.edu.cn"
  551.         weekday = 0;
  552.         month = 0;
  553.         day = 0;
  554.         hour = 0;
  555.         second = 0;
  556.         year = 170;
  557.         strcpy(cmd, "AT+CIPSNTPCFG=1,");
  558.         sprintf(netbuf, "%u,\"%s\",\"time.google.com\"", GMT, defntp);
  559.         strcat(cmd, netbuf);
  560.         sendcommand(cmd);
  561.         getAnswer2(); // OK
  562. retryTime:
  563.         count = 0;
  564.         delay(250);
  565.         sendcommand("AT+CIPSNTPTIME?");
  566.         do
  567.         {
  568.                 byte = uart_readBlock();
  569.                 // printf("[%c]", byte);
  570.                 if (byte == timeUpdated[count])
  571.                 {
  572.                         count++;
  573.                 }
  574.                 else
  575.                 {
  576.                         count = 0;
  577.                 }
  578.         } while (count < strlen(timeUpdated));
  579.         getAnswer2(); // TIME
  580.  
  581.         strncpy(cmd, netbuf, 3);
  582.         cmd[3] = 0;
  583.  
  584.         if (cmd[0] == 'S' && cmd[1] == 'u')
  585.         {
  586.                 weekday = 1;
  587.         }
  588.         else if (cmd[0] == 'M' && cmd[1] == 'o')
  589.         {
  590.                 weekday = 2;
  591.         }
  592.         else if (cmd[0] == 'T' && cmd[1] == 'u')
  593.         {
  594.                 weekday = 3;
  595.         }
  596.         else if (cmd[0] == 'W' && cmd[1] == 'e')
  597.         {
  598.                 weekday = 4;
  599.         }
  600.         else if (cmd[0] == 'T' && cmd[1] == 'h')
  601.         {
  602.                 weekday = 5;
  603.         }
  604.         else if (cmd[0] == 'F' && cmd[1] == 'r')
  605.         {
  606.                 weekday = 6;
  607.         }
  608.         else if (cmd[0] == 'S' && cmd[1] == 'a')
  609.         {
  610.                 weekday = 7;
  611.         }
  612.  
  613.         strncpy(cmd, netbuf + 4, 3);
  614.         cmd[3] = 0;
  615.  
  616.         if (cmd[0] == 'J' && cmd[1] == 'a')
  617.         {
  618.                 month = 1;
  619.         }
  620.         else if (cmd[0] == 'F' && cmd[1] == 'e')
  621.         {
  622.                 month = 2;
  623.         }
  624.         else if (cmd[0] == 'M' && cmd[2] == 'r')
  625.         {
  626.                 month = 3;
  627.         }
  628.         else if (cmd[0] == 'A' && cmd[1] == 'p')
  629.         {
  630.                 month = 4;
  631.         }
  632.         else if (cmd[0] == 'M' && cmd[2] == 'y')
  633.         {
  634.                 month = 5;
  635.         }
  636.         else if (cmd[0] == 'J' && cmd[2] == 'n')
  637.         {
  638.                 month = 6;
  639.         }
  640.         else if (cmd[0] == 'J' && cmd[2] == 'l')
  641.         {
  642.                 month = 7;
  643.         }
  644.         else if (cmd[0] == 'A' && cmd[1] == 'u')
  645.         {
  646.                 month = 8;
  647.         }
  648.         else if (cmd[0] == 'S' && cmd[1] == 'e')
  649.         {
  650.                 month = 9;
  651.         }
  652.         else if (cmd[0] == 'O' && cmd[1] == 'c')
  653.         {
  654.                 month = 10;
  655.         }
  656.         else if (cmd[0] == 'N' && cmd[1] == 'o')
  657.         {
  658.                 month = 11;
  659.         }
  660.         else if (cmd[0] == 'D' && cmd[1] == 'e')
  661.         {
  662.                 month = 12;
  663.         }
  664.  
  665.         strncpy(cmd, netbuf + 8, 2);
  666.         cmd[2] = 0;
  667.         day = atoi(cmd);
  668.  
  669.         strncpy(cmd, netbuf + 11, 2);
  670.         hour = atoi(cmd);
  671.  
  672.         strncpy(cmd, netbuf + 14, 2);
  673.         minute = atoi(cmd);
  674.  
  675.         strncpy(cmd, netbuf + 17, 2);
  676.         second = atoi(cmd);
  677.  
  678.         strncpy(cmd, netbuf + 22, 2);
  679.         cmd[4] = 0;
  680.         year = atoi(cmd) + 100;
  681.  
  682.         getAnswer2(); // OK
  683.  
  684.         // 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);
  685.  
  686.         if (year == 170)
  687.         {
  688.                 YIELD();
  689.                 if (retry != 0)
  690.                 {
  691.                         retry--;
  692.                         printf("Retry [%u]\r\n", retry);
  693.                         delay(250);
  694.                         goto retryTime;
  695.                 }
  696.                 puts("error getting time...");
  697.                 exit(255);
  698.         }
  699. }
  700.  
  701. void set_datetime(void)
  702. {
  703.         writecmos(0x0b, readcmos(0x0b) | 6);
  704.         writecmos(0x07, day);
  705.         writecmos(0x08, month);
  706.         if (is_atm == 2 || is_atm == 3)
  707.         {
  708.                 writecmos(0x09, year - 80);
  709.         }
  710.         else
  711.         {
  712.                 writecmos(0x09, year - 100);
  713.         }
  714.  
  715.         writecmos(0x00, second);
  716.         writecmos(0x02, minute);
  717.         writecmos(0x04, hour);
  718. }
  719. void get_datetime(void)
  720. {
  721.         writecmos(0x0b, readcmos(0x0b) | 6);
  722.         second = readcmos(0x00);
  723.         minute = readcmos(0x02);
  724.         hour = readcmos(0x04);
  725.         weekday = readcmos(0x06) - 1;
  726.         day = readcmos(0x07);
  727.         month = readcmos(0x08);
  728.         if (is_atm == 2 || is_atm == 3)
  729.         {
  730.                 year = readcmos(0x09) + 80;
  731.         }
  732.         else
  733.         {
  734.                 year = readcmos(0x09) + 100;
  735.         }
  736. }
  737.  
  738. C_task main(int argc, char *argv[])
  739. {
  740.         unsigned char i = 1;
  741.         os_initstdio();
  742.         is_atm = (unsigned char)OS_GETCONFIG();
  743.  
  744.         if (argc == 1)
  745.         {
  746.                 get_datetime();
  747.                 puts(help);
  748.         }
  749.         while (i != argc)
  750.         {
  751.                 char *p = argv[i];
  752.                 if (p[0] != '-')
  753.                         exit((int)"Wrong parameter. Use -H for help");
  754.                 switch (p[1] & 0xdf)
  755.                 {
  756.                 case 'T':
  757.                         get_datetime();
  758.                         if (sscanf(p + 2, "%d:%d:%d", &hour, &minute, &second) == 3)
  759.                         {
  760.                                 disable_interrupt();
  761.                                 set_datetime();
  762.                                 enable_interrupt();
  763.                         }
  764.                         break;
  765.                 case 'D':
  766.                         get_datetime();
  767.                         if (sscanf(p + 2, "%d-%d-%d", &day, &month, &year) == 3)
  768.                         {
  769.                                 disable_interrupt();
  770.                                 year -= 1900;
  771.                                 set_datetime();
  772.                                 enable_interrupt();
  773.                         }
  774.                         break;
  775.                 case 'N':
  776.                         defntp = p + 2;
  777.                         break;
  778.                 case 'Z':
  779.                         if (sscanf(p + 2, "%d", &GMT) != 1)
  780.                         {
  781.                                 GMT = 3;
  782.                         }
  783.                         break;
  784.                 case 'H':
  785.                         exit((int)help);
  786.                         break;
  787.                 case 'I':
  788.                         inet = 1;
  789.                         break;
  790.                 case 'E':
  791.                         espInet = 1;
  792.                         break;
  793.  
  794.                 default:
  795.                         exit((int)"Wrong parameter. Use -H for help");
  796.                 }
  797.                 i++;
  798.         }
  799.         if (inet)
  800.         {
  801.                 ntp_resolver();
  802.                 set_datetime();
  803.                 writecmos(0x06, weekday + 1);
  804.         }
  805.  
  806.         if (espInet)
  807.         {
  808.                 espntp_resolver();
  809.                 set_datetime();
  810.                 writecmos(0x06, weekday + 1);
  811.         }
  812.         puts("Now time:");
  813.         printf("%02u-%02u-%04u ", day, month, year + 1900);
  814.         printf("%02u:%02u:%02u\r\n", hour, minute, second);
  815.         uart_setrts(1);
  816.         exit(0);
  817.         return 0;
  818. }
  819.