1 ;---------------------------------------------------------------------------;
\r
2 ; Software implemented UART module ;
\r
3 ; (C)ChaN, 2005 (http://elm-chan.org/) ;
\r
4 ;---------------------------------------------------------------------------;
\r
7 ; 1MHz 2MHz 4MHz 6MHz 8MHz 10MHz 12MHz 16MHz 20MHz
\r
8 ; 2.4kbps 138 - - - - - - - -
\r
9 ; 4.8kbps 68 138 - - - - - - -
\r
10 ; 9.6kbps 33 68 138 208 - - - - -
\r
11 ; 19.2kbps - 33 68 102 138 173 208 - -
\r
12 ; 38.4kbps - - 33 50 68 85 102 138 172
\r
13 ; 57.6kbps - - 21 33 44 56 68 91 114
\r
14 ; 115.2kbps - - - - 21 27 33 44 56
\r
20 #define BPS 44 /* Bit delay. (see above table) */
\r
21 #define BIDIR 0 /* 0:Separated Tx/Rx, 1:Shared Tx/Rx */
\r
23 #define OUT_1 sbi _SFR_IO_ADDR(SUART_OUT_PORT), SUART_OUT_BIT /* Output 1 */
\r
24 #define OUT_0 cbi _SFR_IO_ADDR(SUART_OUT_PORT), SUART_OUT_BIT /* Output 0 */
\r
25 #define SKIP_IN_1 sbis _SFR_IO_ADDR(SUART_IN_PIN), SUART_IN_BIT /* Skip if 1 */
\r
26 #define SKIP_IN_0 sbic _SFR_IO_ADDR(SUART_IN_PIN), SUART_IN_BIT /* Skip if 0 */
\r
34 .macro _MOVW dh,dl, sh,sl
\r
43 .macro _MOVW dh,dl, sh,sl
\r
51 ;---------------------------------------------------------------------------;
\r
52 ; Transmit a byte in serial format of N81
\r
54 ;Prototype: void xmit (uint8_t data);
\r
61 ldi r23, BPS-1 ;Pre-idle time for bidirectional data line
\r
65 in r0, _SFR_IO_ADDR(SREG) ;Save flags
\r
67 com r24 ;C = start bit
\r
68 ldi r25, 10 ;Bit counter
\r
69 cli ;Start critical section
\r
71 1: ldi r23, BPS-1 ;----- Bit transferring loop
\r
72 2: dec r23 ;Wait for a bit time
\r
74 brcs 3f ;MISO = bit to be sent
\r
78 4: lsr r24 ;Get next bit into C
\r
79 dec r25 ;All bits sent?
\r
80 brne 1b ; no, coutinue
\r
82 out _SFR_IO_ADDR(SREG), r0 ;End of critical section
\r
88 ;---------------------------------------------------------------------------;
\r
91 ;Prototype: uint8_t rcvr (void);
\r
97 in r0, _SFR_IO_ADDR(SREG) ;Save flags
\r
99 ldi r24, 0x80 ;Receiving shift reg
\r
100 cli ;Start critical section
\r
102 1: SKIP_IN_1 ;Wait for idle
\r
104 2: SKIP_IN_0 ;Wait for start bit
\r
106 ldi r25, BPS/2 ;Wait for half bit time
\r
110 4: ldi r25, BPS ;----- Bit receiving loop
\r
111 5: dec r25 ;Wait for a bit time
\r
114 SKIP_IN_0 ;Get a data bit into r24.7
\r
116 brcc 4b ;All bits received? no, continue
\r
118 out _SFR_IO_ADDR(SREG), r0 ;End of critical section
\r
123 ; Not wait for start bit. This should be called after detecting start bit.
\r
127 in r0, _SFR_IO_ADDR(SREG) ;Save flags
\r
129 ldi r24, 0x80 ;Receiving shift reg
\r
130 cli ;Start critical section
\r
132 ;1: SKIP_IN_1 ;Wait for idle
\r
134 ;2: SKIP_IN_0 ;Wait for start bit
\r
136 ldi r25, BPS/2 ;Wait for half bit time
\r
140 4: ldi r25, BPS ;----- Bit receiving loop
\r
141 5: dec r25 ;Wait for a bit time
\r
144 SKIP_IN_0 ;Get a data bit into r24.7
\r
146 brcc 4b ;All bits received? no, continue
\r
148 ldi r25, BPS/2 ;Wait for half bit time
\r
151 7: SKIP_IN_1 ;Wait for stop bit
\r
154 out _SFR_IO_ADDR(SREG), r0 ;End of critical section
\r