Subversion Repositories Projects

Rev

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

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