Subversion Repositories NaviCtrl

Rev

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

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