+++ /dev/null
-//\r
-// YOU MUST COMPILE THIS WITH -O3 OR THE AVR WILL NOT BE ABLE TO KEEP UP!!!!\r
-//\r
-\r
-#define F_CPU 3960000\r
-#define __AVR_AT94K__\r
-#include <avr/wdt.h>\r
-#include <avr/delay.h>\r
-#include <avr/io.h>\r
-#include <avr/signal.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
- UCSR1B |= ((1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1)); \r
- /*\r
- if (doubleRate)\r
- UCSR1A |= (1 << U2X1);\r
- else\r
- UCSR1A &= ~(1 << U2X1);\r
- */\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
-\r
-inline void portd(int bit, int on) {\r
- if (on) {\r
- PORTD &= ~(1<<bit);\r
- } else {\r
- PORTD |= (1<<bit);\r
- }\r
-}\r
-inline void cts(int c) {\r
- if (c) {\r
- PORTE &= ~(1 << 4);\r
- portd(0, 1);\r
- } else {\r
- PORTE |= (1 << 4);\r
- portd(0, 0);\r
- }\r
-}\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
-}\r
-inline int empty() { return head==tail; }\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
- return ret;\r
-}\r
-\r
-void init() {\r
- EIMF = 0xFF; /* Enalbe External Interrrupt*/ \r
- DDRD = 0xFF; /* Configure PORTD as Output */\r
- DDRE = 1 << 4; /* ability to write to E */\r
- initUART1(1, 0);\r
- SREG |= 0x80;\r
- sei();\r
-}\r
-\r
-void conf(int z, int y, int x, int d) {\r
- FPGAX = x;\r
- FPGAY = y;\r
- FPGAZ = z;\r
- FPGAD = d;\r
-}\r
-\r
-void doreset() {\r
- int i;\r
- for(i=0; i<5; i++) {\r
- PORTD = ~0x01;\r
- _delay_ms(50);\r
- PORTD = ~0x02;\r
- _delay_ms(50);\r
- PORTD = ~0x04;\r
- _delay_ms(50);\r
- PORTD = ~0x08;\r
- _delay_ms(50);\r
- }\r
- PORTD = ~0x00;\r
- wdt_enable(WDTO_250MS);\r
- while(1) { }\r
-}\r
-\r
-SIGNAL(SIG_INTERRUPT1) {\r
- doreset();\r
-}\r
-\r
-void die() { cli(); cts(0); _delay_ms(2000); while(1) { } }\r
-SIGNAL(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
- SREG |= 0x80;\r
- sei();\r
- if (nearlyFull()) cts(0);\r
-}\r
-\r
-inline int hex(char c) {\r
- if (c >= '0' && c <= '9') return (c - '0');\r
- if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);\r
- if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);\r
- return -1;\r
-}\r
-\r
-int main() {\r
- int count;\r
- init();\r
- cts(0);\r
- send('O');\r
- send('B');\r
- send('I');\r
- send('T');\r
- send('S');\r
- send('\n');\r
- cts(1);\r
- for(;;) {\r
- int i, x=0, y=0, z=0, d=0;\r
- switch(recv()) {\r
- case 1:\r
- z = recv();\r
- y = recv();\r
- x = recv();\r
- d = recv();\r
- portd(1,1);\r
- conf(z, y, x, d);\r
- portd(1,0);\r
- break;\r
- default: die();\r
- }\r
- }\r
- return 0;\r
-} \r
-\r