2 // YOU MUST COMPILE THIS WITH -O3 OR THE AVR WILL NOT BE ABLE TO KEEP UP!!!!
\r
5 //#define F_CPU 3960000
\r
6 #define F_CPU 12000000
\r
8 #if !defined(__AVR_AT94K__)
\r
9 #error you forgot to put -mmcu=at94k on the command line
\r
12 #include <avr/wdt.h>
\r
13 #include <util/delay.h>
\r
15 #include <avr/interrupt.h>
\r
19 void initUART0(unsigned int baudRate, unsigned int doubleRate) {
\r
20 UBRRHI = (((baudRate) >> 8) & 0x000F);
\r
21 UBRR0 = ((baudRate) & 0x00FF);
\r
22 UCSR0B |= ((1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0));
\r
25 UCSR0A |= (1 << U2X0);
\r
27 UCSR0A &= ~(1 << U2X0);
\r
30 #define BUFSIZE (1024)
\r
32 inline void portd(int bit, int on) {
\r
42 long int numread = 0;
\r
43 inline void cts(int c) {
\r
55 static volatile int sending = 0;
\r
56 static volatile int32_t interrupt_count = 0;
\r
58 // RECV //////////////////////////////////////////////////////////////////////////////
\r
60 char read_buf[BUFSIZE];
\r
61 volatile int read_buf_head;
\r
62 volatile int read_buf_tail;
\r
63 char write_buf[BUFSIZE];
\r
64 volatile int write_buf_head;
\r
65 volatile int write_buf_tail;
\r
67 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }
\r
68 inline int read_full() { return inc(read_buf_tail)==read_buf_head; }
\r
69 inline int abs(int x) { return x<0 ? -x : x; }
\r
70 inline int read_size() { return read_buf_tail<read_buf_head ? (read_buf_head-read_buf_tail) : (read_buf_tail-read_buf_head); }
\r
71 inline int read_empty() { return read_buf_head==read_buf_tail; }
\r
72 inline int read_nearlyFull() {
\r
73 if (read_buf_tail==read_buf_head) return 0;
\r
74 if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2);
\r
75 return (read_buf_tail-read_buf_head) > (BUFSIZE/2);
\r
78 inline int write_full() { return inc(write_buf_tail)==write_buf_head; }
\r
79 inline int write_empty() { return write_buf_head==write_buf_tail; }
\r
80 inline int write_nearlyFull() {
\r
81 if (write_buf_tail==write_buf_head) return 0;
\r
82 if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2);
\r
83 return (write_buf_tail-write_buf_head) > (BUFSIZE/2);
\r
86 inline char recv() {
\r
89 while(read_empty()) cts(1);
\r
90 ret = read_buf[read_buf_head];
\r
91 read_buf_head = inc(read_buf_head);
\r
92 if (!read_nearlyFull()) cts(1);
\r
93 if (PORTE & (1<<3)) PORTE &= ~(1<<3);
\r
94 else PORTE |= (1<<3);
\r
98 ISR(SIG_UART0_DATA) {
\r
99 if (write_empty()) {
\r
100 UCSR0B &= ~(1 << UDRIE0);
\r
103 char ret = write_buf[write_buf_head];
\r
104 write_buf_head = inc(write_buf_head);
\r
109 void send(char c) {
\r
110 while (write_full());
\r
111 write_buf[write_buf_tail] = c;
\r
112 write_buf_tail = inc(write_buf_tail);
\r
113 if (PORTE & (1<<2)) PORTE &= ~(1<<2);
\r
114 else PORTE |= (1<<2);
\r
115 UCSR0B |= (1 << UDRIE0);
\r
119 void fpga_interrupts(int on) {
\r
120 if (on/* && interrupt_count<301*/) {
\r
133 write_buf_head = 0;
\r
134 write_buf_tail = 0;
\r
135 //initUART0(1, 0); //for slow board
\r
136 initUART0(0, 0); //for slow board
\r
137 //initUART0(0, 1); //for slow board
\r
140 void conf(int z, int y, int x, int d) {
\r
149 for(i=0; i<5; i++) {
\r
160 wdt_enable(WDTO_250MS);
\r
164 #define TIMERVAL 100
\r
167 ISR(SIG_FPGA_INTERRUPT15) {
\r
170 //PORTD = ~(interrupt_count & 0xff);
\r
171 //if (interrupt_count >= 301) fpga_interrupts(0);
\r
173 fpga_interrupts(1);
\r
176 ISR(SIG_OVERFLOW0) {
\r
177 fpga_interrupts(0);
\r
179 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
180 TIMSK |= (1<<TOIE0); // enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
181 if (sending) UDR1 = FISUA;
\r
182 fpga_interrupts(1);
\r
185 void init_timer() {
\r
186 TCCR0 |= (1<<CS00); // set the timer0 prescaler to CK
\r
187 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
188 TIMSK |= (1<<TOIE0); //enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
191 ISR(SIG_INTERRUPT0) { // use interrupt1 since interrupt0 is sent by the watchdog (I think)
\r
196 void die() { cli(); PORTE|=(1<<5); _delay_ms(2000); while(1) { portd(2,0); portd(2,1); } }
\r
198 ISR(SIG_UART0_RECV) {
\r
200 if (UCSR0A & (1 << FE0)) err = 201;//{ portd(2,0); portd(3,1); die(); } // framing error, lock up with LED=01
\r
201 if ((UCSR0A & (1 << OR0))) err = 202;//{ portd(2,1); portd(3,0); die(); } // overflow; lock up with LED=10
\r
202 if (read_full()) err = 203;//{ portd(2,1); portd(3,1); die(); } // buffer overrun
\r
204 read_buf[read_buf_tail] = UDR0;
\r
205 read_buf_tail = inc(read_buf_tail);
\r
206 if (read_nearlyFull()) cts(0);
\r
211 inline int hex(char c) {
\r
212 if (c >= '0' && c <= '9') return (c - '0');
\r
213 if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);
\r
214 if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);
\r
219 DDRE = (1<<7) | (1<<5) | (1<<3) | (1<<2);
\r
239 long int i = recv() & 0xff;
\r
240 //if (i < 0) err = 200;
\r
242 long int newi = oldi+1;
\r
243 if (newi >= 256) newi -= 256;
\r
244 if (i != newi) err++;
\r
248 send(err >= 10 ? 255 : i);
\r
263 //while(1) send(/*FISUA*/2);
\r
266 if (PORTE & (1<<6)) PORTE &= ~(1<<6);
\r
267 else PORTE |= (1<<6);
\r
282 //fpga_interrupts(0);
\r
286 //fpga_interrupts(1);
\r
299 int32_t local_interrupt_count = interrupt_count;
\r
300 interrupt_count = 0;
\r
301 send((local_interrupt_count >> 24) & 0xff);
\r
302 send((local_interrupt_count >> 16) & 0xff);
\r
303 send((local_interrupt_count >> 8) & 0xff);
\r
304 send((local_interrupt_count >> 0) & 0xff);
\r
305 fpga_interrupts(1);
\r
311 if ((r & 0x80) == 0x80) {
\r
312 switch (r & 0x44) {
\r
313 case 0x44: z = recv(); break;
\r
314 case 0x40: z++; break;
\r
315 case 0x04: z--; break;
\r
317 switch (r & 0x22) {
\r
318 case 0x22: y = recv(); break;
\r
319 case 0x20: y++; break;
\r
320 case 0x02: y--; break;
\r
322 switch (r & 0x11) {
\r
323 case 0x11: x = recv(); break;
\r
324 case 0x10: x++; break;
\r
325 case 0x01: x--; break;
\r