teletype / teletype.c

  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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
 * \file 45 baud sync teletype interface
 *
 * Uses a boost converter to make the 50-100V for driving the coil.
 * Test string:
 * now is the time for all good men to come to the aid of their country
 * the quick brown fox jumps over the lazy dogs
 * abcdefghijklmnopqrstuvwxyz
 * ryryryryryryryryryryryryry
 * y becomes q or x
 * r becomes g (rarely)
 * cvcvcvcvcvcvcvcvcvcvcvcvcvcv
 * 
 *
 * At 2 MHz the 45.45 baud (22ms) can be generated with the clock.
 */

#undef F_CPU
#define F_CPU  2000000UL
#define CONFIG_UART

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>
#include "usb_serial.h"
#include "bits.h"


// Send a string to the USB serial port.  The string must be in
// flash memory, using PSTR
//
void send_str(const char *s)
{
	char c;
	while (1) {
		c = pgm_read_byte(s++);
		if (!c) break;
		usb_serial_putchar(c);
	}
}


static char
hexdigit(
	uint8_t x
)
{
	x &= 0xF;
	if (x < 0xA)
		return x + '0';
	else
		return x + 'A' - 0xA;
}


#if 1
#define BAUDOT(a,b,c,d,e,m) ( \
	((!a) << 0) | \
	((!b) << 1) | \
	((!c) << 2) | \
	((!d) << 3) | \
	((!e) << 4) | \
	(m << 5) | \
	0 )
#else
#define BAUDOT(a,b,c,d,e,m) ( \
	(a << 0) | \
	(b << 1) | \
	(c << 2) | \
	(d << 3) | \
	(e << 4) | \
	(m << 5) | \
	0 )
#endif

#define BAUDOT_LC(a,b,c,d,e) BAUDOT(a,b,c,d,e,0x1)
#define BAUDOT_UC(a,b,c,d,e) BAUDOT(a,b,c,d,e,0x2)
#define BAUDOT_BOTH(a,b,c,d,e) BAUDOT(a,b,c,d,e,0x3)

static uint8_t baudot[] = 
{
	[' '] = BAUDOT_BOTH(1,1,0,1,1),
	['\n'] = BAUDOT_BOTH(1,1,1,0,1),
	['\r'] = BAUDOT_BOTH(1,0,1,1,1),

	['a'] = BAUDOT_LC(0,0,1,1,1),
	['b'] = BAUDOT_LC(0,1,1,0,0),
	['c'] = BAUDOT_LC(1,0,0,0,1),
	['d'] = BAUDOT_LC(0,1,1,0,1),
	['e'] = BAUDOT_LC(0,1,1,1,1),
	['f'] = BAUDOT_LC(0,1,0,0,1),
	['g'] = BAUDOT_LC(1,0,1,0,0),
	['h'] = BAUDOT_LC(1,1,0,1,0),
	['i'] = BAUDOT_LC(1,0,0,1,1),
	['j'] = BAUDOT_LC(0,0,1,0,1),
	['k'] = BAUDOT_LC(0,0,0,0,1),
	['l'] = BAUDOT_LC(1,0,1,1,0),
	['m'] = BAUDOT_LC(1,1,0,0,0),
	['n'] = BAUDOT_LC(1,1,0,0,1),
	['o'] = BAUDOT_LC(1,1,1,0,0),
	['p'] = BAUDOT_LC(1,0,0,1,0),
	['q'] = BAUDOT_LC(0,0,0,1,0),
	['r'] = BAUDOT_LC(1,0,1,0,1),
	['s'] = BAUDOT_LC(0,1,0,1,1),
	['t'] = BAUDOT_LC(1,1,1,1,0),
	['u'] = BAUDOT_LC(0,0,0,1,1),
	['v'] = BAUDOT_LC(1,0,0,0,0),
	['w'] = BAUDOT_LC(0,0,1,1,0),
	['x'] = BAUDOT_LC(0,1,0,0,0),
	['y'] = BAUDOT_LC(0,1,0,1,0),
	['z'] = BAUDOT_LC(0,1,1,1,0),

	['0'] = BAUDOT_UC(1,0,0,1,0),
	['1'] = BAUDOT_UC(0,0,0,1,0),
	['2'] = BAUDOT_UC(0,0,1,1,0),
	['3'] = BAUDOT_UC(0,1,1,1,1),
	['4'] = BAUDOT_UC(1,0,1,0,1),
	['5'] = BAUDOT_UC(1,1,1,1,0),
	['6'] = BAUDOT_UC(0,1,0,1,0),
	['7'] = BAUDOT_UC(0,0,0,1,1),
	['8'] = BAUDOT_UC(1,0,0,1,1),
	['9'] = BAUDOT_UC(1,1,1,0,0),
	['-'] = BAUDOT_UC(0,0,1,1,1),
	['?'] = BAUDOT_UC(0,1,1,0,0),
	[':'] = BAUDOT_UC(1,0,0,0,1),
	['$'] = BAUDOT_UC(0,1,1,0,1),
	['!'] = BAUDOT_UC(0,1,0,0,1),
	['&'] = BAUDOT_UC(1,0,1,0,0),
	['#'] = BAUDOT_UC(1,1,0,1,0),
	['\''] = BAUDOT_UC(0,0,1,0,1),
	['('] = BAUDOT_UC(0,0,0,0,1),
	[')'] = BAUDOT_UC(1,0,1,1,0),
	['.'] = BAUDOT_UC(1,1,0,0,0),
	[','] = BAUDOT_UC(1,1,0,0,1),
	['\a'] = BAUDOT_UC(0,1,0,1,1),
	[';'] = BAUDOT_UC(1,0,0,0,0),
	['/'] = BAUDOT_UC(0,1,0,0,0),
	['"'] = BAUDOT_UC(0,1,1,1,0),

};

static uint8_t current_mode;

static const uint8_t mode_select[] = {
	[1] = BAUDOT_BOTH(0,0,0,0,0),
	[2] = BAUDOT_BOTH(0,0,1,0,0),
};

//#define BIT_CLOCK 22
#define BIT_CLOCK 22000
#define one_advance 0
#define TX_PIN 0xD3


static void
tty_out_raw(
	uint8_t c
)
{
#ifdef CONFIG_UART
	// The UART has 5N1 configured; we add a half stop
	sbi(UCSR1A, TXC1);
	UDR1 = c;
	while (bit_is_clear(UCSR1A, TXC1))
		;
	_delay_us(BIT_CLOCK/2);
/*
	_delay_us(BIT_CLOCK);
	_delay_us(BIT_CLOCK);
	_delay_us(BIT_CLOCK);
	_delay_us(BIT_CLOCK);
	_delay_us(BIT_CLOCK);
*/
#else
	uint8_t ocr_one = OCR1C;
	uint8_t ocr_zero = OCR1C;


	// start bit
	out(TX_PIN, 0);
	OCR1C = ocr_zero;

	_delay_us(BIT_CLOCK - 1000);

	for (int i = 0 ; i < 5 ; i++)
	{
		int x = c & 1;
		c >>= 1;

		if (!x)
		{
			OCR1C = ocr_one;
			out(TX_PIN, 1);
			_delay_us(100);
		} else {
			OCR1C = ocr_zero;
			out(TX_PIN, 0);
		}

		_delay_us(BIT_CLOCK);
	}

	// And now send 1.5 stop bits
	OCR1C = ocr_one;
	out(TX_PIN, 1);

	_delay_us(BIT_CLOCK);
	_delay_us(BIT_CLOCK/2); // 1.42 stop bits

	_delay_us(BIT_CLOCK); // give it a few bits, slow but reliable
	_delay_us(BIT_CLOCK); // give it a few bits, slow but reliable
	_delay_us(BIT_CLOCK); // give it a few bits, slow but reliable
	_delay_us(BIT_CLOCK); // give it a few bits, slow but reliable
#endif
}


static void
tty_out(
	uint8_t ch
)
{
	if ('A' <= ch && ch <= 'Z')
		ch += ('a' - 'A');

	uint8_t c = ch ? baudot[ch] : 0x00;
	if (c == 0 && ch != 0)
		return;

	const uint8_t mode = (c >> 5) & 0x3;

	// If we are not in the current mode, send the special
	// code to switch to the other typebar and record that
	// we are in that mode.
	if (current_mode != mode && mode != 0x3)
	{
		tty_out_raw(mode_select[mode]);
		_delay_us(BIT_CLOCK); // give it a few bits
		_delay_us(BIT_CLOCK); // give it a few bits
		_delay_us(BIT_CLOCK); // give it a few bits
		_delay_us(BIT_CLOCK); // give it a few bits
		current_mode = mode;
	}

	tty_out_raw(c);
}


static void
boost_setup(void)
{

        // Configure OC1x in fast-PWM mode, 8-bit
        sbi(TCCR1B, WGM12);
        cbi(TCCR1A, WGM11);
        sbi(TCCR1A, WGM10);

        // OC1C is used to generate the boost converter pump
        // Configure output mode to clear on match, set at top
        sbi(TCCR1A, COM1C1);
        cbi(TCCR1A, COM1C0);

        // Configure clock 1 at clk/1
        cbi(TCCR1B, CS12);
        cbi(TCCR1B, CS11);
        sbi(TCCR1B, CS10);

	// for the big shiny coil
	//OCR1C = 0xF0; // very little off time makes the mosfet unhappy

	// for the small black coil
	OCR1C = 0xD0;

	ddr(0xB7, 1);
	out(0xB7, 1);
}


static void
ubrr_adjust(
	int delta
)
{
	UBRR1 += delta;

	char buf[] = {
		hexdigit(UBRR1 >> 12),
		hexdigit(UBRR1 >>  8),
		hexdigit(UBRR1 >>  4),
		hexdigit(UBRR1 >>  0),
		'\r',
		'\n'
	};

	usb_serial_write(buf, sizeof(buf));
}

// ckckckckckckckckckckckckckckckck
static void
boost_adjust(
	int delta
)
{
	OCR1C += delta;

	char buf[] = {
		hexdigit(OCR1C >> 12),
		hexdigit(OCR1C >>  8),
		hexdigit(OCR1C >>  4),
		hexdigit(OCR1C >>  0),
		'\r',
		'\n'
	};

	usb_serial_write(buf, sizeof(buf));
}

// ckckckckckckckckckckckckckckckck
// ryryryryryryryryryryryryryryryry




int main(void)
{
	// set for 2 MHz clock so that we can do 45 baud
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
	CPU_PRESCALE(3);

	// Disable the ADC
	ADMUX = 0;

	ddr(TX_PIN, 1);
	out(TX_PIN, 1);

#define CONFIG_BOOST
#ifdef CONFIG_BOOST
	boost_setup();
#else
	// do no enable boost; allow a separate current loop disable
	// the to diode drive coil.  Pull the MOSFET high to turn it off
	ddr(0xB7, 1);
	out(0xB7, 1);
#endif

#ifdef CONFIG_UART
	// UBRR is 12 bits, max 4096.  So we run at 2 MHz
	// to keep UBRR0 in range.
	UBRR1 = 2749; // = 45.45 / (16 * 2 MHz) - 1
	UCSR1B = 0
		| (1 << RXEN1)
		| (1 << TXEN1)
		;

	UCSR1C = 0
		| (0 << UPM11) // no parity
		| (0 << UPM10)
		| (0 << UCSZ12) // 5 data bits
		| (0 << UCSZ11)
		| (0 << UCSZ10)
		| (0 << USBS1) // 1 stop bit; we will make a separate half stop
		//| (1 << USBS1) // 2 stop bits
		;
#endif

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured())
		continue;

	_delay_ms(500);

	// wait for the user to run their terminal emulator program
	// which sets DTR to indicate it is ready to receive.
	while (!(usb_serial_get_control() & USB_SERIAL_DTR))
		continue;

	// discard anything that was received prior.  Sometimes the
	// operating system or other software will send a modem
	// "AT command", which can still be buffered.
	usb_serial_flush_input();

	ddr(0xD6, 1); // LED

	int status = 0;

	while (1)
	{
		int c = usb_serial_getchar();
		if (c == -1)
			continue;

		if (c == '{')
		{
			boost_adjust(10);
			continue;
		} else
		if (c == '}')
		{
			boost_adjust(-10);
			continue;
		} else
		if (c == '[')
		{
			ubrr_adjust(-10);
			continue;
		} else
		if (c == ']')
		{
			ubrr_adjust(+10);
			continue;
		}

		out(0xD6, 1);
		tty_out(c);
		//tty_out(0x00);
		out(0xD6, 0);
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.