1 /*-------------------------------------------------------------------------
2 ser_ir.c - source file for serial routines
4 Written By - Josef Wolf <jw@raven.inka.de> (1999)
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
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.
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.
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!
24 -------------------------------------------------------------------------*/
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.
38 /* BUG: those definitions (and the #include) should be set dynamically
39 * (while linking or at runtime) to make this file a _real_ library.
45 static unsigned char rbuf[RBUFLEN], xbuf[XBUFLEN];
46 static unsigned char rcnt, xcnt, rpos, xpos;
47 static unsigned char busy;
52 rcnt = xcnt = rpos = xpos = 0; /* init buffers */
55 PCON |= 0x80; /* SMOD = 1; */
56 TMOD &= 0x0f; /* use timer 1 */
58 TL1 = -3; TH1 = -3; TR1 = 1; /* 19200bps with 11.059MHz crystal */
62 void ser_handler (void) interrupt 4
66 /* don't overwrite chars already in buffer */
68 rbuf [(rpos+rcnt++) % RBUFLEN] = SBUF;
72 if (busy = xcnt) { /* Assignment, _not_ comparison! */
81 void ser_putc (unsigned char c)
83 while (xcnt >= XBUFLEN) /* wait for room in buffer */
87 xbuf[(xpos+xcnt++) % XBUFLEN] = c;
95 unsigned char ser_getc (void)
98 while (!rcnt) /* wait for character */
110 void ser_puts (unsigned char *s)
114 if (c == '\n') ser_putc ('\r');
119 void ser_gets (unsigned char *s, unsigned char len)
121 unsigned char pos, c;
126 if (c == '\r') continue; /* discard CR's */
128 if (c == '\n') break; /* NL terminates */
133 unsigned char ser_can_xmt (void)
135 return XBUFLEN - xcnt;
138 unsigned char ser_can_rcv (void)