Subversion Repositories NaviCtrl

Rev

Rev 120 | Rev 138 | Go to most recent revision | 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
6
// + Nur für den privaten Gebrauch
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 28
// + Die PORTIERUNG 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
1 ingob 44
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
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"
88 killagreg 76
#include "settings.h"
1 ingob 77
#include "main.h"
78
 
79
u32 TimerCompassUpdate;
41 ingob 80
u32 TimerCheckError;
81
u32 ErrorCode = 0;
82
u16 BeepTime;
92 killagreg 83
u8  NCFlags = 0;
1 ingob 84
 
41 ingob 85
u8 ClearMKFlags = 0;
86
u8 StopNavigation = 0;
87
Param_t Parameter;
78 holgerb 88
volatile FC_t FC;
1 ingob 89
 
41 ingob 90
s8 ErrorMSG[25];
91
 
1 ingob 92
//----------------------------------------------------------------------------------------------------
41 ingob 93
void SCU_Config(void)
1 ingob 94
{
41 ingob 95
        /* configure PLL and set it as master clock source */
96
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
97
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
98
        #ifdef MCLK96MHZ
99
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
100
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
101
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
102
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
103
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
104
        #else
105
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
106
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
107
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
108
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
109
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
110
        #endif
111
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
112
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
113
}
1 ingob 114
 
41 ingob 115
//----------------------------------------------------------------------------------------------------
116
void GetNaviCtrlVersion(void)
1 ingob 117
{
41 ingob 118
        u8 msg[25];
1 ingob 119
 
41 ingob 120
        sprintf(msg,"\n\r NaviCtrl V%d.%d%c",  VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
110 killagreg 121
        UART1_PutString(msg);
1 ingob 122
}
123
 
124
//----------------------------------------------------------------------------------------------------
41 ingob 125
void CheckErrors(void)
126
{
127
        if(!CheckSPIOkay)
128
        {
129
                LED_RED_ON;
130
                sprintf(ErrorMSG,"no FC communication ");
131
                ErrorCode = 3;
132
                StopNavigation = 1;
133
        }
134
        else if(!CheckI2COkay)
135
        {
136
                LED_RED_ON;
137
                sprintf(ErrorMSG,"no MK3Mag communication ");
138
                ErrorCode = 4;
139
                StopNavigation = 1;
140
        }
141
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
142
        {
143
                LED_RED_ON;
144
                sprintf(ErrorMSG,"FC not compatible ");
145
                ErrorCode = 1;
146
                StopNavigation = 1;
147
        }
148
        else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
149
        {
150
                sprintf(ErrorMSG,"MK3Mag not compatible ");
151
                LED_RED_ON;
152
                ErrorCode = 2;
153
                StopNavigation = 1;
154
        }
155
        else if(!CheckGPSOkay)
156
        {
157
                LED_RED_ON;
158
                sprintf(ErrorMSG,"no GPS communication ");
159
                ErrorCode = 5;
160
                StopNavigation = 1;
161
        }
162
        else if(I2C_Heading.Heading < 0)
163
        {
164
                LED_RED_ON;
165
                sprintf(ErrorMSG,"bad compass value ");
166
                ErrorCode = 6;
167
                StopNavigation = 1;
168
        }
169
        else if(FC.RC_Quality < 100)
170
        {
171
                LED_RED_ON;
172
                sprintf(ErrorMSG,"RC Signal lost ");
173
                ErrorCode = 7;
174
        }
175
        else // no error occured
176
        {
177
                sprintf(ErrorMSG,"No Error               ");
178
                ErrorCode = 0;
179
                StopNavigation = 0;
180
                LED_RED_OFF;
181
        }
182
        CheckSPIOkay = 0;
183
        CheckGPSOkay = 0;
184
        CheckI2COkay = 0;
185
}
24 StephanB 186
 
187
 
1 ingob 188
 
41 ingob 189
//----------------------------------------------------------------------------------------------------
190
int main(void)
191
{
192
        /* Configure the system clocks */
193
        SCU_Config();
194
        /* init VIC (Vectored Interrupt Controller)     */
195
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
196
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
197
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
198
        // initialize the interrupt handler
199
        Interrupt_Init();
119 killagreg 200
        // initialize timer 1 for System Clock and delay rountines
41 ingob 201
        TIMER1_Init();
202
        // initialize the LEDs (needs Timer 1)
203
        Led_Init();
204
        // initialize the debug UART1
205
        UART1_Init();
110 killagreg 206
        UART1_PutString("\r\n---------------------------------------------");
120 killagreg 207
        // initialize timer 2 for servo outputs
208
        TIMER2_Init();
41 ingob 209
        // initialize UART2 to FLIGHTCTRL
210
        UART2_Init();
211
        // initialize UART0 (to MKGPS or MK3MAG)
212
        UART0_Init();
134 killagreg 213
        // initialize adc
214
        Analog_Init();
41 ingob 215
        // initialize usb
216
        USB_ConfigInit();
217
        // initialize SPI0 to FC
218
        SPI0_Init();
219
        // initialize i2c bus to MK3MAG (needs Timer 1)
220
        I2C1_Init();
221
        // initialize the gps position controller (needs Timer 1)
222
        GPS_Init();
223
        // initialize fat16 partition on sd card (needs Timer 1)
224
        Fat16_Init();
88 killagreg 225
        // initialize the settings
226
        Settings_Init();
92 killagreg 227
        // initialize logging (needs settings)
82 killagreg 228
        Logging_Init();
1 ingob 229
 
41 ingob 230
        TimerCompassUpdate = SetDelay(5);
231
        TimerCheckError = SetDelay(3000);
110 killagreg 232
        UART1_PutString("\r\n---------------------------------------------");
233
        UART1_PutString("\n\r Version information:");
116 killagreg 234
 
41 ingob 235
        GetNaviCtrlVersion();
116 killagreg 236
 
41 ingob 237
        SPI0_GetFlightCtrlVersion();
238
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
239
        {
240
                //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE);
110 killagreg 241
                //UART1_PutString(ErrorMSG);
242
                UART1_PutString("\n\r Flight-Ctrl not compatible");
41 ingob 243
                LED_RED_ON;
244
        }
78 holgerb 245
 
41 ingob 246
        I2C1_GetMK3MagVersion();
247
        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
248
        {
110 killagreg 249
                UART1_PutString("\n\r MK3Mag not compatible");
41 ingob 250
                LED_RED_ON;
251
        }
1 ingob 252
 
116 killagreg 253
        UART0_GetMKOSDVersion();
254
 
110 killagreg 255
        UART1_PutString("\r\n---------------------------------------------\r\n\r\n");
24 StephanB 256
 
41 ingob 257
        for (;;)
258
        {
92 killagreg 259
                UART1_ProcessRxData();  // process request
41 ingob 260
                UART1_TransmitTxData();         // send answer
24 StephanB 261
 
111 killagreg 262
                UART0_ProcessRxData();  // process request
263
                UART0_TransmitTxData();         // send answer
264
 
112 killagreg 265
                USB_ProcessRxData();    // process request
266
                USB_TransmitTxData();   // send answer
267
 
41 ingob 268
                SPI0_UpdateBuffer();
269
                // ------------------------- I2C Timing --------------------------------
270
                if(I2C_State != I2C_OFF)
271
                {
272
                        if (CheckDelay(TimerCompassUpdate))
273
                        {
274
                                // check for hanging I2C bus
275
                                if(CheckDelay(I2C1_Timeout))
276
                                {       // reset I2C
110 killagreg 277
                                        UART1_PutString("\n\rCommunication-Error: MK3Mag\n\r");
41 ingob 278
                                        I2C1_Deinit();
279
                                        I2C1_Init();
280
                                }
281
                                else
282
                                {       // check for incomming compass calibration request
283
                                        // update CalByte from spi input queue
284
                                        fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
285
                                        // send new calstate
286
                                        if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
287
                                        {
288
                                                I2C1_SendCommand(I2C_CMD_WRITE_CAL);
289
                                        }
290
                                        else // request current heading
291
                                        {
292
                                                I2C1_SendCommand(I2C_CMD_READ_HEADING);
293
                                        }
294
                                        TimerCompassUpdate = SetDelay(25);    // every 25 ms
295
                                }
24 StephanB 296
 
41 ingob 297
                        }
298
                }
1 ingob 299
 
41 ingob 300
                // ---------------- Error Check Timing ----------------------------
301
                if(CheckDelay(TimerCheckError))
1 ingob 302
                {
114 killagreg 303
                        TimerCheckError = SetDelay(1000);
41 ingob 304
                        if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected
305
                        CheckErrors();
77 holgerb 306
                        if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time
307
//                      else NaviData.FlyingTime = 0;
110 killagreg 308
                        UART1_Request_SendFollowMe = TRUE;
92 killagreg 309
                        if(SerialLinkOkay) SerialLinkOkay--;
310
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
311
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
1 ingob 312
                }
82 killagreg 313
                // ---------------- Logging  ---------------------------------------
314
                Logging_Update();
24 StephanB 315
        }
1 ingob 316
}
317
 
24 StephanB 318