?login_element?

Subversion Repositories NedoOS

Rev

Rev 1868 | Rev 2099 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.     MODULE Wifi
  2. bytes_avail dw 0
  3. buffer_pointer dw 0
  4. closed db 1
  5. ; Initialize Wifi chip to work
  6. init:
  7.    
  8.     ld hl, .uartIniting : call TextMode.printZ
  9.     call Uart.init
  10.     ld hl, .chipIniting : call TextMode.printZ
  11.    
  12.     EspCmdOkErr "ATE0"
  13.     jp c, .initError
  14.  
  15. ; Reading auth.pwd and send it to ESP
  16.     ld hl, creds, b, Dos.FMODE_READ : call Dos.fopen
  17.     push af
  18.     ld hl,outputBuffer2, bc, 255 : call Dos.fread
  19.     pop af
  20.     call Dos.fclose
  21.    
  22.     ld hl, .doneInit1 : call TextMode.printZ
  23.    
  24.     ld hl,outputBuffer2
  25.     call espSendT
  26.     ld a, 13 : call Uart.write
  27.     ld a, 10 : call Uart.write
  28.     call checkOkErr
  29.     jp c, .initError    
  30. ;    
  31.         EspCmdOkErr "AT+CIPSERVER=0"
  32.     EspCmdOkErr "AT+CIPCLOSE" ; Close if there some connection was. Don't care about result
  33.     EspCmdOkErr "AT+CIPMUX=0" ; Single connection mode
  34.     jp c, .initError
  35.    
  36.     EspCmdOkErr "AT+CIPDINFO=0" ; Disable additional info
  37.     jp c, .initError
  38.  
  39.     ld hl, .doneInit : call TextMode.printZ
  40.    
  41.     or a
  42.     ret
  43. .initError
  44.     ld hl, .errMsg : call DialogBox.msgBox
  45.     scf
  46.     ret
  47. .errMsg      db "WiFi chip init failed!", CRLF, 0
  48. .uartIniting db "Uart initing...", CRLF, 0
  49. .chipIniting db "Chip initing...", CRLF, 0
  50. .doneInit    db "Done!",CRLF, 0
  51. .doneInit1   db "Sending auth.pwd to ESP",CRLF, 0
  52.     IFNDEF PROXY  
  53. ; HL - host pointer in gopher row
  54. ; DE - port pointer in gopher row
  55. openTCP:
  56.     push de
  57.     push hl
  58.     EspCmdOkErr "AT+CIPCLOSE" ; Don't care about result. Just close if it didn't happens before
  59.     EspSend 'AT+CIPSTART="TCP","'
  60.     pop hl
  61.     call espSendT
  62.     EspSend '",'
  63.     pop hl
  64.     call espSendT
  65.     ld a, 13 : call Uart.write
  66.     ld a, 10 : call Uart.write
  67.     xor a : ld (closed), a
  68.     jp checkOkErr
  69.  
  70. continue:
  71.     ret
  72.     ENDIF
  73.  
  74.  
  75.  
  76. checkOkErr:
  77.     call Uart.read
  78.     cp 'O' : jp z, .okStart ; OK
  79.     cp 'E' : jp z, .errStart ; ERROR
  80.     cp 'F' : jp z, .failStart ; FAIL
  81.     jp checkOkErr
  82. .okStart
  83.     call Uart.read : cp 'K' : jp nz, checkOkErr
  84.     call Uart.read : cp 13  : jp nz, checkOkErr
  85.     call .flushToLF
  86.     or a
  87.     ret
  88. .errStart
  89.     call Uart.read : cp 'R' : jp nz, checkOkErr
  90.     call Uart.read : cp 'R' : jp nz, checkOkErr
  91.     call Uart.read : cp 'O' : jp nz, checkOkErr
  92.     call Uart.read : cp 'R' : jp nz, checkOkErr
  93.     call .flushToLF
  94.     scf
  95.     ret
  96. .failStart
  97.     call Uart.read : cp 'A' : jp nz, checkOkErr
  98.     call Uart.read : cp 'I' : jp nz, checkOkErr
  99.     call Uart.read : cp 'L' : jp nz, checkOkErr
  100.     call .flushToLF
  101.     scf
  102.     ret
  103. .flushToLF
  104.     call Uart.read
  105.     cp 10 : jp nz, .flushToLF
  106.     ret
  107.  
  108. ; Send buffer to UART
  109. ; HL - buff
  110. ; E - count
  111. espSend:
  112.     ld a, (hl) : call Uart.write
  113.     inc hl
  114.     dec e
  115.     jp nz, espSend
  116.     ret
  117.  
  118. ; HL - string that ends with one of the terminator(CR/LF/TAB/NULL)
  119. espSendT:
  120.     ld a, (hl)
  121.  
  122.     and a : ret z
  123.     cp 9 : ret z
  124.     cp 13 : ret z
  125.     cp 10 : ret z
  126.    
  127.     call Uart.write
  128.     inc hl
  129.     jp espSendT
  130.  
  131. ; HL - stringZ to send
  132. ; Adds CR LF
  133. tcpSendZ:
  134.     push hl
  135.     EspSend "AT+CIPSEND="
  136.     pop de : push de
  137.     call strLen
  138.     inc hl : inc hl ; +CRLF
  139.     call hlToNumEsp
  140.     ld a, 13 : call Uart.write
  141.     ld a, 10 : call Uart.write
  142.     call checkOkErr : ret c
  143. .wait
  144.     call Uart.read : cp '>' : jp nz, .wait
  145.     pop hl
  146. .loop
  147.     ld a, (hl) : and a : jp z, .exit
  148.     call Uart.write
  149.     inc hl
  150.     jp .loop
  151. .exit
  152.     ld a, 13 : call Uart.write
  153.     ld a, 10 : call Uart.write
  154.     jp checkOkErr
  155.  
  156. getPacket:
  157.     call Uart.read
  158.     cp '+' : jp z, .ipdBegun    ; "+IPD," packet
  159.     cp 'O' : jp z, .closedBegun ; It enough to check "OSED\n" :-)
  160.     jp getPacket
  161. .closedBegun
  162.     call Uart.read : cp 'S' : jp nz, .closedBegun
  163.     call Uart.read : cp 'E' : jp nz, .closedBegun
  164.     call Uart.read : cp 'D' : jp nz, .closedBegun
  165.     call Uart.read : cp 13 : jp nz, .closedBegun
  166.     ld a, 1, (closed), a
  167.     ret
  168. .ipdBegun
  169.     call Uart.read : cp 'I' : jp nz, .ipdBegun
  170.     call Uart.read : cp 'P' : jp nz, .ipdBegun
  171.     call Uart.read : cp 'D' : jp nz, .ipdBegun
  172.     call Uart.read  ;Comma
  173.     call .count_ipd_lenght : ld (bytes_avail), hl
  174.     ld de, hl
  175.     ld hl, (buffer_pointer)
  176. .readp
  177.     ld a, h : cp #ff : jp nc, .skipbuff
  178.     call Uart.read
  179.     ld (hl), a
  180.     dec de
  181.     inc hl
  182.     ld a, d : or e : jp nz, .readp
  183.     ld (buffer_pointer), hl
  184.     ret
  185. .skipbuff
  186.     call Uart.read
  187.     dec de : ld a, d : or e : jp nz, .skipbuff
  188.     ret
  189. .count_ipd_lenght
  190.                 ld hl,0                 ; count lenght
  191. .cil1   push  hl
  192.         call Uart.read
  193.         pop hl
  194.                 cp ':'
  195.         ret z
  196.                 sub 0x30
  197.         ld c,l
  198.         ld b,h
  199.         add hl,hl
  200.         add hl,hl
  201.         add hl,bc
  202.         add hl,hl
  203.         ld c,a
  204.         ld b,0
  205.         add hl,bc
  206.                 jp .cil1
  207.  
  208. ; Based on: https://wikiti.brandonw.net/index.php?title=Z80_Routines:Other:DispHL
  209. ; HL - number
  210. ; It will be written to UART
  211. hlToNumEsp:
  212.         ld      bc,-10000
  213.         call    .n1
  214.         ld      bc,-1000
  215.         call    .n1
  216.         ld      bc,-100
  217.         call    .n1
  218.         ld      c,-10
  219.         call    .n1
  220.         ld      c,-1
  221. .n1     ld      a,'0'-1
  222. .n2     inc     a
  223.         add     hl,bc
  224.         jp      c, .n2
  225.         sbc     hl,bc
  226.     push bc
  227.         call Uart.write
  228.     pop bc
  229.     ret
  230. flushToLF1
  231.     call Uart.read
  232.     cp 10 : jp nz, flushToLF1
  233.     ret
  234.     ENDMODULE