Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

suart.S 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;---------------------------------------------------------------------------;
  2. ; Software implemented UART module ;
  3. ; (C)ChaN, 2005 (http://elm-chan.org/) ;
  4. ;---------------------------------------------------------------------------;
  5. ; Bit rate settings:
  6. ;
  7. ; 1MHz 2MHz 4MHz 6MHz 8MHz 10MHz 12MHz 16MHz 20MHz
  8. ; 2.4kbps 138 - - - - - - - -
  9. ; 4.8kbps 68 138 - - - - - - -
  10. ; 9.6kbps 33 68 138 208 - - - - -
  11. ; 19.2kbps - 33 68 102 138 173 208 - -
  12. ; 38.4kbps - - 33 50 68 85 102 138 172
  13. ; 57.6kbps - - 21 33 44 56 68 91 114
  14. ; 115.2kbps - - - - 21 27 33 44 56
  15. .nolist
  16. #include <avr/io.h>
  17. .list
  18. #define BPS 102 /* Bit delay. (see above table) */
  19. #define BIDIR 0 /* 0:Separated Tx/Rx, 1:Shared Tx/Rx */
  20. #define OUT_1 sbi _SFR_IO_ADDR(SUART_OUT_PORT), SUART_OUT_BIT /* Output 1 */
  21. #define OUT_0 cbi _SFR_IO_ADDR(SUART_OUT_PORT), SUART_OUT_BIT /* Output 0 */
  22. #define SKIP_IN_1 sbis _SFR_IO_ADDR(SUART_IN_PIN), SUART_IN_BIT /* Skip if 1 */
  23. #define SKIP_IN_0 sbic _SFR_IO_ADDR(SUART_IN_PIN), SUART_IN_BIT /* Skip if 0 */
  24. #ifdef SPM_PAGESIZE
  25. .macro _LPMI reg
  26. lpm \reg, Z+
  27. .endm
  28. .macro _MOVW dh,dl, sh,sl
  29. movw \dl, \sl
  30. .endm
  31. #else
  32. .macro _LPMI reg
  33. lpm
  34. mov \reg, r0
  35. adiw ZL, 1
  36. .endm
  37. .macro _MOVW dh,dl, sh,sl
  38. mov \dl, \sl
  39. mov \dh, \sh
  40. .endm
  41. #endif
  42. ;---------------------------------------------------------------------------;
  43. ; Transmit a byte in serial format of N81
  44. ;
  45. ;Prototype: void xmit (uint8_t data);
  46. ;Size: 16 words
  47. .global xmit
  48. .func xmit
  49. xmit:
  50. #if BIDIR
  51. ldi r23, BPS-1 ;Pre-idle time for bidirectional data line
  52. 5: dec r23 ;
  53. brne 5b ;/
  54. #endif
  55. in r0, _SFR_IO_ADDR(SREG) ;Save flags
  56. com r24 ;C = start bit
  57. ldi r25, 10 ;Bit counter
  58. cli ;Start critical section
  59. 1: ldi r23, BPS-1 ;----- Bit transferring loop
  60. 2: dec r23 ;Wait for a bit time
  61. brne 2b ;/
  62. brcs 3f ;MISO = bit to be sent
  63. OUT_1 ;
  64. 3: brcc 4f ;
  65. OUT_0 ;/
  66. 4: lsr r24 ;Get next bit into C
  67. dec r25 ;All bits sent?
  68. brne 1b ; no, coutinue
  69. out _SFR_IO_ADDR(SREG), r0 ;End of critical section
  70. ret
  71. .endfunc
  72. ;---------------------------------------------------------------------------;
  73. ; Receive a byte
  74. ;
  75. ;Prototype: uint8_t rcvr (void);
  76. ;Size: 19 words
  77. .global rcvr
  78. .func rcvr
  79. rcvr:
  80. in r0, _SFR_IO_ADDR(SREG) ;Save flags
  81. ldi r24, 0x80 ;Receiving shift reg
  82. cli ;Start critical section
  83. 1: SKIP_IN_1 ;Wait for idle
  84. rjmp 1b
  85. 2: SKIP_IN_0 ;Wait for start bit
  86. rjmp 2b
  87. ldi r25, BPS/2 ;Wait for half bit time
  88. 3: dec r25
  89. brne 3b
  90. 4: ldi r25, BPS ;----- Bit receiving loop
  91. 5: dec r25 ;Wait for a bit time
  92. brne 5b ;/
  93. lsr r24 ;Next bit
  94. SKIP_IN_0 ;Get a data bit into r24.7
  95. ori r24, 0x80
  96. brcc 4b ;All bits received? no, continue
  97. out _SFR_IO_ADDR(SREG), r0 ;End of critical section
  98. ret
  99. .endfunc
  100. ; Not wait for start bit. This should be called after detecting start bit.
  101. .global recv
  102. .func recv
  103. recv:
  104. in r0, _SFR_IO_ADDR(SREG) ;Save flags
  105. ldi r24, 0x80 ;Receiving shift reg
  106. cli ;Start critical section
  107. ;1: SKIP_IN_1 ;Wait for idle
  108. ; rjmp 1b
  109. ;2: SKIP_IN_0 ;Wait for start bit
  110. ; rjmp 2b
  111. ldi r25, BPS/2 ;Wait for half bit time
  112. 3: dec r25
  113. brne 3b
  114. 4: ldi r25, BPS ;----- Bit receiving loop
  115. 5: dec r25 ;Wait for a bit time
  116. brne 5b ;/
  117. lsr r24 ;Next bit
  118. SKIP_IN_0 ;Get a data bit into r24.7
  119. ori r24, 0x80
  120. brcc 4b ;All bits received? no, continue
  121. ldi r25, BPS/2 ;Wait for half bit time
  122. 6: dec r25
  123. brne 6b
  124. 7: SKIP_IN_1 ;Wait for stop bit
  125. rjmp 7b
  126. out _SFR_IO_ADDR(SREG), r0 ;End of critical section
  127. ret
  128. .endfunc