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