00001
00029
00030
00031 #include "config.h"
00032 #include <avr/io.h>
00033 #include <util/delay.h>
00034
00035 #include "roboot_if_comm.h"
00036
00037 #define BAUD BOOTLOADER_USART_SPEED
00038
00039 #if BOOTLOADER_USART == USART_0
00040 #define UBRRH UBRR0H
00041 #define UBRRL UBRR0L
00042 #define UCSRA UCSR0A
00043 #define UCSRB UCSR0B
00044 #define UCSRC UCSR0C
00045 #define UDR UDR0
00046 #elif BOOTLOADER_USART == USART_1
00047 #define UBRRH UBRR1H
00048 #define UBRRL UBRR1L
00049 #define UCSRA UCSR1A
00050 #define UCSRB UCSR1B
00051 #define UCSRC UCSR1C
00052 #define UDR UDR1
00053 #else
00054 #error "Wrong USART configured"
00055 #endif
00056
00057
00058
00059 void CommHwInit(void)
00060 {
00061
00062 UCSRB = 0;
00063
00064 #include <util/setbaud.h>
00065 UBRRH = UBRRH_VALUE;
00066 UBRRL = UBRRL_VALUE;
00067 UCSRA = 0;
00068 #if USE_2X
00069 UCSRA |= (1 << U2X);
00070 #endif
00071
00072 UCSRC = _BV(UCSZ1) | _BV(UCSZ0);
00073 UCSRB = _BV(RXEN) | _BV(TXEN);
00074
00075 while (UCSRA & _BV(RXC))
00076 {
00077 (void) UDR;
00078 }
00079
00080 }
00081
00082
00083
00084
00085 void CommHwDeinit(void)
00086 {
00087
00088 UCSRA = 0;
00089 UCSRB = 0;
00090 UCSRC = _BV(UCSZ1) | _BV(UCSZ0);
00091 UBRRH = 0;
00092 UBRRL = 0;
00093
00094 }
00095
00096
00097
00098 void CommHwSendByte(uint8_t c)
00099 {
00100 while (0 == (UCSRA & _BV(UDRE)))
00101 {
00102 ;
00103 }
00104
00105 UDR = c;
00106 }
00107
00108
00109
00110
00111 int16_t CommHwGetByte(void)
00112 {
00113 uint8_t a_byte;
00114
00115 if (UCSRA & _BV(RXC))
00116 {
00117 a_byte = UDR;
00118 return a_byte;
00119 }
00120 else
00121 {
00122 return -1;
00123 }
00124 }
00125
00126
00127
00128 void MCUBusyDelay(void)
00129 {
00130 _delay_us(10);
00131 }
00132