blob: 7800bb1902beaea7b146c48402228d1f01193b36 [file] [log] [blame]
kthacker6de67752006-04-17 15:02:26 +00001/*
2 * Copyright (c) 2001, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This file is part of the uIP TCP/IP stack.
30 *
31 * $Id: rs232dev-ss.c,v 1.1 2006/04/17 15:02:41 kthacker Exp $
32 *
33 */
34
35/*
36 * This is a generic implementation of the SLIP protocol over an RS232
37 * (serial) device. While initially intented for the C64, the code can
38 * easily be ported to other platforms as well.
39 *
40 * Huge thanks to Ullrich von Bassewitz <uz@cc65.org> of cc65 fame for
41 * and endless supply of bugfixes, insightsful comments and
42 * suggestions, and improvements to this code!
43 */
44
45#include "rs232silversurfer.h"
46#include <time.h>
47#include <string.h>
48
49 /* This will include the system specific header files as well */
50#if defined(__CBM__)
51# include <cbm.h>
52#elif defined(__ATARI__)
53# include <atari.h>
54#endif
55
56#include "uip.h"
57
58#define SLIP_END 0300
59#define SLIP_ESC 0333
60#define SLIP_ESC_END 0334
61#define SLIP_ESC_ESC 0335
62
63
64#define SIO_RECV(c) while(rs232_get(&c) == RS_ERR_NO_DATA)
65#define SIO_POLL(c) (rs232_get(&c) != RS_ERR_NO_DATA)
66#define SIO_SEND(c) while(rs232_put(c) == RS_ERR_OVERFLOW)
67
68#define MAX_SIZE (UIP_BUFSIZE - UIP_LLH_LEN)
69
70static u8_t slip_buf[MAX_SIZE + 2];
71
72#if MAX_SIZE > 255
73static u16_t len, tmplen;
74#else
75static u8_t len, tmplen;
76#endif /* MAX_SIZE > 255 */
77
78#if 1
79#define printf(x)
80#else
81#include <stdio.h>
82#endif
83
84
85/*-----------------------------------------------------------------------------------*/
86static void
87rs232_err(char err)
88{
89 switch(err) {
90 case RS_ERR_OK:
91 printf("RS232 OK\n");
92 break;
93 case RS_ERR_NOT_INITIALIZED:
94 printf("RS232 not initialized\n");
95 break;
96 case RS_ERR_BAUD_TOO_FAST:
97 printf("RS232 baud too fast\n");
98 break;
99 case RS_ERR_BAUD_NOT_AVAIL:
100 printf("RS232 baud rate not available\n");
101 break;
102 case RS_ERR_NO_DATA:
103 printf("RS232 nothing to read\n");
104 break;
105 case RS_ERR_OVERFLOW:
106 printf("RS232 overflow\n");
107 break;
108 }
109
110}
111/*-----------------------------------------------------------------------------------*/
112/*
113 * rs232dev_send():
114 *
115 * Sends the packet in the uip_buf and uip_appdata buffers. The first
116 * 40 bytes of the packet (the IP and TCP headers) are read from the
117 * uip_buf buffer, and the following bytes (the application data) are
118 * read from the uip_appdata buffer.
119 *
120 */
121/*-----------------------------------------------------------------------------------*/
122void
123rs232dev_send(void)
124{
125#if MAX_SIZE > 255
126 u16_t i;
127#else
128 u8_t i;
129#endif /* MAX_SIZE > 255 */
130 u8_t *ptr;
131 u8_t c;
132
133 SIO_SEND(SLIP_END);
134
135 ptr = &uip_buf[UIP_LLH_LEN];
136 for(i = 0; i < uip_len; ++i) {
137 if(i == 40) {
138 ptr = uip_appdata;
139 }
140 c = *ptr++;
141 switch(c) {
142 case SLIP_END:
143 SIO_SEND(SLIP_ESC);
144 SIO_SEND(SLIP_ESC_END);
145 break;
146 case SLIP_ESC:
147 SIO_SEND(SLIP_ESC);
148 SIO_SEND(SLIP_ESC_ESC);
149 break;
150 default:
151 SIO_SEND(c);
152 break;
153 }
154 }
155 SIO_SEND(SLIP_END);
156}
157/*-----------------------------------------------------------------------------------*/
158/*
159 * rs232dev_poll():
160 *
161 * Read all avaliable bytes from the RS232 interface into the slip_buf
162 * buffer. If no more bytes are avaliable, it returns with 0 to
163 * indicate that no packet was immediately ready. When a full packet
164 * has been read into the buffer, the packet is copied into the
165 * uip_buf buffer and the length of the packet is returned.
166 *
167 */
168/*-----------------------------------------------------------------------------------*/
169#if MAX_SIZE > 255
170u16_t
171#else
172u8_t
173#endif /* MAX_SIZE > 255 */
174rs232dev_poll(void)
175{
176 u8_t c;
177 static u8_t lastc;
178
179 while(SIO_POLL(c)) {
180 /* printf("c %x\n", c);*/
181 switch(c) {
182 case SLIP_ESC:
183 lastc = c;
184 break;
185
186 case SLIP_END:
187 lastc = c;
188 /* End marker found, we copy our input buffer to the uip_buf
189 buffer and return the size of the packet we copied. */
190 memcpy(&uip_buf[UIP_LLH_LEN], slip_buf, len);
191 tmplen = len;
192 len = 0;
193 return tmplen;
194
195 default:
196 if(lastc == SLIP_ESC) {
197 lastc = c;
198 /* Previous read byte was an escape byte, so this byte will be
199 interpreted differently from others. */
200 switch(c) {
201 case SLIP_ESC_END:
202 c = SLIP_END;
203 break;
204 case SLIP_ESC_ESC:
205 c = SLIP_ESC;
206 break;
207 }
208 } else {
209 lastc = c;
210 }
211
212
213 slip_buf[len] = c;
214 ++len;
215
216 if(len > MAX_SIZE) {
217 len = 0;
218 }
219
220 break;
221 }
222 }
223 return 0;
224}
225/*-----------------------------------------------------------------------------------*/
226/*
227 * rs232dev_init():
228 *
229 * Initializes the RS232 device and sets the parameters of the device.
230 *
231 */
232/*-----------------------------------------------------------------------------------*/
233void
234rs232dev_init(void)
235{
236 char err;
237
238 err = rs232_init(0);
239 rs232_err(err);
240 err = rs232_params(RS_BAUD_9600 | RS_BITS_8 | RS_STOP_1, RS_PAR_NONE);
241 rs232_err(err);
242
243 len = 0;
244
245 return;
246}
247/*-----------------------------------------------------------------------------------*/
248