Subversion Repositories Projects

Rev

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

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