?login_element?

Subversion Repositories NedoOS

Rev

Blame | 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. init:
  22.     outp MCR,     #0d  // Assert RTS
  23.     outp IIR_FCR, #87  // Enable fifo 8 level, and clear it
  24.     outp LCR,     #83  // 8n1, DLAB=1
  25.     outp RBR_THR, #01  // 115200 (divider 1)
  26.     outp IER,     #00  // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  27.  
  28.     outp LCR,     #03 // 8n1, DLAB=0
  29.     outp IER,     #00 // Disable int
  30.     outp MCR,     #2f // Enable AFE
  31.     ret
  32.        
  33. retry_rec_count_max equ 50 ;ждать 1 байт максимум столько прерываний
  34.    
  35. ; Flag C <- Data available
  36. ; isAvailable:
  37.     ; ld a, LSR
  38.     ; in a, (#EF)
  39.     ; rrca
  40.     ; ret
  41.  
  42. ; Non-blocking read
  43. ; Flag C <- is byte was readen
  44. ; A <- byte
  45. ; read1:
  46.     ; ld a, LSR
  47.     ; in a, (#EF)
  48.     ; rrca
  49.     ; ret nc
  50.     ; ld a, RBR_THR    
  51.     ; in a, (#EF)
  52.     ; scf
  53.     ; ret
  54.  
  55. ; Tries read byte with timeout
  56. ; Flag C <- is byte read
  57. ; A <- byte
  58. read:
  59.         xor a ;4
  60.         ld (#5C78),a ;обнулить счётчик ожидания ;13
  61. .wait
  62.     ld a, LSR
  63.     in a, (#EF)
  64.     rrca
  65.         jr nc, .readW
  66.     ld a, RBR_THR      
  67.     in a, (#EF)
  68.         ret    
  69. .readW 
  70.         ld a,(#5C78)
  71.         cp retry_rec_count_max
  72.         jr c, .wait ;ещё попытка
  73.         xor a ;выключим флаг переноса если время вышло
  74.         ret
  75.        
  76.        
  77.        
  78.  
  79. ; Blocking read
  80. ; A <- Byte
  81. ; readB:
  82.     ; ld a, LSR
  83.     ; in a, (#EF)
  84.     ; rrca
  85.     ; jr nc, readB
  86.         ; ld a, RBR_THR
  87.     ; in a, (#EF)
  88.     ; ret
  89.  
  90. ; A -> byte to send
  91. write:
  92.     push af
  93. .wait
  94.         ld a, LSR
  95.     in a, (#EF)
  96.     and #20
  97.     jr z, .wait
  98.     pop af
  99.         ld b, RBR_THR
  100.         ld c, #EF      
  101.     out (c), a
  102.     ret
  103.  
  104.     endmodule