1 /*-------------------------------------------------------------------------
2 ser_ir_cts_rts.c - source file for serial routines
4 Copyright (C) 1999, Josef Wolf <jw AT raven.inka.de>
7 1.0 Bela Torok <bela.torok.kssg.ch> Jul. 2000
8 RTS / CTS protocol added
10 This library is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this library; see the file COPYING. If not, write to the
22 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
25 As a special exception, if you link this library with other files,
26 some of which are compiled with SDCC, to produce an executable,
27 this library does not by itself cause the resulting executable to
28 be covered by the GNU General Public License. This exception does
29 not however invalidate any other reasons why the executable file
30 might be covered by the GNU General Public License.
31 -------------------------------------------------------------------------*/
33 /* This file implements a serial interrupt handler and its supporting
34 * routines. Compared with the existing serial.c and _ser.c it has
35 * following advantages:
36 * - You can specify arbitrary buffer sizes (umm, up to 255 bytes),
37 * so it can run on devices with _little_ memory like at89cx051.
38 * - It won't overwrite characters which already are stored in the
39 * receive-/transmit-buffer.
42 /* BUG: those definitions (and the #include) should be set dynamically
43 * (while linking or at runtime) to make this file a _real_ library.
46 /* RTS/CTS protocol howto:
49 Shematic of cable for RTS/CTS protocol (B. Torok - Jun. 2000)
51 <- DB9 female connector -><- RS232 driver/receiver -><- 8051 system ->
52 connect to PC e.g. MAX232
59 Transmitters/Receivers
61 Pin2-----------------------------<<<-------------------TXD
64 Pin3----------------------------->>>-------------------RXD
67 Pin5---------------------------------------------------GND
70 Pin6---Pin8----------------------<<<-------------------CTS (see #define CTS)
73 Pin7----------------------------->>>-------------------RTS (see #define RTS)
81 #define RXBUFLEN 18 // The minimum rx buffer size for safe communications
82 // is 17. (The UART in the PC has a 16-byte FIFO.)
83 // TXBUFLEN & RXBUFLEN can be highher if rxbuf[] and txbuf[] is in xdata, max size is limited to 256!
89 #define CTS P3_6 // CTS & RTS can be assigned to any free pins
92 // You might want to specify idata, pdata or xdata for the buffers
93 static unsigned char rxbuf
[RXBUFLEN
], txbuf
[TXBUFLEN
];
94 static unsigned char rxcnt
, txcnt
, rxpos
, txpos
;
101 rxcnt
= txcnt
= rxpos
= txpos
= 0; // init buffers
103 SCON
= 0x50; // mode 1 - 8-bit UART
104 PCON
|= 0x80; // SMOD = 1;
105 TMOD
&= 0x0f; // use timer 1
107 // TL1 = TH1 = 256 - 104; // 600bps with 12 MHz crystal
108 // TL1 = TH1 = 256 - 52; // 1200bps with 12 MHz crystal
109 // TL1 = TH1 = 256 - 26; // 2400bps with 12 MHz crystal
110 TL1
= TH1
= 256 - 13; // 4800bps with 12 MHz crystal
112 TR1
= 1; // Enable timer 1
119 ser_handler (void) interrupt (4)
123 /* don't overwrite chars already in buffer */
124 if(rxcnt
< RXBUFLEN
) rxbuf
[(unsigned char)(rxpos
+ rxcnt
++) % RXBUFLEN
] = SBUF
;
125 if(rxcnt
>= (RXBUFLEN
- THRESHOLD
)) CTS
= DISABLE
;
130 if (busy
= txcnt
) { // Assignment, _not_ comparison!
132 SBUF
= txbuf
[txpos
++];
133 if(txpos
>= TXBUFLEN
) txpos
= 0;
139 ser_putc (unsigned char c
)
141 while(txcnt
>= TXBUFLEN
); // wait for room in buffer
143 while(RTS
== DISABLE
);
147 txbuf
[(unsigned char)(txpos
+ txcnt
++) % TXBUFLEN
] = c
;
160 while (!rxcnt
) { // wait for a character
167 if (rxpos
>= RXBUFLEN
) rxpos
= 0;
175 ser_puts (unsigned char *s
)
179 if (c
== '\n') ser_putc('\r');
186 ser_gets (unsigned char *s
, unsigned char len
)
188 unsigned char pos
, c
;
193 if (c
== '\r') continue; // discard CR's
195 if (c
== '\n') break; // NL terminates
197 s
[pos
] = '\0'; // terminate string
203 return TXBUFLEN
- txcnt
;