#include <avr/io.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
*/\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
+#define BUFSIZE (1024)\r
\r
inline void portd(int bit, int on) {\r
if (on) {\r
}\r
}\r
\r
+\r
+// RECV //////////////////////////////////////////////////////////////////////////////\r
+\r
+char read_buf[BUFSIZE];\r
+volatile int read_buf_head;\r
+volatile int read_buf_tail;\r
+char write_buf[BUFSIZE];\r
+volatile int write_buf_head;\r
+volatile int write_buf_tail;\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
+inline int read_full() { return inc(read_buf_tail)==read_buf_head; }\r
+inline int read_empty() { return read_buf_head==read_buf_tail; }\r
+inline int read_nearlyFull() {\r
+ if (read_buf_tail==read_buf_head) return 0;\r
+ if (read_buf_tail < read_buf_head) return (read_buf_head-read_buf_tail) < (BUFSIZE/2);\r
+ return (read_buf_tail-read_buf_head) > (BUFSIZE/2);\r
}\r
-inline int empty() { return head==tail; }\r
-\r
+/*\r
+inline int write_full() { return inc(write_buf_tail)==write_buf_head; }\r
+inline int write_empty() { return write_buf_head==write_buf_tail; }\r
+inline int write_nearlyFull() {\r
+ if (write_buf_tail==write_buf_head) return 0;\r
+ if (write_buf_tail < write_buf_head) return (write_buf_head-write_buf_tail) < (BUFSIZE/2);\r
+ return (write_buf_tail-write_buf_head) > (BUFSIZE/2);\r
+}\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
+ while(read_empty()) cts(1);\r
+ ret = read_buf[read_buf_head];\r
+ read_buf_head = inc(read_buf_head);\r
+ if (!read_nearlyFull()) cts(0);\r
return ret;\r
}\r
\r
+void send(char c) {\r
+\r
+ write_buf[write_buf_tail] = c;\r
+ write_buf_tail = inc(write_buf_tail);\r
+\r
+ char ret = write_buf[write_buf_head];\r
+ write_buf_head = inc(write_buf_head);\r
+\r
+ while(!(UCSR1A & (1 << UDRE1))); /* Wait for data Regiester to be empty */\r
+ UDR1 = (int)ret;\r
+}\r
+\r
+\r
void init() {\r
+ read_buf_head = 0;\r
+ read_buf_tail = 0;\r
+ write_buf_head = 0;\r
+ write_buf_tail = 0;\r
EIMF = 0xFF; /* Enalbe External Interrrupt*/ \r
DDRD = 0xFF; /* Configure PORTD as Output */\r
DDRE = 1 << 4; /* ability to write to E */\r
doreset();\r
}\r
\r
-void die() { cli(); cts(0); _delay_ms(2000); while(1) { } }\r
+void die() { cli(); cts(0); _delay_ms(2000); while(1) { portd(2,0); portd(2,1); } }\r
ISR(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
+ if (read_full()) { portd(2,1); portd(3,1); die(); } // buffer overrun\r
+ read_buf[read_buf_tail] = UDR1;\r
+ read_buf_tail = inc(read_buf_tail);\r
SREG |= 0x80;\r
sei();\r
- if (nearlyFull()) cts(0);\r
+ if (read_nearlyFull()) cts(0);\r
}\r
\r
inline int hex(char c) {\r