Subversion Repositories Projects

Rev

Rev 2225 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2225 Rev 2598
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009-2016 by Claas Anders "CaScAdE" Rathje               *
2
 *   Copyright (C) 2009-2017 by Claas Anders "CaScAdE" Rathje               *
3
 *   admiralcascade@gmail.com                                               *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
6
 *   This program is free software; you can redistribute it and/or modify   *
7
 *   it under the terms of the GNU General Public License as published by   *
7
 *   it under the terms of the GNU General Public License as published by   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 ****************************************************************************/
19
 ****************************************************************************/
20
 
20
 
21
#include "main.h"
21
#include "main.h"
22
#include <avr/io.h>
22
#include <avr/io.h>
23
#include <avr/interrupt.h>
23
#include <avr/interrupt.h>
24
#include <util/delay.h>
24
#include <util/delay.h>
25
#include "usart0.h"
25
#include "usart0.h"
26
 
26
 
27
#ifdef SERIALDEBUGDRAW
27
#ifdef SERIALDEBUGDRAW
28
#define USART0ENABLE 1
28
#define USART0ENABLE 1
29
#endif
29
#endif
30
#ifdef ANTENNATRACKTEST
30
#ifdef ANTENNATRACKTEST
31
#define USART0ENABLE 1
31
#define USART0ENABLE 1
32
#endif
32
#endif
33
 
33
 
34
 
34
 
35
 
35
 
36
#ifdef USART0ENABLE
36
#ifdef USART0ENABLE
37
 
37
 
38
 
38
 
39
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
39
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
40
 
40
 
41
 
41
 
42
/* ##########################################################################
42
/* ##########################################################################
43
 * USART0 stuff
43
 * USART0 stuff
44
 * ##########################################################################*/
44
 * ##########################################################################*/
45
 
45
 
46
/**
46
/**
47
 * init usart1
47
 * init usart1
48
 */
48
 */
49
void usart0_init() {
49
void usart0_init() {
50
    UBRR0H = ((F_CPU / (16UL * baud)) - 1) >> 8;
50
    UBRR0H = ((F_CPU / (16UL * baud)) - 1) >> 8;
51
    UBRR0L = (F_CPU / (16UL * baud)) - 1;
51
    UBRR0L = (F_CPU / (16UL * baud)) - 1;
52
 
52
 
53
    // Enable receiver and transmitter; enable RX interrupt
53
    // Enable receiver and transmitter; enable RX interrupt
54
    UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
54
    UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
55
 
55
 
56
    //asynchronous 8N1
56
    //asynchronous 8N1
57
    UCSR0C = (1 << URSEL0) | (3 << UCSZ00);
57
    UCSR0C = (1 << URSEL0) | (3 << UCSZ00);
58
 
58
 
59
 
59
 
60
    DDRD |= (1 << DDD1); // set TXD pin as output
60
    DDRD |= (1 << DDD1); // set TXD pin as output
61
    PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin
61
    PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin
62
}
62
}
63
 
63
 
64
 
64
 
65
/**
65
/**
66
 * send a single <character> through usart1
66
 * send a single <character> through usart1
67
 */
67
 */
68
void usart0_putc(unsigned char character) {
68
void usart0_putc(unsigned char character) {
69
    // wait until UDR ready
69
    // wait until UDR ready
70
    while (!(UCSR0A & (1 << UDRE0)));
70
    while (!(UCSR0A & (1 << UDRE0)));
71
    UDR0 = character;
71
    UDR0 = character;
72
}
72
}
73
 
73
 
74
/**
74
/**
75
 * send a <string> throught usart0
75
 * send a <string> throught usart0
76
 */
76
 */
77
void usart0_puts(char *s) {
77
void usart0_puts(char *s) {
78
    while (*s) {
78
    while (*s) {
79
        usart0_putc(*s);
79
        usart0_putc(*s);
80
        s++;
80
        s++;
81
    }
81
    }
82
}
82
}
83
 
83
 
84
/**
84
/**
85
 * send a PGM<string> throught usart1
85
 * send a PGM<string> throught usart1
86
 */
86
 */
87
void usart0_puts_pgm(const char* string) {
87
void usart0_puts_pgm(const char* string) {
88
    while (pgm_read_byte(string) != 0x00)
88
    while (pgm_read_byte(string) != 0x00)
89
        usart0_putc(pgm_read_byte(string++));
89
        usart0_putc(pgm_read_byte(string++));
90
}
90
}
91
 
91
 
92
/**
92
/**
93
 * transmit interrupt handler
93
 * transmit interrupt handler
94
 * unused
94
 * unused
95
 */
95
 */
96
ISR(USART0_TXC_vect) {
96
ISR(USART0_TXC_vect) {
97
}
97
}
98
 
98
 
99
/*
99
/*
100
 * receive data through usart1
100
 * receive data through usart1
101
 * portions taken and adapted from
101
 * portions taken and adapted from
102
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c
102
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c
103
 */
103
 */
104
ISR(USART0_RXC_vect) {
104
ISR(USART0_RXC_vect) {
105
    uint8_t c;
105
    uint8_t c;
106
    // catch the received byte
106
    // catch the received byte
107
    c = UDR0;
107
    c = UDR0;
108
 
108
 
109
        // echo 
109
        // echo 
110
        UDR0 = c;
110
        UDR0 = c;
111
}
111
}
112
 
112
 
113
 
113
 
114
 
114
 
115
#endif
115
#endif
116
 
116
 
117
#endif // USART0ENABLE
117
#endif // USART0ENABLE
118
 
118