Subversion Repositories NaviCtrl

Rev

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

Rev 331 Rev 338
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
u8 ErrorGpsFixLost = 0;
92
u8 ErrorGpsFixLost = 0;
93
 
93
 
94
u8 ClearFCStatusFlags = 0;
94
u8 ClearFCStatusFlags = 0;
95
u8 StopNavigation = 0;
95
u8 StopNavigation = 0;
96
Param_t Parameter;
96
Param_t Parameter;
97
volatile FC_t FC;
97
volatile FC_t FC;
98
 
98
 
99
s8 ErrorMSG[25];
99
s8 ErrorMSG[25];
100
 
100
 
101
//----------------------------------------------------------------------------------------------------
101
//----------------------------------------------------------------------------------------------------
102
void SCU_Config(void)
102
void SCU_Config(void)
103
{
103
{
104
        /* configure PLL and set it as master clock source */
104
        /* configure PLL and set it as master clock source */
105
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
105
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
106
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
106
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
107
        #ifdef MCLK96MHZ
107
        #ifdef MCLK96MHZ
108
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
108
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
109
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
109
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
110
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
110
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
111
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
111
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
112
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
112
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
113
        #else
113
        #else
114
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
114
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
115
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
115
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
116
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
116
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
117
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
117
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
118
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
118
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
119
        #endif
119
        #endif
120
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
120
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
121
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
121
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
122
}
122
}
123
 
123
 
124
//----------------------------------------------------------------------------------------------------
124
//----------------------------------------------------------------------------------------------------
125
void GetNaviCtrlVersion(void)
125
void GetNaviCtrlVersion(void)
126
{
126
{
127
        u8 msg[25];
127
        u8 msg[25];
128
 
128
 
129
        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
        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);
130
        UART1_PutString(msg);
130
        UART1_PutString(msg);
131
}
131
}
132
 
132
 
133
//----------------------------------------------------------------------------------------------------
133
//----------------------------------------------------------------------------------------------------
134
 
134
 
135
void CheckErrors(void)
135
void CheckErrors(void)
136
{
136
{
137
    static s32 no_error_delay = 0;
137
    static s32 no_error_delay = 0;
138
        s32 newErrorCode = 0;
138
        s32 newErrorCode = 0;
139
        UART_VersionInfo.HardwareError[0] = 0;
139
        UART_VersionInfo.HardwareError[0] = 0;
140
 
140
 
141
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08;
141
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS;
142
        else DebugOut.Status[1] &= ~0x08; // MK3Mag green status
142
        else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
143
 
143
 
-
 
144
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL;
-
 
145
        else DebugOut.StatusRed &= ~AMPEL_BL; // BL-Ctrl green status
-
 
146
 
144
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02;
147
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
145
        else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status
148
        else DebugOut.StatusRed &= ~AMPEL_NC;
146
 
149
 
147
        if(CheckDelay(SPI0_Timeout))
150
        if(CheckDelay(SPI0_Timeout))
148
        {
151
        {
149
                LED_RED_ON;
152
                LED_RED_ON;
150
                sprintf(ErrorMSG,"no FC communication ");
153
                sprintf(ErrorMSG,"no FC communication ");
151
                newErrorCode = 3;
154
                newErrorCode = 3;
152
                StopNavigation = 1;
155
                StopNavigation = 1;
153
                DebugOut.Status[0] &= ~0x01; // status of FC Present
156
                DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present
154
                DebugOut.Status[0] &= ~0x02; // status of BL Present
157
                DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present
155
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
158
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
156
        }
159
        }
157
        else if(CheckDelay(I2C1_Timeout))
160
        else if(CheckDelay(I2C1_Timeout))
158
        {
161
        {
159
                LED_RED_ON;
162
                LED_RED_ON;
160
                sprintf(ErrorMSG,"no compass communication ");
163
                sprintf(ErrorMSG,"no compass communication ");
161
                //Reset I2CBus
164
                //Reset I2CBus
162
                I2C1_Deinit();
165
                I2C1_Deinit();
163
                I2C1_Init();
166
                I2C1_Init();
164
                newErrorCode = 4;
167
                newErrorCode = 4;
165
                StopNavigation = 1;
168
                StopNavigation = 1;
166
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
169
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
167
                DebugOut.Status[1] |= 0x08;
170
                DebugOut.StatusRed |= AMPEL_COMPASS;
168
        }
171
        }
169
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
172
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
170
        {
173
        {
171
                LED_RED_ON;
174
                LED_RED_ON;
172
#ifndef FOLLOW_ME
175
#ifndef FOLLOW_ME
173
                sprintf(ErrorMSG,"FC not compatible ");
176
                sprintf(ErrorMSG,"FC not compatible ");
174
#else
177
#else
175
                sprintf(ErrorMSG,"! FollowMe only ! ");
178
                sprintf(ErrorMSG,"! FollowMe only ! ");
176
#endif
179
#endif
177
                newErrorCode = 1;
180
                newErrorCode = 1;
178
                StopNavigation = 1;
181
                StopNavigation = 1;
179
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
182
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
-
 
183
                DebugOut.StatusRed |= AMPEL_NC;
180
        }
184
        }
181
 
185
 
182
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
186
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
183
        {
187
        {
184
                LED_RED_ON;
188
                LED_RED_ON;
185
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
189
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
186
                newErrorCode = 10;
190
                newErrorCode = 10;
187
        }
191
        }
188
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
192
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
189
        {
193
        {
190
                LED_RED_ON;
194
                LED_RED_ON;
191
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
195
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
192
                newErrorCode = 11;
196
                newErrorCode = 11;
193
        }
197
        }
194
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
198
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
195
        {
199
        {
196
                LED_RED_ON;
200
                LED_RED_ON;
197
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
201
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
198
                newErrorCode = 12;
202
                newErrorCode = 12;
199
        }
203
        }
200
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
204
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
201
        {
205
        {
202
                LED_RED_ON;
206
                LED_RED_ON;
203
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
207
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
204
                newErrorCode = 13;
208
                newErrorCode = 13;
205
        }
209
        }
206
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
210
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
207
        {
211
        {
208
                LED_RED_ON;
212
                LED_RED_ON;
209
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
213
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
210
                newErrorCode = 14;
214
                newErrorCode = 14;
211
        }
215
        }
212
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
216
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
213
        {
217
        {
214
                LED_RED_ON;
218
                LED_RED_ON;
215
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
219
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
216
                newErrorCode = 15;
220
                newErrorCode = 15;
217
        }
221
        }
218
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
222
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
219
        {
223
        {
220
                LED_RED_ON;
224
                LED_RED_ON;
221
                sprintf(ErrorMSG,"ERR:Pressure sensor");
225
                sprintf(ErrorMSG,"ERR:Pressure sensor");
222
                newErrorCode = 16;
226
                newErrorCode = 16;
223
        }
227
        }
224
        else if(FC.Error[1] &  FC_ERROR1_I2C)
228
        else if(FC.Error[1] &  FC_ERROR1_I2C)
225
        {
229
        {
226
                LED_RED_ON;
230
                LED_RED_ON;
227
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
231
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
228
                newErrorCode = 17;
232
                newErrorCode = 17;
229
        }
233
        }
230
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
234
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
231
        {
235
        {
232
                LED_RED_ON;
236
                LED_RED_ON;
233
                sprintf(ErrorMSG,"ERR: Bl Missing");
237
                sprintf(ErrorMSG,"ERR: Bl Missing");
234
                newErrorCode = 18;
238
                newErrorCode = 18;
235
        }
239
        }
236
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
240
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
237
        {
241
        {
238
                LED_RED_ON;
242
                LED_RED_ON;
239
                sprintf(ErrorMSG,"Mixer Error");
243
                sprintf(ErrorMSG,"Mixer Error");
240
                newErrorCode = 19;
244
                newErrorCode = 19;
241
        }
245
        }
242
        else if(CheckDelay(UBX_Timeout))
246
        else if(CheckDelay(UBX_Timeout))
243
        {
247
        {
244
                LED_RED_ON;
248
                LED_RED_ON;
245
        if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected ");
249
        if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected ");
246
                else
250
                else
247
                 {
251
                 {
248
                  sprintf(ErrorMSG,"no GPS communication ");
252
                  sprintf(ErrorMSG,"no GPS communication ");
249
                  UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
253
                  UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
250
              newErrorCode = 5;
254
              newErrorCode = 5;
251
                 }
255
                 }
252
                StopNavigation = 1;
256
                StopNavigation = 1;
253
//              UBX_Timeout = SetDelay(500);
257
//              UBX_Timeout = SetDelay(500);
254
        }
258
        }
255
        else if(Compass_Heading < 0)
259
        else if(Compass_Heading < 0)
256
        {
260
        {
257
                LED_RED_ON;
261
                LED_RED_ON;
258
                sprintf(ErrorMSG,"bad compass value ");
262
                sprintf(ErrorMSG,"bad compass value ");
259
                newErrorCode = 6;
263
                newErrorCode = 6;
260
                StopNavigation = 1;
264
                StopNavigation = 1;
261
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
265
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
262
        }
266
        }
263
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
267
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
264
        {
268
        {
265
                LED_RED_ON;
269
                LED_RED_ON;
266
                sprintf(ErrorMSG,"FC spi rx error ");
270
                sprintf(ErrorMSG,"FC spi rx error ");
267
                newErrorCode = 8;
271
                newErrorCode = 8;
268
                StopNavigation = 1;
272
                StopNavigation = 1;
269
        }
273
        }
270
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
274
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
271
        {
275
        {
272
                LED_RED_ON;
276
                LED_RED_ON;
273
                sprintf(ErrorMSG,"FC: Carefree Error");
277
                sprintf(ErrorMSG,"FC: Carefree Error");
274
                newErrorCode = 20;
278
                newErrorCode = 20;
275
        }
279
        }
276
        else if(FC.Error[1] &  FC_ERROR1_PPM)
280
        else if(FC.Error[1] &  FC_ERROR1_PPM)
277
        {
281
        {
278
                LED_RED_ON;
282
                LED_RED_ON;
279
                sprintf(ErrorMSG,"RC Signal lost ");
283
                sprintf(ErrorMSG,"RC Signal lost ");
280
                newErrorCode = 7;
284
                newErrorCode = 7;
281
        }
285
        }
282
        else if(ErrorGpsFixLost)
286
        else if(ErrorGpsFixLost)
283
        {
287
        {
284
         LED_RED_ON;
288
         LED_RED_ON;
285
         sprintf(ErrorMSG,"GPS Fix lost    ");
289
         sprintf(ErrorMSG,"GPS Fix lost    ");
286
         newErrorCode = 21;
290
         newErrorCode = 21;
287
        }
291
        }
288
        else if(ErrorDisturbedEarthMagnetField)
292
        else if(ErrorDisturbedEarthMagnetField)
289
        {
293
        {
290
         LED_RED_ON;
294
         LED_RED_ON;
291
         sprintf(ErrorMSG,"Magnet error    ");
295
         sprintf(ErrorMSG,"Magnet error    ");
292
         newErrorCode = 22;
296
         newErrorCode = 22;
-
 
297
         DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC;
-
 
298
         UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
-
 
299
        }
-
 
300
        else if(BL_MinOfMaxPWM == 40 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
-
 
301
        {
-
 
302
         LED_RED_ON;
-
 
303
         sprintf(ErrorMSG,"ERR:Motor restart  ");
-
 
304
         newErrorCode = 23;
293
         DebugOut.Status[1] |= 0x08;
305
         DebugOut.StatusRed |= AMPEL_BL;
-
 
306
        }
-
 
307
        else if(BL_MinOfMaxPWM != 255 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
-
 
308
        {
-
 
309
         LED_RED_ON;
-
 
310
         sprintf(ErrorMSG,"ERR:BL Limitation   ");
-
 
311
         newErrorCode = 24;
-
 
312
         DebugOut.StatusRed |= AMPEL_BL;
294
        }
313
        }
295
        else // no error occured
314
        else // no error occured
296
        {
315
        {
297
                StopNavigation = 0;
316
                StopNavigation = 0;
298
                LED_RED_OFF;
317
                LED_RED_OFF;
299
                if(no_error_delay) { no_error_delay--;  }
318
                if(no_error_delay) { no_error_delay--;  }
300
                else
319
                else
301
                 {
320
                 {
302
                  sprintf(ErrorMSG,"No Error               ");
321
                  sprintf(ErrorMSG,"No Error               ");
303
                  ErrorCode = 0;
322
                  ErrorCode = 0;
304
                 }
323
                 }
305
        }
324
        }
306
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04;
-
 
307
        else DebugOut.Status[1] &= ~0x04;
-
 
308
 
325
 
309
    if(newErrorCode)
326
    if(newErrorCode)
310
         {
327
         {
311
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 5; // delay the errors if the motors are running
328
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 5; // delay the errors if the motors are running
312
          ErrorCode = newErrorCode;
329
          ErrorCode = newErrorCode;
313
         }
330
         }
314
 FC.Error[0] = 0;
331
 FC.Error[0] = 0;
315
 FC.Error[1] = 0;
332
 FC.Error[1] = 0;
316
 FC.Error[2] = 0;
333
 FC.Error[2] = 0;
317
 FC.Error[3] = 0;
334
 FC.Error[3] = 0;
318
 FC.Error[4] = 0;
335
 FC.Error[4] = 0;
319
 ErrorGpsFixLost = 0;
336
 ErrorGpsFixLost = 0;
320
}
337
}
321
 
338
 
322
// the handler will be cyclic called by the timer 1 ISR
339
// the handler will be cyclic called by the timer 1 ISR
323
// used is for critical timing parts that normaly would handled
340
// used is for critical timing parts that normaly would handled
324
// within the main loop that could block longer at logging activities
341
// within the main loop that could block longer at logging activities
325
void EXTIT3_IRQHandler(void)
342
void EXTIT3_IRQHandler(void)
326
{
343
{
327
        IENABLE;
344
        IENABLE;
328
 
345
 
329
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
346
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
330
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
347
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
331
        Compass_Update();               // update compass communication
348
        Compass_Update();               // update compass communication
332
        Analog_Update();                // get new ADC values
349
        Analog_Update();                // get new ADC values
333
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
350
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
334
 
351
 
335
        IDISABLE;
352
        IDISABLE;
336
}
353
}
337
 
354
 
338
//----------------------------------------------------------------------------------------------------
355
//----------------------------------------------------------------------------------------------------
339
int main(void)
356
int main(void)
340
{
357
{
341
        /*
358
        /*
342
        static u32 ftimer =0;
359
        static u32 ftimer =0;
343
        static u8 fstate = 0;
360
        static u8 fstate = 0;
344
        static File_t* f = NULL;
361
        static File_t* f = NULL;
345
        */
362
        */
346
       
363
       
347
        /* Configure the system clocks */
364
        /* Configure the system clocks */
348
        SCU_Config();
365
        SCU_Config();
349
        /* init VIC (Vectored Interrupt Controller)     */
366
        /* init VIC (Vectored Interrupt Controller)     */
350
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
367
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
351
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
368
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
352
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
369
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
353
        VIC_InitDefaultVectors();
370
        VIC_InitDefaultVectors();
354
 
371
 
355
        // initialize timer 1 for System Clock and delay rountines
372
        // initialize timer 1 for System Clock and delay rountines
356
        TIMER1_Init();
373
        TIMER1_Init();
357
        // initialize the LEDs (needs Timer 1)
374
        // initialize the LEDs (needs Timer 1)
358
        Led_Init();
375
        Led_Init();
359
        // initialize the debug UART1
376
        // initialize the debug UART1
360
        UART1_Init();
377
        UART1_Init();
361
        UART1_PutString("\r\n---------------------------------------------");
378
        UART1_PutString("\r\n---------------------------------------------");
362
        // initialize usb
379
        // initialize usb
363
        USB_ConfigInit();
380
        USB_ConfigInit();
364
        // initialize timer 2 for servo outputs
381
        // initialize timer 2 for servo outputs
365
        //TIMER2_Init();
382
        //TIMER2_Init();
366
        // initialize UART2 to FLIGHTCTRL
383
        // initialize UART2 to FLIGHTCTRL
367
        UART2_Init();
384
        UART2_Init();
368
        // initialize UART0 (to MKGPS or MK3MAG)
385
        // initialize UART0 (to MKGPS or MK3MAG)
369
        UART0_Init();
386
        UART0_Init();
370
        // initialize adc
387
        // initialize adc
371
        Analog_Init();
388
        Analog_Init();
372
        // initialize SPI0 to FC
389
        // initialize SPI0 to FC
373
        SPI0_Init();
390
        SPI0_Init();
374
        // initialize i2c bus (needs Timer 1)
391
        // initialize i2c bus (needs Timer 1)
375
        I2C1_Init();
392
        I2C1_Init();
376
        // initialize fat16 partition on sd card (needs Timer 1)
393
        // initialize fat16 partition on sd card (needs Timer 1)
377
        Fat16_Init();
394
        Fat16_Init();
378
        // initialize NC params
395
        // initialize NC params
379
        NCParams_Init();
396
        NCParams_Init();
380
        // initialize the settings
397
        // initialize the settings
381
        Settings_Init();
398
        Settings_Init();
382
        // initialize logging (needs settings)
399
        // initialize logging (needs settings)
383
        Logging_Init();
400
        Logging_Init();
384
 
401
 
385
        TimerCheckError = SetDelay(3000);
402
        TimerCheckError = SetDelay(3000);
386
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
403
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
387
        UART1_PutString("\n\r Version information:");
404
        UART1_PutString("\n\r Version information:");
388
 
405
 
389
        GetNaviCtrlVersion();
406
        GetNaviCtrlVersion();
390
        DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag
407
        DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag
391
        DebugOut.Status[1] = 0x00;
408
        DebugOut.StatusRed = 0x00;
392
 
409
 
393
        Compass_Init();
410
        Compass_Init();
394
 
411
 
395
#ifdef FOLLOW_ME
412
#ifdef FOLLOW_ME
396
        TransmitAlsoToFC = 1;
413
        TransmitAlsoToFC = 1;
397
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
414
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
398
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
415
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
399
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
416
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
400
        TransmitAlsoToFC = 0;
417
        TransmitAlsoToFC = 0;
401
#else
418
#else
402
        SPI0_GetFlightCtrlVersion();
419
        SPI0_GetFlightCtrlVersion();
403
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
420
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
404
        {
421
        {
405
                UART1_PutString("\n\r Flight-Ctrl not compatible");
422
                UART1_PutString("\n\r Flight-Ctrl not compatible");
406
                LED_RED_ON;
423
                LED_RED_ON;
407
        }
424
        }
408
#endif
425
#endif
409
 
426
 
410
        GPS_Init();
427
        GPS_Init();
411
 
428
 
412
        // ---------- Prepare the isr driven
429
        // ---------- Prepare the isr driven
413
        // set to absolute lowest priority
430
        // set to absolute lowest priority
414
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
431
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
415
        // enable interrupts
432
        // enable interrupts
416
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
433
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
417
 
434
 
418
        Debug_OK("START");
435
        Debug_OK("START");
419
 
436
 
420
        for (;;) // the endless main loop
437
        for (;;) // the endless main loop
421
        {
438
        {
422
                UART0_ProcessRxData();  // process request
439
                UART0_ProcessRxData();  // process request
423
                UART1_ProcessRxData();  // process request
440
                UART1_ProcessRxData();  // process request
424
                USB_ProcessRxData();    // process request
441
                USB_ProcessRxData();    // process request
425
                UART0_TransmitTxData(); // send answer
442
                UART0_TransmitTxData(); // send answer
426
                UART1_TransmitTxData(); // send answer
443
                UART1_TransmitTxData(); // send answer
427
                UART2_TransmitTxData(); // send answer
444
                UART2_TransmitTxData(); // send answer
428
                USB_TransmitTxData();   // send answer
445
                USB_TransmitTxData();   // send answer
429
                SPI0_UpdateBuffer();    // handle new SPI Data
446
                SPI0_UpdateBuffer();    // handle new SPI Data
430
 
447
 
431
                // ---------------- Error Check Timing ----------------------------
448
                // ---------------- Error Check Timing ----------------------------
432
                if(CheckDelay(TimerCheckError))
449
                if(CheckDelay(TimerCheckError))
433
                {
450
                {
434
                        TimerCheckError = SetDelay(1000);
451
                        TimerCheckError = SetDelay(1000);
435
                        if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
452
                        if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
436
       
453
       
437
                        if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
454
                        if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
438
               
455
               
439
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
456
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
440
//                      else NaviData.FlyingTime = 0; // not the time per flight
457
//                      else NaviData.FlyingTime = 0; // not the time per flight
441
                        if(SerialLinkOkay) SerialLinkOkay--;
458
                        if(SerialLinkOkay) SerialLinkOkay--;
442
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
459
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
443
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
460
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
444
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
461
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
445
                }
462
                }
446
 
463
 
447
                // ---------------- Logging  ---------------------------------------
464
                // ---------------- Logging  ---------------------------------------
448
                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
465
                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
449
 
466
 
450
                /*
467
                /*
451
                // test
468
                // test
452
                if(CheckDelay(ftimer))
469
                if(CheckDelay(ftimer))
453
                {
470
                {
454
 
471
 
455
                        s8* filename = "test.txt";
472
                        s8* filename = "test.txt";
456
 
473
 
457
                        ftimer = SetDelay(100);
474
                        ftimer = SetDelay(100);
458
                        if(FC.Poti[3]>100 && fstate == 0)
475
                        if(FC.Poti[3]>100 && fstate == 0)
459
                        {
476
                        {
460
                                fstate = 1;
477
                                fstate = 1;
461
                        }
478
                        }
462
                        else if(FC.Poti[3]<100 && fstate == 2)
479
                        else if(FC.Poti[3]<100 && fstate == 2)
463
                        {
480
                        {
464
                                fstate = 3;
481
                                fstate = 3;
465
                        }
482
                        }
466
 
483
 
467
                        switch(fstate)
484
                        switch(fstate)
468
                        {
485
                        {
469
                                case 1:
486
                                case 1:
470
                                        sprintf(text,"\r\nStart writing file: %s", filename);
487
                                        sprintf(text,"\r\nStart writing file: %s", filename);
471
                                        UART1_PutString(text);
488
                                        UART1_PutString(text);
472
                                        f = fopen_(filename, 'a');
489
                                        f = fopen_(filename, 'a');
473
                                        if(f== NULL) Fat16_Init();
490
                                        if(f== NULL) Fat16_Init();
474
                                        fstate = 2;
491
                                        fstate = 2;
475
                                        break;
492
                                        break;
476
 
493
 
477
                                case 2:
494
                                case 2:
478
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
495
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
479
                                        break;
496
                                        break;
480
                                       
497
                                       
481
                                case 3:
498
                                case 3:
482
                                        sprintf(text,"\r\nClosing file: %s", filename);
499
                                        sprintf(text,"\r\nClosing file: %s", filename);
483
                                        UART1_PutString(text);
500
                                        UART1_PutString(text);
484
                                        fclose_(f);
501
                                        fclose_(f);
485
                                        fstate = 0;
502
                                        fstate = 0;
486
                                        break;
503
                                        break;
487
 
504
 
488
                                default:
505
                                default:
489
                                        break;
506
                                        break;
490
                        }
507
                        }
491
                }
508
                }
492
                */
509
                */
493
        }
510
        }
494
}
511
}
495
 
512
 
496
 
513
 
497
 
514