Subversion Repositories Projects

Rev

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

Rev 303 Rev 333
1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + only for non-profit use
3
// + only for non-profit use
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
 
7
 
8
#include <avr/io.h>
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
9
#include <avr/interrupt.h>
10
#include <avr/wdt.h>
10
#include <avr/wdt.h>
11
#include <stdarg.h>
11
#include <stdarg.h>
12
#include <string.h>
12
#include <string.h>
13
 
13
 
14
#include "main.h"
14
#include "main.h"
15
#include "menu.h"
15
#include "menu.h"
16
#include "timer0.h"
16
#include "timer0.h"
17
#include "uart0.h"
17
#include "uart0.h"
18
#include "ubx.h"
18
#include "ubx.h"
19
#include "led.h"
19
#include "led.h"
20
 
20
 
21
 
21
 
22
#define FC_ADDRESS 1
22
#define FC_ADDRESS 1
23
#define NC_ADDRESS 2
23
#define NC_ADDRESS 2
24
#define MK3MAG_ADDRESS 3
24
#define MK3MAG_ADDRESS 3
25
 
25
 
26
#define FM_ADDRESS 10 // FOLLOW ME
26
#define FM_ADDRESS 10 // FOLLOW ME
27
 
27
 
28
#define FALSE   0
28
#define FALSE   0
29
#define TRUE    1
29
#define TRUE    1
30
 
30
 
31
//int8_t test __attribute__ ((section (".noinit")));
31
//int8_t test __attribute__ ((section (".noinit")));
32
uint8_t Request_VerInfo                 = FALSE;
32
uint8_t Request_VerInfo                 = FALSE;
33
uint8_t Request_Display                 = FALSE;
33
uint8_t Request_Display                 = FALSE;
34
uint8_t Request_Display1                = FALSE;
34
uint8_t Request_Display1                = FALSE;
35
uint8_t Request_ExternalControl = FALSE;
35
uint8_t Request_ExternalControl = FALSE;
36
uint8_t Request_DebugData               = FALSE;
36
uint8_t Request_DebugData               = FALSE;
37
uint8_t Request_DebugLabel              = 255;
37
uint8_t Request_DebugLabel              = 255;
-
 
38
uint8_t Request_SendFollowMe    = FALSE;
38
uint8_t DisplayLine = 0;
39
uint8_t DisplayLine = 0;
39
 
40
 
40
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
41
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
41
volatile uint8_t rxd_buffer_locked = FALSE;
42
volatile uint8_t rxd_buffer_locked = FALSE;
42
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
43
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
43
volatile uint8_t txd_complete = TRUE;
44
volatile uint8_t txd_complete = TRUE;
44
volatile uint8_t ReceivedBytes = 0;
45
volatile uint8_t ReceivedBytes = 0;
45
volatile uint8_t *pRxData = 0;
46
volatile uint8_t *pRxData = 0;
46
volatile uint8_t RxDataLen = 0;
47
volatile uint8_t RxDataLen = 0;
47
 
48
 
48
uint8_t PcAccess = 100;
49
uint8_t PcAccess = 100;
49
 
50
 
50
ExternControl_t ExternControl;
51
ExternControl_t ExternControl;
51
DebugOut_t              DebugOut;
52
DebugOut_t              DebugOut;
52
UART_VersionInfo_t      UART_VersionInfo;
53
UART_VersionInfo_t      UART_VersionInfo;
53
 
54
 
54
uint16_t DebugData_Timer;
55
uint16_t DebugData_Timer;
55
uint16_t DebugData_Interval = 500; // in 1ms
56
uint16_t DebugData_Interval = 500; // in 1ms
-
 
57
 
56
 
58
Waypoint_t FollowMe;
57
 
59
 
58
const uint8_t ANALOG_LABEL[32][16] =
60
const uint8_t ANALOG_LABEL[32][16] =
59
{
61
{
60
   //1234567890123456
62
   //1234567890123456
61
    "Analog Ch0      ", //0
63
    "Analog Ch0      ", //0
62
    "Analog Ch1      ",
64
    "Analog Ch1      ",
63
    "Analog Ch2      ",
65
    "Analog Ch2      ",
64
    "Analog Ch3      ",
66
    "Analog Ch3      ",
65
    "Analog Ch4      ",
67
    "Analog Ch4      ",
66
    "Analog Ch5      ", //5
68
    "Analog Ch5      ", //5
67
    "Analog Ch6      ",
69
    "Analog Ch6      ",
68
    "Analog Ch7      ",
70
    "Analog Ch7      ",
69
    "UBat            ",
71
    "UBat            ",
70
    "Speed_North     ",
72
    "Speed_North     ",
71
    "Speed_East      ", //10
73
    "Speed_East      ", //10
72
    "Speed_Top       ",
74
    "Speed_Top       ",
73
    "NumOfSats       ",
75
    "NumOfSats       ",
74
    "Pos.Longitude   ",
76
    "Pos.Longitude   ",
75
    "Pos.Latitude    ",
77
    "Pos.Latitude    ",
76
    "Pos.Altitude    ", //15
78
    "Pos.Altitude    ", //15
77
        "Zellenzahl      ",
79
        "Zellenzahl      ",
78
    "PowerOn         ",
80
    "PowerOn         ",
79
    "Debug18         ",
81
    "Debug18         ",
80
    "Debug19         ",
82
    "Debug19         ",
81
    "Debug20         ", //20
83
    "Debug20         ", //20
82
    "Debug21         ",
84
    "Debug21         ",
83
    "Debug22         ",
85
    "Debug22         ",
84
    "Debug23         ",
86
    "Debug23         ",
85
    "Debug24         ",
87
    "Debug24         ",
86
    "Debug25         ", //25
88
    "Debug25         ", //25
87
    "Debug26         ",
89
    "Debug26         ",
88
    "Debug27         ",
90
    "Debug27         ",
89
    "Debug28         ",
91
    "Debug28         ",
90
    "Debug29         ",
92
    "Debug29         ",
91
    "Debug30         ", //30
93
    "Debug30         ", //30
92
    "Debug31         "
94
    "Debug31         "
93
};
95
};
94
 
96
 
95
 
97
 
96
 
98
 
97
/****************************************************************/
99
/****************************************************************/
98
/*              Initialization of the USART0                    */
100
/*              Initialization of the USART0                    */
99
/****************************************************************/
101
/****************************************************************/
100
void USART0_Init (void)
102
void USART0_Init (void)
101
{
103
{
102
        uint8_t sreg = SREG;
104
        uint8_t sreg = SREG;
103
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
105
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
104
 
106
 
105
        // disable all interrupts before configuration
107
        // disable all interrupts before configuration
106
        cli();
108
        cli();
107
 
109
 
108
        // disable RX-Interrupt
110
        // disable RX-Interrupt
109
        UCSR0B &= ~(1 << RXCIE0);
111
        UCSR0B &= ~(1 << RXCIE0);
110
        // disable TX-Interrupt
112
        // disable TX-Interrupt
111
        UCSR0B &= ~(1 << TXCIE0);
113
        UCSR0B &= ~(1 << TXCIE0);
112
 
114
 
113
        // set direction of RXD0 and TXD0 pins
115
        // set direction of RXD0 and TXD0 pins
114
        // set RXD0 (PD0) as an input pin
116
        // set RXD0 (PD0) as an input pin
115
        PORTD |= (1 << PORTD0);
117
        PORTD |= (1 << PORTD0);
116
        DDRD &= ~(1 << DDD0);
118
        DDRD &= ~(1 << DDD0);
117
        // set TXD0 (PD1) as an output pin
119
        // set TXD0 (PD1) as an output pin
118
        PORTD |= (1 << PORTD1);
120
        PORTD |= (1 << PORTD1);
119
        DDRD |=  (1 << DDD1);
121
        DDRD |=  (1 << DDD1);
120
 
122
 
121
        // USART0 Baud Rate Register
123
        // USART0 Baud Rate Register
122
        // set clock divider
124
        // set clock divider
123
        UBRR0H = (uint8_t)(ubrr >> 8);
125
        UBRR0H = (uint8_t)(ubrr >> 8);
124
        UBRR0L = (uint8_t)ubrr;
126
        UBRR0L = (uint8_t)ubrr;
125
 
127
 
126
        // USART0 Control and Status Register A, B, C
128
        // USART0 Control and Status Register A, B, C
127
 
129
 
128
        // enable double speed operation in
130
        // enable double speed operation in
129
        UCSR0A |= (1 << U2X0);
131
        UCSR0A |= (1 << U2X0);
130
        // enable receiver and transmitter in
132
        // enable receiver and transmitter in
131
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
133
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
132
        // set asynchronous mode
134
        // set asynchronous mode
133
        UCSR0C &= ~(1 << UMSEL01);
135
        UCSR0C &= ~(1 << UMSEL01);
134
        UCSR0C &= ~(1 << UMSEL00);
136
        UCSR0C &= ~(1 << UMSEL00);
135
        // no parity
137
        // no parity
136
        UCSR0C &= ~(1 << UPM01);
138
        UCSR0C &= ~(1 << UPM01);
137
        UCSR0C &= ~(1 << UPM00);
139
        UCSR0C &= ~(1 << UPM00);
138
        // 1 stop bit
140
        // 1 stop bit
139
        UCSR0C &= ~(1 << USBS0);
141
        UCSR0C &= ~(1 << USBS0);
140
        // 8-bit
142
        // 8-bit
141
        UCSR0B &= ~(1 << UCSZ02);
143
        UCSR0B &= ~(1 << UCSZ02);
142
        UCSR0C |=  (1 << UCSZ01);
144
        UCSR0C |=  (1 << UCSZ01);
143
        UCSR0C |=  (1 << UCSZ00);
145
        UCSR0C |=  (1 << UCSZ00);
144
 
146
 
145
                // flush receive buffer
147
                // flush receive buffer
146
        while ( UCSR0A & (1<<RXC0) ) UDR0;
148
        while ( UCSR0A & (1<<RXC0) ) UDR0;
147
 
149
 
148
        // enable interrupts at the end
150
        // enable interrupts at the end
149
        // enable RX-Interrupt
151
        // enable RX-Interrupt
150
        UCSR0B |= (1 << RXCIE0);
152
        UCSR0B |= (1 << RXCIE0);
151
        // enable TX-Interrupt
153
        // enable TX-Interrupt
152
        UCSR0B |= (1 << TXCIE0);
154
        UCSR0B |= (1 << TXCIE0);
153
 
155
 
154
        // initialize the debug timer
156
        // initialize the debug timer
155
        DebugData_Timer = SetDelay(DebugData_Interval);
157
        DebugData_Timer = SetDelay(DebugData_Interval);
156
 
158
 
157
        // unlock rxd_buffer
159
        // unlock rxd_buffer
158
        rxd_buffer_locked = FALSE;
160
        rxd_buffer_locked = FALSE;
159
        pRxData = 0;
161
        pRxData = 0;
160
        RxDataLen = 0;
162
        RxDataLen = 0;
161
 
163
 
162
        // no bytes to send
164
        // no bytes to send
163
        txd_complete = TRUE;
165
        txd_complete = TRUE;
164
 
166
 
165
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
167
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
166
        UART_VersionInfo.SWMinor = VERSION_MINOR;
168
        UART_VersionInfo.SWMinor = VERSION_MINOR;
167
        UART_VersionInfo.SWPatch = VERSION_PATCH;
169
        UART_VersionInfo.SWPatch = VERSION_PATCH;
168
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
170
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
169
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
171
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
170
 
172
 
171
        // restore global interrupt flags
173
        // restore global interrupt flags
172
    SREG = sreg;
174
    SREG = sreg;
173
}
175
}
174
 
176
 
175
/****************************************************************/
177
/****************************************************************/
176
/*               USART0 transmitter ISR                         */
178
/*               USART0 transmitter ISR                         */
177
/****************************************************************/
179
/****************************************************************/
178
ISR(USART0_TX_vect)
180
ISR(USART0_TX_vect)
179
{
181
{
180
        static uint16_t ptr_txd_buffer = 0;
182
        static uint16_t ptr_txd_buffer = 0;
181
        uint8_t tmp_tx;
183
        uint8_t tmp_tx;
182
        if(!txd_complete) // transmission not completed
184
        if(!txd_complete) // transmission not completed
183
        {
185
        {
184
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
186
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
185
                tmp_tx = txd_buffer[ptr_txd_buffer];
187
                tmp_tx = txd_buffer[ptr_txd_buffer];
186
                // if terminating character or end of txd buffer was reached
188
                // if terminating character or end of txd buffer was reached
187
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
189
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
188
                {
190
                {
189
                        ptr_txd_buffer = 0; // reset txd pointer
191
                        ptr_txd_buffer = 0; // reset txd pointer
190
                        txd_complete = 1; // stop transmission
192
                        txd_complete = 1; // stop transmission
191
                }
193
                }
192
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
194
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
193
        }
195
        }
194
        // transmission completed
196
        // transmission completed
195
        else ptr_txd_buffer = 0;
197
        else ptr_txd_buffer = 0;
196
}
198
}
197
 
199
 
198
/****************************************************************/
200
/****************************************************************/
199
/*               USART0 receiver ISR                            */
201
/*               USART0 receiver ISR                            */
200
/****************************************************************/
202
/****************************************************************/
201
ISR(USART0_RX_vect)
203
ISR(USART0_RX_vect)
202
{
204
{
203
        static uint16_t crc;
205
        static uint16_t crc;
204
        static uint8_t ptr_rxd_buffer = 0;
206
        static uint8_t ptr_rxd_buffer = 0;
205
        uint8_t crc1, crc2;
207
        uint8_t crc1, crc2;
206
        uint8_t c;
208
        uint8_t c;
207
 
209
 
208
        c = UDR0;  // catch the received byte
210
        c = UDR0;  // catch the received byte
209
 
211
 
210
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
212
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
211
 
213
 
212
        // the rxd buffer is unlocked
214
        // the rxd buffer is unlocked
213
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
215
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
214
        {
216
        {
215
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
217
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
216
                crc = c; // init crc
218
                crc = c; // init crc
217
        }
219
        }
218
        #if 0
220
        #if 0
219
        else if (ptr_rxd_buffer == 1) // handle address
221
        else if (ptr_rxd_buffer == 1) // handle address
220
        {
222
        {
221
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
223
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
222
                crc += c; // update crc
224
                crc += c; // update crc
223
        }
225
        }
224
        #endif
226
        #endif
225
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
227
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
226
        {
228
        {
227
                if(c != '\r') // no termination character
229
                if(c != '\r') // no termination character
228
                {
230
                {
229
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
231
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
230
                        crc += c; // update crc
232
                        crc += c; // update crc
231
                }
233
                }
232
                else // termination character was received
234
                else // termination character was received
233
                {
235
                {
234
                        // the last 2 bytes are no subject for checksum calculation
236
                        // the last 2 bytes are no subject for checksum calculation
235
                        // they are the checksum itself
237
                        // they are the checksum itself
236
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
238
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
237
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
239
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
238
                        // calculate checksum from transmitted data
240
                        // calculate checksum from transmitted data
239
                        crc %= 4096;
241
                        crc %= 4096;
240
                        crc1 = '=' + crc / 64;
242
                        crc1 = '=' + crc / 64;
241
                        crc2 = '=' + crc % 64;
243
                        crc2 = '=' + crc % 64;
242
                        // compare checksum to transmitted checksum bytes
244
                        // compare checksum to transmitted checksum bytes
243
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
245
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
244
                        {   // checksum valid
246
                        {   // checksum valid
245
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
247
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
246
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
248
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
247
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
249
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
248
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
250
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
249
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
251
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
250
                        }
252
                        }
251
                        else
253
                        else
252
                        {       // checksum invalid
254
                        {       // checksum invalid
253
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
255
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
254
                        }
256
                        }
255
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
257
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
256
                }
258
                }
257
        }
259
        }
258
        else // rxd buffer overrun
260
        else // rxd buffer overrun
259
        {
261
        {
260
                ptr_rxd_buffer = 0; // reset rxd buffer
262
                ptr_rxd_buffer = 0; // reset rxd buffer
261
                rxd_buffer_locked = FALSE; // unlock rxd buffer
263
                rxd_buffer_locked = FALSE; // unlock rxd buffer
262
        }
264
        }
263
 
265
 
264
}
266
}
265
 
267
 
266
 
268
 
267
// --------------------------------------------------------------------------
269
// --------------------------------------------------------------------------
268
void AddCRC(uint16_t datalen)
270
void AddCRC(uint16_t datalen)
269
{
271
{
270
        uint16_t tmpCRC = 0, i;
272
        uint16_t tmpCRC = 0, i;
271
        for(i = 0; i < datalen; i++)
273
        for(i = 0; i < datalen; i++)
272
        {
274
        {
273
                tmpCRC += txd_buffer[i];
275
                tmpCRC += txd_buffer[i];
274
        }
276
        }
275
        tmpCRC %= 4096;
277
        tmpCRC %= 4096;
276
        txd_buffer[i++] = '=' + tmpCRC / 64;
278
        txd_buffer[i++] = '=' + tmpCRC / 64;
277
        txd_buffer[i++] = '=' + tmpCRC % 64;
279
        txd_buffer[i++] = '=' + tmpCRC % 64;
278
        txd_buffer[i++] = '\r';
280
        txd_buffer[i++] = '\r';
279
        txd_complete = FALSE;
281
        txd_complete = FALSE;
280
        UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
282
        UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
281
}
283
}
282
 
284
 
283
 
285
 
284
 
286
 
285
// --------------------------------------------------------------------------
287
// --------------------------------------------------------------------------
286
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
288
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
287
{
289
{
288
        va_list ap;
290
        va_list ap;
289
        uint16_t pt = 0;
291
        uint16_t pt = 0;
290
        uint8_t a,b,c;
292
        uint8_t a,b,c;
291
        uint8_t ptr = 0;
293
        uint8_t ptr = 0;
292
 
294
 
293
        uint8_t *pdata = 0;
295
        uint8_t *pdata = 0;
294
        int len = 0;
296
        int len = 0;
295
 
297
 
296
        txd_buffer[pt++] = '#';                 // Start character
298
        txd_buffer[pt++] = '#';                 // Start character
297
        txd_buffer[pt++] = 'a' + addr;  // Address (a=0; b=1,...)
299
        txd_buffer[pt++] = 'a' + addr;  // Address (a=0; b=1,...)
298
        txd_buffer[pt++] = cmd;                 // Command
300
        txd_buffer[pt++] = cmd;                 // Command
299
 
301
 
300
        va_start(ap, numofbuffers);
302
        va_start(ap, numofbuffers);
301
        if(numofbuffers)
303
        if(numofbuffers)
302
        {
304
        {
303
                pdata = va_arg(ap, uint8_t*);
305
                pdata = va_arg(ap, uint8_t*);
304
                len = va_arg(ap, int);
306
                len = va_arg(ap, int);
305
                ptr = 0;
307
                ptr = 0;
306
                numofbuffers--;
308
                numofbuffers--;
307
        }
309
        }
308
 
310
 
309
        while(len)
311
        while(len)
310
        {
312
        {
311
                if(len)
313
                if(len)
312
                {
314
                {
313
                        a = pdata[ptr++];
315
                        a = pdata[ptr++];
314
                        len--;
316
                        len--;
315
                        if((!len) && numofbuffers)
317
                        if((!len) && numofbuffers)
316
                        {
318
                        {
317
                                pdata = va_arg(ap, uint8_t*);
319
                                pdata = va_arg(ap, uint8_t*);
318
                                len = va_arg(ap, int);
320
                                len = va_arg(ap, int);
319
                                ptr = 0;
321
                                ptr = 0;
320
                                numofbuffers--;
322
                                numofbuffers--;
321
                        }
323
                        }
322
                }
324
                }
323
                else a = 0;
325
                else a = 0;
324
                if(len)
326
                if(len)
325
                {
327
                {
326
                        b = pdata[ptr++];
328
                        b = pdata[ptr++];
327
                        len--;
329
                        len--;
328
                        if((!len) && numofbuffers)
330
                        if((!len) && numofbuffers)
329
                        {
331
                        {
330
                                pdata = va_arg(ap, uint8_t*);
332
                                pdata = va_arg(ap, uint8_t*);
331
                                len = va_arg(ap, int);
333
                                len = va_arg(ap, int);
332
                                ptr = 0;
334
                                ptr = 0;
333
                                numofbuffers--;
335
                                numofbuffers--;
334
                        }
336
                        }
335
                }
337
                }
336
                else b = 0;
338
                else b = 0;
337
                if(len)
339
                if(len)
338
                {
340
                {
339
                        c = pdata[ptr++];
341
                        c = pdata[ptr++];
340
                        len--;
342
                        len--;
341
                        if((!len) && numofbuffers)
343
                        if((!len) && numofbuffers)
342
                        {
344
                        {
343
                                pdata = va_arg(ap, uint8_t*);
345
                                pdata = va_arg(ap, uint8_t*);
344
                                len = va_arg(ap, int);
346
                                len = va_arg(ap, int);
345
                                ptr = 0;
347
                                ptr = 0;
346
                                numofbuffers--;
348
                                numofbuffers--;
347
                        }
349
                        }
348
                }
350
                }
349
                else c = 0;
351
                else c = 0;
350
                txd_buffer[pt++] = '=' + (a >> 2);
352
                txd_buffer[pt++] = '=' + (a >> 2);
351
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
353
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
352
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
354
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
353
                txd_buffer[pt++] = '=' + ( c & 0x3f);
355
                txd_buffer[pt++] = '=' + ( c & 0x3f);
354
        }
356
        }
355
        va_end(ap);
357
        va_end(ap);
356
        AddCRC(pt); // add checksum after data block and initates the transmission
358
        AddCRC(pt); // add checksum after data block and initates the transmission
357
}
359
}
358
 
360
 
359
 
361
 
360
// --------------------------------------------------------------------------
362
// --------------------------------------------------------------------------
361
void Decode64(void)
363
void Decode64(void)
362
{
364
{
363
        uint8_t a,b,c,d;
365
        uint8_t a,b,c,d;
364
        uint8_t x,y,z;
366
        uint8_t x,y,z;
365
        uint8_t ptrIn = 3;
367
        uint8_t ptrIn = 3;
366
        uint8_t ptrOut = 3;
368
        uint8_t ptrOut = 3;
367
        uint8_t len = ReceivedBytes - 6;
369
        uint8_t len = ReceivedBytes - 6;
368
 
370
 
369
        while(len)
371
        while(len)
370
        {
372
        {
371
                a = rxd_buffer[ptrIn++] - '=';
373
                a = rxd_buffer[ptrIn++] - '=';
372
                b = rxd_buffer[ptrIn++] - '=';
374
                b = rxd_buffer[ptrIn++] - '=';
373
                c = rxd_buffer[ptrIn++] - '=';
375
                c = rxd_buffer[ptrIn++] - '=';
374
                d = rxd_buffer[ptrIn++] - '=';
376
                d = rxd_buffer[ptrIn++] - '=';
375
                //if(ptrIn > ReceivedBytes - 3) break;
377
                //if(ptrIn > ReceivedBytes - 3) break;
376
 
378
 
377
                x = (a << 2) | (b >> 4);
379
                x = (a << 2) | (b >> 4);
378
                y = ((b & 0x0f) << 4) | (c >> 2);
380
                y = ((b & 0x0f) << 4) | (c >> 2);
379
                z = ((c & 0x03) << 6) | d;
381
                z = ((c & 0x03) << 6) | d;
380
 
382
 
381
                if(len--) rxd_buffer[ptrOut++] = x; else break;
383
                if(len--) rxd_buffer[ptrOut++] = x; else break;
382
                if(len--) rxd_buffer[ptrOut++] = y; else break;
384
                if(len--) rxd_buffer[ptrOut++] = y; else break;
383
                if(len--) rxd_buffer[ptrOut++] = z; else break;
385
                if(len--) rxd_buffer[ptrOut++] = z; else break;
384
        }
386
        }
385
        pRxData = &rxd_buffer[3];
387
        pRxData = &rxd_buffer[3];
386
        RxDataLen = ptrOut - 3;
388
        RxDataLen = ptrOut - 3;
387
}
389
}
388
 
390
 
389
 
391
 
390
// --------------------------------------------------------------------------
392
// --------------------------------------------------------------------------
391
void USART0_ProcessRxData(void)
393
void USART0_ProcessRxData(void)
392
{
394
{
393
        // if data in the rxd buffer are not locked immediately return
395
        // if data in the rxd buffer are not locked immediately return
394
        if(!rxd_buffer_locked) return;
396
        if(!rxd_buffer_locked) return;
395
 
397
 
396
        Decode64(); // decode data block in rxd_buffer
398
        Decode64(); // decode data block in rxd_buffer
397
 
399
 
398
 
400
 
399
        switch(rxd_buffer[1] - 'a')
401
        switch(rxd_buffer[1] - 'a')
400
        {
402
        {
401
                case FM_ADDRESS:
403
                case FM_ADDRESS:
402
 
404
 
403
                        switch(rxd_buffer[2])
405
                        switch(rxd_buffer[2])
404
                        {
406
                        {
405
                                default:
407
                                default:
406
                                        //unsupported command received
408
                                        //unsupported command received
407
                                        break;
409
                                        break;
408
                        } // case FC_ADDRESS:
410
                        } // case FC_ADDRESS:
409
 
411
 
410
                default: // any Slave Address
412
                default: // any Slave Address
411
 
413
 
412
                switch(rxd_buffer[2])
414
                switch(rxd_buffer[2])
413
                        {
415
                        {
414
                                case 'a':// request for labels of the analog debug outputs
416
                                case 'a':// request for labels of the analog debug outputs
415
                                        Request_DebugLabel = pRxData[0];
417
                                        Request_DebugLabel = pRxData[0];
416
                                        if(Request_DebugLabel > 31) Request_DebugLabel = 31;
418
                                        if(Request_DebugLabel > 31) Request_DebugLabel = 31;
417
                                        PcAccess = 255;
419
                                        PcAccess = 255;
418
                                        break;
420
                                        break;
419
 
421
 
420
                                case 'h':// request for display columns
422
                                case 'h':// request for display columns
421
                                        PcAccess = 255;
423
                                        PcAccess = 255;
422
                                        RemoteKeys |= pRxData[0];
424
                                        RemoteKeys |= pRxData[0];
423
                                        if(RemoteKeys) DisplayLine = 0;
425
                                        if(RemoteKeys) DisplayLine = 0;
424
                                        Request_Display = TRUE;
426
                                        Request_Display = TRUE;
425
                                        break;
427
                                        break;
426
 
428
 
427
                                case 'l':// request for display columns
429
                                case 'l':// request for display columns
428
                                        PcAccess = 255;
430
                                        PcAccess = 255;
429
                                        MenuItem = pRxData[0];
431
                                        MenuItem = pRxData[0];
430
                                        Request_Display1 = TRUE;
432
                                        Request_Display1 = TRUE;
431
                                        break;
433
                                        break;
432
 
434
 
433
                                case 'v': // request for version and board release
435
                                case 'v': // request for version and board release
434
                                        Request_VerInfo = TRUE;
436
                                        Request_VerInfo = TRUE;
435
                                        break;
437
                                        break;
436
 
438
 
437
                                case 'd': // request for the debug data
439
                                case 'd': // request for the debug data
438
                                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
440
                                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
439
                                        if(DebugData_Interval > 0) Request_DebugData = TRUE;
441
                                        if(DebugData_Interval > 0) Request_DebugData = TRUE;
440
                                        break;
442
                                        break;
441
 
443
 
442
                                case 'g':// get external control data
444
                                case 'g':// get external control data
443
                                        Request_ExternalControl = TRUE;
445
                                        Request_ExternalControl = TRUE;
444
                                        break;
446
                                        break;
445
 
447
 
446
                                default:
448
                                default:
447
                                        //unsupported command received
449
                                        //unsupported command received
448
                                        break;
450
                                        break;
449
                }
451
                }
450
                break; // default:
452
                break; // default:
451
        }
453
        }
452
        // unlock the rxd buffer after processing
454
        // unlock the rxd buffer after processing
453
        pRxData = 0;
455
        pRxData = 0;
454
        RxDataLen = 0;
456
        RxDataLen = 0;
455
        rxd_buffer_locked = FALSE;
457
        rxd_buffer_locked = FALSE;
456
}
458
}
457
 
459
 
458
//############################################################################
460
//############################################################################
459
//Routine für die Serielle Ausgabe
461
//Routine für die Serielle Ausgabe
460
int16_t uart_putchar (int8_t c)
462
int16_t uart_putchar (int8_t c)
461
//############################################################################
463
//############################################################################
462
{
464
{
463
        if (c == '\n')
465
        if (c == '\n')
464
                uart_putchar('\r');
466
                uart_putchar('\r');
465
        // wait until previous character was send
467
        // wait until previous character was send
466
        loop_until_bit_is_set(UCSR0A, UDRE0);
468
        loop_until_bit_is_set(UCSR0A, UDRE0);
467
        // send character
469
        // send character
468
        UDR0 = c;
470
        UDR0 = c;
469
        return (0);
471
        return (0);
470
}
472
}
471
 
473
 
472
 
474
 
473
//---------------------------------------------------------------------------------------------
475
//---------------------------------------------------------------------------------------------
474
void USART0_TransmitTxData(void)
476
void USART0_TransmitTxData(void)
475
{
477
{
476
        if(!txd_complete) return;
478
        if(!txd_complete) return;
477
 
479
 
478
        if(Request_VerInfo && txd_complete)
480
        if(Request_VerInfo && txd_complete)
479
        {
481
        {
480
                SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
482
                SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
481
                Request_VerInfo = FALSE;
483
                Request_VerInfo = FALSE;
482
        }
484
        }
483
        if(Request_Display && txd_complete)
485
        if(Request_Display && txd_complete)
484
        {
486
        {
485
                LCD_PrintMenu();
487
                LCD_PrintMenu();
486
                SendOutData('H', FM_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20);
488
                SendOutData('H', FM_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20);
487
                DisplayLine++;
489
                DisplayLine++;
488
                if(DisplayLine >= 4) DisplayLine = 0;
490
                if(DisplayLine >= 4) DisplayLine = 0;
489
                Request_Display = FALSE;
491
                Request_Display = FALSE;
490
        }
492
        }
491
        if(Request_Display1 && txd_complete)
493
        if(Request_Display1 && txd_complete)
492
        {
494
        {
493
                LCD_PrintMenu();
495
                LCD_PrintMenu();
494
                SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
496
                SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
495
                Request_Display1 = FALSE;
497
                Request_Display1 = FALSE;
496
        }
498
        }
497
        if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten
499
        if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten
498
        {
500
        {
499
                SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), ANALOG_LABEL[Request_DebugLabel], 16);
501
                SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), ANALOG_LABEL[Request_DebugLabel], 16);
500
                Request_DebugLabel = 0xFF;
502
                Request_DebugLabel = 0xFF;
501
        }
503
        }
502
        if(Request_ExternalControl && txd_complete)
504
        if(Request_ExternalControl && txd_complete)
503
        {
505
        {
504
                SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl));
506
                SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl));
505
                Request_ExternalControl = FALSE;
507
                Request_ExternalControl = FALSE;
506
        }
508
        }
507
        if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete)
509
        if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete)
508
        {
510
        {
509
                SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut));
511
                SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut));
510
                DebugData_Timer = SetDelay(DebugData_Interval);
512
                DebugData_Timer = SetDelay(DebugData_Interval);
511
                Request_DebugData = FALSE;
513
                Request_DebugData = FALSE;
512
    }
514
    }
-
 
515
        if(Request_SendFollowMe && txd_complete)
-
 
516
        {
-
 
517
                SendOutData('s', NC_ADDRESS, 1, (uint8_t *)&FollowMe, sizeof(FollowMe));
-
 
518
                FollowMe.Position.Status = PROCESSED;
-
 
519
                Request_SendFollowMe = FALSE;
-
 
520
        }
513
}
521
}
514
 
522
 
515
 
523