Commits

Trammell Hudson committed d84bb7f

Sending to the ASR33 works

Comments (0)

Files changed (2)

 
 
 # Target file name (without extension).
-TARGET = teletype
+TARGET ?= teletype
 
 
 # List C source files here. (C dependencies are automatically generated.)
+/**
+ * \file 110 current loop interface for an ASR33
+ */
+
+#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;
+}
+
+#define TTY_RX 0xD2
+#define TTY_TX 0xD3
+#define BIT_CLOCK 9090
+
+
+static void
+tty_out(
+	uint8_t c
+)
+{
+	// start bit
+	out(TTY_TX, 1);
+
+	_delay_us(BIT_CLOCK);
+	uint8_t parity = 1;
+
+	for (int i = 0 ; i < 7 ; i++)
+	{
+		int x = c & 1;
+		c >>= 1;
+
+		if (x)
+		{
+			out(TTY_TX, 0);
+			parity++;
+		} else {
+			out(TTY_TX, 1);
+		}
+
+		_delay_us(BIT_CLOCK);
+	}
+
+	out(TTY_TX, parity);
+	_delay_us(BIT_CLOCK);
+
+	out(TTY_TX, 0);
+	_delay_us(BIT_CLOCK);
+	_delay_us(BIT_CLOCK);
+}
+
+
+static uint16_t
+tty_in(void)
+{
+	uint16_t byte = 0;
+
+	for (int bit = 0 ; bit < 16 ; bit++)
+	{
+		if (in(TTY_RX))
+			byte <<= 1;
+		else
+			byte = (byte << 1) | 1;
+
+		_delay_us(BIT_CLOCK/2);
+	}
+
+	return byte;
+}
+
+
+int main(void)
+{
+	// set for 16 MHz clock
+#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
+	CPU_PRESCALE(0);
+
+	// Disable the ADC
+	ADMUX = 0;
+
+	ddr(TTY_TX, 1);
+	out(TTY_TX, 0); // pull low against resistor
+
+	// D2 is used for input (since it is also a UART)
+	// activate pull up so that we will sync current when pulled down
+	ddr(TTY_RX, 0);
+	out(TTY_RX, 1);
+
+	// 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)
+		{
+			out(0xD6, 1);
+			tty_out(c);
+			out(0xD6, 0);
+		}
+
+		int x = in(TTY_RX);
+
+		// Wait for start bit
+		if (x == 0)
+			continue;
+
+		uint16_t byte = tty_in();
+		usb_serial_putchar(hexdigit(byte >> 12));
+		usb_serial_putchar(hexdigit(byte >> 8));
+		usb_serial_putchar(hexdigit(byte >> 4));
+		usb_serial_putchar(hexdigit(byte >> 0));
+		usb_serial_putchar('\r');
+		usb_serial_putchar('\n');
+		continue;
+
+	}
+}
+