From: adam Date: Thu, 6 Apr 2006 13:38:47 +0000 (+0100) Subject: check X-Git-Tag: mpar_demo_release~120 X-Git-Url: http://git.megacz.com/?p=slipway.git;a=commitdiff_plain;h=53c49ce035cdb40cddf9920e7e0fa89f65830878 check --- diff --git a/Makefile b/Makefile index bc729bc..4891adf 100644 --- 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 $@ diff --git a/src/edu/berkeley/obits/device/atmel/AvrDrone.c b/src/edu/berkeley/obits/device/atmel/AvrDrone.c index 06253ad..f72cd28 100644 --- a/src/edu/berkeley/obits/device/atmel/AvrDrone.c +++ b/src/edu/berkeley/obits/device/atmel/AvrDrone.c @@ -13,11 +13,6 @@ #include #include -unsigned char val = 0; - -volatile unsigned char dataReady = 0; -volatile unsigned char data = 0; - void initUART1(unsigned int baudRate, unsigned int doubleRate) { UBRRHI = (((baudRate) >> 8) & 0x000F); UBRR1 = ((baudRate) & 0x00FF); @@ -30,15 +25,7 @@ void initUART1(unsigned int baudRate, unsigned int doubleRate) { */ } -#define BUFSIZE (1024 *2) -static char buf[BUFSIZE]; -volatile static int head = 0; -volatile static int tail = 0; - -void send(char c) { - while(!(UCSR1A & (1 << UDRE1))); /* Wait for data Regiester to be empty */ - UDR1 = (int)c; -} +#define BUFSIZE (1024) inline void portd(int bit, int on) { if (on) { @@ -57,26 +44,61 @@ inline void cts(int c) { } } + +// RECV ////////////////////////////////////////////////////////////////////////////// + +char read_buf[BUFSIZE]; +volatile int read_buf_head; +volatile int read_buf_tail; +char write_buf[BUFSIZE]; +volatile int write_buf_head; +volatile int write_buf_tail; + inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; } -inline int full() { return inc(tail)==head; } -inline int nearlyFull() { - if (tail==head) return 0; - if (tail < head) return (head-tail) < (BUFSIZE/2); - return (tail-head) > (BUFSIZE/2); +inline int read_full() { return inc(read_buf_tail)==read_buf_head; } +inline int read_empty() { return read_buf_head==read_buf_tail; } +inline int read_nearlyFull() { + if (read_buf_tail==read_buf_head) return 0; + if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2); + return (read_buf_tail-read_buf_head) > (BUFSIZE/2); } -inline int empty() { return head==tail; } - +/* +inline int write_full() { return inc(write_buf_tail)==write_buf_head; } +inline int write_empty() { return write_buf_head==write_buf_tail; } +inline int write_nearlyFull() { + if (write_buf_tail==write_buf_head) return 0; + if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2); + return (write_buf_tail-write_buf_head) > (BUFSIZE/2); +} +*/ inline char recv() { int q; char ret; - while(empty()) cts(1); - ret = buf[head]; - head = inc(head); - if (!nearlyFull()) cts(0); + while(read_empty()) cts(1); + ret = read_buf[read_buf_head]; + read_buf_head = inc(read_buf_head); + if (!read_nearlyFull()) cts(0); return ret; } +void send(char c) { + + write_buf[write_buf_tail] = c; + write_buf_tail = inc(write_buf_tail); + + char ret = write_buf[write_buf_head]; + write_buf_head = inc(write_buf_head); + + while(!(UCSR1A & (1 << UDRE1))); /* Wait for data Regiester to be empty */ + UDR1 = (int)ret; +} + + void init() { + read_buf_head = 0; + read_buf_tail = 0; + write_buf_head = 0; + write_buf_tail = 0; EIMF = 0xFF; /* Enalbe External Interrrupt*/ DDRD = 0xFF; /* Configure PORTD as Output */ DDRE = 1 << 4; /* ability to write to E */ @@ -128,16 +150,16 @@ ISR(SIG_INTERRUPT1) { // use interrupt1 since interrupt0 is sent by the watchd doreset(); } -void die() { cli(); cts(0); _delay_ms(2000); while(1) { } } +void die() { cli(); cts(0); _delay_ms(2000); while(1) { portd(2,0); portd(2,1); } } ISR(SIG_UART1_RECV) { if (UCSR1A & (1 << FE1)) { portd(2,0); portd(3,1); die(); } // framing error, lock up with LED=01 if ((UCSR1A & (1 << OR1))) { portd(2,1); portd(3,0); die(); } // overflow; lock up with LED=10 - if (full()) { portd(2,1); portd(3,1); die(); } // buffer overrun - buf[tail] = UDR1; - tail = inc(tail); + if (read_full()) { portd(2,1); portd(3,1); die(); } // buffer overrun + read_buf[read_buf_tail] = UDR1; + read_buf_tail = inc(read_buf_tail); SREG |= 0x80; sei(); - if (nearlyFull()) cts(0); + if (read_nearlyFull()) cts(0); } inline int hex(char c) { diff --git a/src/edu/berkeley/obits/device/atmel/AvrDrone.java b/src/edu/berkeley/obits/device/atmel/AvrDrone.java index a4b7b3c..9e7da7e 100644 --- a/src/edu/berkeley/obits/device/atmel/AvrDrone.java +++ b/src/edu/berkeley/obits/device/atmel/AvrDrone.java @@ -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);