?login_element?

Subversion Repositories NedoOS

Rev

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

  1. ////////////////////////ESP32 PROCEDURES//////////////////////
  2. void uart_write(unsigned char data)
  3. {
  4.         unsigned char status;
  5.         switch (comType)
  6.         {
  7.         case 0:
  8.         case 2:
  9.                 while ((input(LSR) & 64) == 0)
  10.                 {
  11.                 }
  12.                 output(RBR_THR, data);
  13.                 break;
  14.         case 1:
  15.                 disable_interrupt();
  16.                 do
  17.                 {
  18.                         input(0x55fe);                  // Переход в режим команд
  19.                         status = input(0x42fe); // Команда прочесть статус
  20.                 } while ((status & 64) == 0); // Проверяем 6 бит
  21.  
  22.                 input(0x55fe);                           // Переход в режим команд
  23.                 input(0x03fe);                           // Команда записать в порт
  24.                 input((data << 8) | 0x00fe); // Записываем data в порт
  25.                 enable_interrupt();
  26.                 break;
  27.         }
  28. }
  29.  
  30. void uart_setrts(unsigned char mode)
  31. {
  32.         switch (comType)
  33.         {
  34.         case 0:
  35.                 switch (mode)
  36.                 {
  37.                 case 1:
  38.                         output(MCR, 2);
  39.                         break;
  40.                 case 0:
  41.                         output(MCR, 0);
  42.                         break;
  43.                 default:
  44.                         disable_interrupt();
  45.                         output(MCR, 2);
  46.                         output(MCR, 0);
  47.                         enable_interrupt();
  48.                         break;
  49.                 }
  50.         case 1:
  51.                 switch (mode)
  52.                 {
  53.                 case 1:
  54.                         disable_interrupt();
  55.                         input(0x55fe); // Переход в режим команд
  56.                         input(0x43fe); // Команда установить статус
  57.                         input(0x03fe); // Устанавливаем готовность DTR и RTS
  58.                         enable_interrupt();
  59.                         break;
  60.                 case 0:
  61.                         disable_interrupt();
  62.                         input(0x55fe); // Переход в режим команд
  63.                         input(0x43fe); // Команда установить статус
  64.                         input(0x00fe); // Снимаем готовность DTR и RTS
  65.                         enable_interrupt();
  66.                         break;
  67.                 default:
  68.                         disable_interrupt();
  69.                         input(0x55fe); // Переход в режим команд
  70.                         input(0x43fe); // Команда установить статус
  71.                         input(0x03fe); // Устанавливаем готовность DTR и RTS
  72.                         input(0x55fe); // Переход в режим команд
  73.                         input(0x43fe); // Команда установить статус
  74.                         input(0x00fe); // Снимаем готовность DTR и RTS
  75.                         enable_interrupt();
  76.                         break;
  77.                 }
  78.         case 2:
  79.                 break;
  80.         }
  81. }
  82.  
  83. void uart_init(unsigned char divisor)
  84. {
  85.         switch (comType)
  86.         {
  87.         case 0:
  88.         case 2:
  89.                 output(MCR, 0x00);                // Disable input
  90.                 output(IIR_FCR, 0x87);    // Enable fifo 8 level, and clear it
  91.                 output(LCR, 0x83);                // 8n1, DLAB=1
  92.                 output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  93.                 output(IER, 0x00);                // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  94.                 output(LCR, 0x03);                // 8n1, DLAB=0
  95.                 output(IER, 0x00);                // Disable int
  96.                 output(MCR, 0x2f);                // Enable AFE
  97.                 break;
  98.         case 1:
  99.                 disable_interrupt();
  100.                 input(0x55fe);
  101.                 input(0xc3fe);
  102.                 input((divisor << 8) | 0x00fe);
  103.                 enable_interrupt();
  104.                 break;
  105.         }
  106. }
  107.  
  108. unsigned char uart_hasByte(void)
  109. {
  110.         unsigned char queue;
  111.         switch (comType)
  112.         {
  113.         case 0:
  114.         case 2:
  115.                 return (1 & input(LSR));
  116.         case 1:
  117.                 disable_interrupt();
  118.                 input(0x55fe);             // Переход в режим команд
  119.                 queue = input(0xc2fe); // Получаем количество байт в приемном буфере
  120.                 enable_interrupt();
  121.                 return queue;
  122.         }
  123.         return 255;
  124. }
  125.  
  126. unsigned char uart_read(void)
  127. {
  128.         unsigned char data;
  129.         switch (comType)
  130.         {
  131.         case 0:
  132.         case 2:
  133.                 return input(RBR_THR);
  134.         case 1:
  135.                 disable_interrupt();
  136.                 input(0x55fe);            // Переход в режим команд
  137.                 data = input(0x02fe); // Команда прочесть из порта
  138.                 enable_interrupt();
  139.                 return data;
  140.         }
  141.         return 255;
  142. }
  143.  
  144. unsigned char uart_readBlock(void)
  145. {
  146.         unsigned char data;
  147.         switch (comType)
  148.         {
  149.         case 0:
  150.                 while (uart_hasByte() == 0)
  151.                 {
  152.                         uart_setrts(2);
  153.                 }
  154.                 return input(RBR_THR);
  155.         case 1:
  156.                 while (uart_hasByte() == 0)
  157.                 {
  158.                         uart_setrts(2);
  159.                 }
  160.                 disable_interrupt();
  161.                 input(0x55fe);            // Переход в режим команд
  162.                 data = input(0x02fe); // Команда прочесть из порта
  163.                 enable_interrupt();
  164.                 return data;
  165.         case 2:
  166.                 while (uart_hasByte() == 0)
  167.                 {
  168.                 }
  169.                 return input(RBR_THR);
  170.         }
  171.         return 255;
  172. }
  173.  
  174. void uart_flush(void)
  175. {
  176.         unsigned int count;
  177.         for (count = 0; count < 6000; count++)
  178.         {
  179.                 uart_setrts(1);
  180.                 uart_read();
  181.         }
  182. }
  183.  
  184. void getdataEsp(unsigned int counted)
  185. {
  186.         unsigned int counter;
  187.         for (counter = 0; counter < counted; counter++)
  188.         {
  189.                 netbuf[counter] = uart_readBlock();
  190.         }
  191.         netbuf[counter] = 0;
  192. }
  193.  
  194. void sendcommand(char *commandline)
  195. {
  196.         unsigned int count, cmdLen;
  197.         cmdLen = strlen(commandline);
  198.         for (count = 0; count < cmdLen; count++)
  199.         {
  200.                 uart_write(commandline[count]);
  201.         }
  202.         uart_write('\r');
  203.         uart_write('\n');
  204.         //printf("Sended:[%s] \r\n", commandline);
  205. }
  206.  
  207. void sendcommandNrn(char *commandline)
  208. {
  209.         unsigned int count, cmdLen;
  210.         cmdLen = strlen(commandline);
  211.         for (count = 0; count < cmdLen; count++)
  212.         {
  213.                 uart_write(commandline[count]);
  214.         }
  215.         //printf("Sended:[%s] \r\n", commandline);
  216. }
  217.  
  218.  
  219.  
  220.  
  221. unsigned char getAnswer2(void)
  222. {
  223.         unsigned char readbyte;
  224.         unsigned int curPos = 0;
  225.         do
  226.         {
  227.                 readbyte = uart_readBlock();
  228.                 // putdec(readbyte);
  229.         } while (((readbyte == 0x0a) || (readbyte == 0x0d)));
  230.  
  231.         netbuf[curPos] = readbyte;
  232.         curPos++;
  233.         do
  234.         {
  235.                 readbyte = uart_readBlock();
  236.                 netbuf[curPos] = readbyte;
  237.                 curPos++;
  238.         } while (readbyte != 0x0d);
  239.         netbuf[curPos - 1] = 0;
  240.         uart_readBlock(); // 0xa
  241.         //printf("Answer:[%s]\r\n", netbuf);
  242.         //    getchar();
  243.         return curPos;
  244. }
  245.  
  246. void espReBoot(void)
  247. {
  248.         unsigned char byte, count;
  249.         uart_flush();
  250.         sendcommand("AT+RST");
  251.         clearStatus();
  252.         printf("Resetting ESP...");
  253.         count = 0;
  254.  
  255.         do
  256.         {
  257.                 byte = uart_readBlock();
  258.                 if (byte == gotWiFi[count])
  259.                 {
  260.                         count++;
  261.                 }
  262.                 else
  263.                 {
  264.                         count = 0;
  265.                 }
  266.         } while (count < strlen(gotWiFi));
  267.         uart_readBlock(); // CR
  268.         uart_readBlock(); // LF
  269.         clearStatus();
  270.         printf("Reset complete.");
  271.  
  272.         sendcommand("ATE0");
  273.         do
  274.         {
  275.                 byte = uart_readBlock();
  276.         } while (byte != 'K'); // OK
  277.         // puts("ATE0 Answer:[OK]");
  278.         uart_readBlock(); // CR
  279.         uart_readBlock(); // LN
  280.  
  281.         sendcommand("AT+CIPCLOSE");
  282.         getAnswer2();
  283.         sendcommand("AT+CIPDINFO=0");
  284.         getAnswer2();
  285.         sendcommand("AT+CIPMUX=0");
  286.         getAnswer2();
  287.         sendcommand("AT+CIPSERVER=0");
  288.         getAnswer2();
  289.         sendcommand("AT+CIPRECVMODE=0");
  290.         getAnswer2();
  291. }
  292.  
  293. unsigned int recvHead(void)
  294. {
  295.         unsigned char byte, dataRead = 0;
  296.         unsigned int loaded, count = 0;
  297.         unsigned char closed[] ="CLOSED" ;
  298.         do
  299.         {
  300.                 byte = uart_readBlock();
  301.                 if (byte == closed[count])
  302.                 {
  303.                         count++;
  304.                 }
  305.                 else
  306.                 {
  307.                         count = 0;
  308.                 }
  309.         if (count == strlen(closed))
  310.         {
  311.                 return 0;
  312.         }
  313.  
  314.         } while (byte != ',');
  315.  
  316.         dataRead = 0;
  317.         do
  318.         {
  319.                 byte = uart_readBlock();
  320.                 netbuf[dataRead] = byte;
  321.                 dataRead++;
  322.         } while (byte != ':');
  323.         netbuf[dataRead] = 0;
  324.         loaded = atoi(netbuf); // <actual_len>
  325.         //printf("\r\n loaded %u\r\n", loaded);
  326.         return loaded;
  327. }
  328.  
  329. void loadEspConfig(void)
  330. {
  331.         unsigned char curParam[256];
  332.         unsigned char res;
  333.         FILE *espcom;
  334.         OS_SETSYSDRV();
  335.         OS_CHDIR("../ini");
  336.         espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  337.         if (((int)espcom) & 0xff)
  338.         {
  339.                 clearStatus();
  340.                 printf("espcom.ini opening error");
  341.                 return;
  342.         }
  343.  
  344.         OS_READHANDLE(curParam, espcom, 256);
  345.  
  346.         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);
  347.         puts("Config loaded:");
  348.         if (comType == 1)
  349.         {
  350.                 puts("     Controller base port: 0x55fe");
  351.         }
  352.         else
  353.         {
  354.                 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);
  355.                 printf("     MCR    :0x%4x     LSR    :0x%4x\r\n     MSR    :0x%4x     SR     :0x%4x\r\n", MCR, LSR, MSR, SR);
  356.         }
  357.         printf("     DIV    :%u    TYPE    :%u    ESP    :%u ", divider, comType, espType);
  358.         switch (comType)
  359.         {
  360.         case 0:
  361.                 puts("(16550 like w/o AFC)");
  362.                 break;
  363.         case 1:
  364.                 puts("(ATM Turbo 2+)");
  365.                 break;
  366.         case 2:
  367.                 puts("(16550 with AFC)");
  368.         default:
  369.                 puts("(Unknown type)");
  370.                 break;
  371.         }
  372. }
  373. ////////////////////////ESP32 PROCEDURES//////////////////////