Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _ser.c
1 /*KA******************************************************************
2 * PROJECT: PL-One/8052 
3 **********************************************************************
4 * FILE: ser.c
5 **********************************************************************
6 * CHANGES:
7 * date      author            description
8 * --------------------------------------------------------------------
9 * 04/26/99  we                final
10 * 04/27/99  we                comments
11 **********************************************************************
12 * DESCRIPTION:
13 * This file contains a simple interrupt driven serial driver with
14 * buffer (no check for overflow!!!).
15 **********************************************************************
16 * FUNCTIONS DECLARED:
17 * ser_init       Initialization; must be called first
18 * ser_putc       output one char on the serial line
19 * ser_getc       return a char if one has been received, else 0
20 * ser_printString print a 0-terminated string
21 * ser_charAvail  return 1 if a char arrived on serial line
22 **********************************************************************
23 * NOTE:
24 * Remember to enable all interrupts (EA=1) outside of this module!!
25 **********************************************************************
26 * COMPILE TIME OPTIONS: -
27 * DEBUG OPTIONS: -
28 ******************************************************************KE*/
29 /*      $Id: _ser.c 4735 2007-04-08 13:05:45Z MaartenBrock $    */
30
31
32 #include <8052.h>
33
34 #include "ser.h"
35
36 #define NON_BLOCKING
37
38 unsigned char __xdata ser_txIndexIn;
39 unsigned char __xdata ser_txIndexOut;
40 unsigned char __xdata ser_rxIndexIn;
41 unsigned char __xdata ser_rxIndexOut;
42
43 unsigned char __xdata ser_txBuffer[0x100];
44 unsigned char __xdata ser_rxBuffer[0x100];
45
46 static __bit ser_txBusy;
47
48 void
49 ser_init(void)
50 {
51   ES = 0;
52
53   ser_txBusy     = 0;
54
55   ser_txIndexIn  = 0;
56   ser_txIndexOut = 0;
57   ser_rxIndexIn  = 0;
58   ser_rxIndexOut = 0;
59   
60   T2CON = 0x30;
61
62   /* Baudrate = 19200, oscillator frq. of my processor is 21.4772 MHz */
63   RCAP2H = 0xFF;
64   RCAP2L = 0xDD;
65
66   /* enable counter */
67   T2CON = 0x34;
68   
69   SCON = 0x50;
70
71   if (TI) {
72     TI = 0;
73   }
74   if (RI) {
75     RI = 0;
76   }
77   
78   ES=1;  
79 }
80
81 void
82 ser_interrupt_handler(void) __interrupt 4 __using 1
83 {
84   ES=0;
85
86   if (RI) {
87     RI = 0;
88     ser_rxBuffer[ser_rxIndexIn++] = SBUF;
89   }
90
91   if (TI) {
92     TI = 0;
93     if (ser_txIndexIn == ser_txIndexOut) {
94       ser_txBusy = 0;
95     }
96     else {
97       SBUF = ser_txBuffer[ser_txIndexOut++];
98     }
99   }
100
101   ES=1;
102 }
103
104 void 
105 ser_putc(unsigned char c)
106 {
107   ES=0;
108
109   if (ser_txBusy) {
110     ser_txBuffer[ser_txIndexIn++] = c;
111   }
112   else {
113     ser_txBusy = 1;
114     SBUF = c;
115   }
116
117   ES=1;
118 }
119
120 unsigned char
121 ser_getc(void)
122 {
123   char tmp;
124
125 #ifdef NON_BLOCKING
126   if (ser_rxIndexIn != ser_rxIndexOut) {
127     tmp = ser_rxBuffer[ser_rxIndexOut++];
128   }
129   else {
130     tmp = 0;
131   }
132 #endif
133
134   return(tmp);
135 }
136
137 void
138 ser_printString(char *String)
139 {
140   while (*String) {
141     ser_putc(*String++);
142   }
143 }
144
145 char
146 ser_charAvail(void)
147 {
148   char ret = 0;
149
150   if (ser_rxIndexIn != ser_rxIndexOut) {
151     ret = 1;
152   }
153
154   return(ret);
155 }
156
157 /*********************End of File************************************/