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 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
65 |
|
|
* \date 21.Nov.2008
|
66 |
|
|
************************************************************************/
|
67 |
|
|
void
|
68 |
|
|
ser_init(void)
|
69 |
|
|
{
|
70 |
|
|
ES0 = 0;
|
71 |
|
|
|
72 |
|
|
ser_txBusy = 0;
|
73 |
|
|
|
74 |
|
|
ser_txIndexIn = 0;
|
75 |
|
|
ser_txIndexOut = 0;
|
76 |
|
|
ser_rxIndexIn = 0;
|
77 |
|
|
ser_rxIndexOut = 0;
|
78 |
|
|
|
79 |
|
|
UART230 = 0x01; /*enable high speed baud rate generator, 115,2 kbaud*/
|
80 |
|
|
|
81 |
13 |
nussgipfel |
/*T2CON = 0x30;*/ /*select timer 2 as baudrate generator*/
|
82 |
9 |
nussgipfel |
/* Baudrate = 19200, oscillator frq. of my processor is 21.4772 MHz */
|
83 |
|
|
/*RCAP2H = 0xFF;*/
|
84 |
|
|
/*RCAP2L = 0xDD;*/
|
85 |
|
|
/* enable counter */
|
86 |
|
|
/*T2CON = 0x34;*/
|
87 |
|
|
|
88 |
|
|
SCON0 = 0x50; /*Serial Port 0 in Mode 1 (async,1 start, 1 stop, no parity)*/
|
89 |
|
|
|
90 |
|
|
if (TI) {
|
91 |
|
|
TI = 0;
|
92 |
|
|
}
|
93 |
|
|
if (RI) {
|
94 |
|
|
RI = 0;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
hook_sv(SV_SERIAL_0, (unsigned short) isr_SERIAL_0);
|
98 |
|
|
|
99 |
|
|
ES0=1;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/************************************************************************/
|
103 |
|
|
/** \brief Interrupt service routine for RS232 handling
|
104 |
|
|
* if there is data to send it copies the next char from the send buffer
|
105 |
|
|
* to the uart
|
106 |
|
|
* if data is received it will be copyied to the receive buffer.
|
107 |
|
|
*************************************************************************
|
108 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
109 |
|
|
* \date 21.Nov.2008
|
110 |
|
|
************************************************************************/
|
111 |
|
|
void
|
112 |
|
|
isr_SERIAL_0(void) interrupt
|
113 |
|
|
{
|
114 |
|
|
ES0=0;
|
115 |
|
|
|
116 |
|
|
if (RI) {
|
117 |
|
|
RI = 0;
|
118 |
|
|
ser_rxBuffer[ser_rxIndexIn++] = SBUF0;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
if (TI) {
|
122 |
|
|
TI = 0;
|
123 |
|
|
if (ser_txIndexIn == ser_txIndexOut) {
|
124 |
|
|
ser_txBusy = 0;
|
125 |
|
|
}
|
126 |
|
|
else {
|
127 |
|
|
SBUF0 = ser_txBuffer[ser_txIndexOut++];
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
ES0=1;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/************************************************************************/
|
135 |
|
|
/** \brief sends one char over the serial line
|
136 |
|
|
*************************************************************************
|
137 |
|
|
* \param[in] c character to send
|
138 |
|
|
*************************************************************************
|
139 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
140 |
|
|
* \date 21.Nov.2008
|
141 |
|
|
************************************************************************/
|
142 |
|
|
void
|
143 |
|
|
ser_putc(unsigned char c)
|
144 |
|
|
{
|
145 |
|
|
ES0=0;
|
146 |
|
|
|
147 |
|
|
if (ser_txBusy) {
|
148 |
|
|
ser_txBuffer[ser_txIndexIn++] = c;
|
149 |
|
|
}
|
150 |
|
|
else {
|
151 |
|
|
ser_txBusy = 1;
|
152 |
|
|
SBUF0 = c;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
ES0=1;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/************************************************************************/
|
159 |
|
|
/** \brief receives one char from the serial line
|
160 |
|
|
*************************************************************************
|
161 |
32 |
nussgipfel |
* \return received character
|
162 |
9 |
nussgipfel |
*************************************************************************
|
163 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
164 |
|
|
* \date 21.Nov.2008
|
165 |
|
|
************************************************************************/
|
166 |
|
|
unsigned char
|
167 |
|
|
ser_getc(void)
|
168 |
|
|
{
|
169 |
|
|
char tmp;
|
170 |
|
|
|
171 |
|
|
#ifdef NON_BLOCKING
|
172 |
|
|
if (ser_rxIndexIn != ser_rxIndexOut) {
|
173 |
|
|
tmp = ser_rxBuffer[ser_rxIndexOut++];
|
174 |
|
|
}
|
175 |
|
|
else {
|
176 |
|
|
tmp = 0;
|
177 |
|
|
}
|
178 |
|
|
#endif
|
179 |
|
|
|
180 |
|
|
return(tmp);
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/************************************************************************/
|
184 |
|
|
/** \brief sends a string of characters over the serial line
|
185 |
|
|
*************************************************************************
|
186 |
32 |
nussgipfel |
* \param[in] *String string to send
|
187 |
9 |
nussgipfel |
*************************************************************************
|
188 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
189 |
|
|
* \date 21.Nov.2008
|
190 |
|
|
************************************************************************/
|
191 |
|
|
void
|
192 |
|
|
ser_printString(char *String)
|
193 |
|
|
{
|
194 |
|
|
while (*String) {
|
195 |
|
|
ser_putc(*String++);
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/************************************************************************/
|
200 |
|
|
/** \brief function to check if there is a new character to read
|
201 |
|
|
*************************************************************************
|
202 |
|
|
* \return returns 1 if a new character is available else 0
|
203 |
|
|
*************************************************************************
|
204 |
|
|
* \author Christoph Zimmermann, SDCC Team
|
205 |
|
|
* \date 21.Nov.2008
|
206 |
|
|
************************************************************************/
|
207 |
|
|
char
|
208 |
|
|
ser_charAvail(void)
|
209 |
|
|
{
|
210 |
|
|
char ret = 0;
|
211 |
|
|
|
212 |
|
|
if (ser_rxIndexIn != ser_rxIndexOut) {
|
213 |
|
|
ret = 1;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
return(ret);
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/*********************End of File************************************/
|