Subversion Repositories NaviCtrl

Rev

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

Rev 114 Rev 115
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
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
31
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
32
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
32
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
35
// + this list of conditions and the following disclaimer.
35
// + this list of conditions and the following disclaimer.
36
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
37
// +     from this software without specific prior written permission.
37
// +     from this software without specific prior written permission.
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
39
// +     for non-commercial use (directly or indirectly)
39
// +     for non-commercial use (directly or indirectly)
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
41
// +     with our written permission
41
// +     with our written permission
42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
43
// +     clearly linked as origin
43
// +     clearly linked as origin
44
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
44
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
45
//
45
//
46
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56
// +  POSSIBILITY OF SUCH DAMAGE.
56
// +  POSSIBILITY OF SUCH DAMAGE.
57
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58
//#define MCLK96MHZ
58
//#define MCLK96MHZ
59
const unsigned long _Main_Crystal = 25000;
59
const unsigned long _Main_Crystal = 25000;
60
#include <stdio.h>
60
#include <stdio.h>
61
#include "91x_lib.h"
61
#include "91x_lib.h"
62
#include "led.h"
62
#include "led.h"
63
#include "uart0.h"
63
#include "uart0.h"
64
#include "uart1.h"
64
#include "uart1.h"
65
#include "uart2.h"
65
#include "uart2.h"
66
#include "GPS.h"
66
#include "GPS.h"
67
#include "i2c.h"
67
#include "i2c.h"
68
#include "timer.h"
68
#include "timer.h"
69
#include "spi_slave.h"
69
#include "spi_slave.h"
70
#include "fat16.h"
70
#include "fat16.h"
71
#include "usb.h"
71
#include "usb.h"
72
#include "sdc.h"
72
#include "sdc.h"
73
#include "logging.h"
73
#include "logging.h"
74
#include "settings.h"
74
#include "settings.h"
75
#include "main.h"
75
#include "main.h"
76
 
76
 
77
u32 TimerCompassUpdate;
77
u32 TimerCompassUpdate;
78
u32 TimerCheckError;
78
u32 TimerCheckError;
79
u32 ErrorCode = 0;
79
u32 ErrorCode = 0;
80
u16 BeepTime;
80
u16 BeepTime;
81
u8  NCFlags = 0;
81
u8  NCFlags = 0;
82
 
82
 
83
u8 ClearMKFlags = 0;
83
u8 ClearMKFlags = 0;
84
u8 StopNavigation = 0;
84
u8 StopNavigation = 0;
85
Param_t Parameter;
85
Param_t Parameter;
86
volatile FC_t FC;
86
volatile FC_t FC;
87
 
87
 
88
s8 ErrorMSG[25];
88
s8 ErrorMSG[25];
89
 
89
 
90
//----------------------------------------------------------------------------------------------------
90
//----------------------------------------------------------------------------------------------------
91
void SCU_Config(void)
91
void SCU_Config(void)
92
{
92
{
93
        /* configure PLL and set it as master clock source */
93
        /* configure PLL and set it as master clock source */
94
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
94
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
95
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
95
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
96
        #ifdef MCLK96MHZ
96
        #ifdef MCLK96MHZ
97
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
97
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
98
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
98
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
99
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
99
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
100
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
100
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
101
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
101
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
102
        #else
102
        #else
103
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
103
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
104
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
104
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
105
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
105
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
106
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
106
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
107
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
107
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
108
        #endif
108
        #endif
109
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
109
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
110
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
110
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
111
}
111
}
112
 
112
 
113
//----------------------------------------------------------------------------------------------------
113
//----------------------------------------------------------------------------------------------------
114
void GetNaviCtrlVersion(void)
114
void GetNaviCtrlVersion(void)
115
{
115
{
116
        u8 msg[25];
116
        u8 msg[25];
117
 
117
 
118
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
118
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
119
        UART1_PutString(msg);
119
        UART1_PutString(msg);
120
}
120
}
121
 
-
 
122
 
-
 
123
//----------------------------------------------------------------------------------------------------
-
 
124
s8* GenerateGPSLogFileName(void)
-
 
125
{
-
 
126
        static u16 filenum = 0; // file name counter
-
 
127
        static s8 filename[30];
-
 
128
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
-
 
129
 
-
 
130
        if(SystemTime.Valid)
-
 
131
        {
-
 
132
                // if the day has been changed
-
 
133
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
-
 
134
                {
-
 
135
                        LastTime.Year = SystemTime.Year;
-
 
136
                        LastTime.Month = SystemTime.Month;
-
 
137
                        LastTime.Day = SystemTime.Day;
-
 
138
                        LastTime.Valid = 1;
-
 
139
                        filenum = 0; // reset file counter
-
 
140
                }
-
 
141
                sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
-
 
142
                filenum++;
-
 
143
                return filename;
-
 
144
        }
-
 
145
        else return NULL;
-
 
146
}
-
 
147
 
-
 
148
 
121
 
149
//----------------------------------------------------------------------------------------------------
122
//----------------------------------------------------------------------------------------------------
150
void CheckErrors(void)
123
void CheckErrors(void)
151
{
124
{
152
        if(!CheckSPIOkay)
125
        if(!CheckSPIOkay)
153
        {
126
        {
154
                LED_RED_ON;
127
                LED_RED_ON;
155
                sprintf(ErrorMSG,"no FC communication ");
128
                sprintf(ErrorMSG,"no FC communication ");
156
                ErrorCode = 3;
129
                ErrorCode = 3;
157
                StopNavigation = 1;
130
                StopNavigation = 1;
158
        }
131
        }
159
        else if(!CheckI2COkay)
132
        else if(!CheckI2COkay)
160
        {
133
        {
161
                LED_RED_ON;
134
                LED_RED_ON;
162
                sprintf(ErrorMSG,"no MK3Mag communication ");
135
                sprintf(ErrorMSG,"no MK3Mag communication ");
163
                ErrorCode = 4;
136
                ErrorCode = 4;
164
                StopNavigation = 1;
137
                StopNavigation = 1;
165
        }
138
        }
166
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
139
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
167
        {
140
        {
168
                LED_RED_ON;
141
                LED_RED_ON;
169
                sprintf(ErrorMSG,"FC not compatible ");
142
                sprintf(ErrorMSG,"FC not compatible ");
170
                ErrorCode = 1;
143
                ErrorCode = 1;
171
                StopNavigation = 1;
144
                StopNavigation = 1;
172
        }
145
        }
173
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
146
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
174
        {
147
        {
175
                sprintf(ErrorMSG,"MK3Mag not compatible ");
148
                sprintf(ErrorMSG,"MK3Mag not compatible ");
176
                LED_RED_ON;
149
                LED_RED_ON;
177
                ErrorCode = 2;
150
                ErrorCode = 2;
178
                StopNavigation = 1;
151
                StopNavigation = 1;
179
        }
152
        }
180
        else if(!CheckGPSOkay)
153
        else if(!CheckGPSOkay)
181
        {
154
        {
182
                LED_RED_ON;
155
                LED_RED_ON;
183
                sprintf(ErrorMSG,"no GPS communication ");
156
                sprintf(ErrorMSG,"no GPS communication ");
184
                ErrorCode = 5;
157
                ErrorCode = 5;
185
                StopNavigation = 1;
158
                StopNavigation = 1;
186
        }
159
        }
187
        else if(I2C_Heading.Heading < 0)
160
        else if(I2C_Heading.Heading < 0)
188
        {
161
        {
189
                LED_RED_ON;
162
                LED_RED_ON;
190
                sprintf(ErrorMSG,"bad compass value ");
163
                sprintf(ErrorMSG,"bad compass value ");
191
                ErrorCode = 6;
164
                ErrorCode = 6;
192
                StopNavigation = 1;
165
                StopNavigation = 1;
193
        }
166
        }
194
        else if(FC.RC_Quality < 100)
167
        else if(FC.RC_Quality < 100)
195
        {
168
        {
196
                LED_RED_ON;
169
                LED_RED_ON;
197
                sprintf(ErrorMSG,"RC Signal lost ");
170
                sprintf(ErrorMSG,"RC Signal lost ");
198
                ErrorCode = 7;
171
                ErrorCode = 7;
199
        }
172
        }
200
        else // no error occured
173
        else // no error occured
201
        {
174
        {
202
                sprintf(ErrorMSG,"No Error               ");
175
                sprintf(ErrorMSG,"No Error               ");
203
                ErrorCode = 0;
176
                ErrorCode = 0;
204
                StopNavigation = 0;
177
                StopNavigation = 0;
205
                LED_RED_OFF;
178
                LED_RED_OFF;
206
        }
179
        }
207
        CheckSPIOkay = 0;
180
        CheckSPIOkay = 0;
208
        CheckGPSOkay = 0;
181
        CheckGPSOkay = 0;
209
        CheckI2COkay = 0;
182
        CheckI2COkay = 0;
210
}
183
}
211
 
184
 
212
 
185
 
213
 
186
 
214
//----------------------------------------------------------------------------------------------------
187
//----------------------------------------------------------------------------------------------------
215
int main(void)
188
int main(void)
216
{
189
{
217
        /* Configure the system clocks */
190
        /* Configure the system clocks */
218
        SCU_Config();
191
        SCU_Config();
219
        /* init VIC (Vectored Interrupt Controller)     */
192
        /* init VIC (Vectored Interrupt Controller)     */
220
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
193
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
221
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
194
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
222
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
195
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
223
        // initialize the interrupt handler
196
        // initialize the interrupt handler
224
        Interrupt_Init();
197
        Interrupt_Init();
225
        // initialize timer 1
198
        // initialize timer 1
226
        TIMER1_Init();
199
        TIMER1_Init();
227
        // initialize the LEDs (needs Timer 1)
200
        // initialize the LEDs (needs Timer 1)
228
        Led_Init();
201
        Led_Init();
229
        // initialize the debug UART1
202
        // initialize the debug UART1
230
        UART1_Init();
203
        UART1_Init();
231
        UART1_PutString("\r\n---------------------------------------------");
204
        UART1_PutString("\r\n---------------------------------------------");
232
        // initialize UART2 to FLIGHTCTRL
205
        // initialize UART2 to FLIGHTCTRL
233
        UART2_Init();
206
        UART2_Init();
234
        // initialize UART0 (to MKGPS or MK3MAG)
207
        // initialize UART0 (to MKGPS or MK3MAG)
235
        UART0_Init();
208
        UART0_Init();
236
        // initialize usb
209
        // initialize usb
237
        USB_ConfigInit();
210
        USB_ConfigInit();
238
        // initialize SPI0 to FC
211
        // initialize SPI0 to FC
239
        SPI0_Init();
212
        SPI0_Init();
240
        // initialize i2c bus to MK3MAG (needs Timer 1)
213
        // initialize i2c bus to MK3MAG (needs Timer 1)
241
        I2C1_Init();
214
        I2C1_Init();
242
        // initialize the gps position controller (needs Timer 1)
215
        // initialize the gps position controller (needs Timer 1)
243
        GPS_Init();
216
        GPS_Init();
244
        // initialize fat16 partition on sd card (needs Timer 1)
217
        // initialize fat16 partition on sd card (needs Timer 1)
245
        Fat16_Init();
218
        Fat16_Init();
246
        // initialize the settings
219
        // initialize the settings
247
        Settings_Init();
220
        Settings_Init();
248
        // initialize logging (needs settings)
221
        // initialize logging (needs settings)
249
        Logging_Init();
222
        Logging_Init();
250
 
223
 
251
        TimerCompassUpdate = SetDelay(5);
224
        TimerCompassUpdate = SetDelay(5);
252
        TimerCheckError = SetDelay(3000);
225
        TimerCheckError = SetDelay(3000);
253
        UART1_PutString("\r\n---------------------------------------------");
226
        UART1_PutString("\r\n---------------------------------------------");
254
        UART1_PutString("\n\r Version information:");
227
        UART1_PutString("\n\r Version information:");
255
        GetNaviCtrlVersion();
228
        GetNaviCtrlVersion();
256
        SPI0_GetFlightCtrlVersion();
229
        SPI0_GetFlightCtrlVersion();
257
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
230
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
258
        {
231
        {
259
                //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE);
232
                //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE);
260
                //UART1_PutString(ErrorMSG);
233
                //UART1_PutString(ErrorMSG);
261
                UART1_PutString("\n\r Flight-Ctrl not compatible");
234
                UART1_PutString("\n\r Flight-Ctrl not compatible");
262
                LED_RED_ON;
235
                LED_RED_ON;
263
        }
236
        }
264
 
237
 
265
        I2C1_GetMK3MagVersion();
238
        I2C1_GetMK3MagVersion();
266
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
239
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
267
        {
240
        {
268
                UART1_PutString("\n\r MK3Mag not compatible");
241
                UART1_PutString("\n\r MK3Mag not compatible");
269
                LED_RED_ON;
242
                LED_RED_ON;
270
        }
243
        }
271
 
244
 
272
        UART1_PutString("\r\n---------------------------------------------\r\n\r\n");
245
        UART1_PutString("\r\n---------------------------------------------\r\n\r\n");
273
 
246
 
274
        for (;;)
247
        for (;;)
275
        {
248
        {
276
                UART1_ProcessRxData();  // process request
249
                UART1_ProcessRxData();  // process request
277
                UART1_TransmitTxData();         // send answer
250
                UART1_TransmitTxData();         // send answer
278
 
251
 
279
                UART0_ProcessRxData();  // process request
252
                UART0_ProcessRxData();  // process request
280
                UART0_TransmitTxData();         // send answer
253
                UART0_TransmitTxData();         // send answer
281
 
254
 
282
                USB_ProcessRxData();    // process request
255
                USB_ProcessRxData();    // process request
283
                USB_TransmitTxData();   // send answer
256
                USB_TransmitTxData();   // send answer
284
 
257
 
285
                SPI0_UpdateBuffer();
258
                SPI0_UpdateBuffer();
286
 
259
 
287
                // ------------------------- I2C Timing --------------------------------
260
                // ------------------------- I2C Timing --------------------------------
288
                if(I2C_State != I2C_OFF)
261
                if(I2C_State != I2C_OFF)
289
                {
262
                {
290
                        if (CheckDelay(TimerCompassUpdate))
263
                        if (CheckDelay(TimerCompassUpdate))
291
                        {
264
                        {
292
                                // check for hanging I2C bus
265
                                // check for hanging I2C bus
293
                                if(CheckDelay(I2C1_Timeout))
266
                                if(CheckDelay(I2C1_Timeout))
294
                                {       // reset I2C
267
                                {       // reset I2C
295
                                        UART1_PutString("\n\rCommunication-Error: MK3Mag\n\r");
268
                                        UART1_PutString("\n\rCommunication-Error: MK3Mag\n\r");
296
                                        I2C1_Deinit();
269
                                        I2C1_Deinit();
297
                                        I2C1_Init();
270
                                        I2C1_Init();
298
                                }
271
                                }
299
                                else
272
                                else
300
                                {       // check for incomming compass calibration request
273
                                {       // check for incomming compass calibration request
301
                                        // update CalByte from spi input queue
274
                                        // update CalByte from spi input queue
302
                                        fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
275
                                        fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
303
                                        // send new calstate
276
                                        // send new calstate
304
                                        if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
277
                                        if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
305
                                        {
278
                                        {
306
                                                I2C1_SendCommand(I2C_CMD_WRITE_CAL);
279
                                                I2C1_SendCommand(I2C_CMD_WRITE_CAL);
307
                                        }
280
                                        }
308
                                        else // request current heading
281
                                        else // request current heading
309
                                        {
282
                                        {
310
                                                I2C1_SendCommand(I2C_CMD_READ_HEADING);
283
                                                I2C1_SendCommand(I2C_CMD_READ_HEADING);
311
                                        }
284
                                        }
312
                                        TimerCompassUpdate = SetDelay(25);    // every 25 ms
285
                                        TimerCompassUpdate = SetDelay(25);    // every 25 ms
313
                                }
286
                                }
314
 
287
 
315
                        }
288
                        }
316
                }
289
                }
317
 
290
 
318
                // ---------------- Error Check Timing ----------------------------
291
                // ---------------- Error Check Timing ----------------------------
319
                if(CheckDelay(TimerCheckError))
292
                if(CheckDelay(TimerCheckError))
320
                {
293
                {
321
                        TimerCheckError = SetDelay(1000);
294
                        TimerCheckError = SetDelay(1000);
322
                        if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected
295
                        if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected
323
                        CheckErrors();
296
                        CheckErrors();
324
                        if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
297
                        if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
325
//                      else NaviData.FlyingTime = 0;
298
//                      else NaviData.FlyingTime = 0;
326
                        UART1_Request_SendFollowMe = TRUE;
299
                        UART1_Request_SendFollowMe = TRUE;
327
                        if(SerialLinkOkay) SerialLinkOkay--;
300
                        if(SerialLinkOkay) SerialLinkOkay--;
328
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
301
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
329
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
302
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
330
                }
303
                }
331
                // ---------------- Logging  ---------------------------------------
304
                // ---------------- Logging  ---------------------------------------
332
                Logging_Update();
305
                Logging_Update();
333
        }
306
        }
334
}
307
}
335
 
308
 
336
 
309
 
337
 
310