2 // YOU MUST COMPILE THIS WITH -O3 OR THE AVR WILL NOT BE ABLE TO KEEP UP!!!!
\r
5 #define F_CPU 12000000
\r
7 #if !defined(__AVR_AT94K__)
\r
8 #error you forgot to put -mmcu=at94k on the command line
\r
11 #include <avr/wdt.h>
\r
12 #include <util/delay.h>
\r
14 #include <avr/interrupt.h>
\r
18 void initUART0(unsigned int baudRate, unsigned int doubleRate) {
\r
19 UBRRHI = (((baudRate) >> 8) & 0x000F);
\r
20 UBRR0 = ((baudRate) & 0x00FF);
\r
21 UCSR0B |= ((1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0));
\r
24 UCSR0A |= (1 << U2X0);
\r
26 UCSR0A &= ~(1 << U2X0);
\r
29 #define BUFSIZE (1024)
\r
31 inline void portd(int bit, int on) {
\r
41 long int numread = 0;
\r
42 inline void cts(int c) {
\r
54 static volatile int sending = 0;
\r
55 static volatile int32_t interrupt_count = 0;
\r
57 // RECV //////////////////////////////////////////////////////////////////////////////
\r
59 char read_buf[BUFSIZE];
\r
60 volatile int read_buf_head;
\r
61 volatile int read_buf_tail;
\r
62 char write_buf[BUFSIZE];
\r
63 volatile int write_buf_head;
\r
64 volatile int write_buf_tail;
\r
66 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }
\r
67 inline int read_full() { return inc(read_buf_tail)==read_buf_head; }
\r
68 inline int abs(int x) { return x<0 ? -x : x; }
\r
69 inline int read_size() { return read_buf_tail<read_buf_head ? (read_buf_head-read_buf_tail) : (read_buf_tail-read_buf_head); }
\r
70 inline int read_empty() { return read_buf_head==read_buf_tail; }
\r
71 inline int read_nearlyFull() {
\r
72 if (read_buf_tail==read_buf_head) return 0;
\r
73 if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2);
\r
74 return (read_buf_tail-read_buf_head) > (BUFSIZE/2);
\r
77 inline int write_full() { return inc(write_buf_tail)==write_buf_head; }
\r
78 inline int write_empty() { return write_buf_head==write_buf_tail; }
\r
79 inline int write_nearlyFull() {
\r
80 if (write_buf_tail==write_buf_head) return 0;
\r
81 if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2);
\r
82 return (write_buf_tail-write_buf_head) > (BUFSIZE/2);
\r
85 inline char recv() {
\r
88 while(read_empty()) cts(1);
\r
89 ret = read_buf[read_buf_head];
\r
90 read_buf_head = inc(read_buf_head);
\r
91 if (!read_nearlyFull()) cts(1);
\r
92 if (PORTE & (1<<3)) PORTE &= ~(1<<3);
\r
93 else PORTE |= (1<<3);
\r
97 ISR(SIG_UART0_DATA) {
\r
98 if (write_empty()) {
\r
99 UCSR0B &= ~(1 << UDRIE0);
\r
102 char ret = write_buf[write_buf_head];
\r
103 write_buf_head = inc(write_buf_head);
\r
108 void send(char c) {
\r
109 while (write_full());
\r
110 write_buf[write_buf_tail] = c;
\r
111 write_buf_tail = inc(write_buf_tail);
\r
112 if (PORTE & (1<<2)) PORTE &= ~(1<<2);
\r
113 else PORTE |= (1<<2);
\r
114 UCSR0B |= (1 << UDRIE0);
\r
118 void fpga_interrupts(int on) {
\r
119 if (on/* && interrupt_count<301*/) {
\r
132 write_buf_head = 0;
\r
133 write_buf_tail = 0;
\r
134 //initUART0(1, 0); //for slow board
\r
135 initUART0(0, 0); //for slow board
\r
136 //initUART0(0, 1); //for slow board
\r
139 void conf(int z, int y, int x, int d) {
\r
148 for(i=0; i<5; i++) {
\r
159 wdt_enable(WDTO_250MS);
\r
163 #define TIMERVAL 100
\r
166 ISR(SIG_FPGA_INTERRUPT15) {
\r
169 //PORTD = ~(interrupt_count & 0xff);
\r
170 //if (interrupt_count >= 301) fpga_interrupts(0);
\r
172 fpga_interrupts(1);
\r
175 ISR(SIG_OVERFLOW0) {
\r
176 fpga_interrupts(0);
\r
178 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
179 TIMSK |= (1<<TOIE0); // enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
180 if (sending) UDR1 = FISUA;
\r
181 fpga_interrupts(1);
\r
184 void init_timer() {
\r
185 TCCR0 |= (1<<CS00); // set the timer0 prescaler to CK
\r
186 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
187 TIMSK |= (1<<TOIE0); //enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
190 ISR(SIG_INTERRUPT0) { // use interrupt1 since interrupt0 is sent by the watchdog (I think)
\r
195 void die() { cli(); PORTE|=(1<<5); _delay_ms(2000); while(1) { portd(2,0); portd(2,1); } }
\r
197 ISR(SIG_UART0_RECV) {
\r
199 if (UCSR0A & (1 << FE0)) err = 201;//{ portd(2,0); portd(3,1); die(); } // framing error, lock up with LED=01
\r
200 if ((UCSR0A & (1 << OR0))) err = 202;//{ portd(2,1); portd(3,0); die(); } // overflow; lock up with LED=10
\r
201 if (read_full()) err = 203;//{ portd(2,1); portd(3,1); die(); } // buffer overrun
\r
203 read_buf[read_buf_tail] = UDR0;
\r
204 read_buf_tail = inc(read_buf_tail);
\r
205 if (read_nearlyFull()) cts(0);
\r
210 inline int hex(char c) {
\r
211 if (c >= '0' && c <= '9') return (c - '0');
\r
212 if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);
\r
213 if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);
\r
218 DDRE = (1<<7) | (1<<5) | (1<<3) | (1<<2);
\r
238 long int i = recv() & 0xff;
\r
239 //if (i < 0) err = 200;
\r
241 long int newi = oldi+1;
\r
242 if (newi >= 256) newi -= 256;
\r
243 if (i != newi) err++;
\r
247 send(err >= 10 ? 255 : i);
\r
253 //while(1) send(/*FISUA*/2);
\r
256 if (PORTE & (1<<6)) PORTE &= ~(1<<6);
\r
257 else PORTE |= (1<<6);
\r
280 //fpga_interrupts(0);
\r
284 //fpga_interrupts(1);
\r
297 int32_t local_interrupt_count = interrupt_count;
\r
298 interrupt_count = 0;
\r
299 send((local_interrupt_count >> 24) & 0xff);
\r
300 send((local_interrupt_count >> 16) & 0xff);
\r
301 send((local_interrupt_count >> 8) & 0xff);
\r
302 send((local_interrupt_count >> 0) & 0xff);
\r
303 fpga_interrupts(1);
\r
309 if ((r & 0x80) == 0x80) {
\r
310 switch (r & 0x44) {
\r
311 case 0x44: z = recv(); break;
\r
312 case 0x40: z++; break;
\r
313 case 0x04: z--; break;
\r
315 switch (r & 0x22) {
\r
316 case 0x22: y = recv(); break;
\r
317 case 0x20: y++; break;
\r
318 case 0x02: y--; break;
\r
320 switch (r & 0x11) {
\r
321 case 0x11: x = recv(); break;
\r
322 case 0x10: x++; break;
\r
323 case 0x01: x--; break;
\r