Subversion Repositories NaviCtrl

Rev

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

Rev 179 Rev 180
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 / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
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 oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung 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 the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
// +   * porting the sources to other systems or using the software on other systems (except 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 "timer1.h"
68
#include "timer1.h"
69
#include "timer2.h"
69
#include "timer2.h"
70
#include "analog.h"
70
#include "analog.h"
71
#include "spi_slave.h"
71
#include "spi_slave.h"
72
#include "fat16.h"
72
#include "fat16.h"
73
#include "usb.h"
73
#include "usb.h"
74
#include "sdc.h"
74
#include "sdc.h"
75
#include "logging.h"
75
#include "logging.h"
76
#include "params.h"
76
#include "params.h"
77
#include "settings.h"
77
#include "settings.h"
78
#include "config.h"
78
#include "config.h"
79
#include "main.h"
79
#include "main.h"
80
 
80
 
81
u32 TimerCheckError;
81
u32 TimerCheckError;
82
u32 ErrorCode = 0;
82
u32 ErrorCode = 0;
83
u16 BeepTime;
83
u16 BeepTime;
84
u8  NCFlags = 0;
84
u8  NCFlags = 0;
85
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
85
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
86
 
86
 
87
u8 ClearMKFlags = 0;
87
u8 ClearMKFlags = 0;
88
u8 StopNavigation = 0;
88
u8 StopNavigation = 0;
89
Param_t Parameter;
89
Param_t Parameter;
90
volatile FC_t FC;
90
volatile FC_t FC;
91
 
91
 
92
s8 ErrorMSG[25];
92
s8 ErrorMSG[25];
93
 
93
 
94
//----------------------------------------------------------------------------------------------------
94
//----------------------------------------------------------------------------------------------------
95
void SCU_Config(void)
95
void SCU_Config(void)
96
{
96
{
97
        /* configure PLL and set it as master clock source */
97
        /* configure PLL and set it as master clock source */
98
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
98
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
99
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
99
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
100
        #ifdef MCLK96MHZ
100
        #ifdef MCLK96MHZ
101
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
101
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
102
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
102
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
103
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
103
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
104
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
104
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
105
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
105
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
106
        #else
106
        #else
107
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
107
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
108
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
108
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
109
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
109
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
110
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
110
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
111
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
111
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
112
        #endif
112
        #endif
113
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
113
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
114
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
114
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
115
}
115
}
116
 
116
 
117
//----------------------------------------------------------------------------------------------------
117
//----------------------------------------------------------------------------------------------------
118
void GetNaviCtrlVersion(void)
118
void GetNaviCtrlVersion(void)
119
{
119
{
120
        u8 msg[25];
120
        u8 msg[25];
121
 
121
 
122
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
122
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
123
        UART1_PutString(msg);
123
        UART1_PutString(msg);
124
}
124
}
125
 
125
 
126
//----------------------------------------------------------------------------------------------------
126
//----------------------------------------------------------------------------------------------------
127
void CheckErrors(void)
127
void CheckErrors(void)
128
{
128
{
129
        if(CheckDelay(SPI0_Timeout))
129
        if(CheckDelay(SPI0_Timeout))
130
        {
130
        {
131
                LED_RED_ON;
131
                LED_RED_ON;
132
                sprintf(ErrorMSG,"no FC communication ");
132
                sprintf(ErrorMSG,"no FC communication ");
133
                TIMER2_Deinit(); // reduce irq load
-
 
134
                ErrorCode = 3;
133
                ErrorCode = 3;
135
                StopNavigation = 1;
134
                StopNavigation = 1;
136
        }
135
        }
137
        else if(CheckDelay(I2C1_Timeout))
136
        else if(CheckDelay(I2C1_Timeout))
138
        {
137
        {
139
                LED_RED_ON;
138
                LED_RED_ON;
140
                sprintf(ErrorMSG,"no MK3Mag communication ");
139
                sprintf(ErrorMSG,"no MK3Mag communication ");
141
                //Reset I2CBus
140
                //Reset I2CBus
142
                I2C1_Deinit();
141
                I2C1_Deinit();
143
                I2C1_Init();
142
                I2C1_Init();
144
                ErrorCode = 4;
143
                ErrorCode = 4;
145
                StopNavigation = 1;
144
                StopNavigation = 1;
146
        }
145
        }
147
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
146
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
148
        {
147
        {
149
                LED_RED_ON;
148
                LED_RED_ON;
150
                sprintf(ErrorMSG,"FC not compatible ");
149
                sprintf(ErrorMSG,"FC not compatible ");
151
                ErrorCode = 1;
150
                ErrorCode = 1;
152
                StopNavigation = 1;
151
                StopNavigation = 1;
153
        }
152
        }
154
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
153
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
155
        {
154
        {
156
                sprintf(ErrorMSG,"MK3Mag not compatible ");
155
                sprintf(ErrorMSG,"MK3Mag not compatible ");
157
                LED_RED_ON;
156
                LED_RED_ON;
158
                ErrorCode = 2;
157
                ErrorCode = 2;
159
                StopNavigation = 1;
158
                StopNavigation = 1;
160
        }
159
        }
161
        else if(CheckDelay(UBX_Timeout))
160
        else if(CheckDelay(UBX_Timeout))
162
        {
161
        {
163
                LED_RED_ON;
162
                LED_RED_ON;
164
                sprintf(ErrorMSG,"no GPS communication ");
163
                sprintf(ErrorMSG,"no GPS communication ");
165
                ErrorCode = 5;
164
                ErrorCode = 5;
166
                StopNavigation = 1;
165
                StopNavigation = 1;
167
        }
166
        }
168
        else if(I2C_Heading.Heading < 0)
167
        else if(I2C_Heading.Heading < 0)
169
        {
168
        {
170
                LED_RED_ON;
169
                LED_RED_ON;
171
                sprintf(ErrorMSG,"bad compass value ");
170
                sprintf(ErrorMSG,"bad compass value ");
172
                ErrorCode = 6;
171
                ErrorCode = 6;
173
                StopNavigation = 1;
172
                StopNavigation = 1;
174
        }
173
        }
175
        else if(FC.RC_Quality < 100)
174
        else if(FC.RC_Quality < 100)
176
        {
175
        {
177
                LED_RED_ON;
176
                LED_RED_ON;
178
                sprintf(ErrorMSG,"RC Signal lost ");
177
                sprintf(ErrorMSG,"RC Signal lost ");
179
                ErrorCode = 7;
178
                ErrorCode = 7;
180
        }
179
        }
181
        else // no error occured
180
        else // no error occured
182
        {
181
        {
183
                sprintf(ErrorMSG,"No Error               ");
182
                sprintf(ErrorMSG,"No Error               ");
184
                if(ErrorCode == 3)
-
 
185
                {
-
 
186
                        TIMER2_Init(); // enbable servo outputs
-
 
187
                }
-
 
188
                ErrorCode = 0;
183
                ErrorCode = 0;
189
                StopNavigation = 0;
184
                StopNavigation = 0;
190
                LED_RED_OFF;
185
                LED_RED_OFF;
191
        }
186
        }
192
}
187
}
193
 
188
 
194
// the handler will be cyclic called by the timer 1 ISR
189
// the handler will be cyclic called by the timer 1 ISR
195
// used is for critical timing parts that normaly would handled 
190
// used is for critical timing parts that normaly would handled 
196
// within the main loop that could block longer at logging activities
191
// within the main loop that could block longer at logging activities
197
void EXTIT3_IRQHandler(void)
192
void EXTIT3_IRQHandler(void)
198
{
193
{
199
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
194
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
200
 
195
 
201
        I2C1_UpdateCompass();   // update compass communication 
196
        I2C1_UpdateCompass();   // update compass communication 
202
        Analog_Update();                // get new ADC values
197
        Analog_Update();                // get new ADC values
203
 
198
 
204
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
199
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
205
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
200
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
206
}
201
}
207
 
202
 
208
//----------------------------------------------------------------------------------------------------
203
//----------------------------------------------------------------------------------------------------
209
int main(void)
204
int main(void)
210
{
205
{
211
        /* Configure the system clocks */
206
        /* Configure the system clocks */
212
        SCU_Config();
207
        SCU_Config();
213
        /* init VIC (Vectored Interrupt Controller)     */
208
        /* init VIC (Vectored Interrupt Controller)     */
214
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
209
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
215
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
210
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
216
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
211
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
217
        // initialize the interrupt handler
212
        // initialize the interrupt handler
218
        Interrupt_Init();
213
        Interrupt_Init();
219
        // initialize timer 1 for System Clock and delay rountines
214
        // initialize timer 1 for System Clock and delay rountines
220
        TIMER1_Init();
215
        TIMER1_Init();
221
        // initialize the LEDs (needs Timer 1)
216
        // initialize the LEDs (needs Timer 1)
222
        Led_Init();
217
        Led_Init();
223
        // initialize the debug UART1
218
        // initialize the debug UART1
224
        UART1_Init();
219
        UART1_Init();
225
        UART1_PutString("\r\n---------------------------------------------");
220
        UART1_PutString("\r\n---------------------------------------------");
226
        // initialize timer 2 for servo outputs
221
        // initialize timer 2 for servo outputs
227
        TIMER2_Init();
222
        //TIMER2_Init();
228
        // initialize UART2 to FLIGHTCTRL
223
        // initialize UART2 to FLIGHTCTRL
229
        UART2_Init();
224
        UART2_Init();
230
        // initialize UART0 (to MKGPS or MK3MAG)
225
        // initialize UART0 (to MKGPS or MK3MAG)
231
        UART0_Init();
226
        UART0_Init();
232
        // initialize adc
227
        // initialize adc
233
        Analog_Init();
228
        Analog_Init();
234
        // initialize usb
229
        // initialize usb
235
        //USB_ConfigInit();
230
        //USB_ConfigInit();
236
        // initialize SPI0 to FC
231
        // initialize SPI0 to FC
237
        SPI0_Init();
232
        SPI0_Init();
238
        // initialize i2c bus to MK3MAG (needs Timer 1)
233
        // initialize i2c bus to MK3MAG (needs Timer 1)
239
        I2C1_Init();
234
        I2C1_Init();
240
        // initialize the gps position controller (needs Timer 1)
235
        // initialize the gps position controller (needs Timer 1)
241
        Fat16_Init();
236
        Fat16_Init();
242
        // initialize NC params
237
        // initialize NC params
243
        NCParams_Init();
238
        NCParams_Init();
244
        // initialize the settings
239
        // initialize the settings
245
        Settings_Init();
240
        Settings_Init();
246
        // initialize logging (needs settings)
241
        // initialize logging (needs settings)
247
        Logging_Init();
242
        Logging_Init();
248
 
243
 
249
        TimerCheckError = SetDelay(3000);
244
        TimerCheckError = SetDelay(3000);
250
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
245
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
251
        UART1_PutString("\n\r Version information:");
246
        UART1_PutString("\n\r Version information:");
252
 
247
 
253
        GetNaviCtrlVersion();
248
        GetNaviCtrlVersion();
254
 
249
 
255
        I2C1_GetMK3MagVersion();
250
        I2C1_GetMK3MagVersion();
256
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
251
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
257
        {
252
        {
258
                UART1_PutString("\n\r MK3Mag not compatible");
253
                UART1_PutString("\n\r MK3Mag not compatible");
259
                LED_RED_ON;
254
                LED_RED_ON;
260
        }
255
        }
261
 
256
 
262
        SPI0_GetFlightCtrlVersion();
257
        SPI0_GetFlightCtrlVersion();
263
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
258
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
264
        {
259
        {
265
                UART1_PutString("\n\r Flight-Ctrl not compatible");
260
                UART1_PutString("\n\r Flight-Ctrl not compatible");
266
                LED_RED_ON;
261
                LED_RED_ON;
267
        }
262
        }
268
 
263
 
269
        UART0_GetMKOSDVersion();
264
        UART0_GetMKOSDVersion();
270
 
265
 
271
        GPS_Init();
266
        GPS_Init();
272
        // initialize fat16 partition on sd card (needs Timer 1)
267
        // initialize fat16 partition on sd card (needs Timer 1)
273
   
268
   
274
        // ---------- Prepare the isr driven 
269
        // ---------- Prepare the isr driven 
275
        // set to absolute lowest priority
270
        // set to absolute lowest priority
276
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
271
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
277
        // enanble interrupts
272
        // enanble interrupts
278
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
273
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
279
 
274
 
280
        for (;;) // the endless main loop
275
        for (;;) // the endless main loop
281
        {
276
        {
282
                UART0_ProcessRxData();  // process request
277
                UART0_ProcessRxData();  // process request
283
                UART1_ProcessRxData();  // process request
278
                UART1_ProcessRxData();  // process request
284
                USB_ProcessRxData();    // process request
279
                USB_ProcessRxData();    // process request
285
                UART0_TransmitTxData(); // send answer
280
                UART0_TransmitTxData(); // send answer
286
                UART1_TransmitTxData(); // send answer
281
                UART1_TransmitTxData(); // send answer
287
                USB_TransmitTxData();   // send answer
282
                USB_TransmitTxData();   // send answer
288
 
283
 
289
                SPI0_UpdateBuffer();    // handle new SPI Data
284
                SPI0_UpdateBuffer();    // handle new SPI Data
290
 
285
 
291
                // ---------------- Error Check Timing ----------------------------
286
                // ---------------- Error Check Timing ----------------------------
292
                if(CheckDelay(TimerCheckError))
287
                if(CheckDelay(TimerCheckError))
293
                {
288
                {
294
                        TimerCheckError = SetDelay(1000);
289
                        TimerCheckError = SetDelay(1000);
295
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(); // process the GPS data even if the FC is not connected
290
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(); // process the GPS data even if the FC is not connected
296
                        CheckErrors();
291
                        CheckErrors();
297
                        if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
292
                        if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
298
//                      else NaviData.FlyingTime = 0; // not the time per flight
293
//                      else NaviData.FlyingTime = 0; // not the time per flight
299
                        UART1_Request_SendFollowMe = TRUE;
294
                        UART1_Request_SendFollowMe = TRUE;
300
                        if(SerialLinkOkay) SerialLinkOkay--;
295
                        if(SerialLinkOkay) SerialLinkOkay--;
301
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
296
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
302
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
297
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
303
                }
298
                }
304
                // ---------------- Logging  ---------------------------------------
299
                // ---------------- Logging  ---------------------------------------
305
                Logging_Update();  // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1
300
                Logging_Update();  // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1
306
                // ---------------- ADC readings -----------------------------------
301
                // ---------------- ADC readings -----------------------------------
307
        }
302
        }
308
}
303
}
309
 
304
 
310
 
305
 
311
 
306