Subversion Repositories Projects

Rev

Rev 759 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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