Commits

Trammell Hudson committed bafb6e0

read analog voltages; looks like one bit is not coming through correctly

Comments (0)

Files changed (1)

 		return x + 'A' - 0xA;
 }
 
-#define TTY_RX 0xD2
+//#define TTY_RX 0xD2
+#define TTY_RX 0xF7
 #define TTY_TX 0xD3
 #define BIT_CLOCK 9090
 
 }
 
 
+/**
+ * Enable ADC and select input ADC7 / F7
+ * Select high-speed mode, left aligned
+ * Use clock divisor of 2
+ * System clock is 16 MHz on teensy, 8 MHz on tiny,
+ * conversions take 13 ticks, so divisor == 128 (1,1,1) should
+ * give 9.6 KHz of samples.
+ */
+static void
+adc_init(void)
+{
+	ADMUX = 0
+		| 7
+		| (0 << REFS1)
+		| (1 << REFS0)
+		;
+
+	ADCSRA = 0
+		| (1 << ADEN) // enable ADC
+		| (0 << ADSC) // don't start yet
+		| (0 << ADIE) // don't enable the interrupts
+		| (0 << ADPS2)
+		| (1 << ADPS1)
+		| (1 << ADPS0)
+		;
+
+	ADCSRB = 0
+		| (1 << ADHSM) // enable highspeed mode
+		;
+
+	DDRF = 0;
+
+	// Start the first conversion!
+	sbi(ADCSRA, ADSC);
+}
+
+
+static uint16_t
+adc_read_block(void)
+{
+	// Wait for the conversion to complete
+	while (bit_is_set(ADCSRA, ADSC))
+		continue;
+
+	const uint16_t value = ADC;
+
+	// Start the next conversion
+	sbi(ADCSRA, ADSC);
+
+	// Read the value
+	return value;
+}
+
+
+
 static uint16_t
 tty_in(void)
 {
 	uint16_t byte = 0;
 
+#if 0
 	for (int bit = 0 ; bit < 16 ; bit++)
 	{
 		if (in(TTY_RX))
+			byte = (byte << 1) | 1;
+		else
 			byte <<= 1;
-		else
-			byte = (byte << 1) | 1;
 
-		_delay_us(BIT_CLOCK/2);
+		_delay_us(BIT_CLOCK/4);
 	}
+#else
+#define TTY_BITS 64
+	char buf[TTY_BITS];
+	adc_read_block();
+
+	for (int bit = 0 ; bit < TTY_BITS ; bit++)
+	{
+		//int x = in(TTY_RX);
+		//buf[bit] = x ? '0' : '1';
+		int x = adc_read_block();
+		buf[bit] = hexdigit(x >> 6);
+		_delay_us(BIT_CLOCK/6);
+	}
+	usb_serial_write(buf, sizeof(buf));
+#endif
 
 	return byte;
 }
 
 	// Disable the ADC
 	ADMUX = 0;
+	adc_init();
 
 	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
+	// it has an external pull up resistor to +5v
 	ddr(TTY_RX, 0);
-	out(TTY_RX, 1);
+	out(TTY_RX, 0);
+
+#if 0
+	// 110 E72 is too SLOW at 16 MHz.  Wow.
+	// bit banging it is...
+	UBRR0 = ;
+	UCSR0B = (1 << RXEN0);
+	UCSR0C = 0
+		| (1 << UPM11) // even parity
+		| (0 << UPM10)
+		| (0 << UCSZ12) // 7 data bits
+		| (1 << UCSZ11)
+		| (0 << UCSZ10)
+		| (1 << USBS1) // 2 stop bits
+		;
+#endif
 
 	// initialize the USB, and then wait for the host
 	// to set configuration.  If the Teensy is powered
 		int x = in(TTY_RX);
 
 		// Wait for start bit
-		if (x == 0)
+		if (x)
 			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;