4 #include <util/delay.h>
7 unsigned char serial0_rx(){
8 while( !(UCSR0A & (1 << RXC0)) );
14 void serial0_tx(unsigned char x){
15 while (!(UCSR0A & (1<<UDRE0)) );
20 //! Set the baud rate.
21 void setbaud0(unsigned char rate){
22 /* disable everything briefly */
46 /* enabling rx/tx must be done before frame/baud setup */
47 UCSR0B = ((1 << TXEN0) | (1 << RXEN0));
49 UCSR0A = (1 << U2X0); /* double the baud rate */
50 UCSR0C = (3 << UCSZ00); /* 8N1 */
52 UBRR0L = (int8_t) (F_CPU/(r*8L)-1);
53 UBRR0H = (F_CPU/(r*8L)-1) >> 8;
60 void zigduino_init_uart0(){
62 _delay_ms(500); //takes a bit to stabilize
67 PLEDDIR |= (1 << PLEDPIN);
71 PLEDOUT |= (1 << PLEDPIN);
75 PLEDOUT &= ~(1 << PLEDPIN);
81 /* explicitly clear interrupts */
84 /* move the vectors */
86 /* move interrupts from boot flash section */
88 /* you MUST use a variable during this process. even highly optimized,
89 * masking the bit, shifting, ANDing, and setting MCUCR will exceed
90 * 4 CPU cycles! set a variable with the desired value for MCUCR and
91 * then set the register once IVCE is enabled
93 x = MCUCR & ~(1 << IVSEL);
95 /* enable change of interrupt vectors */
96 /* NOTE: setting IVCE disables interrupts until the bit is auto-unset
97 * 4 cycles after being set or after IVSEL is written
102 /* disable the watchdog timer; this macro will disable interrupts for us */
103 /* NOTE: ensure that the WDRF flag is unset in the MCUSR or we will spinlock
104 * when the watchdog times out
106 MCUSR &= ~(1 << WDRF);
110 zigduino_init_uart0();
112 /* set the LED as an output */
115 /* enable internal internal pull-up resister
116 in order to supress line noise that prevents
117 bootloader from timing out */
118 SPIDIR &= ~(1 << SPIPIN);
119 SPIOUT |= (1 << SPIPIN);
121 /* explicitly enable interrupts */
129 MCUSR &= ~(1 << WDRF);
130 wdt_enable(WDTO_15MS);
136 zigduino_ramend(void)
139 /* ATmega128rfa1 has 16K SRAM */
140 return (int * )0x4000;