Index: branches/encoders/code/lib/include/libdragonfly/spi.h
===================================================================
--- branches/encoders/code/lib/include/libdragonfly/spi.h	(nonexistent)
+++ branches/encoders/code/lib/include/libdragonfly/spi.h	(revision 546)
@@ -0,0 +1,33 @@
+#ifndef __SPI_H__
+#define __SPI_H__
+
+#define DOUBLE_SCK 1
+#define SPR0_BIT 1
+#define LSB 1
+#define MSB 0
+#define MASTER 1
+#define SLAVE 0
+
+#define SPI_IDLE 0x00
+#define SPI_SEND 0x01
+#define SPI_RECV 0x02
+#define SPI_BOTH 0x03
+
+
+/* Pin Defines */
+#define MOSI _BV(PB2)
+#define MISO _BV(PB3)
+#define SS _BV(PB0)
+#define SCLK _BV(PB1)
+
+//#define size_t int
+typedef void (*spi_fun_recv_t)(char);
+typedef void (*spi_fun_send_t)(char);
+void spi_init(char mode, spi_fun_recv_t recv_func);
+int spi_send(char *data, int bytes);
+void spi_master_recv_on(void);
+void spi_master_recv_off(void);
+void spi_read_one(void);
+void spi_read(int bytes);
+
+#endif
Index: branches/encoders/code/lib/include/libdragonfly/dragonfly_lib.h
===================================================================
--- branches/encoders/code/lib/include/libdragonfly/dragonfly_lib.h	(revision 545)
+++ branches/encoders/code/lib/include/libdragonfly/dragonfly_lib.h	(revision 546)
@@ -65,6 +65,7 @@
 #include <bom.h>
 #include <move.h>
 #include <reset.h>
+//#include "spi.h"
 #include <math.h>
 
 #endif
Index: branches/encoders/code/lib/src/libdragonfly/encoders.c
===================================================================
--- branches/encoders/code/lib/src/libdragonfly/encoders.c	(nonexistent)
+++ branches/encoders/code/lib/src/libdragonfly/encoders.c	(revision 546)
@@ -0,0 +1,2 @@
+
+//Dummy file for now.
Index: branches/encoders/code/lib/src/libdragonfly/spi.c
===================================================================
--- branches/encoders/code/lib/src/libdragonfly/spi.c	(nonexistent)
+++ branches/encoders/code/lib/src/libdragonfly/spi.c	(revision 546)
@@ -0,0 +1,182 @@
+/* @file spi.c
+ * @brief
+ * SPI module
+ * @bug broken
+ */
+
+#include <avr/interrupt.h>
+#include "ring_buffer.h"
+#include "spi.h"
+#include <dragonfly_lib.h>
+
+
+/*
+SS = PB0
+SCK = PB1
+MOSI = PB2
+MISO = PB3
+*/
+/* Controls clock freq. see Table 72 of specs*/
+
+#define DOUBLE_SCK 1
+#define SPR0_BIT 1
+#define SPR1_BIT 0
+#define LSB 1
+#define MSB 0
+
+
+RING_BUFFER_NEW(spi_buffer, 16, char, spi_send_buff, spi_rec_buff);
+volatile char spi_status;
+char spi_mode;
+static spi_fun_recv_t  spi_recv_function;
+//static spi_fun_send_t  spi_send_function;
+
+
+void spi_init(char mode, spi_fun_recv_t recv_func) {
+    usb_puts("spi_init: start\n");
+
+	spi_mode = mode;
+
+	RING_BUFFER_CLEAR(spi_send_buff);
+	RING_BUFFER_CLEAR(spi_rec_buff);
+
+	spi_recv_function = recv_func;
+	//spi_send_function = send_func;
+
+    /* Enables the SPI module
+     * Enable Interrupt, Enable SPI Module, LSB First, Master Mode, Clock div = 64
+     */
+    SPCR = 0x00;
+	SPCR = _BV(SPIE) | _BV(SPE) | _BV(DORD) | _BV(MSTR)| _BV(SPR1) | _BV(SPR0);
+    SPSR = 0x00;
+	SPSR = _BV(SPI2X); 
+	
+	spi_status = SPI_IDLE;
+
+    /* Set SCLK, SS, MOSI as outputs. MISO as input */
+	if(mode == MASTER) {
+	    DDRB |= MOSI | SCLK | SS;
+	    DDRB &= ~MISO;
+		PORTB |= SS;	//Keep SS High until transmit
+	/* Set SCLK, SS, MOSI as inputs. MISO as output */
+	} else {
+	    DDRB &= ~MOSI & ~SCLK & ~SS;
+	    DDRB |= MISO;
+    }
+	
+	//sei();
+	usb_puts("spi_init: end\n");
+}
+
+int spi_send(char *data, int bytes) {
+
+	int i;
+
+    if(bytes == 0) 
+        return -1; /* ...needed?*/
+    
+    //Prevent race condition on the buffer
+    cli();
+    for(i = 1; i < bytes; i++) {
+        // Fail if the buffer is full
+	    if(RING_BUFFER_FULL(spi_send_buff)) {
+	        sei();
+	        return -1;
+	    }
+	
+	    RING_BUFFER_ADD(spi_send_buff, data[i]);
+    }
+    
+    sei();
+    
+    spi_status |= SPI_SEND;
+	
+	if (spi_mode == MASTER ){
+		PORTB &= ~SS;	//Select slave
+	}
+	
+    SPDR = *data;
+    
+	if(spi_mode)
+		usb_puts("MASTER");
+	else
+		usb_puts("SLAVE");
+		
+	usb_puts(": sending [");usb_putc(*data);usb_puts("]\n\r");
+	
+	//sei();
+	
+	return 1;
+}
+
+void spi_read(int bytes) {
+    
+    cli();
+    for(int i = 1; i < bytes; i++) {
+        // Fail if the buffer is full
+	    if(RING_BUFFER_FULL(spi_send_buff)) {
+	        sei();
+	        return;
+	    }
+	
+	    RING_BUFFER_ADD(spi_send_buff, '\n');
+    }
+    
+    sei();
+
+    spi_status |= SPI_RECV;
+	
+	if (spi_mode == MASTER ){
+		PORTB &= ~SS;	//Select slave
+	}
+	
+    SPDR = '\n';
+
+}
+
+void spi_read_one(void)
+{
+	PORTB &= ~SS;
+	SPDR = 'x';
+}
+
+ISR(SIG_SPI) {
+	char c;
+
+    //The clock is running so dequeue another byte to send	
+	if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND
+		RING_BUFFER_REMOVE(spi_send_buff, c);
+		SPDR = c;
+	
+	//If we're the master and we're done sending, end the transmission
+	} else if (spi_mode == MASTER) {
+	    PORTB |= SS;
+	}
+	
+	//You always receive something. You need to handle it.
+    spi_recv_function(SPDR);	
+    
+	/*
+	if(spi_status & SPI_SEND) {
+		spi_status ^= SPI_SEND;
+		
+        if (spi_mode == MASTER) {
+    		PORTB |= SS;
+        }
+    } else if (spi_status & SPI_RECV) {
+           
+    }
+    */
+    //End the transmission if we are not sending or recieving anymore
+	//} else if(spi_mode == MASTER) {
+	//   PORTB |= SS;
+	//}
+	
+	//Call the recv function whenver we recieve a byte
+	//if(spi_status == SPI_RECV){
+		//spi_recv_function(SPDR);
+		//if(spi_mode == MASTER)
+			//PORTB |= SS;
+	//}
+}		
+
Index: branches/encoders/code/lib/bin/libdragonfly.a
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: branches/encoders/code/projects/template/main.c
===================================================================
--- branches/encoders/code/projects/template/main.c	(revision 545)
+++ branches/encoders/code/projects/template/main.c	(revision 546)
@@ -6,10 +6,12 @@
 extern char spi_status;
 
 void recv_func(char c) {
+    if (c != '\n');
 	if(MODE)
 		usb_puts("MASTER");
 	else
 		usb_puts("SLAVE");
+	
 	usb_puts(": received [");usb_putc(c);usb_puts("]\n\r");
 	return;
 }
@@ -24,11 +26,12 @@
 	while(1) {
 		if(TEST){
 			if(MODE){
-				spi_read_one();
+				spi_read(6);
 				delay_ms(1000);
 			} else {
-				if(!(spi_status &SPI_SEND))
-				spi_send("SLAVE",6);
+				//if(!(spi_status & SPI_SEND))
+				spi_send("SLAVE ",6);
+				//delay_ms(800);
 			}
 		} else {
 			if (MODE) {
Index: branches/encoders/code/projects/template/Makefile
===================================================================
--- branches/encoders/code/projects/template/Makefile	(revision 545)
+++ branches/encoders/code/projects/template/Makefile	(revision 546)
@@ -14,9 +14,8 @@
 # USE_WIRELESS = 1
 
 # com1 = serial port. Use lpt1 to connect to parallel port.
-AVRDUDE_PORT = com3
+AVRDUDE_PORT = /dev/ttyUSB0
 #
-#
 ###################################
 
 # Hey Emacs, this is a -*- makefile -*-
Index: branches/encoders/code/projects/libdragonfly/spi.c
===================================================================
--- branches/encoders/code/projects/libdragonfly/spi.c	(revision 545)
+++ branches/encoders/code/projects/libdragonfly/spi.c	(revision 546)
@@ -51,7 +51,7 @@
     SPSR = 0x00;
 	SPSR = _BV(SPI2X); 
 	
-	spi_status = SPI_RECV;
+	spi_status = SPI_IDLE;
 
     /* Set SCLK, SS, MOSI as outputs. MISO as input */
 	if(mode == MASTER) {
@@ -68,7 +68,7 @@
 	usb_puts("spi_init: end\n");
 }
 
-int spi_send(char *data, size_t bytes) {
+int spi_send(char *data, int bytes) {
 
 	int i;
 
@@ -112,14 +112,14 @@
 void spi_read(int bytes) {
     
     cli();
-    for(i = 1; i < bytes; i++) {
+    for(int i = 1; i < bytes; i++) {
         // Fail if the buffer is full
 	    if(RING_BUFFER_FULL(spi_send_buff)) {
 	        sei();
-	        return -1;
+	        return;
 	    }
 	
-	    RING_BUFFER_ADD(spi_send_buff, '\0');
+	    RING_BUFFER_ADD(spi_send_buff, '\n');
     }
     
     sei();
@@ -130,7 +130,7 @@
 		PORTB &= ~SS;	//Select slave
 	}
 	
-    SPDR = *data;
+    SPDR = '\n';
 
 }
 
@@ -142,33 +142,41 @@
 
 ISR(SIG_SPI) {
 	char c;
-	
+
+    //The clock is running so dequeue another byte to send	
 	if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND
 		RING_BUFFER_REMOVE(spi_send_buff, c);
 		SPDR = c;
-		
-		if(spi_mode)
-			usb_puts("MASTER");
-		else
-			usb_puts("SLAVE");
-			
-		usb_puts(": sending [");usb_putc(c);usb_puts("]\n\r");
 	
-	//If we were sending and we are done end the transmission
-	} else if(spi_status == SPI_SEND) {
-		spi_status ^= SPI_SEND;
-		PORTB |= SS; //Sleep slave
-
-    //End the transmission if we are not sending or recieving anymore
-	} else if(spi_mode == MASTER) {
+	//If we're the master and we're done sending, end the transmission
+	} else if (spi_mode == MASTER) {
 	    PORTB |= SS;
 	}
 	
+	//You always receive something. You need to handle it.
+    spi_recv_function(SPDR);	
+    
+	/*
+	if(spi_status & SPI_SEND) {
+		spi_status ^= SPI_SEND;
+		
+        if (spi_mode == MASTER) {
+    		PORTB |= SS;
+        }
+    } else if (spi_status & SPI_RECV) {
+           
+    }
+    */
+    //End the transmission if we are not sending or recieving anymore
+	//} else if(spi_mode == MASTER) {
+	//   PORTB |= SS;
+	//}
+	
 	//Call the recv function whenver we recieve a byte
-	if(spi_status == SPI_RECV){
-		spi_recv_function(SPDR);
+	//if(spi_status == SPI_RECV){
+		//spi_recv_function(SPDR);
 		//if(spi_mode == MASTER)
 			//PORTB |= SS;
-	}
+	//}
 }		
 
Index: branches/encoders/code/projects/libdragonfly/spi.h
===================================================================
--- branches/encoders/code/projects/libdragonfly/spi.h	(revision 545)
+++ branches/encoders/code/projects/libdragonfly/spi.h	(revision 546)
@@ -8,10 +8,10 @@
 #define MASTER 1
 #define SLAVE 0
 
-#define SPI_IDLE _BV(0)
-#define SPI_SEND _BV(1)
-#define SPI_RECV _BV(2)
-#define SPI_BOTH _BV(3)
+#define SPI_IDLE 0x00
+#define SPI_SEND 0x01
+#define SPI_RECV 0x02
+#define SPI_BOTH 0x03
 
 
 /* Pin Defines */
@@ -20,13 +20,14 @@
 #define SS _BV(PB0)
 #define SCLK _BV(PB1)
 
-#define size_t int
+//#define size_t int
 typedef void (*spi_fun_recv_t)(char);
 typedef void (*spi_fun_send_t)(char);
 void spi_init(char mode, spi_fun_recv_t recv_func);
-int spi_send(char *data, size_t bytes);
+int spi_send(char *data, int bytes);
 void spi_master_recv_on(void);
 void spi_master_recv_off(void);
 void spi_read_one(void);
+void spi_read(int bytes);
 
 #endif
