Subversion Repositories NedoOS

Rev

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

  1. ////////////////////////ESP32 PROCEDURES//////////////////////
  2. void portOutput(char port, char data)
  3. {
  4.         disable_interrupt();
  5.         output(0xfb, port);
  6.         output(0xfa, data);
  7.         // output(0xfb, 0x00);
  8.         enable_interrupt();
  9. }
  10.  
  11. char portInput(char port)
  12. {
  13.         char byte;
  14.         disable_interrupt();
  15.         output(0xfb, port);
  16.         byte = input(0xfa);
  17.         // output(0xfb, 0x00);
  18.         enable_interrupt();
  19.         return byte;
  20. }
  21.  
  22. void uart_write(unsigned char data)
  23. {
  24.         unsigned char status;
  25.         switch (comType)
  26.         {
  27.         case 0:
  28.         case 2:
  29.                 while ((input(LSR) & 32) == 0)
  30.                 {
  31.                 }
  32.                 output(RBR_THR, data);
  33.                 break;
  34.         case 1:
  35.                 disable_interrupt();
  36.                 do
  37.                 {
  38.                         input(0x55fe);                  // Переход в режим команд
  39.                         status = input(0x42fe); // Команда прочесть статус
  40.                 } while ((status & 32) == 0); // Проверяем 5 бит
  41.  
  42.                 input(0x55fe);                           // Переход в режим команд
  43.                 input(0x03fe);                           // Команда записать в порт
  44.                 input((data << 8) | 0x00fe); // Записываем data в порт
  45.                 enable_interrupt();
  46.                 break;
  47.         case 3:
  48.                 while ((portInput(LSR) & 32) == 0)
  49.                 {
  50.                 }
  51.                 portOutput(RBR_THR, data);
  52.                 break;
  53.         }
  54. }
  55.  
  56. void uart_setrts(unsigned char mode)
  57. {
  58.         switch (comType)
  59.         {
  60.         case 0:
  61.                 switch (mode)
  62.                 {
  63.                 case 1:
  64.                         output(MCR, 2);
  65.                         break;
  66.                 case 0:
  67.                         output(MCR, 0);
  68.                         break;
  69.                 default:
  70.                         disable_interrupt();
  71.                         output(MCR, 2);
  72.                         output(MCR, 0);
  73.                         enable_interrupt();
  74.                         break;
  75.                 }
  76.         case 1:
  77.                 switch (mode)
  78.                 {
  79.                 case 1:
  80.                         disable_interrupt();
  81.                         input(0x55fe); // Переход в режим команд
  82.                         input(0x43fe); // Команда установить статус
  83.                         input(0x03fe); // Устанавливаем готовность DTR и RTS
  84.                         enable_interrupt();
  85.                         break;
  86.                 case 0:
  87.                         disable_interrupt();
  88.                         input(0x55fe); // Переход в режим команд
  89.                         input(0x43fe); // Команда установить статус
  90.                         input(0x00fe); // Снимаем готовность DTR и RTS
  91.                         enable_interrupt();
  92.                         break;
  93.                 default:
  94.                         disable_interrupt();
  95.                         input(0x55fe); // Переход в режим команд
  96.                         input(0x43fe); // Команда установить статус
  97.                         input(0x03fe); // Устанавливаем готовность DTR и RTS
  98.                         input(0x55fe); // Переход в режим команд
  99.                         input(0x43fe); // Команда установить статус
  100.                         input(0x00fe); // Снимаем готовность DTR и RTS
  101.                         enable_interrupt();
  102.                         break;
  103.                 }
  104.         case 2:
  105.                 break;
  106.         case 3:
  107.                 switch (mode)
  108.                 {
  109.                 case 1:
  110.                         portOutput(MCR, 2);
  111.                         break;
  112.                 case 0:
  113.                         portOutput(MCR, 0);
  114.                         break;
  115.                 default:
  116.                         disable_interrupt();
  117.                         output(0xfb, MCR);
  118.                         output(0xfa, 2);
  119.                         output(0xfa, 0);
  120.                         enable_interrupt();
  121.                         break;
  122.                 }
  123.                 break;
  124.         }
  125. }
  126. void uart_init(unsigned char divisor)
  127. {
  128.         switch (comType)
  129.         {
  130.         case 0:
  131.         case 2:
  132.                 output(IIR_FCR, 0x87);    // Enable fifo 8 level, and clear it
  133.                 output(LCR, 0x83);                // 8n1, DLAB=1
  134.                 output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  135.                 output(IER, 0x00);                // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  136.                 output(LCR, 0x03);                // 8n1, DLAB=0
  137.                 output(IER, 0x00);                // Disable int
  138.                 output(MCR, 0x2f);                // Enable AFE
  139.                 uart_setrts(0);
  140.                 break;
  141.         case 1:
  142.                 disable_interrupt();
  143.                 input(0x55fe);
  144.                 input(0xc3fe);
  145.                 input((divisor << 8) | 0x00fe);
  146.                 enable_interrupt();
  147.                 uart_setrts(0);
  148.                 break;
  149.         case 3:
  150.                 portOutput(IIR_FCR, 0x87);        // Enable fifo 8 level, and clear it
  151.                 portOutput(LCR, 0x83);            // 8n1, DLAB=1
  152.                 portOutput(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  153.                 portOutput(IER, 0x00);            // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  154.                 portOutput(LCR, 0x03);            // 8n1, DLAB=0
  155.                 portOutput(IER, 0x00);            // Disable int
  156.                 portOutput(MCR, 0x22);            // Enable AFE
  157.                 enable_interrupt();
  158.                 uart_setrts(0);
  159.                 break;
  160.         }
  161. }
  162.  
  163. unsigned char uart_hasByte(void)
  164. {
  165.         unsigned char queue;
  166.         switch (comType)
  167.         {
  168.         case 0:
  169.         case 2:
  170.                 return (1 & input(LSR));
  171.         case 1:
  172.                 disable_interrupt();
  173.                 input(0x55fe);             // Переход в режим команд
  174.                 queue = input(0xc2fe); // Получаем количество байт в приемном буфере
  175.                 enable_interrupt();
  176.                 return queue;
  177.         case 3:
  178.                 return 1 & portInput(LSR);
  179.         }
  180.         printf("uart_hasByte () Error 001: Unknown port Type:[%d]", comType);
  181.         getchar();
  182.         return 255;
  183. }
  184.  
  185. unsigned char uart_read(void)
  186. {
  187.         unsigned char data;
  188.         switch (comType)
  189.         {
  190.         case 0:
  191.         case 2:
  192.                 return input(RBR_THR);
  193.         case 1:
  194.                 disable_interrupt();
  195.                 input(0x55fe);            // Переход в режим команд
  196.                 data = input(0x02fe); // Команда прочесть из порта
  197.                 enable_interrupt();
  198.                 return data;
  199.         case 3:
  200.                 // data = portInput(RBR_THR);
  201.                 disable_interrupt();
  202.                 output(0xfb, RBR_THR);
  203.                 data = input(0xfa);
  204.                 output(0xfb, 0x00);
  205.                 enable_interrupt();
  206.                 return data;
  207.         }
  208.         return 255;
  209. }
  210.  
  211. unsigned char uart_readBlock(void)
  212. {
  213.         unsigned char data;
  214.         switch (comType)
  215.         {
  216.         case 0:
  217.                 while ((1 & input(LSR)) == 0)
  218.                 {
  219.                         uart_setrts(2);
  220.                 }
  221.                 return input(RBR_THR);
  222.         case 1:
  223.                 while (uart_hasByte() == 0)
  224.                 {
  225.                         uart_setrts(2);
  226.                 }
  227.                 disable_interrupt();
  228.                 input(0x55fe);            // Переход в режим команд
  229.                 data = input(0x02fe); // Команда прочесть из порта
  230.                 enable_interrupt();
  231.                 return data;
  232.         case 2:
  233.                 while ((1 & input(LSR)) == 0)
  234.                 {
  235.                 }
  236.                 return input(RBR_THR);
  237.         case 3:
  238.                 while ((1 & portInput(LSR)) == 0)
  239.                 {
  240.                         uart_setrts(2);
  241.                 }
  242.                 //      data = portInput(RBR_THR);
  243.  
  244.                 disable_interrupt();
  245.                 output(0xfb, RBR_THR);
  246.                 data = input(0xfa);
  247.                 enable_interrupt();
  248.                 return data;
  249.         }
  250.         return 255;
  251. }
  252.  
  253. void uart_flush(void)
  254. {
  255.         unsigned int count;
  256.         uart_setrts(1);
  257.         for (count = 0; count < 6000; count++)
  258.         {
  259.                 uart_read();
  260.         }
  261. }
  262.  
  263. void getdataEsp(unsigned int counted)
  264. {
  265.         unsigned int counter;
  266.         switch (comType)
  267.         {
  268.         case 0:
  269.                 for (counter = 0; counter < counted; counter++)
  270.                 {
  271.                         while ((1 & input(LSR)) == 0)
  272.                         {
  273.                                 uart_setrts(2);
  274.                         }
  275.                         netbuf[counter] = input(RBR_THR);
  276.                 }
  277.                 return;
  278.         case 1:
  279.                 for (counter = 0; counter < counted; counter++)
  280.                 {
  281.                         while (uart_hasByte() == 0)
  282.                         {
  283.                                 uart_setrts(2);
  284.                         }
  285.                         disable_interrupt();
  286.                         input(0x55fe);                                   // Переход в режим команд
  287.                         netbuf[counter] = input(0x02fe); // Команда прочесть из порта
  288.                         enable_interrupt();
  289.                 }
  290.                 return;
  291.         case 2:
  292.                 for (counter = 0; counter < counted; counter++)
  293.                 {
  294.                         while ((1 & input(LSR)) == 0)
  295.                         {
  296.                         }
  297.                         netbuf[counter] = input(RBR_THR);
  298.                 }
  299.                 return;
  300.         case 3:
  301.                 for (counter = 0; counter < counted; counter++)
  302.                 {
  303.                         while ((1 & portInput(LSR)) == 0)
  304.                         {
  305.                                 uart_setrts(2);
  306.                         }
  307.                         disable_interrupt();
  308.                         output(0xfb, RBR_THR);
  309.                         netbuf[counter] = input(0xfa);
  310.                         enable_interrupt();
  311.                 }
  312.                 return;
  313.         }
  314. }
  315. void sendcommand(const char *commandline)
  316. {
  317.         unsigned int count, cmdLen;
  318.         cmdLen = strlen(commandline);
  319.         for (count = 0; count < cmdLen; count++)
  320.         {
  321.                 uart_write(commandline[count]);
  322.         }
  323.         uart_write('\r');
  324.         uart_write('\n');
  325.         // printf("Sended:[%s] \r\n", commandline);
  326. }
  327.  
  328. void sendcommandNrn(const char *commandline)
  329. {
  330.         unsigned int count, cmdLen;
  331.         cmdLen = strlen(commandline);
  332.         for (count = 0; count < cmdLen; count++)
  333.         {
  334.                 uart_write(commandline[count]);
  335.         }
  336.         // printf("[Nrn]Sended:[%s] \r\n", commandline);
  337. }
  338.  
  339. unsigned char getAnswer2(void)
  340. {
  341.         unsigned char readbyte;
  342.         unsigned int curPos = 0;
  343.         do
  344.         {
  345.                 readbyte = uart_readBlock();
  346.         } while (((readbyte == 0x0a) || (readbyte == 0x0d)));
  347.  
  348.         netbuf[curPos] = readbyte;
  349.         curPos++;
  350.         do
  351.         {
  352.                 readbyte = uart_readBlock();
  353.                 netbuf[curPos] = readbyte;
  354.                 curPos++;
  355.         } while (readbyte != 0x0d);
  356.         netbuf[curPos - 1] = 0;
  357.         uart_readBlock(); // 0xa
  358.         // printf("Answer:[%s]\r\n", netbuf);
  359.         // getchar();
  360.         return curPos;
  361. }
  362.  
  363. void espReBoot(void)
  364. {
  365.         unsigned char byte, count;
  366.         uart_setrts(1);
  367.  
  368.         clearStatus();
  369.         printf("Resetting ESP...");
  370.  
  371.         sendcommand("AT+RST");
  372.         count = 0;
  373.  
  374.         do
  375.         {
  376.                 byte = uart_readBlock();
  377.                 if (byte == gotWiFi[count])
  378.                 {
  379.                         count++;
  380.                 }
  381.                 else
  382.                 {
  383.                         count = 0;
  384.                 }
  385.         } while (count < strlen(gotWiFi));
  386.         uart_readBlock(); // CR
  387.         uart_readBlock(); // LF
  388.         clearStatus();
  389.         printf("Reset complete.");
  390.         sendcommand("ATE0");
  391.         do
  392.         {
  393.                 byte = uart_readBlock();
  394.         } while (byte != 'K'); // OK
  395.         uart_readBlock(); // CR
  396.         uart_readBlock(); // LN
  397.         // puts("ATE0 Answer:[OK]");
  398.         sendcommand("AT+CIPCLOSE");
  399.         getAnswer2();
  400.         sendcommand("AT+CIPDINFO=0");
  401.         getAnswer2();
  402.         sendcommand("AT+CIPMUX=0");
  403.         getAnswer2();
  404.         sendcommand("AT+CIPSERVER=0");
  405.         getAnswer2();
  406.         sendcommand("AT+CIPRECVMODE=0");
  407.         getAnswer2();
  408. }
  409.  
  410. int recvHead(void)
  411. {
  412.         unsigned char byte, dataRead;
  413.         int todo = 0, count = 0, countErr = 0;
  414.         const char closed[] = "CLOSED";
  415.         const char error[] = "ERROR";
  416.         //+IPD<,length>:<data>
  417.         //+CIPRECVDATA:<actual_len>,<data>
  418.         dataRead = 0;
  419.         do
  420.         {
  421.                 byte = uart_readBlock();
  422.                 //printf("[%c]", byte);
  423.  
  424.                 if (byte == closed[count])
  425.                 {
  426.                         count++;
  427.                 }
  428.                 else
  429.                 {
  430.                         count = 0;
  431.                 }
  432.  
  433.                 if (byte == error[countErr])
  434.                 {
  435.                         countErr++;
  436.                 }
  437.                 else
  438.                 {
  439.                         countErr = 0;
  440.                 }
  441.                 if ((count == strlen(closed)) || (countErr == strlen(error)))
  442.                 {
  443.                         //uart_readBlock(); // CR
  444.                         //uart_readBlock(); // LF
  445.                         return todo;
  446.                 }
  447.         } while (byte != ',');
  448.  
  449.         do
  450.         {
  451.                 byte = uart_readBlock();
  452.                 netbuf[dataRead] = byte;
  453.                 dataRead++;
  454.         } while (byte != ':');
  455.         todo = atoi(netbuf);
  456.         // <actual_len>
  457.         // printf("recvHead(); todo = %d   ", todo);
  458.  
  459.         return todo;
  460. }
  461.  
  462. void loadEspConfig(void)
  463. {
  464.         unsigned char curParam[256];
  465.         unsigned char res;
  466.         FILE *espcom;
  467.         OS_SETSYSDRV();
  468.         OS_CHDIR("../ini");
  469.         espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  470.         if (((int)espcom) & 0xff)
  471.         {
  472.                 clearStatus();
  473.                 printf("espcom.ini opening error");
  474.                 return;
  475.         }
  476.         OS_READHANDLE(curParam, espcom, 250);
  477.         OS_CLOSEHANDLE(espcom);
  478.  
  479.         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);
  480.         puts("Config loaded:");
  481.  
  482.         if (comType == 1)
  483.         {
  484.                 puts("     Controller IO port: 0x55fe");
  485.         }
  486.         else
  487.         {
  488.                 printf("     RBR_THR:0x%4x     IER    :0x%4x\r\n     IIR_FCR:0x%4x     LCR    :0x%4x\r\n", RBR_THR, IER, IIR_FCR, LCR);
  489.                 printf("     MCR    :0x%4x     LSR    :0x%4x\r\n     MSR    :0x%4x     SR     :0x%4x\r\n", MCR, LSR, MSR, SR);
  490.         }
  491.         printf("     DIV    :%u    TYPE    :%u    ESP    :%u ", divider, comType, espType);
  492.         switch (comType)
  493.         {
  494.         case 0:
  495.                 puts("(16550 like w/o AFC)");
  496.                 break;
  497.         case 1:
  498.                 puts("(ATM Turbo 2+)");
  499.                 break;
  500.         case 2:
  501.                 puts("(16550 with AFC)");
  502.                 break;
  503.         case 3:
  504.                 puts("(ATM2 IO Card)");
  505.                 break;
  506.         default:
  507.                 puts("(Unknown type)");
  508.                 break;
  509.         }
  510. }
  511. ////////////////////////ESP32 PROCEDURES//////////////////////