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
16 volatile int32_t upper = 0;
\r
20 void initUART0(unsigned int baudRate, unsigned int doubleRate) {
\r
21 UBRRHI = (((baudRate) >> 8) & 0x000F);
\r
22 UBRR0 = ((baudRate) & 0x00FF);
\r
23 UCSR0B |= ((1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0));
\r
26 UCSR0A |= (1 << U2X0);
\r
28 UCSR0A &= ~(1 << U2X0);
\r
31 #define BUFSIZE (1024)
\r
33 long int numread = 0;
\r
34 inline void cts(int c) {
\r
44 static volatile int sending = 0;
\r
45 static volatile int32_t interrupt_count = 0;
\r
47 // RECV //////////////////////////////////////////////////////////////////////////////
\r
49 char read_buf[BUFSIZE];
\r
50 volatile int read_buf_head;
\r
51 volatile int read_buf_tail;
\r
52 char write_buf[BUFSIZE];
\r
53 volatile int write_buf_head;
\r
54 volatile int write_buf_tail;
\r
56 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }
\r
57 inline int read_full() { return inc(read_buf_tail)==read_buf_head; }
\r
58 inline int abs(int x) { return x<0 ? -x : x; }
\r
59 inline int read_size() { return read_buf_tail<read_buf_head ? (read_buf_head-read_buf_tail) : (read_buf_tail-read_buf_head); }
\r
60 inline int read_empty() { return read_buf_head==read_buf_tail; }
\r
61 inline int read_nearlyFull() {
\r
62 if (read_buf_tail==read_buf_head) return 0;
\r
63 if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2);
\r
64 return (read_buf_tail-read_buf_head) > (BUFSIZE/2);
\r
67 inline int write_full() { return inc(write_buf_tail)==write_buf_head; }
\r
68 inline int write_empty() { return write_buf_head==write_buf_tail; }
\r
69 inline int write_nearlyFull() {
\r
70 if (write_buf_tail==write_buf_head) return 0;
\r
71 if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2);
\r
72 return (write_buf_tail-write_buf_head) > (BUFSIZE/2);
\r
77 inline char recv() {
\r
82 while(read_empty()) cts(1);
\r
85 ret = read_buf[read_buf_head];
\r
86 read_buf_head = inc(read_buf_head);
\r
87 if (!read_nearlyFull()) cts(1);
\r
91 // Interrupt Handlers //////////////////////////////////////////////////////////////////////////////
\r
93 ISR(SIG_UART0_DATA) {
\r
94 if (write_empty()) {
\r
95 UCSR0B &= ~(1 << UDRIE0);
\r
98 char ret = write_buf[write_buf_head];
\r
99 write_buf_head = inc(write_buf_head);
\r
104 void send(char c) {
\r
106 while (write_full());
\r
108 write_buf[write_buf_tail] = c;
\r
109 write_buf_tail = inc(write_buf_tail);
\r
110 UCSR0B |= (1 << UDRIE0);
\r
114 void fpga_interrupts(int on) {
\r
125 inline void conf(int z, int y, int x, int d) {
\r
132 #define TIMERVAL 100
\r
134 ISR(SIG_FPGA_INTERRUPT0) {
\r
139 volatile int dead = 0;
\r
141 ISR(SIG_OVERFLOW1) {
\r
145 if (PORTE & (1<<5)) PORTE &= ~(1<<5);
\r
146 else PORTE |= (1<<5);
\r
153 //void die() { dead = 1; cli(); PORTE|=(1<<5); _delay_ms(2000); while(1) { } }
\r
155 void die(int two, int three, int five) {
\r
157 PORTE &~ ((1<<2) | (1<<3) | (1<<5));
\r
158 if (two) PORTE |= (1<<2);
\r
159 if (three) PORTE |= (1<<3);
\r
160 if (five) PORTE |= (1<<5);
\r
164 ISR(SIG_UART0_RECV) {
\r
165 if (UCSR0A & (1 << FE0)) die(0, 0, 1);
\r
166 if ((UCSR0A & (1 << OR0))) die(1, 1, 1);
\r
167 if (read_full()) die(1, 0, 1);
\r
169 read_buf[read_buf_tail] = UDR0;
\r
170 read_buf_tail = inc(read_buf_tail);
\r
171 if (read_nearlyFull()) cts(0);
\r
176 inline int hex(char c) {
\r
177 if (c >= '0' && c <= '9') return (c - '0');
\r
178 if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);
\r
179 if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);
\r
184 fpga_interrupts(0);
\r
186 fpga_interrupts(1);
\r
191 DDRE = (1<<7) | (1<<5) | (1<<3) | (1<<2);
\r
198 write_buf_head = 0;
\r
199 write_buf_tail = 0;
\r
200 initUART0(1, 0); //for slow board
\r
226 fpga_interrupts(1);
\r
227 if (flag) die(0, 1, 1);
\r
244 int32_t local_interrupt_count = interrupt_count;
\r
245 interrupt_count = 0;
\r
246 send((local_interrupt_count >> 24) & 0xff);
\r
247 send((local_interrupt_count >> 16) & 0xff);
\r
248 send((local_interrupt_count >> 8) & 0xff);
\r
249 send((local_interrupt_count >> 0) & 0xff);
\r
251 int32_t local_timer = TCNT1;
\r
252 int32_t local_upper = upper;
\r
259 send((local_upper >> 8) & 0xff);
\r
260 send((local_upper >> 0) & 0xff);
\r
261 send((local_timer >> 8) & 0xff);
\r
262 send((local_timer >> 0) & 0xff);
\r