4 #include <avr/interrupt.h>
11 /* Ring buffer for received data. */
16 /* ........incoming.......
17 * ^head ^head+bytes */
18 unsigned char buffer
[BUF_SIZE
];
21 } buf
= { { 0 }, 0, 0 };
23 static inline void store_char(uint8_t data
)
25 if (buf
.bytes
>= BUF_SIZE
)
28 int i
= (buf
.head
+ buf
.bytes
) % BUF_SIZE
;
36 /* USART hardware handling. */
38 #define BAUD 38400 // Rychlost prenosu dat
39 #define MYUBRR (F_CPU/16/BAUD-1) // Konstanta pouzita pro nastaveni prenosu
41 static void USART0_Init(uint16_t ubrr
) { // fukce pro inicializaci USART0 komunikace
42 UBRR0H
= (uint8_t) (ubrr
>> 8);
43 UBRR0L
= (uint8_t) ubrr
;
45 UCSR0C
= (1<<UCSZ01
)|(1<<UCSZ00
) | (1<<UPM01
)|(1<<UPM00
)/* | (1<<USBS0)*/; // 8 N 1
46 UCSR0A
= 0; // no special stuff
48 UCSR0B
= (1<<RXEN0
)|(1<<TXEN0
)
55 static void USART0_Transmit(uint8_t data
) { // poslani dat po USART0
56 while (!(UCSR0A
& (1<<UDRE0
))) ; // cekani na odesilaci buffer
57 UDR0
= data
; // zapsani dat = odeslani dat po USART0
61 SIGNAL(SIG_UART0_RECV
)
63 unsigned char data
= UDR0
;
65 if (data
>= 'A' && data
<= 'Z')
67 USART0_Transmit(data
);
74 void serial_init(void)
79 void serial_send(uint8_t data
)
81 USART0_Transmit(data
);
84 int serial_waiting(void)
87 return UCSR0A
& _BV(RXC0
);
93 uint8_t serial_recv(void)
97 while (!serial_waiting()) x
++;
101 if (data
>= 'A' && data
<= 'Z')
103 USART0_Transmit(data
);
107 // cli-sti is actually not required, this is race-free!
108 uint8_t data
= buf
.buffer
[buf
.head
];
110 buf
.head
= (buf
.head
+ 1) % BUF_SIZE
;