* device/include/z180.h,
[fw/sdcc] / device / lib / ser_ir.c
1 /*-------------------------------------------------------------------------
2   ser_ir.c - source file for serial routines 
3   
4   Written By - Josef Wolf <jw@raven.inka.de> (1999) 
5   
6          This program is free software; you can redistribute it and/or modify it
7          under the terms of the GNU General Public License as published by the
8          Free Software Foundation; either version 2, or (at your option) any
9          later version.
10          
11          This program is distributed in the hope that it will be useful,
12          but WITHOUT ANY WARRANTY; without even the implied warranty of
13          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14          GNU General Public License for more details.
15          
16          You should have received a copy of the GNU General Public License
17          along with this program; if not, write to the Free Software
18          Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19          
20          In other words, you are welcome to use, share and improve this program.
21          You are forbidden to forbid anyone else to use, share and improve
22          what you give them.   Help stamp out software-hoarding!
23
24 -------------------------------------------------------------------------*/
25 #include "ser_ir.h"
26
27 /* This file implements a serial interrupt handler and its supporting
28 * routines. Compared with the existing serial.c and _ser.c it has
29 * following advantages:
30 * - You can specify arbitrary buffer sizes (umm, up to 255 bytes),
31 *   so it can run on devices with _little_ memory like at89cx051.
32 * - It won't overwrite characters which already are stored in the
33 *   receive-/transmit-buffer.
34 * - It checks receiver first to minimize probability for overruns
35 *   in the serial receiver.
36 */
37
38 /* BUG: those definitions (and the #include) should be set dynamically
39 * (while linking or at runtime) to make this file a _real_ library.
40 */
41 #include <8051.h>
42 #define XBUFLEN 10
43 #define RBUFLEN 10
44
45 static unsigned char rbuf[RBUFLEN], xbuf[XBUFLEN];
46 static unsigned char rcnt, xcnt, rpos, xpos;
47 static unsigned char busy;
48
49 void ser_init (void)
50 {
51    ES = 0;
52    rcnt = xcnt = rpos = xpos = 0;  /* init buffers */
53    busy = 0;
54    SCON = 0x50;
55    PCON |= 0x80;                   /* SMOD = 1; */
56    TMOD &= 0x0f;                   /* use timer 1 */
57    TMOD |= 0x20;
58    TL1 = -3; TH1 = -3; TR1 = 1;    /* 19200bps with 11.059MHz crystal */
59    ES = 1;
60 }
61
62 void ser_handler (void) interrupt 4
63 {
64    if (RI) {
65            RI = 0;
66            /* don't overwrite chars already in buffer */
67            if (rcnt < RBUFLEN)
68                    rbuf [(rpos+rcnt++) % RBUFLEN] = SBUF;
69    }
70    if (TI) {
71            TI = 0;
72            if (busy = xcnt) {   /* Assignment, _not_ comparison! */
73                    xcnt--;
74                    SBUF = xbuf [xpos++];
75                    if (xpos >= XBUFLEN)
76                            xpos = 0;
77            }
78    }
79 }
80
81 void ser_putc (unsigned char c)
82 {
83    while (xcnt >= XBUFLEN) /* wait for room in buffer */
84            ;
85    ES = 0;
86    if (busy) {
87            xbuf[(xpos+xcnt++) % XBUFLEN] = c;
88    } else {
89            SBUF = c;
90            busy = 1;
91    }
92    ES = 1;
93 }
94
95 unsigned char ser_getc (void)
96 {
97    unsigned char c;
98    while (!rcnt)   /* wait for character */
99            ;
100    ES = 0;
101    rcnt--;
102    c = rbuf [rpos++];
103    if (rpos >= RBUFLEN)
104            rpos = 0;
105    ES = 1;
106    return (c);
107 }
108 #pragma save
109 #pragma noinduction
110 void ser_puts (unsigned char *s)
111 {
112    unsigned char c;
113    while (c=*s++) {
114            if (c == '\n') ser_putc ('\r');
115            ser_putc (c);
116    }
117 }
118 #pragma restore
119 void ser_gets (unsigned char *s, unsigned char len)
120 {
121    unsigned char pos, c;
122
123    pos = 0;
124    while (pos <= len) {
125            c = ser_getc ();
126            if (c == '\r') continue;        /* discard CR's */
127            s[pos++] = c;
128            if (c == '\n') break;           /* NL terminates */
129    }
130    s[pos] = '\0';
131 }
132
133 unsigned char ser_can_xmt (void)
134 {
135    return XBUFLEN - xcnt;
136 }
137
138 unsigned char ser_can_rcv (void)
139 {
140    return rcnt;
141 }