File: serial.c

package info (click to toggle)
emile 0.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,716 kB
  • ctags: 2,737
  • sloc: ansic: 18,908; makefile: 726; asm: 622; sh: 2
file content (362 lines) | stat: -rw-r--r-- 5,670 bytes parent folder | download
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * 
 * (c) 2004 Laurent Vivier <LaurentVivier@wanadoo.fr>
 *
 */

#include <stdio.h>
#include <unistd.h>

#include <macos/types.h>
#include <macos/devices.h>
#include <macos/serial.h>

#include "misc.h"
#include "head.h"
#include "driver.h"
#include "config.h"

static short out_refnum0 = -1;
static short out_refnum1 = -1;
#ifdef USE_CLI
static short in_refnum0 = -1;
static short in_refnum1 = -1;
#endif

#if USE_BUFFER
#define BUFFER_LEN 80
static char buffer[256];
static int buff_len;
#endif

/*
 * Technical Note TN1119 "Serial Port Apocrypha"
 *
 * from http://developer.apple.com/technotes/tn/tn1119.html
 *
 * Inside Macintosh, Devices, "Using the Serial Driver" :
 *
 * http://developer.apple.com/documentation/mac/Devices/Devices-315.html
 *
 */

/*
 *
 * ".AOut" Serial port A (modem) output
 * ".AIn" Serial port A (modem) input
 * ".BOut" Serial port B (printer) output
 * ".BIn" Serial port B (printer) input
 *
 */

int setserial(short refNum, unsigned int bitrate, unsigned int datasize, 
			    int parity, int stopbits)
{
	CntrlParam param;
	short seropts;
	int res;

	switch(bitrate)
	{
		case 150:
			seropts = baud150;
			break;

		case 300:
			seropts = baud300;
			break;

		case 600:
			seropts = baud600;
			break;

		case 1200:
			seropts = baud1200;
			break;

		case 1800:
			seropts = baud1800;
			break;

		case 2400:
			seropts = baud2400;
			break;

		case 3600:
			seropts = baud3600;
			break;

		case 4800:
			seropts = baud3600;
			break;

		case 7200:
			seropts = baud7200;
			break;

		case 9600:
			seropts = baud9600;
			break;

		case 14400:
			seropts = baud14400;
			break;

		case 19200:
			seropts = baud19200;
			break;

		case 28800:
			seropts = baud28800;
			break;

		case 38400:
			seropts = baud38400;
			break;

		case 57600:
			seropts = baud57600;
			break;

		default:
			seropts = baud9600;
			break;
	}

	switch(datasize)
	{
		case 5:
			seropts |= data5;
			break;

		case 6:
			seropts |= data6;
			break;

		case 7:
			seropts |= data7;
			break;

		case 8:
			seropts |= data8;
			break;

		default:
			seropts |= data8;
			break;
	}

	switch(parity)
	{
		case 0:
			seropts |= noParity;
			break;

		case 1:
			seropts |= oddParity;
			break;

		case 2:
			seropts |= evenParity;
			break;
	}

	switch(stopbits)
	{
		case 0:
			seropts |= stop10;
			break;

		case 1:
			seropts |= stop15;
			break;

		case 2:
			seropts |= stop20;
			break;
	}

	param.csCode = kSERDConfiguration;
	param.csParam[0] = seropts;
	param.ioCRefNum = refNum;
	param.ioVRefNum = 0;
	param.ioCompletion = 0;
	res = PBControlSync((ParmBlkPtr)&param);

	return res;
}

void serial_put(char c)
{
#if USE_BUFFER
	buffer[buff_len++] = c;

	if ( c == '\n' )
	{
		/* add '\r' and flush buffer */

		buffer[buff_len++] = '\r';
		
		goto flush;
	}
	/* if buffer is full (BUFFER_LEN - 1), flush it
 	 * we take BUFFER_LEN - 1 to have enough room
	 * if we need to add '\r' on '\n'
	 */

	if (buff_len == BUFFER_LEN - 1)
		goto flush;

	return;
flush:
	if (out_refnum0 != -1)
		write(out_refnum0, buffer, buff_len);
	if (out_refnum1 != -1)
		write(out_refnum1, buffer, buff_len);
	buff_len = 0;
#else
	if ( c == '\n' )
	{
		if (out_refnum0 != -1)
			write(out_refnum0, "\n\r", 2);
		if (out_refnum1 != -1)
			write(out_refnum1, "\n\r", 2);
	}
	else
	{
		if (out_refnum0 != -1)
			write(out_refnum0, &c, 1);
		if (out_refnum1 != -1)
			write(out_refnum1, &c, 1);
	}
#endif
}

void serial_init(emile_l2_header_t* info)
{
	int res;
	int bitrate, parity, datasize, stopbits;

	res = read_config_modem(info->configuration, 
				&bitrate, &parity, &datasize, &stopbits);
	if (res != -1)
	{
		res = OpenDriver(c2pstring(".AOut"), &out_refnum0);
		if (res != noErr) {
			printf("Cannot open modem output port (%d)\n", res);
		}
		else
		{
			res = setserial(out_refnum0, bitrate,
					datasize,
					parity,
					stopbits);
			if (res != noErr) {
				printf("Cannot setup modem output port (%d)\n",
					res);
			}
		}
#ifdef USE_CLI
		res = OpenDriver(c2pstring(".AIn"), &in_refnum0);
		if (res != noErr) {
			printf("Cannot open modem input port (%d)\n", res);
		}
		else
		{
			res = setserial(in_refnum0, bitrate,
					datasize,
					parity,
					stopbits);
			if (res != noErr) {
				printf("Cannot setup modem input port (%d)\n",
					res);
			}
		}
#endif /* USE_CLI */
	}

	res = read_config_printer(info->configuration, 
				  &bitrate, &parity, &datasize, &stopbits);
	if (res != -1) {
		res = OpenDriver(c2pstring(".BOut"), &out_refnum1);
		if (res != noErr) {
			printf("Cannot open printer output port (%d)\n", res);
		}
		else
		{
			res = setserial(out_refnum1, bitrate,
						datasize,
						parity,
						stopbits);
			if (res != noErr) {
				printf("Cannot setup printer output port (%d)\n"
					, res);
			}
		}
#ifdef USE_CLI
		res = OpenDriver(c2pstring(".BIn"), &in_refnum1);
		if (res != noErr) {
			printf("Cannot open printer input port (%d)\n", res);
		}
		else
		{
			res = setserial(in_refnum1, bitrate,
						datasize,
						parity,
						stopbits);
			if (res != noErr) {
				printf("Cannot setup printer input port (%d)\n"
					, res);
			}
		}
#endif /* USE_CLI */
	}

#if USE_BUFFER
	buff_len = 0;
#endif
}

#ifdef USE_CLI
void serial_cursor_save(void)
{
	serial_put('');
	serial_put('7');
}

void serial_cursor_restore(void)
{
	serial_put('');
	serial_put('8');
}

int serial_getchar(void)
{
	int count;
	char c;

	if (in_refnum0 != -1)
	{
		count = read(in_refnum0, &c, 1);
		if (count == 1)
			return c;
	}

	if (in_refnum1 != -1)
	{
		count = read(in_refnum1, &c, 1);
		if (count == 1)
			return c;
	}

	return 0;
}

int serial_keypressed()
{
	if (serial_getchar() != 0)
		return 1;

	return 0;
}
#endif