Subversion Repositories NaviCtrl

Rev

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

Rev 49 Rev 110
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch
6
// + Nur für den privaten Gebrauch
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
43
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
57
#include <stdio.h>
-
 
58
#include <stdarg.h>
57
#include "91x_lib.h"
59
#include "91x_lib.h"
-
 
60
#include "main.h"
58
#include "uart0.h"
61
#include "uart0.h"
59
#include "uart1.h"
62
#include "uart1.h"
-
 
63
#include "timer.h"
60
#include "ubx.h"
64
#include "ubx.h"
61
#include <stdio.h>
65
#include "mkprotocol.h"
62
#include <stdarg.h>
-
 
-
 
66
 
-
 
67
u8 UART0_Request_VersionInfo    = FALSE;
-
 
68
u8 UART0_Request_NaviData               = FALSE;
-
 
69
u32 UART0_NaviData_Timer;
63
 
70
u32 UART0_NaviData_Interval = 0;        // in ms
64
//------------------------------------------------------------------------------------
71
//------------------------------------------------------------------------------------
65
// global variables
72
// global variables
66
 
-
 
-
 
73
 
67
 
74
#define UART0_BAUD_RATE 57600           //Baud Rate for the serial interfaces
68
// UART0 MUXER
-
 
69
 
75
// UART0 MUXER
70
UART0_MuxerState_t UART0_Muxer = UART0_UNDEF;
76
UART0_MuxerState_t UART0_Muxer = UART0_UNDEF;
71
u16 Uart0Baudrate = BAUD_RATE;
77
u16 Uart0Baudrate = UART0_BAUD_RATE;
-
 
78
u16 Uart0MK3MagBaudrate = UART0_BAUD_RATE;
-
 
79
 
-
 
80
// the tx buffer
-
 
81
#define UART0_TX_BUFFER_LEN  150
-
 
82
u8 UART0_tbuffer[UART0_TX_BUFFER_LEN];
-
 
83
Buffer_t UART0_tx_buffer;
-
 
84
 
-
 
85
// the rx buffer
-
 
86
#define UART0_RX_BUFFER_LEN  150
-
 
87
u8 UART0_rbuffer[UART0_RX_BUFFER_LEN];
72
u16 Uart0MK3MagBaudrate = BAUD_RATE;
-
 
73
 
-
 
74
volatile u8 txd_buffer0[TXD_BUFFER_LEN];
-
 
75
volatile u8 txd_complete0 = TRUE;
88
Buffer_t UART0_rx_buffer;
76
static u16 ptr_txd_buffer0 = 0;
89
 
77
 
90
 
78
//------------------------------------------------------------------------------------
91
//------------------------------------------------------------------------------------
79
// functions
92
// functions
80
 
93
 
81
/********************************************************/
94
/********************************************************/
82
/*              Connect RXD & TXD to GPS                */
95
/*              Connect RXD & TXD to GPS                */
83
/********************************************************/
96
/********************************************************/
84
void UART0_Connect_to_MKGPS(void)
97
void UART0_Connect_to_MKGPS(void)
85
{
98
{
86
        GPIO_InitTypeDef GPIO_InitStructure;
99
        GPIO_InitTypeDef GPIO_InitStructure;
87
 
100
 
88
        UART0_Muxer = UART0_UNDEF;
101
        UART0_Muxer = UART0_UNDEF;
89
 
102
 
90
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
103
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
91
        // unmap UART0 from Compass
104
        // unmap UART0 from Compass
92
        // set port pin 5.1 (serial data from compass) to input and disconnect from IP
105
        // set port pin 5.1 (serial data from compass) to input and disconnect from IP
93
        GPIO_StructInit(&GPIO_InitStructure);
106
        GPIO_StructInit(&GPIO_InitStructure);
94
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
107
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
95
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
108
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
96
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
109
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
97
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
110
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
98
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
111
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
99
    GPIO_Init(GPIO5, &GPIO_InitStructure);
112
    GPIO_Init(GPIO5, &GPIO_InitStructure);
100
        // set port pin 5.0 (serial data to compass) to input
113
        // set port pin 5.0 (serial data to compass) to input
101
        GPIO_StructInit(&GPIO_InitStructure);
114
        GPIO_StructInit(&GPIO_InitStructure);
102
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
115
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
103
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
116
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
104
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
117
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
105
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
118
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
106
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
119
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
107
    GPIO_Init(GPIO5, &GPIO_InitStructure);
120
    GPIO_Init(GPIO5, &GPIO_InitStructure);
108
        // map UART0 to GPS
121
        // map UART0 to GPS
109
        // set port pin 6.6 (serial data from gps) to input and connect to IP
122
        // set port pin 6.6 (serial data from gps) to input and connect to IP
110
        GPIO_StructInit(&GPIO_InitStructure);
123
        GPIO_StructInit(&GPIO_InitStructure);
111
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
124
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
112
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
125
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
113
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
126
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
114
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
127
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
115
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
128
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
116
    GPIO_Init(GPIO6, &GPIO_InitStructure);
129
    GPIO_Init(GPIO6, &GPIO_InitStructure);
117
        // set port pin 6.7 (serial data to gps) to output
130
        // set port pin 6.7 (serial data to gps) to output
118
        GPIO_StructInit(&GPIO_InitStructure);
131
        GPIO_StructInit(&GPIO_InitStructure);
119
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
132
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
120
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
133
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
121
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
134
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
122
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Enable;
135
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Enable;
123
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
136
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
124
    GPIO_Init(GPIO6, &GPIO_InitStructure);
137
    GPIO_Init(GPIO6, &GPIO_InitStructure);
125
       
138
       
126
        UART0_Muxer = UART0_MKGPS;
139
        UART0_Muxer = UART0_MKGPS;
127
}
140
}
128
 
141
 
129
/********************************************************/
142
/********************************************************/
130
/*              Connect RXD & TXD to MK3MAG             */
143
/*              Connect RXD & TXD to MK3MAG             */
131
/********************************************************/
144
/********************************************************/
132
void UART0_Connect_to_MK3MAG(void)
145
void UART0_Connect_to_MK3MAG(void)
133
{
146
{
134
        GPIO_InitTypeDef GPIO_InitStructure;
147
        GPIO_InitTypeDef GPIO_InitStructure;
135
 
148
 
136
        UART0_Muxer = UART0_UNDEF;
149
        UART0_Muxer = UART0_UNDEF;
137
 
150
 
138
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
151
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
139
        // unmap UART0 from GPS
152
        // unmap UART0 from GPS
140
        // set port pin 6.6 (serial data from gps) to input and disconnect from IP
153
        // set port pin 6.6 (serial data from gps) to input and disconnect from IP
141
        GPIO_StructInit(&GPIO_InitStructure);
154
        GPIO_StructInit(&GPIO_InitStructure);
142
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
155
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
143
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
156
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
144
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
157
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
145
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
158
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
146
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
159
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
147
    GPIO_Init(GPIO6, &GPIO_InitStructure);
160
    GPIO_Init(GPIO6, &GPIO_InitStructure);
148
        // set port pin 6.7 (serial data to gps) to input
161
        // set port pin 6.7 (serial data to gps) to input
149
        GPIO_StructInit(&GPIO_InitStructure);
162
        GPIO_StructInit(&GPIO_InitStructure);
150
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
163
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
151
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
164
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
152
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
165
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
153
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
166
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
154
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
167
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
155
    GPIO_Init(GPIO6, &GPIO_InitStructure);
168
    GPIO_Init(GPIO6, &GPIO_InitStructure);
156
 
169
 
157
        // map UART0 to Compass
170
        // map UART0 to Compass
158
        // set port pin 5.1 (serial data from compass) to input and connect to IP
171
        // set port pin 5.1 (serial data from compass) to input and connect to IP
159
        GPIO_StructInit(&GPIO_InitStructure);
172
        GPIO_StructInit(&GPIO_InitStructure);
160
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
173
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
161
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
174
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
162
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
175
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
163
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
176
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
164
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
177
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; //UART0_RxD
165
    GPIO_Init(GPIO5, &GPIO_InitStructure);
178
    GPIO_Init(GPIO5, &GPIO_InitStructure);
166
        // set port pin 5.0 (serial data to compass) to output
179
        // set port pin 5.0 (serial data to compass) to output
167
        GPIO_StructInit(&GPIO_InitStructure);
180
        GPIO_StructInit(&GPIO_InitStructure);
168
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
181
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
169
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
182
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
170
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
183
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
171
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Enable;
184
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Enable;
172
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
185
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
173
    GPIO_Init(GPIO5, &GPIO_InitStructure);
186
    GPIO_Init(GPIO5, &GPIO_InitStructure);
174
 
187
 
175
        UART0_Muxer = UART0_MK3MAG;
188
        UART0_Muxer = UART0_MK3MAG;
176
}
189
}
177
 
190
 
178
/********************************************************/
191
/********************************************************/
179
/*                  Initialize UART0                    */
192
/*                  Initialize UART0                    */
180
/********************************************************/
193
/********************************************************/
181
void UART0_Init(void)
194
void UART0_Init(void)
182
{
195
{
183
    UART_InitTypeDef UART_InitStructure;
196
    UART_InitTypeDef UART_InitStructure;
184
       
197
       
185
        SerialPutString("\r\n UART0 init...");
198
        UART1_PutString("\r\n UART0 init...");
186
 
199
 
187
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock
200
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock
188
 
201
 
189
        Uart0Baudrate = BAUD_RATE + ((BAUD_RATE * 2)/100);   // MK3Mag baudrate is a litlle bit higher...
202
        Uart0Baudrate = UART0_BAUD_RATE + ((UART0_BAUD_RATE * 2)/100);   // MK3Mag baudrate is a litlle bit higher...
190
 
203
 
191
        /* UART0 configured as follow:
204
        /* UART0 configured as follow:
192
          - Word Length = 8 Bits
205
          - Word Length = 8 Bits
193
          - One Stop Bit
206
          - One Stop Bit
194
          - No parity
207
          - No parity
195
          - BaudRate = 57600 baud
208
          - BaudRate = 57600 baud
196
          - Hardware flow control Disabled
209
          - Hardware flow control Disabled
197
          - Receive and transmit enabled
210
          - Receive and transmit enabled
198
          - Receive and transmit FIFOs are Disabled
211
          - Receive and transmit FIFOs are Disabled
199
    */
212
    */
200
        UART_StructInit(&UART_InitStructure);
213
        UART_StructInit(&UART_InitStructure);
201
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
214
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
202
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
215
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
203
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
216
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
204
    UART_InitStructure.UART_BaudRate =                          Uart0Baudrate;
217
    UART_InitStructure.UART_BaudRate =                          Uart0Baudrate;
205
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
218
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
206
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
219
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
207
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
220
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
208
    UART_InitStructure.UART_TxFIFOLevel =                       UART_FIFOLevel_1_2;
221
    UART_InitStructure.UART_TxFIFOLevel =                       UART_FIFOLevel_1_2;
209
    UART_InitStructure.UART_RxFIFOLevel =                       UART_FIFOLevel_1_2;
222
    UART_InitStructure.UART_RxFIFOLevel =                       UART_FIFOLevel_1_2;
210
 
223
 
211
        UART_DeInit(UART0);     // reset uart 0 to default
224
        UART_DeInit(UART0);     // reset uart 0 to default
212
    UART_Init(UART0, &UART_InitStructure);  // initialize uart 0
225
    UART_Init(UART0, &UART_InitStructure);  // initialize uart 0
213
 
226
 
214
        // enable uart 0 interrupts selective
227
        // enable uart 0 interrupts selective
215
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut  /*| UART_IT_FrameError*/, ENABLE);
228
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut  /*| UART_IT_FrameError*/, ENABLE);
216
        UART_Cmd(UART0, ENABLE); // enable uart 0
229
        UART_Cmd(UART0, ENABLE); // enable uart 0
217
        // configure the uart 0 interupt line as an IRQ with priority 10 (0 is highest)
230
        // configure the uart 0 interupt line as an IRQ with priority 10 (0 is highest)
218
        VIC_Config(UART0_ITLine, VIC_IRQ, 10);
231
        VIC_Config(UART0_ITLine, VIC_IRQ, 10);
219
        // enable the uart 0 IRQ
232
        // enable the uart 0 IRQ
220
        VIC_ITCmd(UART0_ITLine, ENABLE);
233
        VIC_ITCmd(UART0_ITLine, ENABLE);
221
        UART0_Connect_to_MKGPS();
234
        UART0_Connect_to_MKGPS();
222
        SerialPutString("ok");
-
 
223
}
-
 
224
 
235
 
225
/**************************************************************/
236
        // initialize txd buffer
226
/*         Transmit tx buffer via debug uart                  */
237
        UART0_tx_buffer.pData = UART0_tbuffer;
227
/**************************************************************/
238
        UART0_tx_buffer.Size = UART0_TX_BUFFER_LEN;
228
void UART0_Transmit(void)
-
 
229
{
-
 
230
        u8 tmp_tx;
239
        UART0_tx_buffer.Position = 0;
231
        // if something has to be send and the txd fifo is not full
-
 
232
        if((!txd_complete0) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET))
240
        UART0_tx_buffer.Locked = FALSE;
233
        {
-
 
234
                tmp_tx = txd_buffer0[ptr_txd_buffer0]; // read byte from txd buffer
241
       
235
                // if terminating character or end of txd buffer reached
-
 
236
                if((tmp_tx == '\r') || (ptr_txd_buffer0 == TXD_BUFFER_LEN))
-
 
237
                {
242
        // initialize rxd buffer
238
                        ptr_txd_buffer0 = 0;    // reset txd buffer pointer
243
        UART0_rx_buffer.pData = UART0_rbuffer;
239
                        txd_complete0 = TRUE;// set complete flag
-
 
240
                }
-
 
241
                UART_SendData(UART0, tmp_tx); // put character to txd fifo
244
        UART0_rx_buffer.Size = UART0_RX_BUFFER_LEN;
242
                // set pointer to next byte
245
        UART0_rx_buffer.Position = 0;
243
                ptr_txd_buffer0++;
-
 
244
        }
-
 
245
}
-
 
246
 
-
 
247
/**************************************************************/
-
 
248
/* Add CRC and initiate transmission via debug uart           */
-
 
249
/**************************************************************/
-
 
250
void AddCRC0(u16 datalen)
-
 
251
{
-
 
252
        u16 tmpCRC = 0, i;
-
 
253
        for(i = 0; i < datalen; i++)
-
 
254
        {
-
 
255
                tmpCRC += txd_buffer0[i];
-
 
256
        }
-
 
257
        tmpCRC %= 4096;
-
 
258
        txd_buffer0[i++] = '=' + tmpCRC / 64;
-
 
259
        txd_buffer0[i++] = '=' + tmpCRC % 64;
-
 
260
        txd_buffer0[i++] = '\r';
-
 
261
 
246
        UART0_rx_buffer.Locked = FALSE;
262
        ptr_txd_buffer0 = 0;
-
 
263
        txd_complete0 = FALSE;
247
 
264
        UART_SendData(UART0, txd_buffer0[ptr_txd_buffer0++]);   // send first byte, to be continued in the txd irq
-
 
265
}
-
 
266
 
-
 
267
 
-
 
268
 
-
 
269
/**************************************************************/
-
 
270
/* Code output data                                           */
-
 
271
/**************************************************************/
-
 
272
void SendOutData0(u8 cmd, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
-
 
273
{
-
 
274
        va_list ap;
-
 
275
 
-
 
276
        u16 pt = 0;
-
 
277
        u8 a,b,c;
-
 
278
        u8 ptr = 0;
-
 
279
 
-
 
280
        u8* pdata = NULL;
-
 
281
        int len = 0;
-
 
282
 
-
 
283
        txd_buffer0[pt++] = '#';                                // Start character
-
 
284
        txd_buffer0[pt++] = 'a' + Address;      // Address (a=0; b=1,...)
-
 
285
        txd_buffer0[pt++] = cmd;                                // Command
-
 
286
 
-
 
287
        va_start(ap, numofbuffers);
-
 
288
        if(numofbuffers)
-
 
289
        {
-
 
290
                pdata = va_arg(ap, u8*);
-
 
291
                len = va_arg(ap, int);
-
 
292
                ptr = 0;
-
 
293
                numofbuffers--;
-
 
294
        }
-
 
295
        while(len)
-
 
296
        {
-
 
297
                if(len)
-
 
298
                {
-
 
299
                        a = pdata[ptr++];
-
 
300
                        len--;
-
 
301
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
302
                        {
-
 
303
                                pdata = va_arg(ap, u8*);
-
 
304
                                len = va_arg(ap, int);
-
 
305
                                ptr = 0;
-
 
306
                                numofbuffers--;
-
 
307
                        }
-
 
308
                }
-
 
309
                else a = 0;
-
 
310
                if(len)
-
 
311
                {
-
 
312
                        b = pdata[ptr++];
-
 
313
                        len--;
-
 
314
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
315
                        {
-
 
316
                                pdata = va_arg(ap, u8*);
-
 
317
                                len = va_arg(ap, int);
-
 
318
                                ptr = 0;
-
 
319
                                numofbuffers--;
-
 
320
                        }
-
 
321
                }
-
 
322
                else b = 0;
-
 
323
                if(len)
-
 
324
                {
-
 
325
                        c = pdata[ptr++];
-
 
326
                        len--;
-
 
327
                        if((!len) && numofbuffers) // try to jump to next buffer
-
 
328
                        {
-
 
329
                                pdata = va_arg(ap, u8*);
-
 
330
                                len = va_arg(ap, int);
-
 
331
                                ptr = 0;
-
 
332
                                numofbuffers--;
-
 
333
                        }
-
 
334
                }
-
 
335
                else c = 0;
-
 
336
                txd_buffer0[pt++] = '=' + (a >> 2);
-
 
337
                txd_buffer0[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
-
 
338
                txd_buffer0[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
-
 
339
                txd_buffer0[pt++] = '=' + ( c & 0x3f);
-
 
340
        }
-
 
341
        va_end(ap);
-
 
342
        AddCRC0(pt);    // add checksum after data block and initates the transmission
-
 
343
}
248
        UART1_PutString("ok");
344
 
249
}
345
 
250
 
346
/********************************************************/
251
/********************************************************/
347
/*            UART0 Interrupt Handler                   */
252
/*            UART0 Interrupt Handler                   */
348
/********************************************************/
253
/********************************************************/
349
void UART0_IRQHandler(void)
254
void UART0_IRQHandler(void)
350
{
255
{
351
        u8 c;
256
        u8 c;
352
        /*
-
 
353
        static u8 frame_error_cnt = 0;
-
 
354
       
-
 
355
    // use frame error to adjust baudrate for MK3MAG
-
 
356
        if((UART_GetITStatus(UART0, UART_IT_FrameError) == SET))
-
 
357
        {
-
 
358
           u8 msg[20];
-
 
359
           frame_error_cnt++;
-
 
360
         
-
 
361
           if (frame_error_cnt == 200)
-
 
362
           {
-
 
363
            frame_error_cnt = 0;
-
 
364
            VIC_ITCmd(UART0_ITLine, DISABLE);
-
 
365
           
-
 
366
            Uart0MK3MagBaudrate +=100;
-
 
367
            if (Uart0MK3MagBaudrate > (BAUD_RATE+1000)) Uart0MK3MagBaudrate = BAUD_RATE;
-
 
368
           
-
 
369
            Uart0Baudrate = Uart0MK3MagBaudrate;
-
 
370
           
-
 
371
            SerialPutString(" adjusting UART0 baudrate to");
-
 
372
            sprintf(msg, " %d baud...", Uart0MK3MagBaudrate); SerialPutString(msg);
-
 
373
           
-
 
374
                while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
-
 
375
                        {
-
 
376
                                c =  UART_ReceiveData(UART0);
-
 
377
                        }
-
 
378
       
-
 
379
            UART_ClearITPendingBit(UART0, UART_IT_FrameError);                  // clear frame error interrupt flag
-
 
380
           
-
 
381
            UART0_Init();
-
 
382
            return;
-
 
383
         }
-
 
384
 
-
 
385
        }*/
-
 
386
        // if receive irq or receive timeout irq has occured
257
        // if receive irq or receive timeout irq has occured
387
        if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
258
        if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
388
        {
259
        {
389
                UART_ClearITPendingBit(UART0, UART_IT_Receive);                 // clear receive interrupt flag
260
                UART_ClearITPendingBit(UART0, UART_IT_Receive);                 // clear receive interrupt flag
390
                UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag
261
                UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag
391
 
262
 
392
                // if debug UART is UART0
263
                // if debug UART is UART0
393
                if (DebugUART == UART0)
264
                if (DebugUART == UART0)
394
                {       // forward received data to the UART1 tx buffer
265
                {       // forward received data to the UART1 tx buffer
395
                        while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
266
                        while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
396
                        {
267
                        {
397
                                // wait for space in the tx buffer of the UART1
268
                                // wait for space in the tx buffer of the UART1
398
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
269
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
399
                                // move the byte from the rx buffer of UART0 to the tx buffer of UART1
270
                                // move the byte from the rx buffer of UART0 to the tx buffer of UART1
400
                                UART_SendData(UART1, UART_ReceiveData(UART0));
271
                                UART_SendData(UART1, UART_ReceiveData(UART0));
401
                        }
272
                        }
402
                }
273
                }
403
                else // UART0 is not the DebugUART (normal operation)
274
                else // UART0 is not the DebugUART (normal operation)
404
                {
275
                {
405
                        // repeat until no byte is in the RxFIFO
276
                        // repeat until no byte is in the RxFIFO
406
                        while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
277
                        while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
407
                        {  
278
                        {  
408
                                c = UART_ReceiveData(UART0); // get byte from rx fifo
279
                                c = UART_ReceiveData(UART0); // get byte from rx fifo
409
                                switch(UART0_Muxer)
280
                                switch(UART0_Muxer)
410
                                {
281
                                {
411
                                        case UART0_MKGPS:
282
                                        case UART0_MKGPS:
412
                                                UBX_Parser(c); // if connected to GPS forward byte to ubx parser
283
                                                UBX_Parser(c); // if connected to GPS forward byte to ubx parser
-
 
284
                                                MKProtocol_CollectSerialFrame(&UART0_rx_buffer, c);     // ckeck for MK-Frames also
413
                                                break;
285
                                                break;
414
                                        case UART0_MK3MAG:
286
                                        case UART0_MK3MAG:
415
                                                // ignore any byte send from MK3MAG
287
                                                // ignore any byte send from MK3MAG
416
                                                break;
288
                                                break;
417
                                        case UART0_UNDEF:
289
                                        case UART0_UNDEF:
418
                                        default:
290
                                        default:
419
                                                // ignore the byte from unknown source
291
                                                // ignore the byte from unknown source
420
                                                break;
292
                                                break;
421
                                } // eof switch(UART0_Muxer)
293
                                } // eof switch(UART0_Muxer)
422
                        } // eof while
294
                        } // eof while
423
                }  // eof UART0 is not the DebugUART
295
                }  // eof UART0 is not the DebugUART
424
        } // eof receive irq or receive timeout irq
296
        } // eof receive irq or receive timeout irq
425
}
297
}
-
 
298
 
-
 
299
/**************************************************************/
-
 
300
/* Process incomming data from debug uart                     */
-
 
301
/**************************************************************/
-
 
302
void UART0_ProcessRxData(void)
-
 
303
{
-
 
304
        SerialMsg_t SerialMsg;
-
 
305
        // if data in the rxd buffer are not locked immediately return
-
 
306
        if((UART0_rx_buffer.Locked == FALSE) || (DebugUART == UART0) ) return;
-
 
307
       
-
 
308
        MKProtocol_DecodeSerialFrame(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
-
 
309
 
-
 
310
        switch(SerialMsg.Address) // check for Slave Address
-
 
311
        {
-
 
312
                case NC_ADDRESS:  // own Slave Address
-
 
313
                switch(SerialMsg.CmdID)
-
 
314
                {
-
 
315
                        default:
-
 
316
                                break;
-
 
317
                } // case NC_ADDRESS
-
 
318
                // "break;" is missing here to fall thru to the common commands
-
 
319
 
-
 
320
                default:  // and any other Slave Address
-
 
321
 
-
 
322
                switch(SerialMsg.CmdID) // check CmdID
-
 
323
                {
-
 
324
                        case 'o': // request for navigation information
-
 
325
                                UART0_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
-
 
326
                                if(UART0_NaviData_Interval > 0) UART0_Request_NaviData = TRUE;
-
 
327
                                break;
-
 
328
 
-
 
329
                        case 'v': // request for version info
-
 
330
                                UART0_Request_VersionInfo = TRUE;
-
 
331
                                break;
-
 
332
                        default:
-
 
333
                                // unsupported command recieved
-
 
334
                                break;
-
 
335
                }
-
 
336
                break; // default:
-
 
337
        }
-
 
338
        // unlock the rxd buffer after processing
-
 
339
        UART0_rx_buffer.Position = 0;
-
 
340
        UART0_rx_buffer.Locked = FALSE;
-
 
341
}
-
 
342
 
-
 
343
/**************************************************************/
-
 
344
/*         Transmit tx buffer via uart0                       */
-
 
345
/**************************************************************/
-
 
346
void UART0_Transmit(void)
-
 
347
{
-
 
348
        u8 tmp_tx;
-
 
349
        if(DebugUART == UART0) return; // no data output if debug uart is rederected to UART0
-
 
350
        // if something has to be send and the txd fifo is not full
-
 
351
        if((UART0_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET))
-
 
352
        {
-
 
353
                tmp_tx = UART0_tx_buffer.pData[UART0_tx_buffer.Position++]; // read next byte from txd buffer
-
 
354
                UART_SendData(UART0, tmp_tx); // put character to txd fifo
-
 
355
                // if terminating character or end of txd buffer reached
-
 
356
                if((tmp_tx == '\r') || (UART0_tx_buffer.Position == UART0_tx_buffer.Size))
-
 
357
                {
-
 
358
                        UART0_tx_buffer.Position = 0; // reset txd buffer positon
-
 
359
                        UART0_tx_buffer.Locked = FALSE;// unlock tx buffer
-
 
360
                }
-
 
361
        }
-
 
362
}
-
 
363
 
-
 
364
 
-
 
365
/**************************************************************/
-
 
366
/* Send the answers to incomming commands at the uart0        */
-
 
367
/**************************************************************/
-
 
368
void UART0_TransmitTxData(void)
-
 
369
{
-
 
370
        if(DebugUART == UART0) return;
-
 
371
        UART0_Transmit(); // output pending bytes in tx buffer
-
 
372
        if(UART0_tx_buffer.Locked == TRUE) return;
-
 
373
 
-
 
374
        if(( (UART0_NaviData_Interval && CheckDelay(UART0_NaviData_Timer) ) || UART0_Request_NaviData) && (UART0_tx_buffer.Locked == FALSE))
-
 
375
        {
-
 
376
                NaviData.Errorcode = ErrorCode;
-
 
377
                MKProtocol_CreateSerialFrame(&UART0_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData));
-
 
378
                UART0_NaviData_Timer = SetDelay(UART0_NaviData_Interval);
-
 
379
                UART0_Request_NaviData = FALSE;
-
 
380
        }      
-
 
381
        UART0_Transmit(); // output pending bytes in tx buffer
-
 
382
}
426
 
383