tari

HDLP

Jul 31st, 2010
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;Simple half-duplex communication:
  2. ;;  Bit values are sent inverted, so they come out as the literal bit on the
  3. ;;      other end (write 1 to pull a line low).
  4. ;;  Bytes are clocked in MSB first
  5. ;;  Sample on falling edge
  6.  
  7. ;Should be enough even when linking 16 MHz->6 MHz
  8. #define HDLP_SEND_BITTIME 16
  9. ;Really loose timing here since we can afford to
  10. #define HDLP_RECV_TIMEOUT 256
  11.  
  12. .module hdlp_sendCByte
  13. ;;hdlp_sendCByte: send the byte in C via HDLP
  14. ;;Inputs:
  15. ;;  C=byte to send
  16. ;;Outputs:
  17. ;;  None
  18. ;;Modifies:
  19. ;;  AF, B
  20. ;;Errors:
  21. ;;  None
  22. hdlp_sendCByte:
  23.     ld b,8
  24. _bitLoop:
  25.     ;Write bit (provide time to stabilize before clocking out)
  26.     xor a
  27.     rlc c
  28.     rla
  29.     xor 1
  30.     out (0),a
  31.     ;Clock out
  32.     set 1,a
  33.     out (0),a
  34.     ;Wait a bit to ensure reception
  35.     ld a,16    
  36. _bitStall:
  37.     dec a
  38.     jr nz,_bitStall
  39.     ;Release both lines
  40.     xor a
  41.     out (0),a
  42.     ;Next bit or done
  43.     djnz _bitLoop
  44.     or a
  45.     ret         ;Clean exit
  46. .endmodule
  47.  
  48. .module hdlp_recvCByte
  49. ;;hdlp_recvCByte: recieve a byte via HDLP
  50. ;;Outputs:
  51. ;;  C=byte recieved
  52. ;;Modifies:
  53. ;;  AF,BC,DE
  54. ;;Errors:
  55. ;;  
  56. hdlp_recvCByte:
  57.     ld b,8
  58.     ld de,HDLP_RECV_TIMEOUT
  59. _waitClock:     ;Wait for clock to go low
  60.     in a,(0)
  61.     bit 1,a             ;Not in (c) as a safety net against odd port behaviour
  62.     jr z,_bitRotate
  63.     dec de
  64.     ld a,d
  65.     or e
  66.     jr nz,_waitClock
  67.     jr hdlp_timeout     ;Fail out
  68.     ;Rotate bit into result
  69. _bitRotate
  70.     rra
  71.     rl c
  72.     ;Wait for clock high again
  73.     ;(guard against spurious reception from quick re-entry)
  74.     ld de,HDLP_RECV_TIMEOUT
  75. _waitClockH:
  76.     in a,(0)
  77.     bit 1,a
  78.     jr nz,_bitLoopback
  79.     ;Check timeout
  80.     dec de
  81.     ld a,d
  82.     or e
  83.     jr nz,_waitClockH
  84. hdlp_timeout:
  85.     ErrorOut(eLink,eLink_Timeout)
  86. _bitLoopback:
  87.     djnz _waitClock
  88.     or a
  89.     ret         ;clean exit
  90. .endmodule
Add Comment
Please, Sign In to add comment