Subversion Repositories FlightCtrl

Rev

Rev 942 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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