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