Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
331 cascade 1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
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   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 ****************************************************************************/
20
 
346 cascade 21
#include <avr/io.h>
22
#include <avr/interrupt.h>
23
#include <util/delay.h>
24
#include "main.h"
331 cascade 25
#include "usart1.h"
26
 
514 cascade 27
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
28
 
346 cascade 29
volatile uint8_t rxd_buffer_locked = 0;
30
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
31
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
32
volatile uint8_t ReceivedBytes = 0;
33
volatile uint8_t *pRxData = 0;
34
volatile uint8_t RxDataLen = 0;
35
 
331 cascade 36
/* ##########################################################################
37
 * USART stuff
38
 * ##########################################################################*/
39
 
40
/**
41
 * init usart1
42
 */
43
void usart1_init() {
44
    UBRR1H = ((F_CPU / (16UL * baud)) - 1) >> 8;
45
    UBRR1L = (F_CPU / (16UL * baud)) - 1;
46
 
47
    // Enable receiver and transmitter; enable RX interrupt
48
    UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1);
49
 
50
    //asynchronous 8N1
51
    UCSR1C = (1 << URSEL1) | (3 << UCSZ10);
52
}
53
 
54
/**
55
 * disable the txd pin of usart1
56
 */
57
void usart1_DisableTXD(void) {
389 cascade 58
        UCSR1B &= ~(1 << TXCIE1); // disable TX-Interrupt
387 cascade 59
        UCSR1B &= ~(1 << TXEN1); // disable TX in USART
331 cascade 60
        DDRB &= ~(1<<DDB3); // set TXD pin as input
389 cascade 61
        PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin
331 cascade 62
}
63
 
64
/**
65
 * enable the txd pin of usart1
66
 */
67
void usart1_EnableTXD(void) {
68
        DDRB |= (1<<DDB3); // set TXD pin as output
389 cascade 69
        PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin
331 cascade 70
        UCSR1B |= (1 << TXEN1); // enable TX in USART
389 cascade 71
        UCSR1B |= (1 << TXCIE1); // enable TX-Interrupt
331 cascade 72
}
73
 
74
/**
75
 * send a single <character> through usart1
76
 */
77
void usart1_putc(unsigned char character) {
78
    // wait until UDR ready
79
    while (!(UCSR1A & (1 << UDRE1)));
80
    UDR1 = character;
81
}
82
 
83
/**
84
 * send a <string> throught usart1
85
 */
86
void usart1_puts(char *s) {
87
    while (*s) {
88
        usart1_putc(*s);
89
        s++;
90
    }
91
}
92
 
93
/**
387 cascade 94
 * transmit interrupt handler
95
 * unused
96
 */
389 cascade 97
ISR(SIG_USART1_DATA) {
387 cascade 98
}
99
 
100
/**
331 cascade 101
 * receive data through usart1
102
 * portions taken and adapted from
103
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c
104
 */
105
ISR(SIG_USART1_RECV) {
106
    if (rxd_buffer_locked) return; // if rxd buffer is locked immediately return
107
    LED1_ON
108
            static uint16_t crc;
109
    static uint8_t ptr_rxd_buffer = 0;
110
    uint8_t crc1, crc2;
111
    uint8_t c;
112
 
113
    c = UDR1; // catch the received byte
114
 
115
    // the rxd buffer is unlocked
116
    if ((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
117
    {
118
                /*
119
                // skip other datasets
120
        if (ptr_rxd_buffer == 2 && rxd_buffer[ptr_rxd_buffer] != 'O') {
121
                        ptr_rxd_buffer = 0; // reset rxd buffer
122
                rxd_buffer_locked = 0; // unlock rxd buffer
123
                }*/
124
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
125
        crc = c; // init crc
126
    } else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
127
    {
128
        if (c != '\r') // no termination character
129
        {
130
            rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
131
            crc += c; // update crc
132
        } else // termination character was received
133
        {
134
            // the last 2 bytes are no subject for checksum calculation
135
            // they are the checksum itself
136
            crc -= rxd_buffer[ptr_rxd_buffer - 2];
137
            crc -= rxd_buffer[ptr_rxd_buffer - 1];
138
            // calculate checksum from transmitted data
139
            crc %= 4096;
140
            crc1 = '=' + crc / 64;
141
            crc2 = '=' + crc % 64;
142
            // compare checksum to transmitted checksum bytes
143
            if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2 == rxd_buffer[ptr_rxd_buffer - 1])) { // checksum valid
144
                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
145
                ReceivedBytes = ptr_rxd_buffer + 1; // store number of received bytes
146
                rxd_buffer_locked = 1; // lock the rxd buffer
147
            } else { // checksum invalid
148
                rxd_buffer_locked = 0; // unlock rxd buffer
149
            }
150
            ptr_rxd_buffer = 0; // reset rxd buffer pointer
151
        }
152
    } else // rxd buffer overrun
153
    {
154
        ptr_rxd_buffer = 0; // reset rxd buffer
155
        rxd_buffer_locked = 0; // unlock rxd buffer
156
    }
157
    LED1_OFF
158
}
159
 
160
/**
161
 * Decode the recevied Buffer
162
 * portions taken and adapted from
163
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c
164
 */
165
void Decode64(void) {
166
    uint8_t a, b, c, d;
167
    uint8_t x, y, z;
168
    uint8_t ptrIn = 3;
169
    uint8_t ptrOut = 3;
170
    uint8_t len = ReceivedBytes - 6;
171
 
172
    while (len) {
173
        a = rxd_buffer[ptrIn++] - '=';
174
        b = rxd_buffer[ptrIn++] - '=';
175
        c = rxd_buffer[ptrIn++] - '=';
176
        d = rxd_buffer[ptrIn++] - '=';
177
 
178
        x = (a << 2) | (b >> 4);
179
        y = ((b & 0x0f) << 4) | (c >> 2);
180
        z = ((c & 0x03) << 6) | d;
181
 
182
        if (len--) rxd_buffer[ptrOut++] = x;
183
        else break;
184
        if (len--) rxd_buffer[ptrOut++] = y;
185
        else break;
186
        if (len--) rxd_buffer[ptrOut++] = z;
187
        else break;
188
    }
189
    pRxData = &rxd_buffer[3];
190
    RxDataLen = ptrOut - 3;
191
}
192
 
193
/**
194
 * request Data through USART in special MK format by adding checksum and
195
 * encode data in modified Base64
196
 * portions taken and adapted from
197
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c
198
 */
199
void sendMKData(unsigned char cmd, unsigned char addr, unsigned char *snd, unsigned char len) {
200
    unsigned int pt = 0;
201
    unsigned char a, b, c;
202
    unsigned char ptr = 0;
203
 
204
    txd_buffer[pt++] = '#'; // Start-Byte
205
    txd_buffer[pt++] = 'a' + addr; // Adress
206
    txd_buffer[pt++] = cmd; // Command
207
    while (len) {
208
        if (len) {
209
            a = snd[ptr++];
210
            len--;
211
        } else a = 0;
212
        if (len) {
213
            b = snd[ptr++];
214
            len--;
215
        } else b = 0;
216
        if (len) {
217
            c = snd[ptr++];
218
            len--;
219
        } else c = 0;
220
        txd_buffer[pt++] = '=' + (a >> 2);
221
        txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
222
        txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
223
        txd_buffer[pt++] = '=' + (c & 0x3f);
224
    }
225
 
226
    // add crc
227
    unsigned int tmpCRC = 0, i;
228
    for (i = 0; i < pt; i++) {
229
        tmpCRC += txd_buffer[i];
230
    }
231
    tmpCRC %= 4096;
232
    txd_buffer[i++] = '=' + tmpCRC / 64;
233
    txd_buffer[i++] = '=' + tmpCRC % 64;
234
    txd_buffer[i++] = '\r';
235
 
236
    usart1_puts((char*) txd_buffer);
237
}
346 cascade 238
 
239
/**
240
 * short script to directly send a request thorugh usart including en- and disabling it
241
 * where <address> is the address of the receipient, <label> is which data set to request
242
 * and <ms> represents the milliseconds delay between data
243
 */
244
void usart1_request_mk_data(uint8_t address, char label, uint8_t ms) {
245
            // re-enable TXD pin
246
            usart1_EnableTXD();
247
 
248
            unsigned char mstenth = ms/10;
249
                        sendMKData(label, address, &mstenth, 1);
389 cascade 250
                        // wait until UDR ready
251
                        while (!(UCSR1A & (1 << UDRE1)));                                              
346 cascade 252
            // disable TXD pin again
253
            usart1_DisableTXD();
254
}
514 cascade 255
 
256
#endif