Subversion Repositories NaviCtrl

Rev

Rev 310 | Rev 315 | 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"
251 killagreg 62
#include "main.h"
136 killagreg 63
#include "config.h"
41 ingob 64
#include "menu.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"
243 killagreg 73
#include "compass.h"
41 ingob 74
#include "waypoints.h"
110 killagreg 75
#include "mkprotocol.h"
156 killagreg 76
#include "params.h"
153 killagreg 77
#include "fifo.h"
254 killagreg 78
#include "debug.h" 
41 ingob 79
 
80
#define FALSE   0
81
#define TRUE    1
82
 
198 killagreg 83
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
84
u32 UART1_AboTimeOut = 0;
92 killagreg 85
 
110 killagreg 86
u8 UART1_Request_VersionInfo    = FALSE;
87
u8 UART1_Request_ExternalControl= FALSE;
88
u8 UART1_Request_Display                = FALSE;
89
u8 UART1_Request_Display1               = FALSE;
90
u8 UART1_Request_DebugData              = FALSE;
91
u8 UART1_Request_DebugLabel             = 255;
92
u8 UART1_Request_NaviData               = FALSE;
93
u8 UART1_Request_ErrorMessage   = FALSE;
295 killagreg 94
u8 UART1_Request_WritePoint             = 0xFF;
280 killagreg 95
u8 UART1_Request_ReadPoint              = 0;
110 killagreg 96
u8 UART1_Request_Data3D             = FALSE;
97
u8 UART1_Request_Echo               = FALSE;
156 killagreg 98
u8 UART1_Request_ParameterId    = 0;
158 killagreg 99
u8 UART1_Request_Parameter              = FALSE;
201 killagreg 100
u8 UART1_DisplayKeys                    = 0;
112 killagreg 101
u8 UART1_DisplayLine                    = 0;
102
u8 UART1_ConfirmFrame                   = 0;
41 ingob 103
 
104
UART_TypeDef *DebugUART = UART1;
105
 
314 killagreg 106
#ifdef FOLLOW_ME
107
#define FOLLOW_ME_INTERVAL 200 // 5 Hz
108
u8 UART1_FollowMe_Timer = 0;
109
Point_t FollowMe;
110
#endif
111
 
153 killagreg 112
// the primary rx fifo
247 killagreg 113
#define UART1_RX_FIFO_LEN 512
153 killagreg 114
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
115
fifo_t UART1_rx_fifo;
110 killagreg 116
 
117
// the rx buffer
247 killagreg 118
#define UART1_RX_BUFFER_LEN  150
110 killagreg 119
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
120
Buffer_t UART1_rx_buffer;
121
 
153 killagreg 122
// the tx buffer
247 killagreg 123
#define UART1_TX_BUFFER_LEN  150
153 killagreg 124
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
125
Buffer_t UART1_tx_buffer;
126
 
127
 
128
 
92 killagreg 129
volatile u8 SerialLinkOkay = 0;
41 ingob 130
 
150 killagreg 131
u8 text[200];
41 ingob 132
 
133
const u8 ANALOG_LABEL[32][16] =
134
{
90 killagreg 135
   //1234567890123456
41 ingob 136
        "AngleNick       ", //0
137
        "AngleRoll       ",
138
        "AccNick         ",
139
        "AccRoll         ",
214 killagreg 140
        "OperatingRadius ",
200 killagreg 141
        "FC-Flags        ", //5
95 killagreg 142
        "NC-Flags        ",
121 killagreg 143
        "NickServo       ",
144
        "RollServo       ",
41 ingob 145
        "GPS Data        ",
61 holgerb 146
        "CompassHeading  ", //10
147
        "GyroHeading     ",
41 ingob 148
        "SPI Error       ",
149
        "SPI Okay        ",
189 killagreg 150
        "I2C Error       ",
41 ingob 151
        "I2C Okay        ", //15
283 holgerb 152
        "*POI_INDEX      ",//    "Kalman_K        ",
41 ingob 153
        "ACC_Speed_N     ",
154
        "ACC_Speed_E     ",
148 holgerb 155
        "Speed_z         ",//    "GPS ACC         ",
300 holgerb 156
        "20              ",//20
41 ingob 157
        "N_Speed         ",
158
        "E_Speed         ",
300 holgerb 159
        "23              ",
310 holgerb 160
        "Magnet X        ",
161
        "Magnet Y        ",//25
162
        "Magnet Z        ",
41 ingob 163
        "Distance N      ",
164
        "Distance E      ",
165
        "GPS_Nick        ",
166
        "GPS_Roll        ", //30
167
        "Used_Sats       "
168
};
169
 
112 killagreg 170
DebugOut_t DebugOut;
171
ExternControl_t ExternControl;
172
UART_VersionInfo_t UART_VersionInfo;
173
NaviData_t NaviData;
174
Data3D_t Data3D;
175
u16 Echo; // 2 bytes recieved will be sent back as echo
41 ingob 176
 
193 killagreg 177
u32 UART1_DebugData_Timer = 0;
198 killagreg 178
u32 UART1_DebugData_Interval = 0;       // in ms
193 killagreg 179
u32 UART1_NaviData_Timer = 0;
198 killagreg 180
u32 UART1_NaviData_Interval = 0;        // in ms
181
u32 UART1_Data3D_Timer = 0;
182
u32 UART1_Data3D_Interval = 0;          // in ms
193 killagreg 183
u32 UART1_Display_Timer = 0;
201 killagreg 184
u32 UART1_Display_Interval = 0;         // in ms
112 killagreg 185
 
41 ingob 186
/********************************************************/
187
/*            Initialization the UART1                  */
188
/********************************************************/
189
void UART1_Init (void)
190
{
191
        GPIO_InitTypeDef GPIO_InitStructure;
192
        UART_InitTypeDef UART_InitStructure;
193
 
153 killagreg 194
        // initialize txd buffer
195
        Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
165 killagreg 196
 
153 killagreg 197
        // initialize rxd buffer
198
        Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
199
 
244 killagreg 200
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
201
        fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN, NO_ITLine, UART1_ITLine);
153 killagreg 202
 
41 ingob 203
        SCU_APBPeriphClockConfig(__UART1, ENABLE);  // Enable the UART1 Clock
204
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
205
 
206
        /*Configure UART1_Rx pin GPIO3.2*/
207
        GPIO_StructInit(&GPIO_InitStructure);
208
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
209
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2;
210
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
196 killagreg 211
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
41 ingob 212
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1; // UART1_RxD
213
        GPIO_Init(GPIO3, &GPIO_InitStructure);
214
 
215
        /*Configure UART1_Tx pin GPIO3.3*/
216
        GPIO_StructInit(&GPIO_InitStructure);
217
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
218
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_3;
219
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
220
        GPIO_InitStructure.GPIO_Alternate =     GPIO_OutputAlt2; // UART1_TX
221
        GPIO_Init(GPIO3, &GPIO_InitStructure);
222
 
223
        /* UART1 configured as follow:
224
        - Word Length = 8 Bits
225
        - One Stop Bit
226
        - No parity
227
        - BaudRate = 57600 baud
228
        - Hardware flow control Disabled
229
        - Receive and transmit enabled
230
        - Receive and transmit FIFOs are Disabled
231
        */
232
        UART_StructInit(&UART_InitStructure);
233
        UART_InitStructure.UART_WordLength =                    UART_WordLength_8D;
234
        UART_InitStructure.UART_StopBits =                              UART_StopBits_1;
235
        UART_InitStructure.UART_Parity =                                UART_Parity_No ;
110 killagreg 236
        UART_InitStructure.UART_BaudRate =                              UART1_BAUD_RATE;
41 ingob 237
        UART_InitStructure. UART_HardwareFlowControl =  UART_HardwareFlowControl_None;
238
        UART_InitStructure.UART_Mode =                                  UART_Mode_Tx_Rx;
239
        UART_InitStructure.UART_FIFO =                                  UART_FIFO_Enable;
240
        UART_InitStructure.UART_TxFIFOLevel =                   UART_FIFOLevel_1_2;
139 killagreg 241
        UART_InitStructure.UART_RxFIFOLevel =                   UART_FIFOLevel_1_2;
41 ingob 242
 
243
        UART_DeInit(UART1); // reset uart 1     to default
165 killagreg 244
        UART_Init(UART1, &UART_InitStructure); // initialize uart 1
41 ingob 245
        // enable uart 1 interrupts selective
246
        UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
247
        UART_Cmd(UART1, ENABLE); // enable uart 1
136 killagreg 248
        // configure the uart 1 interupt line
249
        VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
244 killagreg 250
        // enable the uart 1 IRQ
41 ingob 251
        VIC_ITCmd(UART1_ITLine, ENABLE);
153 killagreg 252
 
41 ingob 253
        // initialize the debug timer
110 killagreg 254
        UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
255
        UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
256
 
41 ingob 257
        // Fill Version Info Structure
258
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
259
        UART_VersionInfo.SWMinor = VERSION_MINOR;
260
        UART_VersionInfo.SWPatch = VERSION_PATCH;
261
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
262
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
263
 
89 killagreg 264
        NaviData.Version = NAVIDATA_VERSION;
92 killagreg 265
 
212 killagreg 266
        UART1_PutString("\r\n UART1 init...ok");
41 ingob 267
}
268
 
269
 
270
/****************************************************************/
271
/*               USART1 receiver ISR                            */
272
/****************************************************************/
273
void UART1_IRQHandler(void)
274
{
275
        static u8 abortState = 0;
276
        u8 c;
277
 
195 killagreg 278
        IENABLE;
279
 
41 ingob 280
        if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
281
        {
153 killagreg 282
                // clear the pending bits!
41 ingob 283
                UART_ClearITPendingBit(UART1, UART_IT_Receive);
284
                UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
285
                // if debug UART is not UART1
286
                if (DebugUART != UART1)
287
                {       // forward received data to the debug UART tx buffer
288
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
289
                        {
290
                                // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART
291
                                c = UART_ReceiveData(UART1);
292
 
293
                                // check for abort condition (ESC ESC 0x55 0xAA 0x00)
294
                                switch (abortState)
295
                                {
165 killagreg 296
                                        case 0:
139 killagreg 297
                                                if (c == 27) abortState++;
41 ingob 298
                                                break;
165 killagreg 299
                                        case 1:
300
                                                if (c == 27) abortState++;
139 killagreg 301
                                                else abortState = 0;
41 ingob 302
                                        break;
139 killagreg 303
                                        case 2:
165 killagreg 304
                                                if (c == 0x55) abortState++;
139 killagreg 305
                                                else abortState = 0;
41 ingob 306
                                                break;
139 killagreg 307
                                        case 3:
308
                                                if (c == 0xAA) abortState++;
309
                                                else abortState = 0;
41 ingob 310
                                                break;
165 killagreg 311
                                        case 4:
139 killagreg 312
                                                if (c == 0x00)
313
                                                {
314
                                                        if(DebugUART == UART0)
315
                                                        {
245 killagreg 316
                                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
179 killagreg 317
                                                                TIMER2_Init(); // enbable servo outputs
153 killagreg 318
                                                                fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
139 killagreg 319
                                                        }
320
                                                        DebugUART = UART1;
165 killagreg 321
                                                }
139 killagreg 322
                                                abortState = 0;
323
                                                break;
324
                                } // end switch abort state
136 killagreg 325
                                // if the Debug uart is not UART1, redirect input to the Debug UART
110 killagreg 326
                                if (DebugUART != UART1)
327
                                {
328
                                        // wait for space in the tx buffer of the DebugUART
329
                                        while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {};
141 killagreg 330
                                        // move byte to the tx fifo of the debug uart
110 killagreg 331
                                        UART_SendData(DebugUART, c);
332
                                }
41 ingob 333
                        }
334
                }
335
                else  // DebugUART == UART1 (normal operation)
336
                {
110 killagreg 337
                        while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
154 killagreg 338
                        { // some byes in the hardware fifo
153 killagreg 339
                            // get byte from hardware fifo
41 ingob 340
                        c = UART_ReceiveData(UART1);
153 killagreg 341
                                // put into the software fifo
342
                                if(!fifo_put(&UART1_rx_fifo, c))
343
                                {       // fifo overflow
155 killagreg 344
                                        //fifo_purge(&UART1_rx_fifo); // flush the whole buffer
153 killagreg 345
                                }
154 killagreg 346
                        } // EOF while some byes in the hardware fifo
41 ingob 347
                } // eof DebugUart = UART1
348
        }
195 killagreg 349
 
350
        IDISABLE;
41 ingob 351
}
352
 
353
/**************************************************************/
354
/* Process incomming data from debug uart                     */
355
/**************************************************************/
356
void UART1_ProcessRxData(void)
357
{
154 killagreg 358
        // return on forwarding uart  or unlocked rx buffer
155 killagreg 359
        if(DebugUART != UART1) return;
165 killagreg 360
 
155 killagreg 361
        u8 c;
362
        // if rx buffer is not locked
363
        if(UART1_rx_buffer.Locked == FALSE)
244 killagreg 364
        {
365
                //collect data from primary rx fifo
155 killagreg 366
                while(fifo_get(&UART1_rx_fifo, &c))
244 killagreg 367
                {      
368
                        // break if complete frame is collected
155 killagreg 369
                        if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
370
                }
371
        }
372
        if(UART1_rx_buffer.Locked == FALSE) return;
373
 
280 killagreg 374
        Point_t * pPoint = NULL;
153 killagreg 375
        SerialMsg_t SerialMsg;
41 ingob 376
 
190 killagreg 377
        // analyze header first
378
        MKProtocol_DecodeSerialFrameHeader(&UART1_rx_buffer, &SerialMsg);
194 killagreg 379
        if( SerialMsg.Address == FC_ADDRESS )
41 ingob 380
        {
192 killagreg 381
                switch(SerialMsg.CmdID)
382
                {
231 holgerb 383
//                      case 'v': // version
192 killagreg 384
                        case 'y': // serial poti values
385
                        case 'b': // extern control
386
                                Buffer_Copy(&UART1_rx_buffer, &UART2_tx_buffer); //forward to FC
387
                                Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
194 killagreg 388
                                return; //end process rx data
192 killagreg 389
                        break;
390
                }
195 killagreg 391
        }
190 killagreg 392
 
393
        MKProtocol_DecodeSerialFrameData(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
394
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
395
        switch(SerialMsg.Address) // check for Slave Address
396
        {
41 ingob 397
                case NC_ADDRESS:  // own Slave Address
190 killagreg 398
                switch(SerialMsg.CmdID)
41 ingob 399
                {
101 holgerb 400
                        case 'z': // connection checker
110 killagreg 401
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
402
                                UART1_Request_Echo = TRUE;
92 killagreg 403
                                break;
404
 
41 ingob 405
                        case 'e': // request for the text of the error status
110 killagreg 406
                                UART1_Request_ErrorMessage = TRUE;
41 ingob 407
                                break;
408
 
409
                        case 's'://  new target position
280 killagreg 410
                                pPoint = (Point_t*)SerialMsg.pData;
411
                                if(pPoint->Position.Status == NEWDATA)
41 ingob 412
                                {
301 holgerb 413
                                        //if(!(FC.StatusFlags & FC_STATUS_FLY)) PointList_Clear(); // flush the list    
300 holgerb 414
                                        //pPoint->Index = 1; // must be one after empty list
296 killagreg 415
                                        PointList_SetAt(pPoint);
416
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
295 killagreg 417
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
302 holgerb 418
                                        if(GPS_pWaypoint != NULL) // if new WP exist
419
                                        {   // update WP hold time stamp immediately!
420
/*                                              if(GPS_pWaypoint->Heading > 0 && GPS_pWaypoint->Heading <= 360)
421
                                                {
422
                                                 CAM_Orientation.Azimuth = GPS_pWaypoint->Heading;
423
                                                 CAM_Orientation.UpdateMask |= CAM_UPDATE_AZIMUTH;
424
                                                }
425
*/
426
                                        }
295 killagreg 427
                                        BeepTime = 50;
41 ingob 428
                                }
429
                                break;
430
 
431
                        case 'u': // redirect debug uart
110 killagreg 432
                                switch(SerialMsg.pData[0])
41 ingob 433
                                {
434
                                        case UART_FLIGHTCTRL:
435
                                                UART2_Init();                           // initialize UART2 to FC pins
153 killagreg 436
                                                fifo_purge(&UART1_rx_fifo);
180 killagreg 437
                                                TIMER2_Deinit();                        // reduce irq load
41 ingob 438
                                                DebugUART = UART2;
439
                                                break;
440
                                        case UART_MK3MAG:
255 killagreg 441
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
41 ingob 442
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
443
                                                GPSData.Status = INVALID;
153 killagreg 444
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 445
                                                DebugUART = UART0;
446
                                                break;
447
                                        case UART_MKGPS:
255 killagreg 448
                                                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) break; // not if the motors are running
179 killagreg 449
                                                TIMER2_Deinit();                        // disable servo outputs to reduce irq load
245 killagreg 450
                                                UART0_Connect_to_MKGPS(UART0_BAUD_RATE);        // connect UART0 to MKGPS pins
41 ingob 451
                                                GPSData.Status = INVALID;
153 killagreg 452
                                                fifo_purge(&UART1_rx_fifo);
41 ingob 453
                                                DebugUART = UART0;
454
                                                break;
153 killagreg 455
                                        default:
456
                                                break;
41 ingob 457
                                }
458
                                break;
459
 
295 killagreg 460
                        case 'w'://  Set point in list at index
165 killagreg 461
                                {
280 killagreg 462
                                        pPoint = (Point_t*)SerialMsg.pData;
224 killagreg 463
 
280 killagreg 464
                                        if((pPoint->Position.Status == INVALID) && (pPoint->Index == 0))
224 killagreg 465
                                        {
280 killagreg 466
                                                PointList_Clear();
467
                                                GPS_pWaypoint = PointList_WPBegin();
295 killagreg 468
                                                UART1_Request_WritePoint = 0; // return new point count 
165 killagreg 469
                                        }
227 killagreg 470
                                        else
295 killagreg 471
                                        {  // update WP in list at index
472
                                                UART1_Request_WritePoint = PointList_SetAt(pPoint);
296 killagreg 473
                                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
295 killagreg 474
                                                if(UART1_Request_WritePoint == pPoint->Index)
225 killagreg 475
                                                {
476
                                                        BeepTime = 500;
165 killagreg 477
                                                }
478
                                        }
41 ingob 479
                                }
480
                                break;
481
 
92 killagreg 482
                        case 'x'://  Read Waypoint from List
280 killagreg 483
                                UART1_Request_ReadPoint = SerialMsg.pData[0];
92 killagreg 484
                                break;
485
 
160 holgerb 486
                        case 'j':// Set/Get NC-Parameter
156 killagreg 487
                                switch(SerialMsg.pData[0])
488
                                {
489
                                        case 0: // get
490
                                        break;
165 killagreg 491
 
156 killagreg 492
                                        case 1: // set
159 killagreg 493
                                        {
494
                                                s16 value;
495
                                                value = SerialMsg.pData[2] + (s16)SerialMsg.pData[3] * 0x0100;
496
                                                NCParams_SetValue(SerialMsg.pData[1], &value);
497
                                        }
156 killagreg 498
                                        break;
499
 
500
                                        default:
165 killagreg 501
                                        break;
156 killagreg 502
                                }
503
                                UART1_Request_ParameterId = SerialMsg.pData[1];
504
                                UART1_Request_Parameter = TRUE;
505
                                break;
41 ingob 506
                        default:
507
                                // unsupported command recieved
508
                                break;
509
                } // case NC_ADDRESS
65 killagreg 510
                // "break;" is missing here to fall thru to the common commands
41 ingob 511
 
512
                default:  // and any other Slave Address
513
 
190 killagreg 514
                switch(SerialMsg.CmdID) // check CmdID
41 ingob 515
                {
516
                        case 'a':// request for the labels of the analog debug outputs
110 killagreg 517
                                UART1_Request_DebugLabel = SerialMsg.pData[0];
518
                                if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31;
41 ingob 519
                                break;
193 killagreg 520
                        /*
41 ingob 521
                        case 'b': // submit extern control
121 killagreg 522
                                memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl));
112 killagreg 523
                                UART1_ConfirmFrame = ExternControl.Frame;
41 ingob 524
                                break;
195 killagreg 525
                        */
41 ingob 526
                        case 'd': // request for debug data;
110 killagreg 527
                                UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
528
                                if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE;
198 killagreg 529
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
41 ingob 530
                                break;
531
 
63 killagreg 532
                        case 'c': // request for 3D data;
110 killagreg 533
                                UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
534
                                if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE;
198 killagreg 535
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
63 killagreg 536
                                break;
275 killagreg 537
 
41 ingob 538
                        case 'h':// reqest for display line
193 killagreg 539
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
540
                                {
541
                                        UART1_DisplayLine = 2;
195 killagreg 542
                                        UART1_Display_Interval = 0;
193 killagreg 543
                                }
544
                                else
545
                                {
201 killagreg 546
                                        UART1_DisplayKeys |= ~SerialMsg.pData[0];
193 killagreg 547
                                        UART1_Display_Interval = (u32) SerialMsg.pData[1] * 10;
548
                                        UART1_DisplayLine = 4;
198 killagreg 549
                                        UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
193 killagreg 550
                                }
110 killagreg 551
                                UART1_Request_Display = TRUE;
41 ingob 552
                                break;
553
 
554
                        case 'l':// reqest for display columns
110 killagreg 555
                                MenuItem = SerialMsg.pData[0];
556
                                UART1_Request_Display1 = TRUE;
41 ingob 557
                                break;
64 holgerb 558
 
559
                        case 'o': // request for navigation information
110 killagreg 560
                                UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
561
                                if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE;
198 killagreg 562
                                UART1_AboTimeOut = SetDelay(ABO_TIMEOUT);
64 holgerb 563
                                break;
564
 
41 ingob 565
                        case 'v': // request for version info
110 killagreg 566
                                UART1_Request_VersionInfo = TRUE;
41 ingob 567
                                break;
568
                        default:
569
                                // unsupported command recieved
570
                                break;
571
                }
572
                break; // default:
573
        }
153 killagreg 574
        Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
41 ingob 575
}
576
 
577
 
578
/*****************************************************/
579
/*                   Send a character                */
580
/*****************************************************/
110 killagreg 581
s16 UART1_Putchar(char c)
41 ingob 582
{
110 killagreg 583
        if (c == '\n') UART1_Putchar('\r');
41 ingob 584
        // wait until txd fifo is not full
585
        while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);
586
        // transmit byte
587
        UART_SendData(UART1, c);
314 killagreg 588
        #ifdef FOLLOW_ME
589
        if(TransmitAlsoToFC) UART_SendData(UART2, c);
590
        #endif
41 ingob 591
        return (0);
592
}
593
 
594
/*****************************************************/
595
/*       Send a string to the debug uart              */
596
/*****************************************************/
110 killagreg 597
void UART1_PutString(u8 *s)
41 ingob 598
{
599
        if(s == NULL) return;
600
        while (*s != '\0' && DebugUART == UART1)
601
        {
110 killagreg 602
                UART1_Putchar(*s);
41 ingob 603
                s ++;
604
        }
605
}
606
 
113 killagreg 607
 
110 killagreg 608
/**************************************************************/
609
/*         Transmit tx buffer via debug uart                  */
610
/**************************************************************/
611
void UART1_Transmit(void)
612
{
613
        u8 tmp_tx;
614
        if(DebugUART != UART1) return;
615
        // if something has to be send and the txd fifo is not full
113 killagreg 616
        if(UART1_tx_buffer.Locked == TRUE)
110 killagreg 617
        {
113 killagreg 618
                // while there is some space in the tx fifo
619
                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET)
110 killagreg 620
                {
113 killagreg 621
                        tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer
622
                        UART_SendData(UART1, tmp_tx); // put character to txd fifo
314 killagreg 623
                        #ifdef FOLLOW_ME
624
                        if(TransmitAlsoToFC)
625
                        {
626
                                UART_SendData(UART2, tmp_tx); // put character to txd fifo
627
                        }
628
                        #endif
113 killagreg 629
                        // if terminating character or end of txd buffer reached
630
                        if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes))
631
                        {
632
                                Buffer_Clear(&UART1_tx_buffer); // clear txd buffer
314 killagreg 633
                                #ifdef FOLLOW_ME
634
                                TransmitAlsoToFC = 0;
635
                                #endif
113 killagreg 636
                                break; // end while loop
637
                        }
110 killagreg 638
                }
639
        }
640
}
41 ingob 641
 
642
/**************************************************************/
643
/* Send the answers to incomming commands at the debug uart   */
644
/**************************************************************/
645
void UART1_TransmitTxData(void)
646
{
110 killagreg 647
        if(DebugUART != UART1) return;
198 killagreg 648
 
649
        if(CheckDelay(UART1_AboTimeOut))
650
        {
651
                UART1_DebugData_Interval = 0;
652
                UART1_NaviData_Interval = 0;
653
                UART1_Data3D_Interval = 0;
654
                UART1_Display_Interval = 0;
655
        }
656
 
110 killagreg 657
        UART1_Transmit(); // output pending bytes in tx buffer
658
        if((UART1_tx_buffer.Locked == TRUE)) return;
41 ingob 659
 
156 killagreg 660
        if(UART1_Request_Parameter && (UART1_tx_buffer.Locked == FALSE))
92 killagreg 661
        {
298 holgerb 662
                s16 ParamValue;  
299 killagreg 663
                NCParams_GetValue(UART1_Request_ParameterId, &ParamValue);
160 holgerb 664
                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 665
                UART1_Request_Parameter = FALSE;
666
        }
667
        else if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE))
668
        {
110 killagreg 669
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request
92 killagreg 670
                Echo = 0; // reset echo value
110 killagreg 671
                UART1_Request_Echo = FALSE;
92 killagreg 672
        }
295 killagreg 673
        else if((UART1_Request_WritePoint!= 0xFF) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 674
        {
295 killagreg 675
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &UART1_Request_WritePoint, sizeof(UART1_Request_WritePoint));
676
                UART1_Request_WritePoint = 0xFF;
154 killagreg 677
        }
280 killagreg 678
        else if((UART1_Request_ReadPoint) && (UART1_tx_buffer.Locked == FALSE))
154 killagreg 679
        {
280 killagreg 680
                u8 PointCount = PointList_GetCount();
681
                if (UART1_Request_ReadPoint <= PointCount)
154 killagreg 682
                {
280 killagreg 683
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &PointCount, 1, &UART1_Request_ReadPoint, 1, PointList_GetAt(UART1_Request_ReadPoint), sizeof(Point_t));
154 killagreg 684
                }
685
                else
686
                {
280 killagreg 687
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &PointCount, sizeof(PointCount));
154 killagreg 688
                }
280 killagreg 689
                UART1_Request_ReadPoint = 0;
154 killagreg 690
        }
691
        else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
692
        {
693
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
694
                UART1_Request_DebugLabel = 0xFF;
695
        }
151 killagreg 696
        else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
41 ingob 697
        {
151 killagreg 698
                NaviData.Errorcode = ErrorCode;
699
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
700
                UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval);
701
                UART1_Request_NaviData = FALSE;
702
        }
703
        else if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE))
704
        {
110 killagreg 705
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut));
706
                UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
707
                UART1_Request_DebugData = FALSE;
41 ingob 708
        }
151 killagreg 709
        else if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE))
61 holgerb 710
        {
110 killagreg 711
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D));
712
                UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
713
                UART1_Request_Data3D = FALSE;
61 holgerb 714
        }
193 killagreg 715
        /*
153 killagreg 716
        else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
717
        {
718
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
719
                UART1_ConfirmFrame = 0;
720
        }
193 killagreg 721
        */
722
        /*
151 killagreg 723
        else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
41 ingob 724
        {
110 killagreg 725
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
726
                UART1_Request_ExternalControl = FALSE;
41 ingob 727
        }
193 killagreg 728
        */
729
        else if( (( (UART1_Display_Interval > 0) && CheckDelay(UART1_Display_Timer)) || UART1_Request_Display) && (UART1_tx_buffer.Locked == FALSE))
195 killagreg 730
        {
193 killagreg 731
                if(UART1_DisplayLine > 3)
732
                {
201 killagreg 733
                        Menu_Update(UART1_DisplayKeys);
734
                        UART1_DisplayKeys = 0;
193 killagreg 735
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 1, (u8*)DisplayBuff, sizeof(DisplayBuff));
736
                }
737
                else
738
                {
739
                        UART1_DisplayLine = 2;
740
                        sprintf(text,"!!! incompatible !!!");
741
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&text, 20);
742
                        if(UART1_DisplayLine++ > 3) UART1_DisplayLine = 0;
743
                }
744
                UART1_Display_Timer = SetDelay(UART1_Display_Interval);
110 killagreg 745
                UART1_Request_Display = FALSE;
41 ingob 746
        }
151 killagreg 747
        else if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE))
41 ingob 748
        {
201 killagreg 749
                Menu_Update(0);
110 killagreg 750
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff));
751
                UART1_Request_Display1 = FALSE;
41 ingob 752
        }
151 killagreg 753
        else if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE))
41 ingob 754
        {
110 killagreg 755
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
756
                UART1_Request_VersionInfo = FALSE;
41 ingob 757
        }
151 killagreg 758
        else if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE))
41 ingob 759
        {
110 killagreg 760
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
761
                UART1_Request_ErrorMessage = FALSE;
41 ingob 762
        }
314 killagreg 763
#ifdef FOLLOW_ME
764
        else if(CheckDelay(UART1_FollowMe_Timer) && (UART1_tx_buffer.Locked == FALSE))
765
        {
766
                if((GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.NumOfSats >= 4))
767
                {      
768
                        TransmitAlsoToFC = 1;
769
                        // update FollowMe content
770
                        FollowMe.Position.Longitude     = GPSData.Position.Longitude;
771
                        FollowMe.Position.Latitude      = GPSData.Position.Latitude;
772
                        FollowMe.Position.Status = NEWDATA;
773
                        FollowMe.Position.Altitude = 1;
774
                        // 0  -> no Orientation
775
                        // 1-360 -> CompassCourse Setpoint
776
                        // -1 -> points to  WP1 -> itself
777
                FollowMe.Heading = -1;
778
                        FollowMe.ToleranceRadius = 1;
779
                        FollowMe.HoldTime = 60;
780
                        FollowMe.Event_Flag = 1;
781
                        FollowMe.Index = 1; // 0 = Delete List, 1 place at first entry in the list
782
                        FollowMe.Type = POINT_TYPE_WP;
783
                        FollowMe.WP_EventChannelValue = 100;  // set servo value
784
                        FollowMe.AltitudeRate = 0;                        // do not change height
785
                        FollowMe.reserve[0] = 0;                // reserve
786
                        FollowMe.reserve[1] = 0;                // reserve
787
                        FollowMe.reserve[2] = 0;                // reserve
788
                        FollowMe.reserve[3] = 0;                // reserve
789
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe));
790
                }
791
                UART1_FollowMe_Timer = SetDelay(FOLLOW_ME_INTERVAL); // set new update time
792
        }
793
#endif
250 ingob 794
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
314 killagreg 795
        else if(SendDebugOutput && (UART1_tx_buffer.Locked == FALSE))
254 killagreg 796
        {
797
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'0', NC_ADDRESS, 1, (u8 *) &tDebug, sizeof(tDebug));
798
                SendDebugOutput = 0;
799
        }
250 ingob 800
#endif  
110 killagreg 801
        UART1_Transmit(); // output pending bytes in tx buffer
41 ingob 802
}
803