Subversion Repositories NaviCtrl

Rev

Rev 199 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 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.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
41 ingob 32
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
1 ingob 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 34
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 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
37
// +     from this software without specific prior written permission.
41 ingob 38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
1 ingob 39
// +     for non-commercial use (directly or indirectly)
41 ingob 40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 41
// +     with our written permission
41 ingob 42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
43
// +     clearly linked as origin
171 ingob 44
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 45
//
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
48
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
41 ingob 56
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 57
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 58
//#define MCLK96MHZ
1 ingob 59
const unsigned long _Main_Crystal = 25000;
41 ingob 60
#include <stdio.h>
61
#include "91x_lib.h"
62
#include "led.h"
63
#include "uart0.h"
64
#include "uart1.h"
65
#include "uart2.h"
66
#include "GPS.h"
67
#include "i2c.h"
119 killagreg 68
#include "timer1.h"
69
#include "timer2.h"
134 killagreg 70
#include "analog.h"
41 ingob 71
#include "spi_slave.h"
72
#include "fat16.h"
73
#include "usb.h"
74
#include "sdc.h"
82 killagreg 75
#include "logging.h"
156 killagreg 76
#include "params.h"
88 killagreg 77
#include "settings.h"
154 killagreg 78
#include "config.h"
1 ingob 79
#include "main.h"
80
 
41 ingob 81
u32 TimerCheckError;
82
u32 ErrorCode = 0;
83
u16 BeepTime;
92 killagreg 84
u8  NCFlags = 0;
153 killagreg 85
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
1 ingob 86
 
199 killagreg 87
u8 ClearFCFlags = 0;
41 ingob 88
u8 StopNavigation = 0;
89
Param_t Parameter;
78 holgerb 90
volatile FC_t FC;
1 ingob 91
 
41 ingob 92
s8 ErrorMSG[25];
93
 
1 ingob 94
//----------------------------------------------------------------------------------------------------
41 ingob 95
void SCU_Config(void)
1 ingob 96
{
41 ingob 97
        /* configure PLL and set it as master clock source */
98
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
99
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
100
        #ifdef MCLK96MHZ
101
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
102
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
103
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
104
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
105
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
106
        #else
107
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
108
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
109
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
110
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
111
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
112
        #endif
113
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
114
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
115
}
1 ingob 116
 
41 ingob 117
//----------------------------------------------------------------------------------------------------
118
void GetNaviCtrlVersion(void)
1 ingob 119
{
41 ingob 120
        u8 msg[25];
1 ingob 121
 
41 ingob 122
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
110 killagreg 123
        UART1_PutString(msg);
1 ingob 124
}
125
 
126
//----------------------------------------------------------------------------------------------------
41 ingob 127
void CheckErrors(void)
128
{
146 killagreg 129
        if(CheckDelay(SPI0_Timeout))
41 ingob 130
        {
131
                LED_RED_ON;
132
                sprintf(ErrorMSG,"no FC communication ");
133
                ErrorCode = 3;
134
                StopNavigation = 1;
135
        }
146 killagreg 136
        else if(CheckDelay(I2C1_Timeout))
41 ingob 137
        {
138
                LED_RED_ON;
139
                sprintf(ErrorMSG,"no MK3Mag communication ");
149 killagreg 140
                //Reset I2CBus
141
                I2C1_Deinit();
142
                I2C1_Init();
41 ingob 143
                ErrorCode = 4;
144
                StopNavigation = 1;
145
        }
146
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
147
        {
148
                LED_RED_ON;
149
                sprintf(ErrorMSG,"FC not compatible ");
150
                ErrorCode = 1;
151
                StopNavigation = 1;
152
        }
153
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
154
        {
155
                sprintf(ErrorMSG,"MK3Mag not compatible ");
156
                LED_RED_ON;
157
                ErrorCode = 2;
158
                StopNavigation = 1;
159
        }
146 killagreg 160
        else if(CheckDelay(UBX_Timeout))
41 ingob 161
        {
162
                LED_RED_ON;
163
                sprintf(ErrorMSG,"no GPS communication ");
164
                ErrorCode = 5;
165
                StopNavigation = 1;
166
        }
167
        else if(I2C_Heading.Heading < 0)
168
        {
169
                LED_RED_ON;
170
                sprintf(ErrorMSG,"bad compass value ");
171
                ErrorCode = 6;
172
                StopNavigation = 1;
173
        }
199 killagreg 174
        else if(FC.Flags & FCFLAG_SPI_RX_ERR)
175
        {
176
                LED_RED_ON;
177
                sprintf(ErrorMSG,"FC spi rx error ");
178
                ErrorCode = 8;
179
                StopNavigation = 1;    
180
        }
41 ingob 181
        else if(FC.RC_Quality < 100)
182
        {
183
                LED_RED_ON;
184
                sprintf(ErrorMSG,"RC Signal lost ");
185
                ErrorCode = 7;
186
        }
187
        else // no error occured
188
        {
189
                sprintf(ErrorMSG,"No Error               ");
190
                ErrorCode = 0;
191
                StopNavigation = 0;
192
                LED_RED_OFF;
193
        }
194
}
24 StephanB 195
 
154 killagreg 196
// the handler will be cyclic called by the timer 1 ISR
197
// used is for critical timing parts that normaly would handled 
198
// within the main loop that could block longer at logging activities
199
void EXTIT3_IRQHandler(void)
200
{
195 killagreg 201
        IENABLE;
202
 
154 killagreg 203
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
195 killagreg 204
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
155 killagreg 205
        I2C1_UpdateCompass();   // update compass communication 
154 killagreg 206
        Analog_Update();                // get new ADC values
195 killagreg 207
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
154 killagreg 208
 
195 killagreg 209
        IDISABLE;
154 killagreg 210
}
211
 
41 ingob 212
//----------------------------------------------------------------------------------------------------
213
int main(void)
214
{
215
        /* Configure the system clocks */
216
        SCU_Config();
217
        /* init VIC (Vectored Interrupt Controller)     */
218
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
219
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
220
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
196 killagreg 221
        VIC_InitDefaultVectors();
222
 
119 killagreg 223
        // initialize timer 1 for System Clock and delay rountines
41 ingob 224
        TIMER1_Init();
225
        // initialize the LEDs (needs Timer 1)
226
        Led_Init();
227
        // initialize the debug UART1
228
        UART1_Init();
110 killagreg 229
        UART1_PutString("\r\n---------------------------------------------");
120 killagreg 230
        // initialize timer 2 for servo outputs
180 killagreg 231
        //TIMER2_Init();
41 ingob 232
        // initialize UART2 to FLIGHTCTRL
233
        UART2_Init();
234
        // initialize UART0 (to MKGPS or MK3MAG)
235
        UART0_Init();
134 killagreg 236
        // initialize adc
237
        Analog_Init();
41 ingob 238
        // initialize usb
141 killagreg 239
        //USB_ConfigInit();
41 ingob 240
        // initialize SPI0 to FC
241
        SPI0_Init();
242
        // initialize i2c bus to MK3MAG (needs Timer 1)
243
        I2C1_Init();
244
        // initialize the gps position controller (needs Timer 1)
245
        Fat16_Init();
156 killagreg 246
        // initialize NC params
247
        NCParams_Init();
88 killagreg 248
        // initialize the settings
249
        Settings_Init();
92 killagreg 250
        // initialize logging (needs settings)
82 killagreg 251
        Logging_Init();
1 ingob 252
 
41 ingob 253
        TimerCheckError = SetDelay(3000);
175 holgerb 254
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
110 killagreg 255
        UART1_PutString("\n\r Version information:");
116 killagreg 256
 
41 ingob 257
        GetNaviCtrlVersion();
149 killagreg 258
 
150 killagreg 259
        I2C1_GetMK3MagVersion();
260
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
41 ingob 261
        {
150 killagreg 262
                UART1_PutString("\n\r MK3Mag not compatible");
41 ingob 263
                LED_RED_ON;
264
        }
78 holgerb 265
 
150 killagreg 266
        SPI0_GetFlightCtrlVersion();
267
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
41 ingob 268
        {
150 killagreg 269
                UART1_PutString("\n\r Flight-Ctrl not compatible");
41 ingob 270
                LED_RED_ON;
271
        }
1 ingob 272
 
116 killagreg 273
        UART0_GetMKOSDVersion();
274
 
175 holgerb 275
        GPS_Init();
276
        // initialize fat16 partition on sd card (needs Timer 1)
154 killagreg 277
 
278
        // ---------- Prepare the isr driven 
279
        // set to absolute lowest priority
280
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
195 killagreg 281
        // enable interrupts
154 killagreg 282
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
24 StephanB 283
 
154 killagreg 284
        for (;;) // the endless main loop
41 ingob 285
        {
155 killagreg 286
                UART0_ProcessRxData();  // process request
287
                UART1_ProcessRxData();  // process request
288
                USB_ProcessRxData();    // process request
154 killagreg 289
                UART0_TransmitTxData(); // send answer
146 killagreg 290
                UART1_TransmitTxData(); // send answer
190 killagreg 291
                UART2_TransmitTxData(); // send answer
112 killagreg 292
                USB_TransmitTxData();   // send answer
293
 
155 killagreg 294
                SPI0_UpdateBuffer();    // handle new SPI Data
41 ingob 295
                // ---------------- Error Check Timing ----------------------------
296
                if(CheckDelay(TimerCheckError))
1 ingob 297
                {
114 killagreg 298
                        TimerCheckError = SetDelay(1000);
146 killagreg 299
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(); // process the GPS data even if the FC is not connected
41 ingob 300
                        CheckErrors();
199 killagreg 301
                        if(FC.Flags & FCFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
146 killagreg 302
//                      else NaviData.FlyingTime = 0; // not the time per flight
110 killagreg 303
                        UART1_Request_SendFollowMe = TRUE;
92 killagreg 304
                        if(SerialLinkOkay) SerialLinkOkay--;
305
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
306
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
1 ingob 307
                }
82 killagreg 308
                // ---------------- Logging  ---------------------------------------
146 killagreg 309
                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
24 StephanB 310
        }
1 ingob 311
}
312
 
24 StephanB 313