Subversion Repositories NaviCtrl

Rev

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

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