Subversion Repositories NaviCtrl

Rev

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

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