check
authoradam <adam@megacz.com>
Thu, 6 Apr 2006 13:38:47 +0000 (14:38 +0100)
committeradam <adam@megacz.com>
Thu, 6 Apr 2006 13:38:47 +0000 (14:38 +0100)
Makefile
src/edu/berkeley/obits/device/atmel/AvrDrone.c
src/edu/berkeley/obits/device/atmel/AvrDrone.java

index bc729bc..4891adf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,8 +19,8 @@ obits.jar: $(shell find src -name \*.java)
        cd build; jar cvf ../$@ .
 
 # -O3 is required; otherwise the poor AVR can't keep up with us!
-avrdrone.hex:
-       avr-gcc -O3 -mmcu=at94k src/edu/berkeley/obits/device/atmel/AvrDrone.c
+avrdrone.hex: src/edu/berkeley/obits/device/atmel/AvrDrone.c
+       avr-gcc -O3 -mmcu=at94k $<
        avr-objcopy -O ihex a.out $@
 
 
index 06253ad..f72cd28 100644 (file)
 #include <avr/io.h>\r
 #include <avr/interrupt.h>\r
 \r
-unsigned char val = 0;\r
-\r
-volatile unsigned char dataReady = 0;\r
-volatile unsigned char data = 0;\r
-\r
 void initUART1(unsigned int baudRate, unsigned int doubleRate) {\r
   UBRRHI = (((baudRate) >> 8) & 0x000F); \r
   UBRR1  = ((baudRate) & 0x00FF); \r
@@ -30,15 +25,7 @@ void initUART1(unsigned int baudRate, unsigned int doubleRate) {
   */\r
 }\r
 \r
-#define BUFSIZE (1024 *2)\r
-static char buf[BUFSIZE];\r
-volatile static int head = 0;\r
-volatile static int tail = 0;\r
-\r
-void send(char c) {\r
-  while(!(UCSR1A & (1 << UDRE1)));           /* Wait for data Regiester to be empty */\r
-  UDR1 = (int)c;\r
-}\r
+#define BUFSIZE (1024)\r
 \r
 inline void portd(int bit, int on) {\r
   if (on) {\r
@@ -57,26 +44,61 @@ inline void cts(int c) {
   }\r
 }\r
 \r
+\r
+// RECV //////////////////////////////////////////////////////////////////////////////\r
+\r
+char read_buf[BUFSIZE];\r
+volatile int read_buf_head;\r
+volatile int read_buf_tail;\r
+char write_buf[BUFSIZE];\r
+volatile int write_buf_head;\r
+volatile int write_buf_tail;\r
+\r
 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }\r
-inline int full() { return inc(tail)==head; }\r
-inline int nearlyFull() {\r
-  if (tail==head) return 0;\r
-  if (tail < head) return (head-tail) < (BUFSIZE/2);\r
-  return (tail-head) > (BUFSIZE/2);\r
+inline int read_full() { return inc(read_buf_tail)==read_buf_head; }\r
+inline int read_empty() { return read_buf_head==read_buf_tail; }\r
+inline int read_nearlyFull() {\r
+  if (read_buf_tail==read_buf_head) return 0;\r
+  if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2);\r
+  return (read_buf_tail-read_buf_head) > (BUFSIZE/2);\r
 }\r
-inline int empty() { return head==tail; }\r
-\r
+/*\r
+inline int write_full() { return inc(write_buf_tail)==write_buf_head; }\r
+inline int write_empty() { return write_buf_head==write_buf_tail; }\r
+inline int write_nearlyFull() {\r
+  if (write_buf_tail==write_buf_head) return 0;\r
+  if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2);\r
+  return (write_buf_tail-write_buf_head) > (BUFSIZE/2);\r
+}\r
+*/\r
 inline char recv() {\r
   int q;\r
   char ret;\r
-  while(empty()) cts(1);\r
-  ret = buf[head];\r
-  head = inc(head);\r
-  if (!nearlyFull()) cts(0);\r
+  while(read_empty()) cts(1);\r
+  ret = read_buf[read_buf_head];\r
+  read_buf_head = inc(read_buf_head);\r
+  if (!read_nearlyFull()) cts(0);\r
   return ret;\r
 }\r
 \r
+void send(char c) {\r
+\r
+  write_buf[write_buf_tail] = c;\r
+  write_buf_tail = inc(write_buf_tail);\r
+\r
+  char ret = write_buf[write_buf_head];\r
+  write_buf_head = inc(write_buf_head);\r
+\r
+  while(!(UCSR1A & (1 << UDRE1)));           /* Wait for data Regiester to be empty */\r
+  UDR1 = (int)ret;\r
+}\r
+\r
+\r
 void init() {\r
+  read_buf_head = 0;\r
+  read_buf_tail = 0;\r
+  write_buf_head = 0;\r
+  write_buf_tail = 0;\r
   EIMF  = 0xFF;                          /* Enalbe External Interrrupt*/  \r
   DDRD = 0xFF;                           /* Configure PORTD as Output */\r
   DDRE = 1 << 4;                         /* ability to write to E */\r
@@ -128,16 +150,16 @@ ISR(SIG_INTERRUPT1) {   // use interrupt1 since interrupt0 is sent by the watchd
   doreset();\r
 }\r
 \r
-void die() { cli(); cts(0); _delay_ms(2000); while(1) { } }\r
+void die() { cli(); cts(0); _delay_ms(2000); while(1) { portd(2,0); portd(2,1); } }\r
 ISR(SIG_UART1_RECV) {\r
   if (UCSR1A & (1 << FE1))   { portd(2,0); portd(3,1); die(); }  // framing error, lock up with LED=01\r
   if ((UCSR1A & (1 << OR1))) { portd(2,1); portd(3,0); die(); }  // overflow; lock up with LED=10\r
-  if (full())                { portd(2,1); portd(3,1); die(); }  // buffer overrun\r
-  buf[tail] = UDR1;\r
-  tail = inc(tail);\r
+  if (read_full())           { portd(2,1); portd(3,1); die(); }  // buffer overrun\r
+  read_buf[read_buf_tail] = UDR1;\r
+  read_buf_tail = inc(read_buf_tail);\r
   SREG |= 0x80;\r
   sei();\r
-  if (nearlyFull()) cts(0);\r
+  if (read_nearlyFull()) cts(0);\r
 }\r
 \r
 inline int hex(char c) {\r
index a4b7b3c..9e7da7e 100644 (file)
@@ -41,6 +41,7 @@ public class AvrDrone extends AtmelDevice {
             out.flush();
         } catch (IOException e) { throw new DeviceException(e); }
     }
+
     public synchronized byte readBus() throws DeviceException {
         try {
             out.writeByte(2);