source: avrstuff/libs/usart/usart.c@ c956fd4

main
Last change on this file since c956fd4 was c956fd4, checked in by Adrien Destugues <pulkomandy@…>, 5 years ago

usart: add suport for AT90USB1287

  • Property mode set to 100644
File size: 2.1 KB
Line 
1/* AVR USART acces functions - stolen from Extreme Electronics */
2
3#include <avr/io.h>
4#include <inttypes.h>
5
6#ifndef BAUD
7 #error You must define BAUD to use libuart.
8#endif
9
10// FIXME there are two USARTs in this case and we should be able to drive either
11
12// Compatibility for devices with single USART only
13#ifdef __AVR_ATmega8__
14#define UBRR0H UBRRH
15#define UBRR0L UBRRL
16#define UCSR0A UCSRA
17#define UCSR0C UCSRC
18#define UCSR0B UCSRB
19#define UDR0 UDR
20#define UDRE0 UDRE
21#define RXC0 RXC
22#endif
23
24#ifdef __AVR_AT90USB1287__
25#define UBRR0H UBRR1H
26#define UBRR0L UBRR1L
27#define UCSR0A UCSR1A
28#define UCSR0C UCSR1C
29#define UCSR0B UCSR1B
30#define UDR0 UDR1
31#define UDRE0 UDRE1
32#define RXC0 RXC1
33
34#define RXEN RXEN1
35#define TXEN TXEN1
36#define U2X U2X1
37#endif
38
39void USARTInit()
40{
41 //Set Baud rate
42 #include <util/setbaud.h>
43 UBRR0H = UBRRH_VALUE;
44 UBRR0L = UBRRL_VALUE;
45 #if USE_2X
46 UCSR0A |= (1 << U2X);
47 #else
48 UCSR0A &= ~(1 << U2X);
49 #endif
50
51 /*Set Frame Format
52 >> Asynchronous mode
53 >> No Parity
54 >> 1 StopBit
55 >> char size 8
56 */
57#if defined(__AVR_ATtiny2313__)
58 UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
59#elif defined __AVR_ATmega128__
60 UCSR0C = (2 << UCSZ0) | (2 << UPM0) ; /* 7 bits + even parity.
61 TODO make this configurable. */
62#elif defined(__AVR_AT90USB1287__)
63 UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);
64#else
65 UCSR0C = (1 << URSEL) | (3 << UCSZ0);
66#endif
67
68 //Enable The receiver and transmitter
69 UCSR0B = (1 << RXEN) | (1 << TXEN);
70}
71
72
73void USARTWriteChar(char data)
74{
75 //Wait untill the transmitter is ready
76 while(!(UCSR0A & (1<<UDRE0)))
77 {
78 //Do nothing
79 }
80
81 //Now write the data to USART buffer
82
83 UDR0=data;
84}
85
86
87char USARTReadChar()
88{
89 //Wait untill a data is available
90 while(!(UCSR0A & (1<<RXC0)))
91 {
92 //Do nothing
93 }
94
95 //Now USART has got data from host
96 //and is available is buffer
97
98 return UDR0;
99}
100
101
102void USARTWriteHex(unsigned char i)
103{
104 unsigned char k = i>>4;
105 if (k <=9) k+='0';
106 else k+=('A'-10);
107
108 USARTWriteChar(k);
109
110 k = i&0xF;
111 if (k <=9) k+='0';
112 else k+=('A'-10);
113
114 USARTWriteChar(k);
115}
Note: See TracBrowser for help on using the repository browser.