Commits

Trammell Hudson committed c986c90 Draft

pulse detection is working

Comments (0)

Files changed (1)

 	while (!usb_configured()) /* wait */ ;
 	_delay_ms(1000);
 
+	// 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();
+
+	// print a nice welcome message
+	send_str(PSTR("\r\nRTTY decoder\r\n"));
+
+	// Start TCNT1 to count at clk/1
+	TCCR1B = 0
+		| (0 << CS12)
+		| (0 << CS11)
+		| (1 << CS10)
+		;
+
+	// Enable ADC and select input ADC0 / F0
+	// RTTY is about 1.5 KHz, so we need to sample at least 3 KHz,
+	// although 10 KHz would give us plenty of extra.
+	// 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.
+	ADMUX = 0 | (0 << REFS1) | (1 << REFS0);
+	ADCSRA = 0
+		| (1 << ADEN)
+		| (1 << ADSC)
+		| (1 << ADPS2)
+		| (1 << ADPS1)
+		| (1 << ADPS0)
+		;
+	DDRF = 0;
+	DIDR0 = (1 << 0);
+
+	uint16_t start_crossing = 0;
+
 	while (1)
 	{
-		// wait for the user to run their terminal emulator program
-		// which sets DTR to indicate it is ready to receive.
-		if (!(usb_serial_get_control() & USB_SERIAL_DTR))
+		// Wait for the conversion to complete
+		while ((ADCSRA & (1 << ADSC)))
 			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();
+		// Read the value and start the next conversion
+		const uint16_t val = ADC;
+		ADCSRA |= (1 << ADSC);
 
-		// print a nice welcome message
-		send_str(PSTR("\r\nRTTY decoder\r\n"));
+		// If this is the first zero crossing of the cycle,
+		// record the start time of it.
+		if (val < 16)
+		{
+			if (start_crossing == 0)
+				start_crossing = TCNT1;
+			continue;
+		}
 
-		// Enable ADC and select input ADC0 / F0
-		ADMUX = 0 | (0 << REFS1) | (1 << REFS0);
-		ADCSRA = 0
-			| (1 << ADEN)
-			| (1 << ADSC)
-			| (1 << ADPS2)
-			| (1 << ADPS1)
-			| (1 << ADPS0)
-			;
-		DDRF = 0;
-		DIDR0 = (1 << 0);
+		// If this is the first non-zero reading,
+		// after a period of zero, display the length
+		// of time that we were at zero
+		if (start_crossing == 0)
+			continue;
+		uint16_t delta = TCNT1 - start_crossing;
+		start_crossing = 0;
 
-		// and then listen for commands and process them
-		while (1)
-		{
-			if ((ADCSRA & (1 << ADSC)))
-				continue;
-			//const uint16_t val = ADC;
-			const uint16_t val = ADC;
-			
-			ADCSRA |= (1 << ADSC);
+		// At 16 MHz, 3000 Hz == 5333 ticks == 0x14D5
+		// We use 0x1600 as an approximate mid point
+/*
+		if (delta < 0x1600)
+			byte = (byte << 1) | 1;
+		else
+			byte = (byte << 1) | 0;
 
-			uint8_t h[] = {
-				hexdigit(val >> 12),
-				hexdigit(val >>  8),
-				hexdigit(val >>  4),
-				hexdigit(val >>  0),
-				'\r',
-				'\n',
-			};
+		if ((byte & (1 << 5)) == 0)
+			continue;
+*/
 
-			usb_serial_write(h, sizeof(h));
-		}
+		// We have a five bit code
+		uint8_t h[] = {
+			hexdigit(delta >> 12),
+			hexdigit(delta >>  8),
+			hexdigit(delta >>  4),
+			hexdigit(delta >>  0),
+			'\r',
+			'\n',
+		};
+
+		usb_serial_write(h, sizeof(h));
 	}
 }
 
 		usb_serial_putchar(c);
 	}
 }
-
-#if 0
-// Receive a string from the USB serial port.  The string is stored
-// in the buffer and this function will not exceed the buffer size.
-// A carriage return or newline completes the string, and is not
-// stored into the buffer.
-// The return value is the number of characters received, or 255 if
-// the virtual serial connection was closed while waiting.
-//
-uint8_t recv_str(char *buf, uint8_t size)
-{
-	int16_t r;
-	uint8_t count=0;
-
-	while (count < size) {
-		r = usb_serial_getchar();
-		if (r != -1) {
-			if (r == '\r' || r == '\n') return count;
-			if (r >= ' ' && r <= '~') {
-				*buf++ = r;
-				usb_serial_putchar(r);
-				count++;
-			}
-		} else {
-			if (!usb_configured() ||
-			  !(usb_serial_get_control() & USB_SERIAL_DTR)) {
-				// user no longer connected
-				return 255;
-			}
-			// just a normal timeout, keep waiting
-		}
-	}
-	return count;
-}
-
-// parse a user command and execute it, or print an error message
-//
-void parse_and_execute_command(const char *buf, uint8_t num)
-{
-	uint8_t port, pin, val;
-
-	if (num < 3) {
-		send_str(PSTR("unrecognized format, 3 chars min req'd\r\n"));
-		return;
-	}
-	// first character is the port letter
-	if (buf[0] >= 'A' && buf[0] <= 'F') {
-		port = buf[0] - 'A';
-	} else if (buf[0] >= 'a' && buf[0] <= 'f') {
-		port = buf[0] - 'a';
-	} else {
-		send_str(PSTR("Unknown port \""));
-		usb_serial_putchar(buf[0]);
-		send_str(PSTR("\", must be A - F\r\n"));
-		return;
-	}
-	// second character is the pin number
-	if (buf[1] >= '0' && buf[1] <= '7') {
-		pin = buf[1] - '0';
-	} else {
-		send_str(PSTR("Unknown pin \""));
-		usb_serial_putchar(buf[0]);
-		send_str(PSTR("\", must be 0 to 7\r\n"));
-		return;
-	}
-	// if the third character is a question mark, read the pin
-	if (buf[2] == '?') {
-		// make the pin an input
-		*(uint8_t *)(0x21 + port * 3) &= ~(1 << pin);
-		// read the pin
-		val = *(uint8_t *)(0x20 + port * 3) & (1 << pin);
-		usb_serial_putchar(val ? '1' : '0');
-		send_str(PSTR("\r\n"));
-		return;
-	}
-	// if the third character is an equals sign, write the pin
-	if (num >= 4 && buf[2] == '=') {
-		if (buf[3] == '0') {
-			// make the pin an output
-			*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
-			// drive it low
-			*(uint8_t *)(0x22 + port * 3) &= ~(1 << pin);
-			return;
-		} else if (buf[3] == '1') {
-			// make the pin an output
-			*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
-			// drive it high
-			*(uint8_t *)(0x22 + port * 3) |= (1 << pin);
-			return;
-		} else {
-			send_str(PSTR("Unknown value \""));
-			usb_serial_putchar(buf[3]);
-			send_str(PSTR("\", must be 0 or 1\r\n"));
-			return;
-		}
-	}
-	// otherwise, error message
-	send_str(PSTR("Unknown command \""));
-	usb_serial_putchar(buf[0]);
-	send_str(PSTR("\", must be ? or =\r\n"));
-}
-
-
-#endif
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.