Subversion Repositories NaviCtrl

Rev

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

Rev 247 Rev 360
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
-
 
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
-
 
7
// + FOR NON COMMERCIAL USE ONLY
-
 
8
// + www.MikroKopter.com
5
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
7
// + Software Nutzungsbedingungen (english version: see below)
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
15
// + Verkauf von Luftbildaufnahmen, usw.
-
 
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
22
// + eindeutig als Ursprung verlinkt werden
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
25
// + Benutzung auf eigene Gefahr
22
// + des Mitverschuldens offen.
-
 
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
-
 
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
-
 
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + mit unserer Zustimmung zulässig
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
-
 
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// + Software LICENSING TERMS
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + this list of conditions and the following disclaimer.
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// +     from this software without specific prior written permission.
37
// + The Software may only be used with the Licensor's products.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// +     for non-commercial use (directly or indirectly)
-
 
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// +     with our written permission
40
// + agreement shall be the property of the Licensor.
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// +     clearly linked as origin
-
 
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
44
//
-
 
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
// + The customer shall be responsible for taking reasonable precautions
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
55
// +  POSSIBILITY OF SUCH DAMAGE.
53
// + #### END OF LICENSING TERMS ####
-
 
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
56
#include <stdio.h>
58
#include <stdarg.h>
57
#include <stdarg.h>
59
#include <string.h>
58
#include <string.h>
60
#include "91x_lib.h"
59
#include "91x_lib.h"
61
#include "config.h"
60
#include "config.h"
62
#include "main.h"
61
#include "main.h"
63
#include "uart0.h"
62
#include "uart0.h"
64
#include "uart1.h"
63
#include "uart1.h"
65
#include "timer1.h"
64
#include "timer1.h"
66
#include "ubx.h"
65
#include "ubx.h"
67
#include "mkprotocol.h"
66
#include "mkprotocol.h"
68
 
67
 
69
 
68
 
70
//------------------------------------------------------------------------------------
69
//------------------------------------------------------------------------------------
71
// global variables
70
// global variables
72
 
71
 
73
MKOSD_VersionInfo_t MKOSD_VersionInfo;
72
MKOSD_VersionInfo_t MKOSD_VersionInfo;
74
 
73
 
75
// UART0 MUXER
74
// UART0 MUXER
76
UART0_MuxerState_t UART0_Muxer = UART0_UNDEF;
75
UART0_MuxerState_t UART0_Muxer = UART0_UNDEF;
77
u16 Uart0Baudrate = UART0_BAUD_RATE;
76
u16 Uart0Baudrate = UART0_BAUD_RATE;
78
u16 Uart0MK3MagBaudrate = UART0_BAUD_RATE;
77
u16 Uart0MK3MagBaudrate = UART0_BAUD_RATE;
79
 
78
 
80
// the tx buffer
79
// the tx buffer
81
#define UART0_TX_BUFFER_LEN  150
80
#define UART0_TX_BUFFER_LEN  150
82
u8 UART0_tbuffer[UART0_TX_BUFFER_LEN];
81
u8 UART0_tbuffer[UART0_TX_BUFFER_LEN];
83
Buffer_t UART0_tx_buffer;
82
Buffer_t UART0_tx_buffer;
84
 
83
 
85
// the rx buffer
84
// the rx buffer
86
#define UART0_RX_BUFFER_LEN  150
85
#define UART0_RX_BUFFER_LEN  150
87
u8 UART0_rbuffer[UART0_RX_BUFFER_LEN];
86
u8 UART0_rbuffer[UART0_RX_BUFFER_LEN];
88
Buffer_t UART0_rx_buffer;
87
Buffer_t UART0_rx_buffer;
89
 
88
 
90
u8 UART0_Request_VersionInfo    = FALSE;
89
u8 UART0_Request_VersionInfo    = FALSE;
91
u8 UART0_Request_NaviData               = FALSE;
90
u8 UART0_Request_NaviData               = FALSE;
92
u8 UART0_Request_ErrorMessage   = FALSE;
91
u8 UART0_Request_ErrorMessage   = FALSE;
93
u32 UART0_NaviData_Timer;
92
u32 UART0_NaviData_Timer;
94
u32 UART0_NaviData_Interval = 0;        // in ms
93
u32 UART0_NaviData_Interval = 0;        // in ms
95
 
94
 
96
//------------------------------------------------------------------------------------
95
//------------------------------------------------------------------------------------
97
// functions
96
// functions
98
 
97
 
99
/********************************************************/
98
/********************************************************/
100
/*              Configure uart 0                        */
99
/*              Configure uart 0                        */
101
/********************************************************/
100
/********************************************************/
102
void UART0_Configure(u16 Baudrate)
101
void UART0_Configure(u16 Baudrate)
103
{
102
{
104
        UART_InitTypeDef UART_InitStructure;
103
        UART_InitTypeDef UART_InitStructure;
105
 
104
 
106
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock
105
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock
107
 
106
 
108
        /* UART0 configured as follow:
107
        /* UART0 configured as follow:
109
          - Word Length = 8 Bits
108
          - Word Length = 8 Bits
110
          - One Stop Bit
109
          - One Stop Bit
111
          - No parity
110
          - No parity
112
          - BaudRate taken from function argument
111
          - BaudRate taken from function argument
113
          - Hardware flow control Disabled
112
          - Hardware flow control Disabled
114
          - Receive and transmit enabled
113
          - Receive and transmit enabled
115
          - Receive and transmit FIFOs are Disabled
114
          - Receive and transmit FIFOs are Disabled
116
    */
115
    */
117
        UART_StructInit(&UART_InitStructure);
116
        UART_StructInit(&UART_InitStructure);
118
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
117
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
119
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
118
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
120
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
119
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
121
    UART_InitStructure.UART_BaudRate =                          Baudrate;
120
    UART_InitStructure.UART_BaudRate =                          Baudrate;
122
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
121
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
123
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
122
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
124
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
123
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
125
    UART_InitStructure.UART_TxFIFOLevel =                       UART_FIFOLevel_1_2;
124
    UART_InitStructure.UART_TxFIFOLevel =                       UART_FIFOLevel_1_2;
126
    UART_InitStructure.UART_RxFIFOLevel =                       UART_FIFOLevel_1_2;
125
    UART_InitStructure.UART_RxFIFOLevel =                       UART_FIFOLevel_1_2;
127
 
126
 
128
        UART_DeInit(UART0);     // reset uart 0 to default
127
        UART_DeInit(UART0);     // reset uart 0 to default
129
    UART_Init(UART0, &UART_InitStructure);  // initialize uart 0
128
    UART_Init(UART0, &UART_InitStructure);  // initialize uart 0
130
 
129
 
131
        // enable uart 0 interrupts selective
130
        // enable uart 0 interrupts selective
132
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut  /*| UART_IT_FrameError*/, ENABLE);
131
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut  /*| UART_IT_FrameError*/, ENABLE);
133
        UART_Cmd(UART0, ENABLE); // enable uart 0
132
        UART_Cmd(UART0, ENABLE); // enable uart 0
134
        // configure the uart 0 interupt line
133
        // configure the uart 0 interupt line
135
        VIC_Config(UART0_ITLine, VIC_IRQ, PRIORITY_UART0);
134
        VIC_Config(UART0_ITLine, VIC_IRQ, PRIORITY_UART0);
136
        // enable the uart 0 IRQ
135
        // enable the uart 0 IRQ
137
        VIC_ITCmd(UART0_ITLine, ENABLE);
136
        VIC_ITCmd(UART0_ITLine, ENABLE);
138
}
137
}
139
 
138
 
140
/********************************************************/
139
/********************************************************/
141
/*              Connect RXD & TXD to GPS                */
140
/*              Connect RXD & TXD to GPS                */
142
/********************************************************/
141
/********************************************************/
143
void UART0_Connect_to_MKGPS(u16 Baudrate)
142
void UART0_Connect_to_MKGPS(u16 Baudrate)
144
{
143
{
145
        GPIO_InitTypeDef GPIO_InitStructure;
144
        GPIO_InitTypeDef GPIO_InitStructure;
146
 
145
 
147
        UART0_Muxer = UART0_UNDEF;
146
        UART0_Muxer = UART0_UNDEF;
148
 
147
 
149
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
148
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
150
        // unmap UART0 from Compass
149
        // unmap UART0 from Compass
151
        // set port pin 5.1 (serial data from compass) to input and disconnect from IP
150
        // set port pin 5.1 (serial data from compass) to input and disconnect from IP
152
        GPIO_StructInit(&GPIO_InitStructure);
151
        GPIO_StructInit(&GPIO_InitStructure);
153
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
152
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
154
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
153
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
155
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
154
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
156
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
155
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
157
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
156
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
158
    GPIO_Init(GPIO5, &GPIO_InitStructure);
157
    GPIO_Init(GPIO5, &GPIO_InitStructure);
159
        // set port pin 5.0 (serial data to compass) to input
158
        // set port pin 5.0 (serial data to compass) to input
160
        GPIO_StructInit(&GPIO_InitStructure);
159
        GPIO_StructInit(&GPIO_InitStructure);
161
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
160
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
162
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
161
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
163
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
162
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
164
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
163
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
165
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
164
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
166
    GPIO_Init(GPIO5, &GPIO_InitStructure);
165
    GPIO_Init(GPIO5, &GPIO_InitStructure);
167
        // map UART0 to GPS
166
        // map UART0 to GPS
168
        // set port pin 6.6 (serial data from gps) to input and connect to IP
167
        // set port pin 6.6 (serial data from gps) to input and connect to IP
169
        GPIO_StructInit(&GPIO_InitStructure);
168
        GPIO_StructInit(&GPIO_InitStructure);
170
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
169
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
171
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
170
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
172
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
171
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
173
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
172
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
174
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
173
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
175
    GPIO_Init(GPIO6, &GPIO_InitStructure);
174
    GPIO_Init(GPIO6, &GPIO_InitStructure);
176
        // set port pin 6.7 (serial data to gps) to output
175
        // set port pin 6.7 (serial data to gps) to output
177
        GPIO_StructInit(&GPIO_InitStructure);
176
        GPIO_StructInit(&GPIO_InitStructure);
178
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
177
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
179
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
178
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
180
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
179
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
181
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
180
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
182
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
181
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
183
    GPIO_Init(GPIO6, &GPIO_InitStructure);
182
    GPIO_Init(GPIO6, &GPIO_InitStructure);
184
        // configure the UART0
183
        // configure the UART0
185
        UART0_Configure(Baudrate);
184
        UART0_Configure(Baudrate);
186
       
185
       
187
        UART0_Muxer = UART0_MKGPS;
186
        UART0_Muxer = UART0_MKGPS;
188
}
187
}
189
 
188
 
190
/********************************************************/
189
/********************************************************/
191
/*              Connect RXD & TXD to MK3MAG             */
190
/*              Connect RXD & TXD to MK3MAG             */
192
/********************************************************/
191
/********************************************************/
193
void UART0_Connect_to_MK3MAG(void)
192
void UART0_Connect_to_MK3MAG(void)
194
{
193
{
195
        u16 Baudrate;
194
        u16 Baudrate;
196
 
195
 
197
        GPIO_InitTypeDef GPIO_InitStructure;
196
        GPIO_InitTypeDef GPIO_InitStructure;
198
 
197
 
199
        UART0_Muxer = UART0_UNDEF;
198
        UART0_Muxer = UART0_UNDEF;
200
 
199
 
201
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
200
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
202
        // unmap UART0 from GPS
201
        // unmap UART0 from GPS
203
        // set port pin 6.6 (serial data from gps) to input and disconnect from IP
202
        // set port pin 6.6 (serial data from gps) to input and disconnect from IP
204
        GPIO_StructInit(&GPIO_InitStructure);
203
        GPIO_StructInit(&GPIO_InitStructure);
205
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
204
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
206
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
205
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
207
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
206
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
208
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
207
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
209
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
208
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
210
    GPIO_Init(GPIO6, &GPIO_InitStructure);
209
    GPIO_Init(GPIO6, &GPIO_InitStructure);
211
        // set port pin 6.7 (serial data to gps) to input
210
        // set port pin 6.7 (serial data to gps) to input
212
        GPIO_StructInit(&GPIO_InitStructure);
211
        GPIO_StructInit(&GPIO_InitStructure);
213
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
212
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
214
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
213
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
215
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
214
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
216
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
215
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
217
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
216
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
218
    GPIO_Init(GPIO6, &GPIO_InitStructure);
217
    GPIO_Init(GPIO6, &GPIO_InitStructure);
219
 
218
 
220
        // map UART0 to Compass
219
        // map UART0 to Compass
221
        // set port pin 5.1 (serial data from compass) to input and connect to IP
220
        // set port pin 5.1 (serial data from compass) to input and connect to IP
222
        GPIO_StructInit(&GPIO_InitStructure);
221
        GPIO_StructInit(&GPIO_InitStructure);
223
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
222
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
224
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
223
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
225
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
224
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
226
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
225
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
227
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
226
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
228
    GPIO_Init(GPIO5, &GPIO_InitStructure);
227
    GPIO_Init(GPIO5, &GPIO_InitStructure);
229
        // set port pin 5.0 (serial data to compass) to output
228
        // set port pin 5.0 (serial data to compass) to output
230
        GPIO_StructInit(&GPIO_InitStructure);
229
        GPIO_StructInit(&GPIO_InitStructure);
231
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
230
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
232
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
231
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
233
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
232
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
234
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
233
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
235
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
234
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
236
    GPIO_Init(GPIO5, &GPIO_InitStructure);
235
    GPIO_Init(GPIO5, &GPIO_InitStructure);
237
        Baudrate = UART0_BAUD_RATE + ((UART0_BAUD_RATE * 2)/100);   // MK3Mag baudrate is a little bit higher...
236
        Baudrate = UART0_BAUD_RATE + ((UART0_BAUD_RATE * 2)/100);   // MK3Mag baudrate is a little bit higher...
238
        UART0_Configure(Baudrate);
237
        UART0_Configure(Baudrate);
239
        UART0_Muxer = UART0_MK3MAG;
238
        UART0_Muxer = UART0_MK3MAG;
240
}
239
}
241
 
240
 
242
/********************************************************/
241
/********************************************************/
243
/*                  Initialize UART0                    */
242
/*                  Initialize UART0                    */
244
/********************************************************/
243
/********************************************************/
245
void UART0_Init(void)
244
void UART0_Init(void)
246
{
245
{
247
        UART1_PutString("\r\n UART0 init...");
246
        UART1_PutString("\r\n UART0 init...");
248
 
247
 
249
        UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
248
        UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
250
 
249
 
251
        // initialize txd buffer
250
        // initialize txd buffer
252
        Buffer_Init(&UART0_tx_buffer, UART0_tbuffer, UART0_TX_BUFFER_LEN);
251
        Buffer_Init(&UART0_tx_buffer, UART0_tbuffer, UART0_TX_BUFFER_LEN);
253
       
252
       
254
        // initialize rxd buffer
253
        // initialize rxd buffer
255
        Buffer_Init(&UART0_rx_buffer, UART0_rbuffer, UART0_RX_BUFFER_LEN);
254
        Buffer_Init(&UART0_rx_buffer, UART0_rbuffer, UART0_RX_BUFFER_LEN);
256
 
255
 
257
        UART1_PutString("ok");
256
        UART1_PutString("ok");
258
}
257
}
259
 
258
 
260
/********************************************************/
259
/********************************************************/
261
/*            UART0 Interrupt Handler                   */
260
/*            UART0 Interrupt Handler                   */
262
/********************************************************/
261
/********************************************************/
263
void UART0_IRQHandler(void)
262
void UART0_IRQHandler(void)
264
{
263
{
265
        u8 c;
264
        u8 c;
266
        // if receive irq (FIFO is over trigger level) or receive timeout irq (FIFO is not empty for longer times) has occured
265
        // if receive irq (FIFO is over trigger level) or receive timeout irq (FIFO is not empty for longer times) has occured
267
        if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
266
        if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
268
        {
267
        {
269
                UART_ClearITPendingBit(UART0, UART_IT_Receive);                 // clear receive interrupt flag
268
                UART_ClearITPendingBit(UART0, UART_IT_Receive);                 // clear receive interrupt flag
270
                UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag
269
                UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag
271
 
270
 
272
                // if debug UART is UART0
271
                // if debug UART is UART0
273
                if (DebugUART == UART0)
272
                if (DebugUART == UART0)
274
                {       // forward received data to the UART1 tx buffer
273
                {       // forward received data to the UART1 tx buffer
275
                        while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
274
                        while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
276
                        {
275
                        {
277
                                // wait for space in the tx buffer of the UART1
276
                                // wait for space in the tx buffer of the UART1
278
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
277
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
279
                                // move the byte from the rx buffer of UART0 to the tx buffer of UART1
278
                                // move the byte from the rx buffer of UART0 to the tx buffer of UART1
280
                                UART_SendData(UART1, UART_ReceiveData(UART0));
279
                                UART_SendData(UART1, UART_ReceiveData(UART0));
281
                        }
280
                        }
282
                }
281
                }
283
                else // UART0 is not the DebugUART (normal operation)
282
                else // UART0 is not the DebugUART (normal operation)
284
                {
283
                {
285
                        // repeat until no byte is in the RxFIFO
284
                        // repeat until no byte is in the RxFIFO
286
                        while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
285
                        while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
287
                        {  
286
                        {  
288
                                c = UART_ReceiveData(UART0); // get byte from rx fifo
287
                                c = UART_ReceiveData(UART0); // get byte from rx fifo
289
                                switch(UART0_Muxer)
288
                                switch(UART0_Muxer)
290
                                {
289
                                {
291
                                        case UART0_MKGPS:
290
                                        case UART0_MKGPS:
292
                                                UBX_RxParser(c); // if connected to GPS forward byte to ubx parser
291
                                                UBX_RxParser(c); // if connected to GPS forward byte to ubx parser
293
                                                MKProtocol_CollectSerialFrame(&UART0_rx_buffer, c);     // ckeck for MK-Frames also
292
                                                MKProtocol_CollectSerialFrame(&UART0_rx_buffer, c);     // ckeck for MK-Frames also
294
                                                break;
293
                                                break;
295
                                        case UART0_MK3MAG:
294
                                        case UART0_MK3MAG:
296
                                                // ignore any byte send from MK3MAG
295
                                                // ignore any byte send from MK3MAG
297
                                                break;
296
                                                break;
298
                                        case UART0_UNDEF:
297
                                        case UART0_UNDEF:
299
                                        default:
298
                                        default:
300
                                                // ignore the byte from unknown source
299
                                                // ignore the byte from unknown source
301
                                                break;
300
                                                break;
302
                                } // eof switch(UART0_Muxer)
301
                                } // eof switch(UART0_Muxer)
303
                        } // eof while
302
                        } // eof while
304
                }  // eof UART0 is not the DebugUART
303
                }  // eof UART0 is not the DebugUART
305
        } // eof receive irq or receive timeout irq
304
        } // eof receive irq or receive timeout irq
306
}
305
}
307
 
306
 
308
/**************************************************************/
307
/**************************************************************/
309
/* Process incomming data from debug uart                     */
308
/* Process incomming data from debug uart                     */
310
/**************************************************************/
309
/**************************************************************/
311
void UART0_ProcessRxData(void)
310
void UART0_ProcessRxData(void)
312
{
311
{
313
        SerialMsg_t SerialMsg;
312
        SerialMsg_t SerialMsg;
314
        // if data in the rxd buffer are not locked immediately return
313
        // if data in the rxd buffer are not locked immediately return
315
        if((UART0_rx_buffer.Locked == FALSE) || (DebugUART == UART0) ) return;
314
        if((UART0_rx_buffer.Locked == FALSE) || (DebugUART == UART0) ) return;
316
 
315
 
317
        MKProtocol_DecodeSerialFrameHeader(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer  
316
        MKProtocol_DecodeSerialFrameHeader(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer  
318
        MKProtocol_DecodeSerialFrameData(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
317
        MKProtocol_DecodeSerialFrameData(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
319
 
318
 
320
        switch(SerialMsg.Address) // check for Slave Address
319
        switch(SerialMsg.Address) // check for Slave Address
321
        {
320
        {
322
                case MKOSD_ADDRESS: // answers from the MKOSD
321
                case MKOSD_ADDRESS: // answers from the MKOSD
323
                        switch(SerialMsg.CmdID)
322
                        switch(SerialMsg.CmdID)
324
                        {
323
                        {
325
                                case 'V':
324
                                case 'V':
326
                                        memcpy(&MKOSD_VersionInfo, SerialMsg.pData, sizeof(MKOSD_VersionInfo)); // copy echo pattern
325
                                        memcpy(&MKOSD_VersionInfo, SerialMsg.pData, sizeof(MKOSD_VersionInfo)); // copy echo pattern
327
                                        break;
326
                                        break;
328
                                default:
327
                                default:
329
                                        break;
328
                                        break;
330
                        } // case MKOSD_ADDRESS 
329
                        } // case MKOSD_ADDRESS 
331
                        break;
330
                        break;
332
 
331
 
333
                case NC_ADDRESS:  // own Slave Address
332
                case NC_ADDRESS:  // own Slave Address
334
                        switch(SerialMsg.CmdID)
333
                        switch(SerialMsg.CmdID)
335
                        {
334
                        {
336
                                case 'e': // request for the text of the error status
335
                                case 'e': // request for the text of the error status
337
                                        UART0_Request_ErrorMessage = TRUE;
336
                                        UART0_Request_ErrorMessage = TRUE;
338
                                        break;
337
                                        break;
339
                                case 'o': // request for navigation information
338
                                case 'o': // request for navigation information
340
                                        UART0_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
339
                                        UART0_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
341
                                        if(UART0_NaviData_Interval > 0) UART0_Request_NaviData = TRUE;
340
                                        if(UART0_NaviData_Interval > 0) UART0_Request_NaviData = TRUE;
342
                                        break;
341
                                        break;
343
                                default:
342
                                default:
344
                                        break;
343
                                        break;
345
                        } // case NC_ADDRESS
344
                        } // case NC_ADDRESS
346
                        // "break;" is missing here to fall thru to the common commands
345
                        // "break;" is missing here to fall thru to the common commands
347
 
346
 
348
                default:  // and any other Slave Address
347
                default:  // and any other Slave Address
349
                        switch(SerialMsg.CmdID) // check CmdID
348
                        switch(SerialMsg.CmdID) // check CmdID
350
                        {
349
                        {
351
                                case 'v': // request for version info
350
                                case 'v': // request for version info
352
                                        UART0_Request_VersionInfo = TRUE;
351
                                        UART0_Request_VersionInfo = TRUE;
353
                                        break;
352
                                        break;
354
                                default:
353
                                default:
355
                                        // unsupported command recieved
354
                                        // unsupported command recieved
356
                                        break;
355
                                        break;
357
                        }
356
                        }
358
                        break; // default:
357
                        break; // default:
359
        }
358
        }
360
        Buffer_Clear(&UART0_rx_buffer);
359
        Buffer_Clear(&UART0_rx_buffer);
361
}
360
}
362
 
361
 
363
/**************************************************************/
362
/**************************************************************/
364
/*         Transmit tx buffer via uart0                       */
363
/*         Transmit tx buffer via uart0                       */
365
/**************************************************************/
364
/**************************************************************/
366
void UART0_Transmit(void)
365
void UART0_Transmit(void)
367
{
366
{
368
        u8 tmp_tx;
367
        u8 tmp_tx;
369
 
368
 
370
        IENABLE;
369
        IENABLE;
371
 
370
 
372
        if(DebugUART == UART0) return; // no data output if debug uart is rederected to UART0
371
        if(DebugUART == UART0) return; // no data output if debug uart is rederected to UART0
373
        // if something has to be send and the txd fifo is not full
372
        // if something has to be send and the txd fifo is not full
374
        if((UART0_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET))
373
        if((UART0_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET))
375
        {
374
        {
376
                tmp_tx = UART0_tx_buffer.pData[UART0_tx_buffer.Position++]; // read next byte from txd buffer
375
                tmp_tx = UART0_tx_buffer.pData[UART0_tx_buffer.Position++]; // read next byte from txd buffer
377
                UART_SendData(UART0, tmp_tx); // put character to txd fifo
376
                UART_SendData(UART0, tmp_tx); // put character to txd fifo
378
                // if terminating character or end of txd buffer reached
377
                // if terminating character or end of txd buffer reached
379
                if((tmp_tx == '\r') || (UART0_tx_buffer.Position == UART0_tx_buffer.Size))
378
                if((tmp_tx == '\r') || (UART0_tx_buffer.Position == UART0_tx_buffer.Size))
380
                {
379
                {
381
                        Buffer_Clear(&UART0_tx_buffer);
380
                        Buffer_Clear(&UART0_tx_buffer);
382
                }
381
                }
383
        }
382
        }
384
 
383
 
385
        IDISABLE;
384
        IDISABLE;
386
}
385
}
387
 
386
 
388
 
387
 
389
/**************************************************************/
388
/**************************************************************/
390
/* Send the answers to incomming commands at the uart0        */
389
/* Send the answers to incomming commands at the uart0        */
391
/**************************************************************/
390
/**************************************************************/
392
void UART0_TransmitTxData(void)
391
void UART0_TransmitTxData(void)
393
{
392
{
394
        if(DebugUART == UART0) return;
393
        if(DebugUART == UART0) return;
395
        UART0_Transmit(); // output pending bytes in tx buffer
394
        UART0_Transmit(); // output pending bytes in tx buffer
396
        if(UART0_tx_buffer.Locked == TRUE) return;
395
        if(UART0_tx_buffer.Locked == TRUE) return;
397
 
396
 
398
        else if(UART0_Request_ErrorMessage && (UART0_tx_buffer.Locked == FALSE))
397
        else if(UART0_Request_ErrorMessage && (UART0_tx_buffer.Locked == FALSE))
399
        {
398
        {
400
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
399
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG));
401
                UART0_Request_ErrorMessage = FALSE;
400
                UART0_Request_ErrorMessage = FALSE;
402
        }
401
        }
403
        else if(UART0_Request_VersionInfo && (UART0_tx_buffer.Locked == FALSE))
402
        else if(UART0_Request_VersionInfo && (UART0_tx_buffer.Locked == FALSE))
404
        {
403
        {
405
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
404
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo));
406
                UART0_Request_VersionInfo = FALSE;
405
                UART0_Request_VersionInfo = FALSE;
407
        }
406
        }
408
        else if(( ((UART0_NaviData_Interval >0) && CheckDelay(UART0_NaviData_Timer) ) || UART0_Request_NaviData) && (UART0_tx_buffer.Locked == FALSE))
407
        else if(( ((UART0_NaviData_Interval >0) && CheckDelay(UART0_NaviData_Timer) ) || UART0_Request_NaviData) && (UART0_tx_buffer.Locked == FALSE))
409
        {
408
        {
410
                NaviData.Errorcode = ErrorCode;
409
                NaviData.Errorcode = ErrorCode;
411
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
410
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
412
                UART0_NaviData_Timer = SetDelay(UART0_NaviData_Interval);
411
                UART0_NaviData_Timer = SetDelay(UART0_NaviData_Interval);
413
                UART0_Request_NaviData = FALSE;
412
                UART0_Request_NaviData = FALSE;
414
        }      
413
        }      
415
        UART0_Transmit(); // output pending bytes in tx buffer
414
        UART0_Transmit(); // output pending bytes in tx buffer
416
}
415
}
417
 
416
 
418
 
417
 
419
/**************************************************************/
418
/**************************************************************/
420
/* Get the version of the MKOSD                               */
419
/* Get the version of the MKOSD                               */
421
/**************************************************************/
420
/**************************************************************/
422
u8 UART0_GetMKOSDVersion(void)
421
u8 UART0_GetMKOSDVersion(void)
423
{
422
{
424
        u32 timeout;
423
        u32 timeout;
425
        u8 msg[64];
424
        u8 msg[64];
426
        u8 retval = 0;
425
        u8 retval = 0;
427
 
426
 
428
        MKOSD_VersionInfo.SWMajor = 0xFF;
427
        MKOSD_VersionInfo.SWMajor = 0xFF;
429
        MKOSD_VersionInfo.SWMinor = 0xFF;
428
        MKOSD_VersionInfo.SWMinor = 0xFF;
430
        MKOSD_VersionInfo.SWPatch = 0xFF;
429
        MKOSD_VersionInfo.SWPatch = 0xFF;
431
 
430
 
432
        if(UART0_Muxer != UART0_MKGPS) UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
431
        if(UART0_Muxer != UART0_MKGPS) UART0_Connect_to_MKGPS(UART0_BAUD_RATE);
433
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
432
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
434
 
433
 
435
        MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'v', MKOSD_ADDRESS, 0); // request for version info
434
        MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'v', MKOSD_ADDRESS, 0); // request for version info
436
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
435
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
437
       
436
       
438
        timeout = SetDelay(500);
437
        timeout = SetDelay(500);
439
        do
438
        do
440
        {
439
        {
441
                UART0_ProcessRxData();
440
                UART0_ProcessRxData();
442
                if(MKOSD_VersionInfo.SWMajor != 0xFF) break;
441
                if(MKOSD_VersionInfo.SWMajor != 0xFF) break;
443
        }while(!CheckDelay(timeout));
442
        }while(!CheckDelay(timeout));
444
       
443
       
445
        if(MKOSD_VersionInfo.SWMajor != 0xFF)
444
        if(MKOSD_VersionInfo.SWMajor != 0xFF)
446
        {
445
        {
447
                sprintf(msg, "\n\r MK-OSD V%d.%d%c", MKOSD_VersionInfo.SWMajor, MKOSD_VersionInfo.SWMinor, 'a'+MKOSD_VersionInfo.SWPatch);
446
                sprintf(msg, "\n\r MK-OSD V%d.%d%c", MKOSD_VersionInfo.SWMajor, MKOSD_VersionInfo.SWMinor, 'a'+MKOSD_VersionInfo.SWPatch);
448
                UART1_PutString(msg);
447
                UART1_PutString(msg);
449
                retval = 1;
448
                retval = 1;
450
        }
449
        }
451
        //else UART1_PutString("\n\r No version information from MK-OSD."); 
450
        //else UART1_PutString("\n\r No version information from MK-OSD."); 
452
        return(retval);
451
        return(retval);
453
}
452
}
454
 
453
 
455
/**************************************************************/
454
/**************************************************************/
456
/* Send a  message to the UBLOX device                        */
455
/* Send a  message to the UBLOX device                        */
457
/**************************************************************/
456
/**************************************************************/
458
u8 UART0_UBXSendMsg(u8* pData, u16 Len)
457
u8 UART0_UBXSendMsg(u8* pData, u16 Len)
459
{
458
{
460
        u8 retval = 0;
459
        u8 retval = 0;
461
        // check for connection to GPS
460
        // check for connection to GPS
462
        if(UART0_Muxer != UART0_MKGPS) return(retval);
461
        if(UART0_Muxer != UART0_MKGPS) return(retval);
463
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
462
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
464
        UBX_CreateMsg(&UART0_tx_buffer, pData, Len);  // build ubx message frame
463
        UBX_CreateMsg(&UART0_tx_buffer, pData, Len);  // build ubx message frame
465
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
464
        while(UART0_tx_buffer.Locked == TRUE) UART0_Transmit(); // output pending bytes in tx buffer;
466
        return(1);
465
        return(1);
467
}
466
}
468
 
467
 
469
/**************************************************************/
468
/**************************************************************/
470
/* Send a configuration message to the UBLOX device           */
469
/* Send a configuration message to the UBLOX device           */
471
/**************************************************************/
470
/**************************************************************/
472
u8 UART0_UBXSendCFGMsg(u8* pData, u16 Len)
471
u8 UART0_UBXSendCFGMsg(u8* pData, u16 Len)
473
{
472
{
474
        u32 timeout;
473
        u32 timeout;
475
        u8 retval = 0;
474
        u8 retval = 0;
476
        // if data are not a CFG MSG
475
        // if data are not a CFG MSG
477
        if(pData[0]!= UBX_CLASS_CFG) return(retval);
476
        if(pData[0]!= UBX_CLASS_CFG) return(retval);
478
        // prepare rx msg filter
477
        // prepare rx msg filter
479
        UbxMsg.Hdr.Class = UBX_CLASS_ACK;
478
        UbxMsg.Hdr.Class = UBX_CLASS_ACK;
480
        UbxMsg.Hdr.Id = 0xFF;
479
        UbxMsg.Hdr.Id = 0xFF;
481
        UbxMsg.Hdr.Length = 0;
480
        UbxMsg.Hdr.Length = 0;
482
        UbxMsg.ClassMask = 0xFF;
481
        UbxMsg.ClassMask = 0xFF;
483
        UbxMsg.IdMask = 0x00;
482
        UbxMsg.IdMask = 0x00;
484
        UbxMsg.Status = INVALID;
483
        UbxMsg.Status = INVALID;
485
        UART0_UBXSendMsg(pData, Len);
484
        UART0_UBXSendMsg(pData, Len);
486
        // check for acknowledge msg
485
        // check for acknowledge msg
487
        timeout = SetDelay(100);
486
        timeout = SetDelay(100);
488
        do
487
        do
489
        {
488
        {
490
                if(UbxMsg.Status == NEWDATA) break;
489
                if(UbxMsg.Status == NEWDATA) break;
491
        }while(!CheckDelay(timeout));
490
        }while(!CheckDelay(timeout));
492
        if(UbxMsg.Status == NEWDATA)
491
        if(UbxMsg.Status == NEWDATA)
493
        {       // 2 bytes payload
492
        {       // 2 bytes payload
494
                if((UbxMsg.Data[0] == pData[0]) && (UbxMsg.Data[1] == pData[1]) && (UbxMsg.Hdr.Length == 2)) retval = UbxMsg.Hdr.Id;
493
                if((UbxMsg.Data[0] == pData[0]) && (UbxMsg.Data[1] == pData[1]) && (UbxMsg.Hdr.Length == 2)) retval = UbxMsg.Hdr.Id;
495
        }
494
        }
496
        UbxMsg.Status = INVALID;
495
        UbxMsg.Status = INVALID;
497
        return(retval);
496
        return(retval);
498
}
497
}
499
 
498
 
500
/**************************************************************/
499
/**************************************************************/
501
/* Get Version Info from UBX Module                           */
500
/* Get Version Info from UBX Module                           */
502
/**************************************************************/
501
/**************************************************************/
503
u8 UART0_GetUBXVersion(void)
502
u8 UART0_GetUBXVersion(void)
504
{
503
{
505
        u32 timeout;
504
        u32 timeout;
506
        u8 retval = 0xFF;
505
        u8 retval = 0xFF;
507
        u8 ubxmsg[]={0x0A, 0x04, 0x00, 0x00}; //MON-VER
506
        u8 ubxmsg[]={0x0A, 0x04, 0x00, 0x00}; //MON-VER
508
        // prepare rx msg filter
507
        // prepare rx msg filter
509
        UbxMsg.Hdr.Class = 0x0A;
508
        UbxMsg.Hdr.Class = 0x0A;
510
        UbxMsg.Hdr.Id = 0x04;
509
        UbxMsg.Hdr.Id = 0x04;
511
        UbxMsg.Hdr.Length = 0;
510
        UbxMsg.Hdr.Length = 0;
512
        UbxMsg.ClassMask = 0xFF;
511
        UbxMsg.ClassMask = 0xFF;
513
        UbxMsg.IdMask = 0xFF;
512
        UbxMsg.IdMask = 0xFF;
514
        UbxMsg.Status = INVALID;
513
        UbxMsg.Status = INVALID;
515
        UART0_UBXSendMsg(ubxmsg, sizeof(ubxmsg));
514
        UART0_UBXSendMsg(ubxmsg, sizeof(ubxmsg));
516
        // check for answer
515
        // check for answer
517
        timeout = SetDelay(100);
516
        timeout = SetDelay(100);
518
        do
517
        do
519
        {
518
        {
520
                if(UbxMsg.Status == NEWDATA) break;
519
                if(UbxMsg.Status == NEWDATA) break;
521
        }while(!CheckDelay(timeout));
520
        }while(!CheckDelay(timeout));
522
        if((UbxMsg.Hdr.Length >= 40) && (UbxMsg.Status == NEWDATA))
521
        if((UbxMsg.Hdr.Length >= 40) && (UbxMsg.Status == NEWDATA))
523
        {
522
        {
524
                UbxMsg.Data[4] = 0; //Only the fisrt 4 chsracters 
523
                UbxMsg.Data[4] = 0; //Only the fisrt 4 chsracters 
525
                UbxMsg.Data[39] = 0;
524
                UbxMsg.Data[39] = 0;
526
                UART1_PutString(" V");
525
                UART1_PutString(" V");
527
                UART1_PutString((u8*)&UbxMsg.Data);
526
                UART1_PutString((u8*)&UbxMsg.Data);
528
                UART1_PutString(" HW:");
527
                UART1_PutString(" HW:");
529
                UART1_PutString((u8*)&UbxMsg.Data[30]);
528
                UART1_PutString((u8*)&UbxMsg.Data[30]);
530
                retval = UbxMsg.Data[0]-'0'; // major sw version        
529
                retval = UbxMsg.Data[0]-'0'; // major sw version        
531
        }
530
        }
532
        UbxMsg.Status = INVALID;
531
        UbxMsg.Status = INVALID;
533
        return(retval);
532
        return(retval);
534
}
533
}
535
 
534