blob: 7668a4cf39a60229db5fdcd3c0cb3e1071e5722e [file] [log] [blame]
adamdunkels9819dca2003-04-25 08:48:25 +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.
adamdunkels0ff4dae2004-09-03 10:04:30 +000013 * 3. The name of the author may not be used to endorse or promote
adamdunkels9819dca2003-04-25 08:48:25 +000014 * 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 *
oliverschmidte252f172005-02-23 22:43:23 +000031 * $Id: rs232dev.c,v 1.6 2005/02/23 22:43:23 oliverschmidt Exp $
adamdunkels9819dca2003-04-25 08:48:25 +000032 *
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
adamdunkels0ff4dae2004-09-03 10:04:30 +000045#include <serial.h>
adamdunkels9819dca2003-04-25 08:48:25 +000046#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
adamdunkels0ff4dae2004-09-03 10:04:30 +000064#define SIO_RECV(c) while(ser_get(&c) == SER_ERR_NO_DATA)
65#define SIO_POLL(c) (ser_get(&c) != SER_ERR_NO_DATA)
66#define SIO_SEND(c) ser_put(c)
adamdunkels9819dca2003-04-25 08:48:25 +000067
adamdunkels8718dc42003-07-31 23:56:01 +000068#define MAX_SIZE (UIP_BUFSIZE - UIP_LLH_LEN)
adamdunkels9819dca2003-04-25 08:48:25 +000069
70static u8_t slip_buf[MAX_SIZE + 2];
71
adamdunkels9819dca2003-04-25 08:48:25 +000072static u16_t len, tmplen;
adamdunkels0ff4dae2004-09-03 10:04:30 +000073
74static char loaded = 0;
adamdunkels9819dca2003-04-25 08:48:25 +000075
76#if 1
77#define printf(x)
78#else
79#include <stdio.h>
80#endif
81
82
83/*-----------------------------------------------------------------------------------*/
adamdunkels0ff4dae2004-09-03 10:04:30 +000084/*static void
adamdunkels9819dca2003-04-25 08:48:25 +000085rs232_err(char err)
86{
87 switch(err) {
88 case RS_ERR_OK:
89 printf("RS232 OK\n");
90 break;
91 case RS_ERR_NOT_INITIALIZED:
92 printf("RS232 not initialized\n");
93 break;
94 case RS_ERR_BAUD_TOO_FAST:
95 printf("RS232 baud too fast\n");
96 break;
97 case RS_ERR_BAUD_NOT_AVAIL:
98 printf("RS232 baud rate not available\n");
99 break;
100 case RS_ERR_NO_DATA:
101 printf("RS232 nothing to read\n");
102 break;
103 case RS_ERR_OVERFLOW:
104 printf("RS232 overflow\n");
105 break;
106 }
adamdunkels0ff4dae2004-09-03 10:04:30 +0000107}*/
adamdunkels9819dca2003-04-25 08:48:25 +0000108/*-----------------------------------------------------------------------------------*/
109/*
110 * rs232dev_send():
111 *
112 * Sends the packet in the uip_buf and uip_appdata buffers. The first
113 * 40 bytes of the packet (the IP and TCP headers) are read from the
114 * uip_buf buffer, and the following bytes (the application data) are
115 * read from the uip_appdata buffer.
116 *
117 */
118/*-----------------------------------------------------------------------------------*/
119void
120rs232dev_send(void)
121{
adamdunkels9819dca2003-04-25 08:48:25 +0000122 u16_t i;
adamdunkels9819dca2003-04-25 08:48:25 +0000123 u8_t *ptr;
124 u8_t c;
adamdunkels0ff4dae2004-09-03 10:04:30 +0000125
adamdunkels9819dca2003-04-25 08:48:25 +0000126 SIO_SEND(SLIP_END);
127
adamdunkels8718dc42003-07-31 23:56:01 +0000128 ptr = &uip_buf[UIP_LLH_LEN];
adamdunkels9819dca2003-04-25 08:48:25 +0000129 for(i = 0; i < uip_len; ++i) {
oliverschmidte252f172005-02-23 22:43:23 +0000130 if(i == UIP_TCPIP_HLEN) {
adamdunkels9819dca2003-04-25 08:48:25 +0000131 ptr = uip_appdata;
132 }
133 c = *ptr++;
134 switch(c) {
135 case SLIP_END:
136 SIO_SEND(SLIP_ESC);
137 SIO_SEND(SLIP_ESC_END);
138 break;
139 case SLIP_ESC:
140 SIO_SEND(SLIP_ESC);
141 SIO_SEND(SLIP_ESC_ESC);
142 break;
143 default:
144 SIO_SEND(c);
145 break;
146 }
147 }
148 SIO_SEND(SLIP_END);
149}
150/*-----------------------------------------------------------------------------------*/
151/*
152 * rs232dev_poll():
153 *
154 * Read all avaliable bytes from the RS232 interface into the slip_buf
155 * buffer. If no more bytes are avaliable, it returns with 0 to
156 * indicate that no packet was immediately ready. When a full packet
157 * has been read into the buffer, the packet is copied into the
158 * uip_buf buffer and the length of the packet is returned.
159 *
160 */
161/*-----------------------------------------------------------------------------------*/
adamdunkels9819dca2003-04-25 08:48:25 +0000162u16_t
adamdunkels9819dca2003-04-25 08:48:25 +0000163rs232dev_poll(void)
164{
165 u8_t c;
166 static u8_t lastc;
167
adamdunkels0ff4dae2004-09-03 10:04:30 +0000168 if(loaded == 0) {
169 return 0;
170 }
171
adamdunkels9819dca2003-04-25 08:48:25 +0000172 while(SIO_POLL(c)) {
adamdunkels0ff4dae2004-09-03 10:04:30 +0000173
adamdunkels9819dca2003-04-25 08:48:25 +0000174 switch(c) {
175 case SLIP_ESC:
176 lastc = c;
177 break;
178
179 case SLIP_END:
180 lastc = c;
181 /* End marker found, we copy our input buffer to the uip_buf
182 buffer and return the size of the packet we copied. */
adamdunkels8718dc42003-07-31 23:56:01 +0000183 memcpy(&uip_buf[UIP_LLH_LEN], slip_buf, len);
adamdunkels9819dca2003-04-25 08:48:25 +0000184 tmplen = len;
185 len = 0;
186 return tmplen;
187
188 default:
189 if(lastc == SLIP_ESC) {
190 lastc = c;
191 /* Previous read byte was an escape byte, so this byte will be
192 interpreted differently from others. */
193 switch(c) {
194 case SLIP_ESC_END:
195 c = SLIP_END;
196 break;
197 case SLIP_ESC_ESC:
198 c = SLIP_ESC;
199 break;
200 }
201 } else {
202 lastc = c;
203 }
adamdunkels0ff4dae2004-09-03 10:04:30 +0000204
adamdunkels9819dca2003-04-25 08:48:25 +0000205 slip_buf[len] = c;
206 ++len;
207
208 if(len > MAX_SIZE) {
209 len = 0;
210 }
211
212 break;
213 }
214 }
215 return 0;
216}
217/*-----------------------------------------------------------------------------------*/
218/*
219 * rs232dev_init():
220 *
221 * Initializes the RS232 device and sets the parameters of the device.
222 *
223 */
224/*-----------------------------------------------------------------------------------*/
225void
226rs232dev_init(void)
227{
228 char err;
adamdunkels0ff4dae2004-09-03 10:04:30 +0000229 struct ser_params p;
adamdunkels9819dca2003-04-25 08:48:25 +0000230
adamdunkels0ff4dae2004-09-03 10:04:30 +0000231 err = ser_load_driver("c64-swlink.ser");
232
233 if(err != SER_ERR_OK) {
234 asm("inc $d020");
235 return;
236 }
237
238 p.baudrate = SER_BAUD_9600;
239 p.databits = SER_BITS_8;
240 p.stopbits = SER_STOP_1;
241 p.parity = SER_PAR_NONE;
242 p.handshake = SER_HS_HW;
243
244 err = ser_open(&p);
245
246 if(err != SER_ERR_OK) {
247 asm("inc $d020");
248 return;
249 }
250
251
252 loaded = 1;
253
254 /* err = rs232_init(0); */
255 /* rs232_err(err);*/
256 /* err = rs232_params(RS_BAUD_9600 | RS_BITS_8 | RS_STOP_1, RS_PAR_NONE);*/
257 /* rs232_err(err);*/
adamdunkels9819dca2003-04-25 08:48:25 +0000258
259 len = 0;
260
261 return;
262}
263/*-----------------------------------------------------------------------------------*/
adamdunkels0ff4dae2004-09-03 10:04:30 +0000264void
265rs232dev_unload(void)
266{
267 if(loaded){
268 ser_unload();
269 }
270}
271/*-----------------------------------------------------------------------------------*/
adamdunkels68f6f4b2003-08-24 22:41:55 +0000272