* device/include/z180.h,
[fw/sdcc] / device / lib / ser_ir_cts_rts.c
1 /*-------------------------------------------------------------------------
2   ser_ir.c - source file for serial routines
3
4   Written By - Josef Wolf <jw@raven.inka.de> (1999)
5
6   Revisions:
7   1.0  Bela Torok <bela.torok.kssg.ch> Jul. 2000
8   RTS / CTS protocol added
9
10   This program 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
13   later version.
14
15   This program 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.
19
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24   In other words, you are welcome to use, share and improve this program.
25   You are forbidden to forbid anyone else to use, share and improve
26   what you give them.   Help stamp out software-hoarding!
27
28 -------------------------------------------------------------------------*/
29
30 /* This file implements a serial interrupt handler and its supporting
31 * routines. Compared with the existing serial.c and _ser.c it has
32 * following advantages:
33 * - You can specify arbitrary buffer sizes (umm, up to 255 bytes),
34 *   so it can run on devices with _little_ memory like at89cx051.
35 * - It won't overwrite characters which already are stored in the
36 *   receive-/transmit-buffer.
37 * - It checks receiver first to minimize probability for overruns
38 *   in the serial receiver.
39 */
40
41 /* BUG: those definitions (and the #include) should be set dynamically
42 * (while linking or at runtime) to make this file a _real_ library.
43 */
44
45 /* RTS/CTS protocol howto:
46
47
48    Shematic of cable for RTS/CTS protocol (B. Torok - Jun. 2000)
49
50 <- DB9 female connector -><- RS232 driver/receiver -><- 8051 system ->
51    connect to PC             e.g. MAX232
52
53                              RS232         TTL
54                              level         level
55
56    DCD    DTR                
57    Pin1---Pin4               
58                             Transmitters/Receivers
59    RXD
60    Pin2-----------------------------<<<-------------------TXD
61
62    TXD
63    Pin3----------------------------->>>-------------------RXD
64
65    GND
66    Pin5---------------------------------------------------GND
67
68    DSR    CTS
69    Pin6---Pin8----------------------<<<-------------------CTS (see #define CTS)
70
71    RTS
72    Pin7----------------------------->>>-------------------RTS (see #define RTS)
73 */
74
75
76 #include <8051.h>
77 #include "ser_ir.h"
78
79 #define TXBUFLEN 3
80 #define RXBUFLEN 18      // The minimum rx buffer size for safe communications
81                          // is 17. (The UART in the PC has a 16-byte FIFO.)
82 // TXBUFLEN & RXBUFLEN can be highher if rxbuf[] and txbuf[] is in xdata, max size is limited to 256!
83
84 #define THRESHOLD 16
85 #define ENABLE    0
86 #define DISABLE   1
87
88 #define CTS P3_6          // CTS & RTS can be assigned to any free pins
89 #define RTS P3_7
90
91 static unsigned char rxbuf[RXBUFLEN], txbuf[TXBUFLEN];
92 static unsigned char rxcnt, txcnt, rxpos, txpos;
93 static unsigned char busy;
94
95 void ser_init()
96 {
97   ES = 0;
98   rxcnt = txcnt = rxpos = txpos = 0;  // init buffers
99   busy = 0;
100   SCON = 0x50;               // mode 1 - 8-bit UART
101   PCON |= 0x80;              // SMOD = 1;
102   TMOD &= 0x0f;              // use timer 1
103   TMOD |= 0x20;
104 //  TL1 = TH1 = 256 - 104;      // 600bps with 12 MHz crystal
105 //  TL1 = TH1 = 256 - 52;      // 1200bps with 12 MHz crystal
106 //  TL1 = TH1 = 256 - 26;      // 2400bps with 12 MHz crystal
107   TL1 = TH1 = 256 - 13;      // 4800bps with 12 MHz crystal
108
109   TR1 = 1;                   // Enable timer 1
110   ES = 1;
111
112   CTS = ENABLE;
113 }
114
115 void ser_handler(void) interrupt 4
116 {
117   if (RI) {
118     RI = 0;
119     /* don't overwrite chars already in buffer */
120     if(rxcnt < RXBUFLEN) rxbuf [(rxpos + rxcnt++) % RXBUFLEN] = SBUF;
121     if(rxcnt >= (RXBUFLEN - THRESHOLD)) CTS = DISABLE;
122   }
123
124   if (TI) {
125     TI = 0;
126     if (busy = txcnt) {   /* Assignment, _not_ comparison! */
127       txcnt--;
128       SBUF = txbuf[txpos++];
129       if(txpos >= TXBUFLEN) txpos = 0;
130     }
131   }
132 }
133
134 void ser_putc(unsigned char c)
135 {
136   while(txcnt >= TXBUFLEN);   // wait for room in buffer
137
138   while(RTS == DISABLE);
139
140   ES = 0;
141   if (busy) {
142     txbuf[(txpos + txcnt++) % TXBUFLEN] = c;
143   } else {
144     SBUF = c;
145     busy = 1;
146   }
147   ES = 1;
148 }
149
150 unsigned char ser_getc(void)
151 {
152   unsigned char c;
153
154   while (!rxcnt) {        // wait for a character
155     CTS = ENABLE;
156   }
157
158   ES = 0;
159   rxcnt--;
160   c = rxbuf[rxpos++];
161   if (rxpos >= RXBUFLEN) rxpos = 0;
162   ES = 1;
163   return (c);
164 }
165
166 #pragma save
167 #pragma noinduction
168 void ser_puts(unsigned char *s)
169 {
170   unsigned char c;
171   while (c= *s++) {
172     if (c == '\n') ser_putc('\r');
173     ser_putc (c);
174   }
175 }
176 #pragma restore
177
178 void ser_gets(unsigned char *s, unsigned char len)
179 {
180   unsigned char pos, c;
181
182   pos = 0;
183   while (pos <= len) {
184     c = ser_getc();
185     if (c == '\r') continue;        // discard CR's
186     s[pos++] = c;
187     if (c == '\n') break;           // NL terminates
188   }
189   s[pos] = '\0';                  // terminate string
190 }
191
192 unsigned char ser_can_xmt(void)
193 {
194   return TXBUFLEN - txcnt;
195 }
196
197 unsigned char ser_can_rcv(void)
198 {
199   return rxcnt;
200 }