4 #include <util/delay.h>
7 unsigned char serial0_rx(){
8 while( !(UCSR0A & (1 << RXC0)) );
13 unsigned char serial1_rx(){
18 void serial0_tx(unsigned char x){
19 while (!(UCSR0A & (1<<UDRE0)) );
23 //! Transmit a byte on the second UART.
24 void serial1_tx(unsigned char x){
27 //! Set the baud rate.
28 void setbaud0(unsigned char rate){
32 UBRR0L = 4; /* 500,000 baud at 20MHz */
33 //UBRR0L = 1; /* 500,000 baud at 8MHz */
34 //UBRR0L = 103; /* 9600 baud */
35 // XXX UBRR0L = 8; /* 115200 baud ERROR RATE TOO HIGH */
38 UCSR0A = (1 << U2X0); /* double the baud rate */
39 UCSR0C = (3 << UCSZ00); /* 8N1 */
41 /* enabling rx/tx must be done after frame/baud setup */
42 UCSR0B = ((1 << TXEN0) | (1 << RXEN0));
48 //! Set the baud rate of the second uart.
49 void setbaud1(unsigned char rate){
50 //http://mspgcc.sourceforge.net/baudrate.html
72 void donbfet_init_uart0(){
74 _delay_ms(500); //takes a bit to stabilize
80 PLEDOUT |= (1 << PLEDPIN);
86 PLEDOUT &= ~(1 << PLEDPIN);
94 /* explicitly clear interrupts */
97 /* move the vectors */
99 /* move interrupts from boot flash section */
101 /* you MUST use a variable during this process. even highly optimized,
102 * masking the bit, shifting, ANDing, and setting MCUCR will exceed
103 * 4 CPU cycles! set a variable with the desired value for MCUCR and
104 * then set the register once IVCE is enabled
106 x = MCUCR & ~(1 << IVSEL);
108 /* enable change of interrupt vectors */
109 /* NOTE: setting IVCE disables interrupts until the bit is auto-unset
110 * 4 cycles after being set or after IVSEL is written
112 MCUCR |= (1 << IVCE);
115 /* disable the watchdog timer; this macro will disable interrupts for us */
116 /* NOTE: ensure that the WDRF flag is unset in the MCUSR or we will spinlock
117 * when the watchdog times out
119 MCUSR &= ~(1 << WDRF);
123 donbfet_init_uart0();
125 /* set the LED as an output */
126 PLEDDIR |= (1 << PLEDPIN);
127 PLEDOUT |= (1 << PLEDPIN);
129 /* explicitly enable interrupts */
136 MCUSR &= ~(1 << WDRF);
137 wdt_enable(WDTO_15MS);
142 void donbfet_init_uart1(){
146 donbfet_get_byte(uint16_t v)
149 /* we are only passed in a 16bit word. should
150 * be increased to 32bit if we want to handle
153 return pgm_read_byte_near(v);
160 /* ATmega644P has 4K SRAM */
161 return (int * )0x1000;