Subversion Repositories FlightCtrl

Rev

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
39
        UCSR1B |= (1 << UDRIE1);
40
 
41
        // disable receiver and transmitter (will flush the buffers)
42
        UCSR1B &= ~((1 << TXEN1) | (1 << RXEN1));
43
 
44
        // set direction of RXD1 and TXD1 pins
45
        // set RXD1 (PD2) as an input pin
701 killagreg 46
        DDRD &= ~(1 << DDD2);
684 killagreg 47
        PORTD |= (1 << PORTD2);
701 killagreg 48
 
684 killagreg 49
        // set TXD1 (PD3) as an output pin
701 killagreg 50
        DDRD  |= (1 << DDD3);
684 killagreg 51
        PORTD |= (1 << PORTD3);
52
 
53
        // USART0 Baud Rate Register
54
        // set clock divider
55
        UBRR1H = (uint8_t)(ubrr>>8);
56
        UBRR1L = (uint8_t)ubrr;
57
 
58
        // enable double speed operation
59
        UCSR1A |= (1 << U2X1);
60
        // enable receiver and transmitter
61
        UCSR1B = (1 << TXEN1) | (1 << RXEN1);
62
        // set asynchronous mode
63
        UCSR1C &= ~(1 << UMSEL11);
64
        UCSR1C &= ~(1 << UMSEL10);
65
        // no parity
66
        UCSR1C &= ~(1 << UPM11);
67
        UCSR1C &= ~(1 << UPM10);
68
        // 1 stop bit
69
        UCSR1C &= ~(1 << USBS1);
70
        // 8-bit
71
        UCSR1B &= ~(1 << UCSZ12);
72
        UCSR1C |=  (1 << UCSZ11);
73
        UCSR1C |=  (1 << UCSZ10);
74
 
75
        // flush receive buffer explicit
76
        while ( UCSR1A & (1<<RXC1) ) UDR1;
77
 
78
        // enable interrupts at the end
79
        // enable RX-Interrupt
80
        UCSR1B |= (1 << RXCIE1);
81
        // enable TX-Interrupt
82
        //UCSR1B |= (1 << TXCIE1);
83
        // enable DRE interrupt
84
        //UCSR1B |= (1 << UDRIE1);
85
 
86
 
87
        // restore global interrupt flags
88
    SREG = sreg;
89
 
90
    // inint FIFO buffer
91
        //fifo_init (&infifo,   inbuf, BUFSIZE_IN);
92
    fifo_init (&outfifo, outbuf, BUFSIZE_OUT);
93
}
94
 
95
int16_t USART1_putc (const uint8_t c)
96
{
97
    int16_t ret = fifo_put (&outfifo, c);
98
    // create an data register empty interrupt
99
    UCSR1B |= (1 << UDRIE1);
100
 
101
    return ret;
102
}
103
 
104
/*int16_t USART1_getc_nowait ()
105
{
106
    return fifo_get_nowait (&infifo);
107
}
108
 
109
 
110
uint8_t USART1_getc_wait ()
111
{
112
    return fifo_get_wait (&infifo);
113
}
114
*/
115
 
116
/****************************************************************/
117
/*               USART1 data register empty ISR                 */
118
/****************************************************************/
119
ISR(USART1_UDRE_vect)
120
{
121
// Move a character from the output buffer to the data register.
122
// When the character was processed the next interrupt is generated.
123
// If the output buffer is empty the DRE-interrupt is disabled.
124
    if (outfifo.count > 0)
125
       UDR1 = _inline_fifo_get (&outfifo);
126
    else
127
        UCSR1B &= ~(1 << UDRIE1);
128
}
129
 
130
/****************************************************************/
131
/*               USART1 transmitter ISR                         */
132
/****************************************************************/
133
ISR(USART1_TX_vect)
134
{
135
 
136
}
137
 
138
/****************************************************************/
139
/*               USART1 receiver ISR                            */
140
/****************************************************************/
141
ISR(USART1_RX_vect)
142
{
143
        uint8_t c;
144
        c = UDR0; // get data byte
145
        ubx_parser(c); // and put it into the ubx protocol parser
146
}