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
82 PLEDOUT |= (1 << PLEDPIN);
86 PLEDOUT &= ~(1 << PLEDPIN);
92 /* explicitly clear interrupts */
95 /* move the vectors */
97 /* move interrupts from boot flash section */
99 /* you MUST use a variable during this process. even highly optimized,
100 * masking the bit, shifting, ANDing, and setting MCUCR will exceed
101 * 4 CPU cycles! set a variable with the desired value for MCUCR and
102 * then set the register once IVCE is enabled
104 x = MCUCR & ~(1 << IVSEL);
106 /* enable change of interrupt vectors */
107 /* NOTE: setting IVCE disables interrupts until the bit is auto-unset
108 * 4 cycles after being set or after IVSEL is written
110 MCUCR |= (1 << IVCE);
113 /* disable the watchdog timer; this macro will disable interrupts for us */
114 /* NOTE: ensure that the WDRF flag is unset in the MCUSR or we will spinlock
115 * when the watchdog times out
117 MCUSR &= ~(1 << WDRF);
121 donbfet_init_uart0();
123 /* set the LED as an output */
124 PLEDDIR |= (1 << PLEDPIN);
125 PLEDOUT |= (1 << PLEDPIN);
127 /* explicitly enable interrupts */
134 MCUSR &= ~(1 << WDRF);
135 wdt_enable(WDTO_15MS);
140 void donbfet_init_uart1(){
144 donbfet_get_byte(uint16_t v)
147 /* we are only passed in a 16bit word. should
148 * be increased to 32bit if we want to handle
151 /* XXX should be far on the 1284P, but there are bugs with flash reads using _far */
152 /* XXX until the bugs are figured out (probably my fault?) use _near */
153 return pgm_read_byte_near(v);
160 /* ATmega1284P has 16K SRAM */
161 return (int * )0x4000;