From: adam Date: Tue, 6 Sep 2005 08:11:10 +0000 (-0700) Subject: checkpoing X-Git-Url: http://git.megacz.com/?p=eltron.git;a=commitdiff_plain;h=74764ab4cab924eac0a4120cb856be4c0178a9ba checkpoing darcs-hash:20050906081110-5007d-4c09ddecacace380ed110dbc12480addf529179a.gz --- diff --git a/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java b/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java index 515e9b0..7e3c7fd 100644 --- a/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java +++ b/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java @@ -2,6 +2,32 @@ package edu.berkeley.cs.obits.device.atmel; import edu.berkeley.cs.obits.*; -public interface AtmelDevice extends Device { +public abstract class AtmelDevice extends Device { + + /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */ public void mode4(int z, int y, int x, int d) throws DeviceException; + + public Sector sector(int col, int row) { return new Sector(col, row); } + public final class Sector { + public final int col; + public final int row; + public Sector(int col, int row) { + if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row"); + if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col"); + this.row = row; + this.col = col; + } + } + + public Cell cell(int col, int row) { return new Cell(col, row); } + public final class Cell { + public final int col; + public final int row; + public Sector getSector() { return sector(col - (col % 4), row - (row % 4)); } + public Cell(int col, int row) { + this.row = row; + this.col = col; + } + } + } diff --git a/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.c b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.c new file mode 100644 index 0000000..2b4fa74 --- /dev/null +++ b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.c @@ -0,0 +1,159 @@ +// +// YOU MUST COMPILE THIS WITH -O3 OR THE AVR WILL NOT BE ABLE TO KEEP UP!!!! +// + +#define F_CPU 3960000 +#include +#include +#include +#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); + UCSR1B |= ((1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1)); + /* + if (doubleRate) + UCSR1A |= (1 << U2X1); + else + UCSR1A &= ~(1 << U2X1); + */ +} + +#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; +} + +inline void portd(int bit, int on) { + if (on) { + PORTD &= ~(1<=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 empty() { return head==tail; } + +inline char recv() { + int q; + char ret; + while(empty()) cts(1); + ret = buf[head]; + head = inc(head); + if (!nearlyFull()) cts(0); + return ret; +} + +void init() { + EIMF = 0xFF; /* Enalbe External Interrrupt*/ + DDRD = 0xFF; /* Configure PORTD as Output */ + DDRE = 1 << 4; /* ability to write to E */ + initUART1(1, 0); + SREG |= 0x80; + sei(); +} + +void conf(int z, int y, int x, int d) { + FPGAX = x; + FPGAY = y; + FPGAZ = z; + FPGAD = d; +} + +void doreset() { + int i; + for(i=0; i<5; i++) { + PORTD = ~0x01; + _delay_ms(50); + PORTD = ~0x02; + _delay_ms(50); + PORTD = ~0x04; + _delay_ms(50); + PORTD = ~0x08; + _delay_ms(50); + } + PORTD = ~0x00; + wdt_enable(WDTO_250MS); + while(1) { } +} + +SIGNAL(SIG_INTERRUPT1) { + doreset(); +} + +void die() { cli(); cts(0); _delay_ms(2000); while(1) { } } +SIGNAL(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); + SREG |= 0x80; + sei(); + if (nearlyFull()) cts(0); +} + +inline int hex(char c) { + if (c >= '0' && c <= '9') return (c - '0'); + if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa); + if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa); + return -1; +} + +int main() { + int count; + init(); + cts(0); + send('O'); + send('B'); + send('I'); + send('T'); + send('S'); + send('\n'); + cts(1); + for(;;) { + int i, x=0, y=0, z=0, d=0; + switch(recv()) { + case 1: + z = recv(); + y = recv(); + x = recv(); + d = recv(); + portd(1,1); + conf(z, y, x, d); + portd(1,0); + break; + default: die(); + } + } + return 0; +} + diff --git a/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java index c77f05c..aa12dce 100644 --- a/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java +++ b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java @@ -7,7 +7,7 @@ import java.util.*; import gnu.io.*; /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */ -public class AvrDrone implements AtmelDevice { +public class AvrDrone extends AtmelDevice { final DataInputStream in; @@ -44,7 +44,6 @@ public class AvrDrone implements AtmelDevice { } catch (InterruptedException e) { throw new DeviceException(e); } } - /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */ public void mode4(int z, int y, int x, int d) throws DeviceException { try { Log.debug(this, "writing configuration frame [zyxd]: " +