From: Drew Fisher <drew.m.fisher@gmail.com>
Date: Wed, 24 Feb 2010 02:19:52 +0000 (-0500)
Subject: Setup pins, setup timer, shrink code size (static declarations), test
X-Git-Url: http://git.zarvox.org/shortlog/year?a=commitdiff_plain;h=a4f8312e2978834c8a8d979e3e2fe6a016430955;p=usbsnes.git

Setup pins, setup timer, shrink code size (static declarations), test

This commit sets up all the appropriate I/O for the AVR as well as
declaring some functions static so GCC will inline them.  In addition,
it provides a simple test function to see if the USB HID reports are
being sent correctly - every 60 reports, it adds one to the report that
it returns, which should result in a binary "counting" appearance in a
gamepad state viewer.
---

diff --git a/main.c b/main.c
index aee88a4..bf3f5e4 100644
--- a/main.c
+++ b/main.c
@@ -62,7 +62,20 @@ ISR(TIMER1_COMPA_vect) {
 	buttonState.buttons = pollSnes();
 }
 
-void initTimer() {
+static void initPins() {
+	// Enable PD3 as output high (pull-up to D-)
+	DDRD |= _BV(PIND3);
+	PORTD |= _BV(PIND3);
+
+	// Enable PC1:0 as output, and set data clock high
+	// Serial data is PC2 (input, hi-z)
+	// Data latch is PC1 (output, default LOW)
+	// Data clock is PC0 (output, default HIGH)
+	DDRC |= _BV(PINC1) | _BV(PINC0);
+	PORTC |= _BV(PINC0);
+}
+
+static void initTimer() {
 	// See section 15.11 for reference
 	// Using CTC mode (non-PWM)
 
@@ -84,6 +97,7 @@ int main(void)
 {
 	uchar i;
 	wdt_enable(WDTO_1S); // Enable watchdog with 1s reset time
+
 	/* Even if you don't use the watchdog, turn it off here. On newer devices,
 	 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
 	 */
@@ -95,14 +109,17 @@ int main(void)
 	 * additional hardware initialization on the USB side.
 	 */
 	
-	// We need to setup the pin configuration for D3, which is an output pin 
-	
+	// We need to set up the pin configuration for D3 and everything on the SNES side
+	initPins();
 
 	// Set up the clock prescaler to CLKI_O/8, which gives us 2250000 ticks per second
 	// This makes 37500 such ticks give us a 60Hz signal, which is exactly how we set up
 	// the timer interrupt to poll the SNES controller
-	
+	initTimer();
 
+	// Start with no buttons pressed
+	buttonState.buttons = 0;
+	reportBuffer.buttons = 0;
 
 	//odDebugInit();
 	
@@ -116,14 +133,23 @@ int main(void)
 	usbDeviceConnect();
 	sei(); // Enable interrupts
 	//DBG1(0x01, 0, 0);       /* debug output: main loop starts */
+
+	uchar counter = 0;
+
 	for(;;){                /* main event loop */
 		//DBG1(0x02, 0, 0);   /* debug output: main loop iterates */
 		wdt_reset();
 		usbPoll();
 		if(usbInterruptIsReady()){
-			reportBuffer.buttons = buttonState.buttons;
 			/* called after every poll of the interrupt endpoint */
-			//advanceCircleByFixedAngle();
+			// Actual code to be executed - updates the HID report with the status of the SNES controller
+			//reportBuffer.buttons = buttonState.buttons;
+
+			// Testing USB reports - change button state every second, incrementing the report buffer by 1.
+			if(counter++ == 60) {
+				reportBuffer.buttons++;
+				counter = 0;
+			}
 			//DBG1(0x03, 0, 0);   /* debug output: interrupt report prepared */
 			usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
 		}