aboutsummaryrefslogtreecommitdiff
path: root/src/rs232dev.c
blob: 9738fce0b5f2ef3b7775400c052133b996929ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (c) 2001, Adam Dunkels.
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Adam Dunkels.
 * 4. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.  
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 *
 * This file is part of the uIP TCP/IP stack.
 *
 * $Id: rs232dev.c,v 1.1 2001/11/20 20:49:45 adam Exp $
 *
 */

/*
 * This is a generic implementation of the SLIP protocol over an RS232
 * (serial) device. While initially intented for the C64, the code can
 * easily be ported to other platforms as well.
 *
 * Huge thanks to Ullrich von Bassewitz <uz@cc65.org> of cc65 fame for
 * and endless supply of bugfixes, insightsful comments and
 * suggestions, and improvements to this code!
 */

#include <rs232.h>
#include <time.h>
#include <string.h>

 /* This will include the system specific header files as well */
#if defined(__CBM__)
#  include <cbm.h>
#elif defined(__ATARI__)
#  include <atari.h>
#endif

#include "uip.h"

#include "features.h"

#ifdef FEAT_KEYBOARD_BUFFER
#include "keybuf.h"
#endif

#define SLIP_END     0300
#define SLIP_ESC     0333
#define SLIP_ESC_END 0334
#define SLIP_ESC_ESC 0335


#define SIO_RECV(c)  while(rs232_get(&c) == RS_ERR_NO_DATA)
#define SIO_POLL(c)  (rs232_get(&c) != RS_ERR_NO_DATA)
#define SIO_SEND(c)  rs232_put(c)

#define MAX_SIZE UIP_BUFSIZE

static u8_t slip_buf[MAX_SIZE];

#if MAX_SIZE > 255
static u16_t len, tmplen;
#else
static u8_t len, tmplen;
#endif /* MAX_SIZE > 255 */

#if 0
#define printf(x)
#else
#include <stdio.h>
#endif

#ifdef FEAT_TRAFFIC_INDICATOR
#include <peekpoke.h>
/* FujiChat screen stuff */
static u16_t traffic_indicator;
static char old_char;

#define DOWN_ARROW 0xdd
#define UP_ARROW 0xdc

#endif

/*-----------------------------------------------------------------------------------*/
static void rs232_err(char err) {
	switch(err) {
		case RS_ERR_OK:
			// printf("RS232 OK\n");
			break;
		case RS_ERR_NOT_INITIALIZED:
			printf("RS232 not initialized\n");
			break;
		case RS_ERR_BAUD_TOO_FAST:
			printf("RS232 baud too fast\n");
			break;
		case RS_ERR_BAUD_NOT_AVAIL:
			printf("RS232 baud rate not available\n");
			break;
		case RS_ERR_NO_DATA:
			printf("RS232 nothing to read\n");
			break;
		case RS_ERR_OVERFLOW:
			printf("RS232 overflow\n");
			break;
	}
}

/*-----------------------------------------------------------------------------------*/
/*
 * rs232dev_send():
 *
 * Sends the packet in the uip_buf and uip_appdata buffers. The first
 * 40 bytes of the packet (the IP and TCP headers) are read from the
 * uip_buf buffer, and the following bytes (the application data) are
 * read from the uip_appdata buffer.
 *
 */
/*-----------------------------------------------------------------------------------*/

void rs232dev_send(void) {
#if MAX_SIZE > 255
	u16_t i;
#else
	u8_t i;
#endif /* MAX_SIZE > 255 */
	volatile u8_t *ptr;
	u8_t c;

#ifdef FEAT_TRAFFIC_INDICATOR
	old_char = PEEK(traffic_indicator);
	POKE(traffic_indicator, UP_ARROW);
#endif

	SIO_SEND(SLIP_END);

	ptr = uip_buf;
	for(i = 0; i < uip_len; ++i) {
		if(i == 40) {
			ptr = uip_appdata;
		}
		c = *ptr++;
		switch(c) {
			case SLIP_END:
				SIO_SEND(SLIP_ESC);
				SIO_SEND(SLIP_ESC_END);
				break;
			case SLIP_ESC:
				SIO_SEND(SLIP_ESC);
				SIO_SEND(SLIP_ESC_ESC);
				break;
			default:
				SIO_SEND(c);
				break;
		}
#ifdef FEAT_KEYBOARD_BUFFER
		keybuf_poll_kbd();
#endif
	}
	SIO_SEND(SLIP_END);

#ifdef FEAT_TRAFFIC_INDICATOR
	POKE(traffic_indicator, old_char);
#endif
}

/*-----------------------------------------------------------------------------------*/
/*
 * rs232dev_poll():
 *
 * Read all avaliable bytes from the RS232 interface into the slip_buf
 * buffer. If no more bytes are avaliable, it returns with 0 to
 * indicate that no packet was immediately ready. When a full packet
 * has been read into the buffer, the packet is copied into the
 * uip_buf buffer and the length of the packet is returned.
 *
 */
/*-----------------------------------------------------------------------------------*/

#if MAX_SIZE > 255
u16_t
#else 
u8_t
#endif /* MAX_SIZE > 255 */
rs232dev_poll(void) {
	u8_t c;
	static u8_t lastc;

#ifdef FEAT_TRAFFIC_INDICATOR
	old_char = PEEK(traffic_indicator);
#endif

	while(SIO_POLL(c)) {
#ifdef FEAT_TRAFFIC_INDICATOR
		POKE(traffic_indicator, DOWN_ARROW);
#endif
#ifdef FEAT_KEYBOARD_BUFFER
		keybuf_poll_kbd();
#endif
		switch(c) {
			case SLIP_ESC:
				lastc = c;
				break;

			case SLIP_END:
				lastc = c;
				/* End marker found, we copy our input buffer to the uip_buf
					buffer and return the size of the packet we copied. */
				memcpy(uip_buf, slip_buf, len);
				tmplen = len;
				len = 0;
#ifdef FEAT_TRAFFIC_INDICATOR
				POKE(traffic_indicator, old_char);
#endif
				return tmplen;

			default:     
				if(lastc == SLIP_ESC) {
					lastc = c;
					/* Previous read byte was an escape byte, so this byte will be
						interpreted differently from others. */
					switch(c) {
						case SLIP_ESC_END:
							c = SLIP_END;
							break;
						case SLIP_ESC_ESC:
							c = SLIP_ESC;
							break;
					}
				} else {
					lastc = c;
				}

				slip_buf[len] = c;
				++len;

				if(len > MAX_SIZE) {
					len = 0;
				}

				break;
		}
	}

#ifdef FEAT_TRAFFIC_INDICATOR
	POKE(traffic_indicator, old_char);
#endif
	return 0;
}

/*-----------------------------------------------------------------------------------*/
/*
 * rs232dev_init():
 *
 * Initializes the RS232 device and sets the parameters of the device.
 *
 */ 
/*-----------------------------------------------------------------------------------*/

char rs232dev_init(unsigned char baud) {
	char err;

#ifdef FEAT_TRAFFIC_INDICATOR
	traffic_indicator = PEEKW(0x58) + 39; /* upper-right corner */
#endif

	err = rs232_init(0);
	rs232_err(err);
	err = rs232_params(baud, RS_PAR_NONE);
	rs232_err(err);

	len = 0;

	return err;
}
/*-----------------------------------------------------------------------------------*/

void rs232dev_close(void) {
	rs232_done();
}