Subversion Repositories NaviCtrl

Rev

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

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