?login_element?

Subversion Repositories NedoOS

Rev

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

  1. ; This driver works with 16c550 uart that's support AFE
  2.     module Uart
  3. ; Make init shorter and readable:-)
  4.     macro outp port, value
  5.         ld b, port
  6.         ld c, #EF
  7.     ld a, value
  8.     out (c), a
  9.     endm
  10.  
  11. ; Internal port constants
  12. RBR_THR = #F8
  13. IER     = RBR_THR + 1
  14. IIR_FCR = RBR_THR + 2
  15. LCR     = RBR_THR + 3
  16. MCR     = RBR_THR + 4
  17. LSR     = RBR_THR + 5
  18. MSR     = RBR_THR + 6
  19. SR      = RBR_THR + 7
  20.  
  21.  
  22. init:
  23.     IFDEF GZ
  24.     outp MCR,     #0d  // Assert RTS
  25.     outp IIR_FCR, #87  // Enable fifo 8 level, and clear it
  26.     outp LCR,     #83  // 8n1, DLAB=1
  27.     outp RBR_THR, 12  //(divider 12)
  28.     outp IER,     #00  // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  29.  
  30.     outp LCR,     #03 // 8n1, DLAB=0
  31.     outp IER,     #00 // Disable int
  32.     outp MCR,     #2f // Enable AFE
  33.     ret
  34.     ELSE
  35.     outp MCR,     #0d  // Assert RTS
  36.     outp IIR_FCR, #87  // Enable fifo 8 level, and clear it
  37.     outp LCR,     #83  // 8n1, DLAB=1
  38.     outp RBR_THR, 1  // 115200 (divider 1)
  39.     outp IER,     #00  // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  40.  
  41.     outp LCR,     #03 // 8n1, DLAB=0
  42.     outp IER,     #00 // Disable int
  43.     outp MCR,     #2f // Enable AFE
  44.  
  45.     ret
  46.     ENDIF
  47. retry_rec_count_max equ 50 ;ждать 1 байт максимум столько прерываний
  48.    
  49. ; Flag C <- Data available
  50. ; isAvailable:
  51.     ; ld a, LSR
  52.     ; in a, (#EF)
  53.     ; rrca
  54.     ; ret
  55.  
  56. ; Non-blocking read
  57. ; Flag C <- is byte was readen
  58. ; A <- byte
  59. ; read1:
  60.     ; ld a, LSR
  61.     ; in a, (#EF)
  62.     ; rrca
  63.     ; ret nc
  64.     ; ld a, RBR_THR    
  65.     ; in a, (#EF)
  66.     ; scf
  67.     ; ret
  68.  
  69. ; Tries read byte with timeout
  70. ; Flag C <- is byte read
  71. ; A <- byte
  72. read:
  73.         xor a ;4
  74.         ld (#5C78),a ;обнулить счётчик ожидания ;13
  75. .wait
  76.     ld a, LSR
  77.     in a, (#EF)
  78.     rrca
  79.         jr nc, .readW
  80.     ld a, RBR_THR      
  81.     in a, (#EF)
  82.         ret    
  83. .readW 
  84.         ld a,(#5C78)
  85.         cp retry_rec_count_max
  86.         jr c, .wait ;ещё попытка
  87.         xor a ;выключим флаг переноса если время вышло
  88.         ret
  89.        
  90.        
  91.        
  92.  
  93. ; Blocking read
  94. ; A <- Byte
  95. ; readB:
  96.     ; ld a, LSR
  97.     ; in a, (#EF)
  98.     ; rrca
  99.     ; jr nc, readB
  100.         ; ld a, RBR_THR
  101.     ; in a, (#EF)
  102.     ; ret
  103.  
  104. ; A -> byte to send
  105. write:
  106.     push af
  107. .wait
  108.         ld a, LSR
  109.     in a, (#EF)
  110.     and #20
  111.     jr z, .wait
  112.     pop af
  113.         ld b, RBR_THR
  114.         ld c, #EF      
  115.     out (c), a
  116.     ret
  117.  
  118.     endmodule