Imported Upstream version 2.9.0
[debian/cc1111] / 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 */
35
36 /* BUG: those definitions (and the #include) should be set dynamically
37 * (while linking or at runtime) to make this file a _real_ library.
38 */
39 #include <8051.h>
40 #define XBUFLEN 4
41 #define RBUFLEN 8
42
43 /* You might want to specify idata, pdata or xdata for the buffers */
44 static unsigned char __pdata rbuf[RBUFLEN], xbuf[XBUFLEN];
45 static unsigned char rcnt, xcnt, rpos, xpos;
46 static __bit busy;
47
48 void ser_init (void)
49 {
50    ES = 0;
51    rcnt = xcnt = rpos = xpos = 0;  /* init buffers */
52    busy = 0;
53    SCON = 0x50;
54    PCON |= 0x80;                   /* SMOD = 1; */
55    TMOD &= 0x0f;                   /* use timer 1 */
56    TMOD |= 0x20;
57    TL1 = -3; TH1 = -3; TR1 = 1;    /* 19200bps with 11.059MHz crystal */
58    ES = 1;
59 }
60
61 void ser_handler (void) __interrupt 4
62 {
63    if (RI) {
64            RI = 0;
65            /* don't overwrite chars already in buffer */
66            if (rcnt < RBUFLEN)
67                    rbuf [(unsigned char)(rpos+rcnt++) % RBUFLEN] = SBUF;
68    }
69    if (TI) {
70            TI = 0;
71            if (busy = xcnt) {   /* Assignment, _not_ comparison! */
72                    xcnt--;
73                    SBUF = xbuf [xpos++];
74                    if (xpos >= XBUFLEN)
75                            xpos = 0;
76            }
77    }
78 }
79
80 void ser_putc (unsigned char c)
81 {
82    while (xcnt >= XBUFLEN) /* wait for room in buffer */
83            ;
84    ES = 0;
85    if (busy) {
86            xbuf[(unsigned char)(xpos+xcnt++) % XBUFLEN] = c;
87    } else {
88            SBUF = c;
89            busy = 1;
90    }
91    ES = 1;
92 }
93
94 unsigned char ser_getc (void)
95 {
96    unsigned char c;
97    while (!rcnt)   /* wait for character */
98            ;
99    ES = 0;
100    rcnt--;
101    c = rbuf [rpos++];
102    if (rpos >= RBUFLEN)
103            rpos = 0;
104    ES = 1;
105    return (c);
106 }
107 #pragma save
108 #pragma noinduction
109 void ser_puts (unsigned char *s)
110 {
111    unsigned char c;
112    while (c=*s++) {
113            if (c == '\n') ser_putc ('\r');
114            ser_putc (c);
115    }
116 }
117 #pragma restore
118 void ser_gets (unsigned char *s, unsigned char len)
119 {
120    unsigned char pos, c;
121
122    pos = 0;
123    while (pos <= len) {
124            c = ser_getc ();
125            if (c == '\r') continue;        /* discard CR's */
126            s[pos++] = c;
127            if (c == '\n') break;           /* NL terminates */
128    }
129    s[pos] = '\0';
130 }
131
132 unsigned char ser_can_xmt (void)
133 {
134    return XBUFLEN - xcnt;
135 }
136
137 unsigned char ser_can_rcv (void)
138 {
139    return rcnt;
140 }