Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
684 | killagreg | 1 | #include "main.h" |
2 | #include "uart1.h" |
||
3 | #include "fifo.h" |
||
4 | #include "ubx.h" |
||
5 | |||
6 | |||
7 | |||
8 | // FIFO-objects and buffers for input and output |
||
9 | |||
10 | //#define BUFSIZE_IN 0x96 |
||
11 | //volatile uint8_t inbuf[BUFSIZE_IN]; |
||
12 | //fifo_t infifo; |
||
13 | |||
14 | #define BUFSIZE_OUT 0x96 |
||
15 | volatile uint8_t outbuf[BUFSIZE_OUT]; |
||
16 | fifo_t outfifo; |
||
17 | |||
18 | /****************************************************************/ |
||
19 | /* Initialization of the USART1 */ |
||
20 | /****************************************************************/ |
||
21 | void USART1_Init (void) |
||
22 | { |
||
23 | // USART1 Control and Status Register A, B, C and baud rate register |
||
24 | uint8_t sreg = SREG; |
||
25 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART1_BAUD) - 1); |
||
26 | |||
27 | // disable all interrupts before reconfiguration |
||
28 | cli(); |
||
29 | |||
30 | // disable RX-Interrupt |
||
31 | UCSR1B &= ~(1 << RXCIE1); |
||
32 | // disable TX-Interrupt |
||
33 | UCSR1B &= ~(1 << TXCIE1); |
||
34 | // disable DRE-Interrupt |
||
35 | UCSR1B |= (1 << UDRIE1); |
||
36 | |||
37 | // disable receiver and transmitter (will flush the buffers) |
||
38 | UCSR1B &= ~((1 << TXEN1) | (1 << RXEN1)); |
||
39 | |||
40 | // set direction of RXD1 and TXD1 pins |
||
41 | // set RXD1 (PD2) as an input pin |
||
42 | PORTD |= (1 << PORTD2); |
||
43 | DDRD &= ~(1 << DDD2); |
||
44 | // set TXD1 (PD3) as an output pin |
||
45 | PORTD |= (1 << PORTD3); |
||
46 | DDRD |= (1 << DDD3); |
||
47 | |||
48 | // USART0 Baud Rate Register |
||
49 | // set clock divider |
||
50 | UBRR1H = (uint8_t)(ubrr>>8); |
||
51 | UBRR1L = (uint8_t)ubrr; |
||
52 | |||
53 | // enable double speed operation |
||
54 | UCSR1A |= (1 << U2X1); |
||
55 | // enable receiver and transmitter |
||
56 | UCSR1B = (1 << TXEN1) | (1 << RXEN1); |
||
57 | // set asynchronous mode |
||
58 | UCSR1C &= ~(1 << UMSEL11); |
||
59 | UCSR1C &= ~(1 << UMSEL10); |
||
60 | // no parity |
||
61 | UCSR1C &= ~(1 << UPM11); |
||
62 | UCSR1C &= ~(1 << UPM10); |
||
63 | // 1 stop bit |
||
64 | UCSR1C &= ~(1 << USBS1); |
||
65 | // 8-bit |
||
66 | UCSR1B &= ~(1 << UCSZ12); |
||
67 | UCSR1C |= (1 << UCSZ11); |
||
68 | UCSR1C |= (1 << UCSZ10); |
||
69 | |||
70 | // flush receive buffer explicit |
||
71 | while ( UCSR1A & (1<<RXC1) ) UDR1; |
||
72 | |||
73 | // enable interrupts at the end |
||
74 | // enable RX-Interrupt |
||
75 | UCSR1B |= (1 << RXCIE1); |
||
76 | // enable TX-Interrupt |
||
77 | //UCSR1B |= (1 << TXCIE1); |
||
78 | // enable DRE interrupt |
||
79 | //UCSR1B |= (1 << UDRIE1); |
||
80 | |||
81 | |||
82 | // restore global interrupt flags |
||
83 | SREG = sreg; |
||
84 | |||
85 | // inint FIFO buffer |
||
86 | //fifo_init (&infifo, inbuf, BUFSIZE_IN); |
||
87 | fifo_init (&outfifo, outbuf, BUFSIZE_OUT); |
||
88 | } |
||
89 | |||
90 | int16_t USART1_putc (const uint8_t c) |
||
91 | { |
||
92 | int16_t ret = fifo_put (&outfifo, c); |
||
93 | // create an data register empty interrupt |
||
94 | UCSR1B |= (1 << UDRIE1); |
||
95 | |||
96 | return ret; |
||
97 | } |
||
98 | |||
99 | /*int16_t USART1_getc_nowait () |
||
100 | { |
||
101 | return fifo_get_nowait (&infifo); |
||
102 | } |
||
103 | |||
104 | |||
105 | uint8_t USART1_getc_wait () |
||
106 | { |
||
107 | return fifo_get_wait (&infifo); |
||
108 | } |
||
109 | */ |
||
110 | |||
111 | /****************************************************************/ |
||
112 | /* USART1 data register empty ISR */ |
||
113 | /****************************************************************/ |
||
114 | ISR(USART1_UDRE_vect) |
||
115 | { |
||
116 | // Move a character from the output buffer to the data register. |
||
117 | // When the character was processed the next interrupt is generated. |
||
118 | // If the output buffer is empty the DRE-interrupt is disabled. |
||
119 | if (outfifo.count > 0) |
||
120 | UDR1 = _inline_fifo_get (&outfifo); |
||
121 | else |
||
122 | UCSR1B &= ~(1 << UDRIE1); |
||
123 | } |
||
124 | |||
125 | /****************************************************************/ |
||
126 | /* USART1 transmitter ISR */ |
||
127 | /****************************************************************/ |
||
128 | ISR(USART1_TX_vect) |
||
129 | { |
||
130 | |||
131 | } |
||
132 | |||
133 | /****************************************************************/ |
||
134 | /* USART1 receiver ISR */ |
||
135 | /****************************************************************/ |
||
136 | ISR(USART1_RX_vect) |
||
137 | { |
||
138 | uint8_t c; |
||
139 | c = UDR0; // get data byte |
||
140 | ubx_parser(c); // and put it into the ubx protocol parser |
||
141 | } |