Subversion Repositories Projects

Rev

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