Subversion Repositories NaviCtrl

Rev

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

Rev 314 Rev 318
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 "compass.h"
68
#include "compass.h"
69
#include "timer1.h"
69
#include "timer1.h"
70
#include "timer2.h"
70
#include "timer2.h"
71
#include "analog.h"
71
#include "analog.h"
72
#include "spi_slave.h"
72
#include "spi_slave.h"
73
#include "fat16.h"
73
#include "fat16.h"
74
#include "usb.h"
74
#include "usb.h"
75
#include "sdc.h"
75
#include "sdc.h"
76
#include "logging.h"
76
#include "logging.h"
77
#include "params.h"
77
#include "params.h"
78
#include "settings.h"
78
#include "settings.h"
79
#include "config.h"
79
#include "config.h"
80
#include "main.h"
80
#include "main.h"
81
#include "debug.h"
81
#include "debug.h"
82
#include "eeprom.h"
82
#include "eeprom.h"
83
 
83
 
84
#ifdef FOLLOW_ME
84
#ifdef FOLLOW_ME
85
u8 TransmitAlsoToFC = 0;
85
u8 TransmitAlsoToFC = 0;
86
#endif
86
#endif
87
u32 TimerCheckError;
87
u32 TimerCheckError;
88
u8 ErrorCode = 0;
88
u8 ErrorCode = 0;
89
u16 BeepTime;
89
u16 BeepTime;
90
u8  NCFlags = 0;
90
u8  NCFlags = 0;
91
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
91
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
92
 
92
 
93
u8 ClearFCStatusFlags = 0;
93
u8 ClearFCStatusFlags = 0;
94
u8 StopNavigation = 0;
94
u8 StopNavigation = 0;
95
Param_t Parameter;
95
Param_t Parameter;
96
volatile FC_t FC;
96
volatile FC_t FC;
97
 
97
 
98
s8 ErrorMSG[25];
98
s8 ErrorMSG[25];
99
 
99
 
100
//----------------------------------------------------------------------------------------------------
100
//----------------------------------------------------------------------------------------------------
101
void SCU_Config(void)
101
void SCU_Config(void)
102
{
102
{
103
        /* configure PLL and set it as master clock source */
103
        /* configure PLL and set it as master clock source */
104
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
104
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
105
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
105
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
106
        #ifdef MCLK96MHZ
106
        #ifdef MCLK96MHZ
107
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
107
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
108
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
108
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
109
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
109
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
110
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
110
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
111
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
111
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
112
        #else
112
        #else
113
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
113
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
114
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
114
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
115
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
115
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
116
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
116
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
117
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
117
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
118
        #endif
118
        #endif
119
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
119
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
120
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
120
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
121
}
121
}
122
 
122
 
123
//----------------------------------------------------------------------------------------------------
123
//----------------------------------------------------------------------------------------------------
124
void GetNaviCtrlVersion(void)
124
void GetNaviCtrlVersion(void)
125
{
125
{
126
        u8 msg[25];
126
        u8 msg[25];
127
 
127
 
128
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c",  Version_HW/10, Version_HW%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
128
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c",  Version_HW/10, Version_HW%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
129
        UART1_PutString(msg);
129
        UART1_PutString(msg);
130
}
130
}
131
 
131
 
132
//----------------------------------------------------------------------------------------------------
132
//----------------------------------------------------------------------------------------------------
133
 
133
 
134
void CheckErrors(void)
134
void CheckErrors(void)
135
{
135
{
136
        UART_VersionInfo.HardwareError[0] = 0;
136
        UART_VersionInfo.HardwareError[0] = 0;
137
 
137
 
138
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08;
138
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08;
139
        else DebugOut.Status[1] &= ~0x08; // MK3Mag green status
139
        else DebugOut.Status[1] &= ~0x08; // MK3Mag green status
140
 
140
 
141
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02;
141
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02;
142
        else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status
142
        else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status
143
 
143
 
144
        if(CheckDelay(SPI0_Timeout))
144
        if(CheckDelay(SPI0_Timeout))
145
        {
145
        {
146
                LED_RED_ON;
146
                LED_RED_ON;
147
                sprintf(ErrorMSG,"no FC communication ");
147
                sprintf(ErrorMSG,"no FC communication ");
148
                ErrorCode = 3;
148
                ErrorCode = 3;
149
                StopNavigation = 1;
149
                StopNavigation = 1;
150
                DebugOut.Status[0] &= ~0x01; // status of FC Present
150
                DebugOut.Status[0] &= ~0x01; // status of FC Present
151
                DebugOut.Status[0] &= ~0x02; // status of BL Present
151
                DebugOut.Status[0] &= ~0x02; // status of BL Present
152
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
152
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
153
        }
153
        }
154
        else if(CheckDelay(I2C1_Timeout))
154
        else if(CheckDelay(I2C1_Timeout))
155
        {
155
        {
156
                LED_RED_ON;
156
                LED_RED_ON;
157
                sprintf(ErrorMSG,"no compass communication ");
157
                sprintf(ErrorMSG,"no compass communication ");
158
                //Reset I2CBus
158
                //Reset I2CBus
159
                I2C1_Deinit();
159
                I2C1_Deinit();
160
                I2C1_Init();
160
                I2C1_Init();
161
                ErrorCode = 4;
161
                ErrorCode = 4;
162
                StopNavigation = 1;
162
                StopNavigation = 1;
163
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
163
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
164
                DebugOut.Status[1] |= 0x08;
164
                DebugOut.Status[1] |= 0x08;
165
        }
165
        }
166
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
166
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
167
        {
167
        {
168
                LED_RED_ON;
168
                LED_RED_ON;
-
 
169
#ifndef FOLLOW_ME
169
                sprintf(ErrorMSG,"FC not compatible ");
170
                sprintf(ErrorMSG,"FC not compatible ");
-
 
171
#else
-
 
172
                sprintf(ErrorMSG,"! FollowMe only ! ");
-
 
173
#endif
170
                ErrorCode = 1;
174
                ErrorCode = 1;
171
                StopNavigation = 1;
175
                StopNavigation = 1;
172
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
176
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
173
        }
177
        }
174
 
178
 
175
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
179
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
176
        {
180
        {
177
                LED_RED_ON;
181
                LED_RED_ON;
178
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
182
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
179
                ErrorCode = 10;
183
                ErrorCode = 10;
180
        }
184
        }
181
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
185
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
182
        {
186
        {
183
                LED_RED_ON;
187
                LED_RED_ON;
184
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
188
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
185
                ErrorCode = 11;
189
                ErrorCode = 11;
186
        }
190
        }
187
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
191
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
188
        {
192
        {
189
                LED_RED_ON;
193
                LED_RED_ON;
190
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
194
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
191
                ErrorCode = 12;
195
                ErrorCode = 12;
192
        }
196
        }
193
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
197
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
194
        {
198
        {
195
                LED_RED_ON;
199
                LED_RED_ON;
196
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
200
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
197
                ErrorCode = 13;
201
                ErrorCode = 13;
198
        }
202
        }
199
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
203
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
200
        {
204
        {
201
                LED_RED_ON;
205
                LED_RED_ON;
202
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
206
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
203
                ErrorCode = 14;
207
                ErrorCode = 14;
204
        }
208
        }
205
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
209
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
206
        {
210
        {
207
                LED_RED_ON;
211
                LED_RED_ON;
208
                sprintf(ErrorMSG,"ERR: FC Z-ACC");
212
                sprintf(ErrorMSG,"ERR: FC Z-ACC");
209
                ErrorCode = 15;
213
                ErrorCode = 15;
210
        }
214
        }
211
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
215
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
212
        {
216
        {
213
                LED_RED_ON;
217
                LED_RED_ON;
214
                sprintf(ErrorMSG,"ERR: Pressure sensor");
218
                sprintf(ErrorMSG,"ERR: Pressure sensor");
215
                ErrorCode = 16;
219
                ErrorCode = 16;
216
        }
220
        }
217
        else if(FC.Error[1] &  FC_ERROR1_I2C)
221
        else if(FC.Error[1] &  FC_ERROR1_I2C)
218
        {
222
        {
219
                LED_RED_ON;
223
                LED_RED_ON;
220
                sprintf(ErrorMSG,"ERR: FC I2C");
224
                sprintf(ErrorMSG,"ERR: FC I2C");
221
                ErrorCode = 17;
225
                ErrorCode = 17;
222
        }
226
        }
223
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
227
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
224
        {
228
        {
225
                LED_RED_ON;
229
                LED_RED_ON;
226
                sprintf(ErrorMSG,"ERR: Bl Missing");
230
                sprintf(ErrorMSG,"ERR: Bl Missing");
227
                ErrorCode = 18;
231
                ErrorCode = 18;
228
        }
232
        }
229
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
233
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
230
        {
234
        {
231
                LED_RED_ON;
235
                LED_RED_ON;
232
                sprintf(ErrorMSG,"Mixer Error");
236
                sprintf(ErrorMSG,"Mixer Error");
233
                ErrorCode = 19;
237
                ErrorCode = 19;
234
        }
238
        }
235
        else if(CheckDelay(UBX_Timeout))
239
        else if(CheckDelay(UBX_Timeout))
236
        {
240
        {
237
                LED_RED_ON;
241
                LED_RED_ON;
238
                sprintf(ErrorMSG,"no GPS communication ");
242
                sprintf(ErrorMSG,"no GPS communication ");
239
                ErrorCode = 5;
243
                ErrorCode = 5;
240
                StopNavigation = 1;
244
                StopNavigation = 1;
241
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
245
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
242
        }
246
        }
243
        else if(Compass_Heading < 0)
247
        else if(Compass_Heading < 0)
244
        {
248
        {
245
                LED_RED_ON;
249
                LED_RED_ON;
246
                sprintf(ErrorMSG,"bad compass value ");
250
                sprintf(ErrorMSG,"bad compass value ");
247
                ErrorCode = 6;
251
                ErrorCode = 6;
248
                StopNavigation = 1;
252
                StopNavigation = 1;
249
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
253
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
250
        }
254
        }
251
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
255
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
252
        {
256
        {
253
                LED_RED_ON;
257
                LED_RED_ON;
254
                sprintf(ErrorMSG,"FC spi rx error ");
258
                sprintf(ErrorMSG,"FC spi rx error ");
255
                ErrorCode = 8;
259
                ErrorCode = 8;
256
                StopNavigation = 1;
260
                StopNavigation = 1;
257
        }
261
        }
258
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
262
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
259
        {
263
        {
260
                LED_RED_ON;
264
                LED_RED_ON;
261
                sprintf(ErrorMSG,"FC: Carefree Error");
265
                sprintf(ErrorMSG,"FC: Carefree Error");
262
                ErrorCode = 20;
266
                ErrorCode = 20;
263
        }
267
        }
264
        else if(FC.Error[1] &  FC_ERROR1_PPM)
268
        else if(FC.Error[1] &  FC_ERROR1_PPM)
265
        {
269
        {
266
                LED_RED_ON;
270
                LED_RED_ON;
267
                sprintf(ErrorMSG,"RC Signal lost ");
271
                sprintf(ErrorMSG,"RC Signal lost ");
268
                ErrorCode = 7;
272
                ErrorCode = 7;
269
        }
273
        }
270
        else // no error occured
274
        else // no error occured
271
        {
275
        {
272
                sprintf(ErrorMSG,"No Error               ");
276
                sprintf(ErrorMSG,"No Error               ");
273
                ErrorCode = 0;
277
                ErrorCode = 0;
274
                StopNavigation = 0;
278
                StopNavigation = 0;
275
                LED_RED_OFF;
279
                LED_RED_OFF;
276
        }
280
        }
277
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04;
281
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04;
278
        else DebugOut.Status[1] &= ~0x04;
282
        else DebugOut.Status[1] &= ~0x04;
279
}
283
}
280
 
284
 
281
// the handler will be cyclic called by the timer 1 ISR
285
// the handler will be cyclic called by the timer 1 ISR
282
// used is for critical timing parts that normaly would handled
286
// used is for critical timing parts that normaly would handled
283
// within the main loop that could block longer at logging activities
287
// within the main loop that could block longer at logging activities
284
void EXTIT3_IRQHandler(void)
288
void EXTIT3_IRQHandler(void)
285
{
289
{
286
        IENABLE;
290
        IENABLE;
287
 
291
 
288
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
292
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
289
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
293
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
290
        Compass_Update();               // update compass communication
294
        Compass_Update();               // update compass communication
291
        Analog_Update();                // get new ADC values
295
        Analog_Update();                // get new ADC values
292
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
296
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
293
 
297
 
294
        IDISABLE;
298
        IDISABLE;
295
}
299
}
296
 
300
 
297
//----------------------------------------------------------------------------------------------------
301
//----------------------------------------------------------------------------------------------------
298
int main(void)
302
int main(void)
299
{
303
{
300
        /* Configure the system clocks */
304
        /* Configure the system clocks */
301
        SCU_Config();
305
        SCU_Config();
302
        /* init VIC (Vectored Interrupt Controller)     */
306
        /* init VIC (Vectored Interrupt Controller)     */
303
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
307
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
304
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
308
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
305
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
309
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
306
        VIC_InitDefaultVectors();
310
        VIC_InitDefaultVectors();
307
 
311
 
308
        // initialize timer 1 for System Clock and delay rountines
312
        // initialize timer 1 for System Clock and delay rountines
309
        TIMER1_Init();
313
        TIMER1_Init();
310
        // initialize the LEDs (needs Timer 1)
314
        // initialize the LEDs (needs Timer 1)
311
        Led_Init();
315
        Led_Init();
312
        // initialize the debug UART1
316
        // initialize the debug UART1
313
        UART1_Init();
317
        UART1_Init();
314
        UART1_PutString("\r\n---------------------------------------------");
318
        UART1_PutString("\r\n---------------------------------------------");
315
        // initialize usb
319
        // initialize usb
316
        USB_ConfigInit();
320
        USB_ConfigInit();
317
        // initialize timer 2 for servo outputs
321
        // initialize timer 2 for servo outputs
318
        //TIMER2_Init();
322
        //TIMER2_Init();
319
        // initialize UART2 to FLIGHTCTRL
323
        // initialize UART2 to FLIGHTCTRL
320
        UART2_Init();
324
        UART2_Init();
321
        // initialize UART0 (to MKGPS or MK3MAG)
325
        // initialize UART0 (to MKGPS or MK3MAG)
322
        UART0_Init();
326
        UART0_Init();
323
        // initialize adc
327
        // initialize adc
324
        Analog_Init();
328
        Analog_Init();
325
        // initialize SPI0 to FC
329
        // initialize SPI0 to FC
326
        SPI0_Init();
330
        SPI0_Init();
327
        // initialize i2c bus (needs Timer 1)
331
        // initialize i2c bus (needs Timer 1)
328
        I2C1_Init();
332
        I2C1_Init();
329
        // initialize fat16 partition on sd card (needs Timer 1)
333
        // initialize fat16 partition on sd card (needs Timer 1)
330
        Fat16_Init();
334
        Fat16_Init();
331
        // initialize NC params
335
        // initialize NC params
332
        NCParams_Init();
336
        NCParams_Init();
333
        // initialize the settings
337
        // initialize the settings
334
        Settings_Init();
338
        Settings_Init();
335
        // initialize logging (needs settings)
339
        // initialize logging (needs settings)
336
        Logging_Init();
340
        Logging_Init();
337
 
341
 
338
        TimerCheckError = SetDelay(3000);
342
        TimerCheckError = SetDelay(3000);
339
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
343
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
340
        UART1_PutString("\n\r Version information:");
344
        UART1_PutString("\n\r Version information:");
341
 
345
 
342
        GetNaviCtrlVersion();
346
        GetNaviCtrlVersion();
343
        DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag
347
        DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag
344
        DebugOut.Status[1] = 0x00;
348
        DebugOut.Status[1] = 0x00;
345
 
349
 
346
        Compass_Init();
350
        Compass_Init();
347
 
351
 
348
#ifdef FOLLOW_ME
352
#ifdef FOLLOW_ME
349
        TransmitAlsoToFC = 1;
353
        TransmitAlsoToFC = 1;
350
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
354
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
351
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
355
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
352
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
356
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
353
        TransmitAlsoToFC = 0;
357
        TransmitAlsoToFC = 0;
354
#else
358
#else
355
        SPI0_GetFlightCtrlVersion();
359
        SPI0_GetFlightCtrlVersion();
356
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
360
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
357
        {
361
        {
358
                UART1_PutString("\n\r Flight-Ctrl not compatible");
362
                UART1_PutString("\n\r Flight-Ctrl not compatible");
359
                LED_RED_ON;
363
                LED_RED_ON;
360
        }
364
        }
361
#endif
365
#endif
362
 
366
 
363
        GPS_Init();
367
        GPS_Init();
364
 
368
 
365
        // ---------- Prepare the isr driven
369
        // ---------- Prepare the isr driven
366
        // set to absolute lowest priority
370
        // set to absolute lowest priority
367
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
371
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
368
        // enable interrupts
372
        // enable interrupts
369
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
373
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
370
 
374
 
371
        Debug_OK("START");
375
        Debug_OK("START");
372
 
376
 
373
        for (;;) // the endless main loop
377
        for (;;) // the endless main loop
374
        {
378
        {
375
                UART0_ProcessRxData();  // process request
379
                UART0_ProcessRxData();  // process request
376
                UART1_ProcessRxData();  // process request
380
                UART1_ProcessRxData();  // process request
377
                USB_ProcessRxData();    // process request
381
                USB_ProcessRxData();    // process request
378
                UART0_TransmitTxData(); // send answer
382
                UART0_TransmitTxData(); // send answer
379
                UART1_TransmitTxData(); // send answer
383
                UART1_TransmitTxData(); // send answer
380
                UART2_TransmitTxData(); // send answer
384
                UART2_TransmitTxData(); // send answer
381
                USB_TransmitTxData();   // send answer
385
                USB_TransmitTxData();   // send answer
382
                SPI0_UpdateBuffer();    // handle new SPI Data
386
                SPI0_UpdateBuffer();    // handle new SPI Data
383
                // ---------------- Error Check Timing ----------------------------
387
                // ---------------- Error Check Timing ----------------------------
384
                if(CheckDelay(TimerCheckError))
388
                if(CheckDelay(TimerCheckError))
385
                {
389
                {
386
                        TimerCheckError = SetDelay(1000);
390
                        TimerCheckError = SetDelay(1000);
387
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
391
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
388
                        CheckErrors();
392
                        CheckErrors();
389
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
393
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
390
//                      else NaviData.FlyingTime = 0; // not the time per flight
394
//                      else NaviData.FlyingTime = 0; // not the time per flight
391
                        if(SerialLinkOkay) SerialLinkOkay--;
395
                        if(SerialLinkOkay) SerialLinkOkay--;
392
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
396
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
393
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
397
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
394
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50)) BeepTime = 1000;
398
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50)) BeepTime = 1000;
395
                }
399
                }
396
                // ---------------- Logging  ---------------------------------------
400
                // ---------------- Logging  ---------------------------------------
397
                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
401
                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
398
        }
402
        }
399
}
403
}
400
 
404
 
401
 
405
 
402
 
406