Subversion Repositories NaviCtrl

Rev

Rev 193 | Rev 195 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
41 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
41 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
41 ingob 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
58
#include <stdarg.h>
59
#include <string.h>
60
 
61
#include "91x_lib.h"
136 killagreg 62
#include "config.h"
41 ingob 63
#include "menu.h"
64
#include "printf_P.h"
65
#include "GPS.h"
66
#include "i2c.h"
67
#include "uart0.h"
68
#include "uart1.h"
69
#include "uart2.h"
119 killagreg 70
#include "timer1.h"
139 killagreg 71
#include "timer2.h"
72
#include "analog.h"
41 ingob 73
#include "main.h"
74
#include "waypoints.h"
110 killagreg 75
#include "mkprotocol.h"
156 killagreg 76
#include "params.h"
153 killagreg 77
#include "fifo.h"
41 ingob 78
 
79
#define FALSE   0
80
#define TRUE    1
81
 
92 killagreg 82
 
110 killagreg 83
u8 UART1_Request_VersionInfo    = FALSE;
84
u8 UART1_Request_SendFollowMe   = FALSE;
85
u8 UART1_Request_ExternalControl= FALSE;
86
u8 UART1_Request_Display                = FALSE;
87
u8 UART1_Request_Display1               = FALSE;
88
u8 UART1_Request_DebugData              = FALSE;
89
u8 UART1_Request_DebugLabel             = 255;
90
u8 UART1_Request_NaviData               = FALSE;
91
u8 UART1_Request_ErrorMessage   = FALSE;
92
u8 UART1_Request_NewWaypoint    = FALSE;
93
u8 UART1_Request_ReadWaypoint   = 255;
94
u8 UART1_Request_Data3D             = FALSE;
95
u8 UART1_Request_Echo               = FALSE;
156 killagreg 96
u8 UART1_Request_ParameterId    = 0;
158 killagreg 97
u8 UART1_Request_Parameter              = FALSE;
112 killagreg 98
u8 UART1_DisplayLine                    = 0;
99
u8 UART1_ConfirmFrame                   = 0;
41 ingob 100
 
101
UART_TypeDef *DebugUART = UART1;
102
 
153 killagreg 103
// the primary rx fifo
154 killagreg 104
#define UART1_RX_FIFO_LEN 1024
153 killagreg 105
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
106
fifo_t UART1_rx_fifo;
110 killagreg 107
 
108
// the rx buffer
109
#define UART1_RX_BUFFER_LEN  150
110
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
111
Buffer_t UART1_rx_buffer;
112
 
153 killagreg 113
// the tx buffer
114
#define UART1_TX_BUFFER_LEN  150
115
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
116
Buffer_t UART1_tx_buffer;
117
 
118
 
119
 
92 killagreg 120
volatile u8 SerialLinkOkay = 0;
41 ingob 121
 
150 killagreg 122
u8 text[200];
41 ingob 123
 
124
const u8 ANALOG_LABEL[32][16] =
125
{
90 killagreg 126
   //1234567890123456
41 ingob 127
        "AngleNick       ", //0
128
        "AngleRoll       ",
129
        "AccNick         ",
130
        "AccRoll         ",
131
        "                ",
75 holgerb 132
        "MK-Flags        ", //5
95 killagreg 133
        "NC-Flags        ",
121 killagreg 134
        "NickServo       ",
135
        "RollServo       ",
41 ingob 136
        "GPS Data        ",
61 holgerb 137
        "CompassHeading  ", //10
138
        "GyroHeading     ",
41 ingob 139
        "SPI Error       ",
140
        "SPI Okay        ",
189 killagreg 141
        "I2C Error       ",
41 ingob 142
        "I2C Okay        ", //15
149 killagreg 143
        "                ",//    "Kalman_K        ",
41 ingob 144
        "ACC_Speed_N     ",
145
        "ACC_Speed_E     ",
148 holgerb 146
        "Speed_z         ",//    "GPS ACC         ",
41 ingob 147
        "                ",//    "MAXDrift        ", //20
148
        "N_Speed         ",
149
        "E_Speed         ",
99 killagreg 150
        "P-Part          ",
151
        "I-Part          ",
152
        "D-Part          ",//25
153
        "PID-Part        ",
41 ingob 154
        "Distance N      ",
155
        "Distance E      ",
156
        "GPS_Nick        ",
157
        "GPS_Roll        ", //30
158
        "Used_Sats       "
159
};
160
 
112 killagreg 161
DebugOut_t DebugOut;
162
ExternControl_t ExternControl;
163
UART_VersionInfo_t UART_VersionInfo;
164
NaviData_t NaviData;
165
Waypoint_t FollowMe;
166
Data3D_t Data3D;
167
u16 Echo; // 2 bytes recieved will be sent back as echo
41 ingob 168
 
193 killagreg 169
u32 UART1_DebugData_Timer = 0;
112 killagreg 170
u32 UART1_DebugData_Interval = 5000;    // in ms
193 killagreg 171
u32 UART1_NaviData_Timer = 0;
172
u32 UART1_NaviData_Interval = 5000;             // in ms
173
u32 UART1_Data3D_Timer = 0;                             // in ms
112 killagreg 174
u32 UART1_Data3D_Interval = 0;
193 killagreg 175
u32 UART1_Display_Timer = 0;
176
u32 UART1_Display_Interval = 0;
112 killagreg 177
 
41 ingob 178
/********************************************************/
179
/*            Initialization the UART1                  */
180
/********************************************************/
181
void UART1_Init (void)
182
{
183
        GPIO_InitTypeDef GPIO_InitStructure;
184
        UART_InitTypeDef UART_InitStructure;
185
 
153 killagreg 186
        // initialize txd buffer
187
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
165 killagreg 188
 
153 killagreg 189
        // initialize rxd buffer
190
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
191
 
192
        // initialize the rx fifo
193
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN);
194
 
41 ingob 195
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
196
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
197
 
198
        /*Configure UART1_Rx pin GPIO3.2*/
199
        GPIO_StructInit(&GPIO_InitStructure);
200
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
201
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
202
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
203
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Enable;
204
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
205
        GPIO_Init(GPIO3, &GPIO_InitStructure);
206
 
207
        /*Configure UART1_Tx pin GPIO3.3*/
208
        GPIO_StructInit(&GPIO_InitStructure);
209
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
210
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
211
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
212
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
213
        GPIO_Init(GPIO3, &GPIO_InitStructure);
214
 
215
        /* UART1 configured as follow:
216
        - Word Length = 8 Bits
217
        - One Stop Bit
218
        - No parity
219
        - BaudRate = 57600 baud
220
        - Hardware flow control Disabled
221
        - Receive and transmit enabled
222
        - Receive and transmit FIFOs are Disabled
223
        */
224
        UART_StructInit(&UART_InitStructure);
225
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
226
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
227
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
110 killagreg 228
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
41 ingob 229
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
230
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
231
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
232
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
139 killagreg 233
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
41 ingob 234
 
235
        UART_DeInit(UART1); // reset uart 1     to default
165 killagreg 236
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
41 ingob 237
        // enable uart 1 interrupts selective
238
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
239
        UART_Cmd(UART1, ENABLE); // enable uart 1
136 killagreg 240
        // configure the uart 1 interupt line
241
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
153 killagreg 242
                // enable the uart 1 IRQ
41 ingob 243
        VIC_ITCmd(UART1_ITLine, ENABLE);
153 killagreg 244
 
41 ingob 245
        // initialize the debug timer
110 killagreg 246
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
247
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
248
 
41 ingob 249
        // Fill Version Info Structure
250
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
251
        UART_VersionInfo.SWMinor = VERSION_MINOR;
252
        UART_VersionInfo.SWPatch = VERSION_PATCH;
253
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
254
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
255
 
89 killagreg 256
        NaviData.Version = NAVIDATA_VERSION;
92 killagreg 257
 
110 killagreg 258
        UART1_PutString("\r\nUART1 init...ok");
41 ingob 259
}
260
 
261
 
262
/****************************************************************/
263
/*               USART1 receiver ISR                            */
264
/****************************************************************/
265
void UART1_IRQHandler(void)
266
{
267
        static u8 abortState = 0;
268
        u8 c;
269
 
270
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
271
        {
153 killagreg 272
                // clear the pending bits!
41 ingob 273
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
274
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
275
                // if debug UART is not UART1
276
                if (DebugUART != UART1)
277
                {       // forward received data to the debug UART tx buffer
278
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
279
                        {
280
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
281
                                c = UART_ReceiveData(UART1);
282
 
283
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
284
                                switch (abortState)
285
                                {
165 killagreg 286
                                        case 0:
139 killagreg 287
                                                if (c == 27) abortState++;
41 ingob 288
                                                break;
165 killagreg 289
                                        case 1:
290
                                                if (c == 27) abortState++;
139 killagreg 291
                                                else abortState = 0;
41 ingob 292
                                        break;
139 killagreg 293
                                        case 2:
165 killagreg 294
                                                if (c == 0x55) abortState++;
139 killagreg 295
                                                else abortState = 0;
41 ingob 296
                                                break;
139 killagreg 297
                                        case 3:
298
                                                if (c == 0xAA) abortState++;
299
                                                else abortState = 0;
41 ingob 300
                                                break;
165 killagreg 301
                                        case 4:
139 killagreg 302
                                                if (c == 0x00)
303
                                                {
304
                                                        if(DebugUART == UART0)
305
                                                        {
306
                                                                UART0_Connect_to_MKGPS();
179 killagreg 307
                                                                TIMER2_Init(); // enbable servo outputs
153 killagreg 308
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
139 killagreg 309
                                                        }
310
                                                        DebugUART = UART1;
165 killagreg 311
                                                }
139 killagreg 312
                                                abortState = 0;
313
                                                break;
314
                                } // end switch abort state
136 killagreg 315
                                // if the Debug uart is not UART1, redirect input to the Debug UART
110 killagreg 316
                                if (DebugUART != UART1)
317
                                {
318
                                        // wait for space in the tx buffer of the DebugUART
319
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
141 killagreg 320
                                        // move byte to the tx fifo of the debug uart
110 killagreg 321
                                        UART_SendData(DebugUART, c);
322
                                }
41 ingob 323
                        }
324
                }
325
                else  // DebugUART == UART1 (normal operation)
326
                {
110 killagreg 327
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
154 killagreg 328
                        { // some byes in the hardware fifo
153 killagreg 329
                            // get byte from hardware fifo
41 ingob 330
                        c = UART_ReceiveData(UART1);
153 killagreg 331
                                // put into the software fifo
332
                                if(!fifo_put(&UART1_rx_fifo, c))
333
                                {       // fifo overflow
155 killagreg 334
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
153 killagreg 335
                                }
154 killagreg 336
                        } // EOF while some byes in the hardware fifo
41 ingob 337
                } // eof DebugUart = UART1
338
        }
339
}
340
 
341
/**************************************************************/
342
/* Process incomming data from debug uart                     */
343
/**************************************************************/
344
void UART1_ProcessRxData(void)
345
{
154 killagreg 346
        // return on forwarding uart  or unlocked rx buffer
155 killagreg 347
        if(DebugUART != UART1) return;
165 killagreg 348
 
155 killagreg 349
        u8 c;
350
        // if rx buffer is not locked
351
        if(UART1_rx_buffer.Locked == FALSE)
165 killagreg 352
        {   //collect data from primary rx fifo
155 killagreg 353
                while(fifo_get(&UART1_rx_fifo, &c))
354
                {       // break if complete frame is collected
355
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
356
                }
357
        }
358
        if(UART1_rx_buffer.Locked == FALSE) return;
359
 
41 ingob 360
        Waypoint_t * pWaypoint = NULL;
153 killagreg 361
        SerialMsg_t SerialMsg;
41 ingob 362
 
190 killagreg 363
        // analyze header first
364
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
194 killagreg 365
        if( SerialMsg.Address == FC_ADDRESS )
41 ingob 366
        {
192 killagreg 367
                switch(SerialMsg.CmdID)
368
                {
369
                        case 'y': // serial poti values
370
                        case 'b': // extern control
371
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
372
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
194 killagreg 373
                                return; //end process rx data
192 killagreg 374
                        break;
375
                }
190 killagreg 376
        }      
377
 
378
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
379
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
380
        switch(SerialMsg.Address) // check for Slave Address
381
        {
41 ingob 382
                case NC_ADDRESS:  // own Slave Address
190 killagreg 383
                switch(SerialMsg.CmdID)
41 ingob 384
                {
101 holgerb 385
                        case 'z': // connection checker
110 killagreg 386
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
387
                                UART1_Request_Echo = TRUE;
92 killagreg 388
                                break;
389
 
41 ingob 390
                        case 'e': // request for the text of the error status
110 killagreg 391
                                UART1_Request_ErrorMessage = TRUE;
41 ingob 392
                                break;
393
 
394
                        case 's'://  new target position
110 killagreg 395
                                pWaypoint = (Waypoint_t*)SerialMsg.pData;
41 ingob 396
                                BeepTime = 300;
397
                                if(pWaypoint->Position.Status == NEWDATA)
398
                                {
399
                                        WPList_Clear(); // empty WPList
400
                                        WPList_Append(pWaypoint);
401
                                        GPS_pWaypoint = WPList_Begin();
402
                                }
403
                                break;
404
 
405
                        case 'u': // redirect debug uart
110 killagreg 406
                                switch(SerialMsg.pData[0])
41 ingob 407
                                {
408
                                        case UART_FLIGHTCTRL:
409
                                                UART2_Init();                           // initialize UART2 to FC pins
153 killagreg 410
                                                fifo_purge(&UART1_rx_fifo);
180 killagreg 411
                                                TIMER2_Deinit();                        // reduce irq load
41 ingob 412
                                                DebugUART = UART2;
413
                                                break;
414
                                        case UART_MK3MAG:
415
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
416
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
417
                                                GPSData.Status = INVALID;
153 killagreg 418
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 419
                                                DebugUART = UART0;
420
                                                break;
421
                                        case UART_MKGPS:
422
                                                if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
179 killagreg 423
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
41 ingob 424
                                                UART0_Connect_to_MKGPS();       // connect UART0 to MKGPS pins
425
                                                GPSData.Status = INVALID;
153 killagreg 426
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 427
                                                DebugUART = UART0;
428
                                                break;
153 killagreg 429
                                        default:
430
                                                break;
41 ingob 431
                                }
432
                                break;
433
 
92 killagreg 434
                        case 'w'://  Append Waypoint to List
165 killagreg 435
                                {
436
                                        static u8 oldIndex = 0x00;
437
 
438
                                        pWaypoint = (Waypoint_t*)SerialMsg.pData;
439
                                        if(pWaypoint->Position.Status == INVALID)
440
                                        {  // clear WP List
441
                                                WPList_Clear();
442
                                                oldIndex = 0x00;
443
                                                GPS_pWaypoint = WPList_Begin();
444
                                                UART1_Request_NewWaypoint = TRUE;
445
                                        }
446
                                        else if (pWaypoint->Position.Status == NEWDATA)
447
                                        {  // app current WP to the list
448
                                                 if (pWaypoint->Index == oldIndex + 1)
449
                                                {
450
                                                        WPList_Append(pWaypoint);
451
                                                        BeepTime = 500;
452
                                                        oldIndex = pWaypoint->Index;
453
                                                        UART1_Request_NewWaypoint = TRUE;
454
                                                }
455
                                        }
41 ingob 456
                                }
457
                                break;
458
 
92 killagreg 459
                        case 'x'://  Read Waypoint from List
110 killagreg 460
                                UART1_Request_ReadWaypoint = SerialMsg.pData[0];
92 killagreg 461
                                break;
462
 
160 holgerb 463
                        case 'j':// Set/Get NC-Parameter
156 killagreg 464
                                switch(SerialMsg.pData[0])
465
                                {
466
                                        case 0: // get
467
                                        break;
165 killagreg 468
 
156 killagreg 469
                                        case 1: // set
159 killagreg 470
                                        {
471
                                                s16 value;
472
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
473
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
474
                                        }
156 killagreg 475
                                        break;
476
 
477
                                        default:
165 killagreg 478
                                        break;
156 killagreg 479
                                }
480
                                UART1_Request_ParameterId = SerialMsg.pData[1];
481
                                UART1_Request_Parameter = TRUE;
482
                                break;
41 ingob 483
                        default:
484
                                // unsupported command recieved
485
                                break;
486
                } // case NC_ADDRESS
65 killagreg 487
                // "break;" is missing here to fall thru to the common commands
41 ingob 488
 
489
                default:  // and any other Slave Address
490
 
190 killagreg 491
                switch(SerialMsg.CmdID) // check CmdID
41 ingob 492
                {
493
                        case 'a':// request for the labels of the analog debug outputs
110 killagreg 494
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
495
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
41 ingob 496
                                break;
193 killagreg 497
                        /*
41 ingob 498
                        case 'b': // submit extern control
121 killagreg 499
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
112 killagreg 500
                                UART1_ConfirmFrame = ExternControl.Frame;
41 ingob 501
                                break;
193 killagreg 502
                        */     
41 ingob 503
                        case 'd': // request for debug data;
110 killagreg 504
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
505
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
41 ingob 506
                                break;
507
 
63 killagreg 508
                        case 'c': // request for 3D data;
110 killagreg 509
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
510
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
63 killagreg 511
                                break;
193 killagreg 512
                        /*
41 ingob 513
                        case 'g':// request for external control data
110 killagreg 514
                                UART1_Request_ExternalControl = TRUE;
41 ingob 515
                                break;
193 killagreg 516
                        */
41 ingob 517
                        case 'h':// reqest for display line
193 killagreg 518
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
519
                                {
520
                                        UART1_DisplayLine = 2;
521
                                        UART1_Display_Interval = 0;    
522
                                }
523
                                else
524
                                {
525
                                        RemoteKeys |= ~SerialMsg.pData[0];
526
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
527
                                        UART1_DisplayLine = 4;
528
                                }
110 killagreg 529
                                UART1_Request_Display = TRUE;
41 ingob 530
                                break;
531
 
532
                        case 'l':// reqest for display columns
110 killagreg 533
                                MenuItem = SerialMsg.pData[0];
534
                                UART1_Request_Display1 = TRUE;
41 ingob 535
                                break;
64 holgerb 536
 
537
                        case 'o': // request for navigation information
110 killagreg 538
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
539
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
64 holgerb 540
                                break;
541
 
41 ingob 542
                        case 'v': // request for version info
110 killagreg 543
                                UART1_Request_VersionInfo = TRUE;
41 ingob 544
                                break;
545
                        default:
546
                                // unsupported command recieved
547
                                break;
548
                }
549
                break; // default:
550
        }
153 killagreg 551
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
41 ingob 552
}
553
 
554
 
555
/*****************************************************/
556
/*                   Send a character                */
557
/*****************************************************/
110 killagreg 558
s16 UART1_Putchar(char c)
41 ingob 559
{
110 killagreg 560
        if (c == '\n') UART1_Putchar('\r');
41 ingob 561
        // wait until txd fifo is not full
562
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
563
        // transmit byte
564
        UART_SendData(UART1, c);
565
        return (0);
566
}
567
 
568
/*****************************************************/
569
/*       Send a string to the debug uart              */
570
/*****************************************************/
110 killagreg 571
void UART1_PutString(u8 *s)
41 ingob 572
{
573
        if(s == NULL) return;
574
        while (*s != '\0' && DebugUART == UART1)
575
        {
110 killagreg 576
                UART1_Putchar(*s);
41 ingob 577
                s ++;
578
        }
579
}
580
 
113 killagreg 581
 
110 killagreg 582
/**************************************************************/
583
/*         Transmit tx buffer via debug uart                  */
584
/**************************************************************/
585
void UART1_Transmit(void)
586
{
587
        u8 tmp_tx;
588
        if(DebugUART != UART1) return;
589
        // if something has to be send and the txd fifo is not full
113 killagreg 590
        if(UART1_tx_buffer.Locked == TRUE)
110 killagreg 591
        {
113 killagreg 592
                // while there is some space in the tx fifo
593
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
110 killagreg 594
                {
113 killagreg 595
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
596
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
597
                        // if terminating character or end of txd buffer reached
598
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
599
                        {
600
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
601
                                break; // end while loop
602
                        }
110 killagreg 603
                }
604
        }
605
}
41 ingob 606
 
607
/**************************************************************/
608
/* Send the answers to incomming commands at the debug uart   */
609
/**************************************************************/
610
void UART1_TransmitTxData(void)
611
{
110 killagreg 612
        if(DebugUART != UART1) return;
613
        UART1_Transmit(); // output pending bytes in tx buffer
614
        if((UART1_tx_buffer.Locked == TRUE)) return;
41 ingob 615
 
156 killagreg 616
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
92 killagreg 617
        {
156 killagreg 618
                s16 ParamValue;
157 killagreg 619
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
158 killagreg 620
                //sprintf(text, "\r\nId=%d, value = %d\r\n", UART1_Request_ParameterId, ParamValue);
621
                //UART1_PutString(text);
160 holgerb 622
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'J', NC_ADDRESS, 2, &UART1_Request_ParameterId, sizeof(UART1_Request_ParameterId), &ParamValue, sizeof(ParamValue)); // answer the param request
156 killagreg 623
                UART1_Request_Parameter = FALSE;
624
        }
625
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
626
        {
110 killagreg 627
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
92 killagreg 628
                Echo = 0; // reset echo value
110 killagreg 629
                UART1_Request_Echo = FALSE;
92 killagreg 630
        }
154 killagreg 631
        else if(UART1_Request_NewWaypoint && (UART1_tx_buffer.Locked == FALSE))
632
        {
633
                u8 WPNumber = WPList_GetCount();
634
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
635
                UART1_Request_NewWaypoint = FALSE;
636
        }
637
        else if((UART1_Request_ReadWaypoint != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
638
        {
639
                u8 WPNumber = WPList_GetCount();
640
                if (UART1_Request_ReadWaypoint < WPNumber)
641
                {
642
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &WPNumber, 1, &UART1_Request_ReadWaypoint, 1, WPList_GetAt(UART1_Request_ReadWaypoint), sizeof(Waypoint_t));
643
                }
644
                else
645
                {
646
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber));
647
                }
648
                UART1_Request_ReadWaypoint = 0xFF;
649
        }
650
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
651
        {
652
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
653
                UART1_Request_DebugLabel = 0xFF;
654
        }
151 killagreg 655
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
41 ingob 656
        {
151 killagreg 657
                NaviData.Errorcode = ErrorCode;
658
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
659
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
660
                UART1_Request_NaviData = FALSE;
661
        }
662
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
663
        {
110 killagreg 664
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
665
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
666
                UART1_Request_DebugData = FALSE;
41 ingob 667
        }
151 killagreg 668
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
61 holgerb 669
        {
110 killagreg 670
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
671
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
672
                UART1_Request_Data3D = FALSE;
61 holgerb 673
        }
193 killagreg 674
        /*
153 killagreg 675
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
676
        {
677
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
678
                UART1_ConfirmFrame = 0;
679
        }
193 killagreg 680
        */
681
        /*
151 killagreg 682
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
41 ingob 683
        {
110 killagreg 684
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
685
                UART1_Request_ExternalControl = FALSE;
41 ingob 686
        }
193 killagreg 687
        */
688
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
689
        {      
690
                if(UART1_DisplayLine > 3)
691
                {
692
                        LCD_PrintMenu();
693
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
694
                }
695
                else
696
                {
697
                        UART1_DisplayLine = 2;
698
                        sprintf(text,"!!! incompatible !!!");
699
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
700
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
701
                }
702
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
110 killagreg 703
                UART1_Request_Display = FALSE;
41 ingob 704
        }
151 killagreg 705
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
41 ingob 706
        {
707
                LCD_PrintMenu();
110 killagreg 708
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
709
                UART1_Request_Display1 = FALSE;
41 ingob 710
        }
151 killagreg 711
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
41 ingob 712
        {
110 killagreg 713
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
714
                UART1_Request_VersionInfo = FALSE;
41 ingob 715
        }
151 killagreg 716
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
41 ingob 717
        {
110 killagreg 718
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
719
                UART1_Request_ErrorMessage = FALSE;
41 ingob 720
        }
151 killagreg 721
        else if(UART1_Request_SendFollowMe && (UART1_tx_buffer.Locked == FALSE) && (GPSData.NumOfSats >= 4))              // sending for "Follow me"
41 ingob 722
        {
723
                GPS_CopyPosition(&(GPSData.Position),&(FollowMe.Position));
724
                FollowMe.Position.Status = NEWDATA;
725
                FollowMe.Heading = -1;
726
                FollowMe.ToleranceRadius = 1;
727
                FollowMe.HoldTime = 60;
728
                FollowMe.Event_Flag = 0;
192 killagreg 729
                FollowMe.Index = 1;
41 ingob 730
                FollowMe.reserve[0] = 0;                // reserve
731
                FollowMe.reserve[1] = 0;                // reserve
732
                FollowMe.reserve[2] = 0;                // reserve
733
                FollowMe.reserve[3] = 0;                // reserve
110 killagreg 734
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
735
                UART1_Request_SendFollowMe = FALSE;
41 ingob 736
        }
110 killagreg 737
        UART1_Transmit(); // output pending bytes in tx buffer
41 ingob 738
}
739