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.
36 /* BUG: those definitions (and the #include) should be set dynamically
37 * (while linking or at runtime) to make this file a _real_ library.
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;
51 rcnt = xcnt = rpos = xpos = 0; /* init buffers */
54 PCON |= 0x80; /* SMOD = 1; */
55 TMOD &= 0x0f; /* use timer 1 */
57 TL1 = -3; TH1 = -3; TR1 = 1; /* 19200bps with 11.059MHz crystal */
61 void ser_handler (void) __interrupt 4
65 /* don't overwrite chars already in buffer */
67 rbuf [(unsigned char)(rpos+rcnt++) % RBUFLEN] = SBUF;
71 if (busy = xcnt) { /* Assignment, _not_ comparison! */
80 void ser_putc (unsigned char c)
82 while (xcnt >= XBUFLEN) /* wait for room in buffer */
86 xbuf[(unsigned char)(xpos+xcnt++) % XBUFLEN] = c;
94 unsigned char ser_getc (void)
97 while (!rcnt) /* wait for character */
109 void ser_puts (unsigned char *s)
113 if (c == '\n') ser_putc ('\r');
118 void ser_gets (unsigned char *s, unsigned char len)
120 unsigned char pos, c;
125 if (c == '\r') continue; /* discard CR's */
127 if (c == '\n') break; /* NL terminates */
132 unsigned char ser_can_xmt (void)
134 return XBUFLEN - xcnt;
137 unsigned char ser_can_rcv (void)