1 |
9 |
nussgipfel |
/* GECKO3COM
|
2 |
|
|
*
|
3 |
|
|
* Copyright (C) 2008 by
|
4 |
|
|
* ___ ____ _ _
|
5 |
|
|
* ( _`\ ( __)( ) ( )
|
6 |
|
|
* | (_) )| (_ | |_| | Berne University of Applied Sciences
|
7 |
|
|
* | _ <'| _) | _ | School of Engineering and
|
8 |
|
|
* | (_) )| | | | | | Information Technology
|
9 |
|
|
* (____/'(_) (_) (_)
|
10 |
|
|
*
|
11 |
|
|
*
|
12 |
|
|
* This program is free software: you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
* You should have received a copy of the GNU General Public License
|
22 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
23 |
|
|
*/
|
24 |
|
|
|
25 |
|
|
/********************************************************************/
|
26 |
|
|
/** \file ser.c
|
27 |
|
|
*********************************************************************
|
28 |
|
|
* This file contains a simple interrupt driven serial driver with
|
29 |
|
|
* buffer (no check for overflow!!!).
|
30 |
|
|
*********************************************************************
|
31 |
|
|
* \note:
|
32 |
|
|
* Remember to enable all interrupts (EA=1) outside of this module!!
|
33 |
|
|
********************************************************************/
|
34 |
|
|
|
35 |
|
|
#include "fx2regs.h"
|
36 |
|
|
#include "ser.h"
|
37 |
|
|
#include "isr.h"
|
38 |
|
|
|
39 |
|
|
/** enable non blocking operation of the ser_getc function */
|
40 |
|
|
#define NON_BLOCKING
|
41 |
|
|
|
42 |
|
|
/** Transmit buffer write pointer */
|
43 |
|
|
unsigned char __xdata ser_txIndexIn;
|
44 |
|
|
/** Transmit butter read pointer */
|
45 |
|
|
unsigned char __xdata ser_txIndexOut;
|
46 |
|
|
/** Receive buffer write pointer */
|
47 |
|
|
unsigned char __xdata ser_rxIndexIn;
|
48 |
|
|
/** Receive buffer read pointer */
|
49 |
|
|
unsigned char __xdata ser_rxIndexOut;
|
50 |
|
|
|
51 |
|
|
/** Transmitt buffer */
|
52 |
|
|
unsigned char __xdata ser_txBuffer[0x100];
|
53 |
|
|
/** Receive buffer */
|
54 |
|
|
unsigned char __xdata ser_rxBuffer[0x100];
|
55 |
|
|
|
56 |
|
|
/** Transmitt Busy flag */
|
57 |
|
|
static __bit ser_txBusy;
|
58 |
|
|
|
59 |
|
|
/************************************************************************/
|
60 |
|
|
/** Initializes the UART
|
61 |
|
|
* Initializes the UART for the serial Port 0 with 115,2 Kbaud, 1 start,
|
62 |
|
|
* 1 stop and no parity bit.
|
63 |
|
|
*************************************************************************
|
64 |
|
|
* \param[in]
|
65 |
|
|
* \param[out]
|
66 |
|
|
* \return
|
67 |
|
|
*************************************************************************
|
68 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
69 |
|
|
* \date 21.Nov.2008
|
70 |
|
|
************************************************************************/
|
71 |
|
|
void
|
72 |
|
|
ser_init(void)
|
73 |
|
|
{
|
74 |
|
|
ES0 = 0;
|
75 |
|
|
|
76 |
|
|
ser_txBusy = 0;
|
77 |
|
|
|
78 |
|
|
ser_txIndexIn = 0;
|
79 |
|
|
ser_txIndexOut = 0;
|
80 |
|
|
ser_rxIndexIn = 0;
|
81 |
|
|
ser_rxIndexOut = 0;
|
82 |
|
|
|
83 |
|
|
UART230 = 0x01; /*enable high speed baud rate generator, 115,2 kbaud*/
|
84 |
|
|
|
85 |
13 |
nussgipfel |
/*T2CON = 0x30;*/ /*select timer 2 as baudrate generator*/
|
86 |
9 |
nussgipfel |
/* Baudrate = 19200, oscillator frq. of my processor is 21.4772 MHz */
|
87 |
|
|
/*RCAP2H = 0xFF;*/
|
88 |
|
|
/*RCAP2L = 0xDD;*/
|
89 |
|
|
/* enable counter */
|
90 |
|
|
/*T2CON = 0x34;*/
|
91 |
|
|
|
92 |
|
|
SCON0 = 0x50; /*Serial Port 0 in Mode 1 (async,1 start, 1 stop, no parity)*/
|
93 |
|
|
|
94 |
|
|
if (TI) {
|
95 |
|
|
TI = 0;
|
96 |
|
|
}
|
97 |
|
|
if (RI) {
|
98 |
|
|
RI = 0;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
hook_sv(SV_SERIAL_0, (unsigned short) isr_SERIAL_0);
|
102 |
|
|
|
103 |
|
|
ES0=1;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/************************************************************************/
|
107 |
|
|
/** \brief Interrupt service routine for RS232 handling
|
108 |
|
|
* if there is data to send it copies the next char from the send buffer
|
109 |
|
|
* to the uart
|
110 |
|
|
* if data is received it will be copyied to the receive buffer.
|
111 |
|
|
*************************************************************************
|
112 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
113 |
|
|
* \date 21.Nov.2008
|
114 |
|
|
************************************************************************/
|
115 |
|
|
void
|
116 |
|
|
isr_SERIAL_0(void) interrupt
|
117 |
|
|
{
|
118 |
|
|
ES0=0;
|
119 |
|
|
|
120 |
|
|
if (RI) {
|
121 |
|
|
RI = 0;
|
122 |
|
|
ser_rxBuffer[ser_rxIndexIn++] = SBUF0;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
if (TI) {
|
126 |
|
|
TI = 0;
|
127 |
|
|
if (ser_txIndexIn == ser_txIndexOut) {
|
128 |
|
|
ser_txBusy = 0;
|
129 |
|
|
}
|
130 |
|
|
else {
|
131 |
|
|
SBUF0 = ser_txBuffer[ser_txIndexOut++];
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
ES0=1;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/************************************************************************/
|
139 |
|
|
/** \brief sends one char over the serial line
|
140 |
|
|
*************************************************************************
|
141 |
|
|
* \param[in] c character to send
|
142 |
|
|
*************************************************************************
|
143 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
144 |
|
|
* \date 21.Nov.2008
|
145 |
|
|
************************************************************************/
|
146 |
|
|
void
|
147 |
|
|
ser_putc(unsigned char c)
|
148 |
|
|
{
|
149 |
|
|
ES0=0;
|
150 |
|
|
|
151 |
|
|
if (ser_txBusy) {
|
152 |
|
|
ser_txBuffer[ser_txIndexIn++] = c;
|
153 |
|
|
}
|
154 |
|
|
else {
|
155 |
|
|
ser_txBusy = 1;
|
156 |
|
|
SBUF0 = c;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
ES0=1;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/************************************************************************/
|
163 |
|
|
/** \brief receives one char from the serial line
|
164 |
|
|
*************************************************************************
|
165 |
|
|
* \return receive character
|
166 |
|
|
*************************************************************************
|
167 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
168 |
|
|
* \date 21.Nov.2008
|
169 |
|
|
************************************************************************/
|
170 |
|
|
unsigned char
|
171 |
|
|
ser_getc(void)
|
172 |
|
|
{
|
173 |
|
|
char tmp;
|
174 |
|
|
|
175 |
|
|
#ifdef NON_BLOCKING
|
176 |
|
|
if (ser_rxIndexIn != ser_rxIndexOut) {
|
177 |
|
|
tmp = ser_rxBuffer[ser_rxIndexOut++];
|
178 |
|
|
}
|
179 |
|
|
else {
|
180 |
|
|
tmp = 0;
|
181 |
|
|
}
|
182 |
|
|
#endif
|
183 |
|
|
|
184 |
|
|
return(tmp);
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/************************************************************************/
|
188 |
|
|
/** \brief sends a string of characters over the serial line
|
189 |
|
|
*************************************************************************
|
190 |
|
|
* \param[in] string to send
|
191 |
|
|
*************************************************************************
|
192 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
193 |
|
|
* \date 21.Nov.2008
|
194 |
|
|
************************************************************************/
|
195 |
|
|
void
|
196 |
|
|
ser_printString(char *String)
|
197 |
|
|
{
|
198 |
|
|
while (*String) {
|
199 |
|
|
ser_putc(*String++);
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/************************************************************************/
|
204 |
|
|
/** \brief function to check if there is a new character to read
|
205 |
|
|
*************************************************************************
|
206 |
|
|
* \return returns 1 if a new character is available else 0
|
207 |
|
|
*************************************************************************
|
208 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
209 |
|
|
* \date 21.Nov.2008
|
210 |
|
|
************************************************************************/
|
211 |
|
|
char
|
212 |
|
|
ser_charAvail(void)
|
213 |
|
|
{
|
214 |
|
|
char ret = 0;
|
215 |
|
|
|
216 |
|
|
if (ser_rxIndexIn != ser_rxIndexOut) {
|
217 |
|
|
ret = 1;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
return(ret);
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
/*********************End of File************************************/
|