Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1622 killagreg 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + www.MikroKopter.com
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2050 holgerb 4
// + Software Nutzungsbedingungen (english version: see below)
5
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
6
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
7
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
8
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
9
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
1622 killagreg 10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2050 holgerb 11
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
12
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
13
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
14
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
15
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
16
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
17
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
18
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
19
// + des Mitverschuldens offen.
20
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
21
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
22
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
23
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
24
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
25
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
26
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
27
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
1622 killagreg 28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2050 holgerb 29
// + Software LICENSING TERMS
1622 killagreg 30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2050 holgerb 31
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
32
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
33
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
34
// + The Software may only be used with the Licensor's products.
35
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
36
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
37
// + agreement shall be the property of the Licensor.
38
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
39
// + features that can be used to identify the program may not be altered or defaced by the customer.
40
// + The customer shall be responsible for taking reasonable precautions
41
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
42
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
43
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
44
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
45
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
46
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
47
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
48
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
49
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
50
// + #### END OF LICENSING TERMS ####
51
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
1622 killagreg 52
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
 
54
 
55
#ifndef EEMEM
56
#define EEMEM __attribute__ ((section (".eeprom")))
57
#endif
58
 
59
 
60
#include <avr/eeprom.h>
61
#include <string.h>
62
#include "eeprom.h"
63
#include "uart.h"
64
#include "led.h"
65
#include "main.h"
66
#include "fc.h"
1657 killagreg 67
#include "twimaster.h"
1622 killagreg 68
 
69
paramset_t              EE_Parameter;
70
MixerTable_t    Mixer;
1654 killagreg 71
uint8_t RequiredMotors;
1622 killagreg 72
 
73
 
74
uint8_t RAM_Checksum(uint8_t* pBuffer, uint16_t len)
395 hbuss 75
{
1622 killagreg 76
        uint8_t crc = 0xAA;
77
        uint16_t i;
78
 
79
        for(i=0; i<len; i++)
80
        {
81
                crc += pBuffer[i];
82
        }
83
        return crc;
993 hbuss 84
}
85
 
1622 killagreg 86
uint8_t EEProm_Checksum(uint16_t EEAddr, uint16_t len)
993 hbuss 87
{
1622 killagreg 88
        uint8_t crc = 0xAA;
89
        uint16_t off;
90
 
91
        for(off=0; off<len; off++)
92
        {
93
                crc += eeprom_read_byte((uint8_t*)(EEAddr + off));;
94
        }
95
        return crc;
395 hbuss 96
}
1622 killagreg 97
 
98
void ParamSet_DefaultStickMapping(void)
395 hbuss 99
{
1622 killagreg 100
        EE_Parameter.Kanalbelegung[K_GAS]   = 1;
101
        EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
102
        EE_Parameter.Kanalbelegung[K_NICK]  = 3;
103
        EE_Parameter.Kanalbelegung[K_GIER]  = 4;
104
        EE_Parameter.Kanalbelegung[K_POTI1] = 5;
105
        EE_Parameter.Kanalbelegung[K_POTI2] = 6;
106
        EE_Parameter.Kanalbelegung[K_POTI3] = 7;
107
        EE_Parameter.Kanalbelegung[K_POTI4] = 8;
108
        EE_Parameter.Kanalbelegung[K_POTI5] = 9;
109
        EE_Parameter.Kanalbelegung[K_POTI6] = 10;
110
        EE_Parameter.Kanalbelegung[K_POTI7] = 11;
111
        EE_Parameter.Kanalbelegung[K_POTI8] = 12;
395 hbuss 112
}
113
 
1622 killagreg 114
 
115
/***************************************************/
116
/*    Default Values for parameter set 1           */
117
/***************************************************/
1760 holgerb 118
void CommonDefaults(void)
395 hbuss 119
{
1622 killagreg 120
        EE_Parameter.Revision = EEPARAM_REVISION;
2265 holgerb 121
        memset(EE_Parameter.Name,0,12); // delete name
1622 killagreg 122
        if(PlatinenVersion >= 20)
123
        {
1676 holgerb 124
                EE_Parameter.Gyro_D = 10;
1622 killagreg 125
                EE_Parameter.Driftkomp = 0;
126
                EE_Parameter.GyroAccFaktor = 27;
127
                EE_Parameter.WinkelUmschlagNick = 78;
128
                EE_Parameter.WinkelUmschlagRoll = 78;
129
        }
130
        else
131
        {
132
                EE_Parameter.Gyro_D = 3;
133
                EE_Parameter.Driftkomp = 32;
134
                EE_Parameter.GyroAccFaktor = 30;
135
                EE_Parameter.WinkelUmschlagNick = 85;
136
                EE_Parameter.WinkelUmschlagRoll = 85;
137
        }
1957 holgerb 138
        EE_Parameter.GyroAccAbgleich = 32;        // 1/k
2008 holgerb 139
        EE_Parameter.BitConfig = 0;              // Looping usw.
1762 holgerb 140
        EE_Parameter.GlobalConfig = CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER;
2373 holgerb 141
        EE_Parameter.ExtraConfig = CFG_GPS_AID | CFG2_VARIO_BEEP | CFG_LEARNABLE_CAREFREE | CFG_NO_RCOFF_BEEPING;
142
        EE_Parameter.GlobalConfig3 = CFG3_SPEAK_ALL | CFG3_NO_GPSFIX_NO_START;//
2183 holgerb 143
        EE_Parameter.Receiver = RECEIVER_HOTT;
1762 holgerb 144
        EE_Parameter.MotorSafetySwitch = 0;
1769 holgerb 145
        EE_Parameter.ExternalControl = 0;
1760 holgerb 146
 
1762 holgerb 147
        EE_Parameter.Gas_Min = 8;             // Wert : 0-32
148
        EE_Parameter.Gas_Max = 230;           // Wert : 33-247
1839 holgerb 149
        EE_Parameter.KompassWirkung = 64;    // Wert : 0-247
1762 holgerb 150
 
2390 holgerb 151
        EE_Parameter.HoeheChannel = 5;         // Wert : 0-32
1762 holgerb 152
        EE_Parameter.Hoehe_MinGas = 30;
2390 holgerb 153
 
2309 holgerb 154
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
155
        if(ACC_AltitudeControl)
156
        {
157
                EE_Parameter.Hoehe_P      = 20;          // Wert : 0-32
158
                EE_Parameter.Luftdruck_D  = 40;          // Wert : 0-247
2316 holgerb 159
                EE_Parameter.Hoehe_ACC_Wirkung = 30;     // Wert : 0-247
2309 holgerb 160
                EE_Parameter.Hoehe_HoverBand = 1;         // Wert : 0-247
161
                EE_Parameter.Hoehe_GPS_Z = 0;           // Wert : 0-247
2337 holgerb 162
                EE_Parameter.Hoehe_StickNeutralPoint = 127;// Wert : 0-247 (0 = Hover-Estimation)
2342 holgerb 163
                EE_Parameter.FailSafeTime = 30;                   // 0 = off
2309 holgerb 164
        }
165
        else
166
#endif
167
        {
168
                EE_Parameter.Hoehe_P      = 15;          // Wert : 0-32
169
                EE_Parameter.Luftdruck_D  = 30;          // Wert : 0-247
170
                EE_Parameter.Hoehe_ACC_Wirkung = 0;     // Wert : 0-247
171
                EE_Parameter.Hoehe_HoverBand = 8;         // Wert : 0-247
172
                EE_Parameter.Hoehe_GPS_Z = 20;           // Wert : 0-247
2337 holgerb 173
                EE_Parameter.Hoehe_StickNeutralPoint = 0;// Wert : 0-247 (0 = Hover-Estimation)
2342 holgerb 174
                EE_Parameter.FailSafeTime = 0;            // 0 = off
2309 holgerb 175
        }
176
 
1942 holgerb 177
        EE_Parameter.Hoehe_Verstaerkung = 15;    // Wert : 0-50 (15 -> ca. +/- 5m/sek bei Stick-Voll-Ausschlag)
2340 holgerb 178
        EE_Parameter.StartLandChannel = 0;  
179
        EE_Parameter.LandingSpeed = 12;  
1762 holgerb 180
 
1622 killagreg 181
        EE_Parameter.UserParam1 =   0;           // zur freien Verwendung
182
        EE_Parameter.UserParam2 =   0;           // zur freien Verwendung
183
        EE_Parameter.UserParam3 =   0;           // zur freien Verwendung
184
        EE_Parameter.UserParam4 =   0;           // zur freien Verwendung
185
        EE_Parameter.UserParam5 =   0;           // zur freien Verwendung
186
        EE_Parameter.UserParam6 =   0;           // zur freien Verwendung
187
        EE_Parameter.UserParam7 = 0;             // zur freien Verwendung
188
        EE_Parameter.UserParam8 = 0;             // zur freien Verwendung
1762 holgerb 189
 
1848 holgerb 190
        EE_Parameter.ServoNickControl = 128;     // Wert : 0-247     // Stellung des Servos
191
        EE_Parameter.ServoNickComp = 50;         // Wert : 0-247     // Einfluss Gyro/Servo
1771 holgerb 192
        EE_Parameter.ServoCompInvert = 2;        // Wert : 0-247     // Richtung Einfluss Gyro/Servo
2349 holgerb 193
        EE_Parameter.ServoNickMin = 24;          // Wert : 0-247     // Anschlag
1848 holgerb 194
        EE_Parameter.ServoNickMax = 230;         // Wert : 0-247     // Anschlag
2373 holgerb 195
        EE_Parameter.ServoNickRefresh = 3;
1622 killagreg 196
        EE_Parameter.Servo3 = 125;
197
        EE_Parameter.Servo4 = 125;
198
        EE_Parameter.Servo5 = 125;
1848 holgerb 199
        EE_Parameter.ServoRollControl = 128;     // Wert : 0-247     // Stellung des Servos
200
        EE_Parameter.ServoRollComp = 85;         // Wert : 0-247     // Einfluss Gyro/Servo
1876 holgerb 201
        EE_Parameter.ServoRollMin = 70;          // Wert : 0-247     // Anschlag
1848 holgerb 202
        EE_Parameter.ServoRollMax = 220;         // Wert : 0-247     // Anschlag
203
        EE_Parameter.ServoManualControlSpeed = 60;
1876 holgerb 204
        EE_Parameter.CamOrientation = 0;         // Wert : 0-24 -> 0-360 -> 15° steps
1762 holgerb 205
 
2373 holgerb 206
        EE_Parameter.J16Bitmask = 0xAA;
207
        EE_Parameter.J17Bitmask = 0xCC;
208
        EE_Parameter.WARN_J16_Bitmask = 0x00;
1622 killagreg 209
        EE_Parameter.WARN_J17_Bitmask = 0xAA;
2040 holgerb 210
        EE_Parameter.J16Timing = 40;
211
        EE_Parameter.J17Timing = 40;
2183 holgerb 212
    EE_Parameter.NaviOut1Parameter = 0;       // Photo release in meter
1762 holgerb 213
        EE_Parameter.LoopGasLimit = 50;
214
        EE_Parameter.LoopThreshold = 90;         // Wert: 0-247  Schwelle für Stickausschlag
215
        EE_Parameter.LoopHysterese = 50;
216
 
2342 holgerb 217
        EE_Parameter.NaviGpsModeChannel = 6; // Kanal 6
1622 killagreg 218
        EE_Parameter.NaviGpsGain = 100;
2344 holgerb 219
        EE_Parameter.NaviGpsP =  100;
220
        EE_Parameter.NaviGpsI =   90;
221
        EE_Parameter.NaviGpsD =  120;
222
        EE_Parameter.NaviGpsA =   40;
1622 killagreg 223
        EE_Parameter.NaviGpsPLimit = 75;
1931 holgerb 224
        EE_Parameter.NaviGpsILimit = 85;
1622 killagreg 225
        EE_Parameter.NaviGpsDLimit = 75;
226
        EE_Parameter.NaviGpsMinSat = 6;
227
        EE_Parameter.NaviStickThreshold = 8;
2348 holgerb 228
        EE_Parameter.NaviWindCorrection = 50;
1936 holgerb 229
        EE_Parameter.NaviAccCompensation = 42;
1857 holgerb 230
        EE_Parameter.NaviOperatingRadius = 245;
1931 holgerb 231
        EE_Parameter.NaviAngleLimitation = 140;
2348 holgerb 232
        EE_Parameter.NaviPH_LoginTime = 2;
1668 holgerb 233
        EE_Parameter.OrientationAngle = 0;
2342 holgerb 234
        EE_Parameter.CareFreeChannel = 0;
1760 holgerb 235
        EE_Parameter.UnterspannungsWarnung = 33; // Wert : 0-247 ( Automatische Zellenerkennung bei < 50)
2061 holgerb 236
        EE_Parameter.NotGas = 65;                // Wert : 0-247     // Gaswert bei Empangsverlust (ggf. in Prozent)
1760 holgerb 237
        EE_Parameter.NotGasZeit = 90;            // Wert : 0-247     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
1915 holgerb 238
        EE_Parameter.MotorSmooth = 0;          
239
        EE_Parameter.ComingHomeAltitude = 0;      // 0 = don't change 
1936 holgerb 240
        EE_Parameter.MaxAltitude = 150;           // 0 = off
2275 holgerb 241
        EE_Parameter.AchsKopplung1 = 125;
242
        EE_Parameter.AchsKopplung2 = 52;
2008 holgerb 243
        EE_Parameter.FailsafeChannel = 0;
2012 holgerb 244
        EE_Parameter.ServoFilterNick = 0;
245
        EE_Parameter.ServoFilterRoll = 0;
2390 holgerb 246
        EE_Parameter.CompassOffset = 0;
2402 holgerb 247
        EE_Parameter.AutoLandingVoltage = 31;
1760 holgerb 248
}
1944 holgerb 249
/*
1760 holgerb 250
void ParamSet_DefaultSet1(void) // sport
251
{
252
        CommonDefaults();
253
        EE_Parameter.Stick_P = 14;            // Wert : 1-20
254
        EE_Parameter.Stick_D = 16;            // Wert : 0-20
1944 holgerb 255
        EE_Parameter.StickGier_P = 12;             // Wert : 1-20
1760 holgerb 256
        EE_Parameter.Gyro_P = 80;             // Wert : 0-247
257
        EE_Parameter.Gyro_I = 150;            // Wert : 0-247
258
        EE_Parameter.Gyro_Gier_P = 80;        // Wert : 0-247
259
        EE_Parameter.Gyro_Gier_I = 150;       // Wert : 0-247
260
        EE_Parameter.Gyro_Stability = 6;          // Wert : 1-8
261
        EE_Parameter.I_Faktor = 32;
262
        EE_Parameter.CouplingYawCorrection = 1;
263
        EE_Parameter.GyroAccAbgleich = 16;        // 1/k;
264
        EE_Parameter.DynamicStability = 100;
1622 killagreg 265
        memcpy(EE_Parameter.Name, "Sport\0", 12);
266
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
395 hbuss 267
}
1944 holgerb 268
*/
1622 killagreg 269
 
270
/***************************************************/
1944 holgerb 271
/*    Default Values for parameter set 1           */
1622 killagreg 272
/***************************************************/
1944 holgerb 273
void ParamSet_DefaultSet1(void) // normal
1622 killagreg 274
{
1760 holgerb 275
        CommonDefaults();
1664 holgerb 276
        EE_Parameter.Stick_P = 10;               // Wert : 1-20
277
        EE_Parameter.Stick_D = 16;               // Wert : 0-20
1944 holgerb 278
        EE_Parameter.StickGier_P = 6;                 // Wert : 1-20
1622 killagreg 279
        EE_Parameter.Gyro_P = 90;                // Wert : 0-247
280
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
281
        EE_Parameter.Gyro_Gier_P = 90;           // Wert : 0-247
282
        EE_Parameter.Gyro_Gier_I = 120;          // Wert : 0-247
1685 holgerb 283
        EE_Parameter.Gyro_Stability = 6;                  // Wert : 1-8
1622 killagreg 284
        EE_Parameter.I_Faktor = 32;
285
        EE_Parameter.CouplingYawCorrection = 60;
286
        EE_Parameter.DynamicStability = 75;
2265 holgerb 287
        memcpy(EE_Parameter.Name, "Fast",4);
1622 killagreg 288
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
289
}
290
 
291
 
292
/***************************************************/
1944 holgerb 293
/*    Default Values for parameter set 2           */
1622 killagreg 294
/***************************************************/
2344 holgerb 295
void ParamSet_DefaultSet2(void) // Agil
1622 killagreg 296
{
1762 holgerb 297
        CommonDefaults();
1664 holgerb 298
        EE_Parameter.Stick_P = 8;                // Wert : 1-20
299
        EE_Parameter.Stick_D = 16;               // Wert : 0-20
1944 holgerb 300
        EE_Parameter.StickGier_P  = 6;                // Wert : 1-20
1622 killagreg 301
        EE_Parameter.Gyro_P = 100;               // Wert : 0-247
302
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
303
        EE_Parameter.Gyro_Gier_P = 100;          // Wert : 0-247
304
        EE_Parameter.Gyro_Gier_I = 120;          // Wert : 0-247
1685 holgerb 305
        EE_Parameter.Gyro_Stability = 6;                  // Wert : 1-8
1622 killagreg 306
        EE_Parameter.I_Faktor = 16;
307
        EE_Parameter.CouplingYawCorrection = 70;
1690 holgerb 308
        EE_Parameter.DynamicStability = 70;
2344 holgerb 309
        memcpy(EE_Parameter.Name, "Agile",5);
1622 killagreg 310
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
311
}
312
 
313
/***************************************************/
1944 holgerb 314
/*    Default Values for parameter set 3           */
315
/***************************************************/
2344 holgerb 316
void ParamSet_DefaultSet3(void) // Easy
1944 holgerb 317
{
318
        CommonDefaults();
319
        EE_Parameter.Stick_P = 6;                // Wert : 1-20
320
        EE_Parameter.Stick_D = 10;               // Wert : 0-20
321
        EE_Parameter.StickGier_P  = 4;           // Wert : 1-20
322
        EE_Parameter.Gyro_P = 100;               // Wert : 0-247
323
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
324
        EE_Parameter.Gyro_Gier_P = 100;          // Wert : 0-247
325
        EE_Parameter.Gyro_Gier_I = 120;          // Wert : 0-247
326
        EE_Parameter.Gyro_Stability = 6;              // Wert : 1-8
327
        EE_Parameter.I_Faktor = 16;
328
        EE_Parameter.CouplingYawCorrection = 70;
329
        EE_Parameter.DynamicStability = 70;
2265 holgerb 330
        memcpy(EE_Parameter.Name, "Easy", 4);
1944 holgerb 331
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
332
}
333
 
334
 
335
/***************************************************/
1622 killagreg 336
/*       Read Parameter from EEPROM as byte        */
337
/***************************************************/
338
uint8_t GetParamByte(uint16_t param_id)
339
{
340
        return eeprom_read_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + param_id));
341
}
342
 
343
/***************************************************/
344
/*       Write Parameter to EEPROM as byte         */
345
/***************************************************/
346
void SetParamByte(uint16_t param_id, uint8_t value)
347
{
348
        eeprom_write_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + param_id), value);
349
}
350
 
351
/***************************************************/
352
/*       Read Parameter from EEPROM as word        */
353
/***************************************************/
354
uint16_t GetParamWord(uint16_t param_id)
355
{
356
        return eeprom_read_word((uint16_t *)(EEPROM_ADR_PARAM_BEGIN + param_id));
357
}
358
 
359
/***************************************************/
360
/*       Write Parameter to EEPROM as word         */
361
/***************************************************/
362
void SetParamWord(uint16_t param_id, uint16_t value)
363
{
364
        eeprom_write_word((uint16_t*)(EEPROM_ADR_PARAM_BEGIN + param_id), value);
365
}
366
 
367
/***************************************************/
368
/*       Read Parameter Set from EEPROM            */
369
/***************************************************/
370
// number [1..5]
371
uint8_t ParamSet_ReadFromEEProm(uint8_t setnumber)
372
{
373
        uint8_t crc;
374
        uint16_t eeaddr;
375
 
376
        // range the setnumber
377
        if((1 > setnumber) || (setnumber > 5)) setnumber = 3;
378
 
379
        // calculate eeprom addr
380
        eeaddr = EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1);
381
 
382
        // calculate checksum from eeprom
383
        crc = EEProm_Checksum(eeaddr, PARAMSET_STRUCT_LEN - 1);
384
 
385
        // check crc
386
        if(crc != eeprom_read_byte((uint8_t*)(eeaddr + PARAMSET_STRUCT_LEN - 1))) return 0;
387
 
388
        // check revision
389
        if(eeprom_read_byte((uint8_t*)(eeaddr)) != EEPARAM_REVISION) return 0;
390
 
391
        // read paramset from eeprom
392
        eeprom_read_block((void *) &EE_Parameter, (void*)(EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1)), PARAMSET_STRUCT_LEN);
393
        LED_Init();
1925 holgerb 394
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1921 holgerb 395
        LIBFC_HoTT_Clear();
1925 holgerb 396
#endif
1622 killagreg 397
        return 1;
398
}
399
 
400
/***************************************************/
401
/*        Write Parameter Set to EEPROM            */
402
/***************************************************/
403
// number [1..5]
404
uint8_t ParamSet_WriteToEEProm(uint8_t setnumber)
405
{
406
        uint8_t crc;
407
 
408
        if(EE_Parameter.Revision == EEPARAM_REVISION) // write only the right revision to eeprom
409
        {
410
                if(setnumber > 5) setnumber = 5;
411
                if(setnumber < 1) return 0;
1916 holgerb 412
        LIBFC_CheckSettings();
2028 holgerb 413
                if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE)         // check the Setting: Not more than 100% emergency gas
414
                        {
415
                         if(EE_Parameter.NotGas > 99) EE_Parameter.NotGas = 80; // i.e. 80% of Hovergas
416
                        }
1622 killagreg 417
                // update checksum
418
                EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
419
 
420
                // write paramset to eeprom
421
                eeprom_write_block((void *) &EE_Parameter, (void*)(EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1)), PARAMSET_STRUCT_LEN);
422
 
423
                // backup channel settings to separate block in eeprom
424
                eeprom_write_block( (void*)(EE_Parameter.Kanalbelegung), (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
425
 
426
                // write crc of channel block to eeprom
427
                crc = RAM_Checksum((uint8_t*)(EE_Parameter.Kanalbelegung), sizeof(EE_Parameter.Kanalbelegung));
428
                eeprom_write_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung)), crc);
429
 
430
                // update active settings number
431
                SetActiveParamSet(setnumber);
432
                LED_Init();
1925 holgerb 433
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1921 holgerb 434
                LIBFC_HoTT_Clear();
1925 holgerb 435
#endif
1622 killagreg 436
                return 1;
437
        }
438
        // wrong revision
439
        return 0;
440
}
441
 
442
/***************************************************/
443
/*          Read MixerTable from EEPROM            */
444
/***************************************************/
445
uint8_t MixerTable_ReadFromEEProm(void)
446
{
447
        uint8_t crc;
448
 
449
        // calculate checksum in eeprom
450
        crc = EEProm_Checksum(EEPROM_ADR_MIXERTABLE, sizeof(Mixer) - 1);
451
 
452
        // check crc
453
        if( crc != eeprom_read_byte((uint8_t*)(EEPROM_ADR_MIXERTABLE + sizeof(Mixer) - 1)) ) return 0;
454
 
455
        // check revision
456
        if(eeprom_read_byte((uint8_t*)(EEPROM_ADR_MIXERTABLE)) != EEMIXER_REVISION) return 0;
457
 
458
        // read mixer table
459
        eeprom_read_block((void *) &Mixer, (void*)(EEPROM_ADR_MIXERTABLE), sizeof(Mixer));
460
        return 1;
461
}
462
 
463
/***************************************************/
464
/*          Write Mixer Table to EEPROM            */
465
/***************************************************/
466
uint8_t MixerTable_WriteToEEProm(void)
467
{
468
        if(Mixer.Revision == EEMIXER_REVISION)
469
        {
470
                // update crc
471
                Mixer.crc = RAM_Checksum((uint8_t*)(&Mixer), sizeof(Mixer) - 1);
472
 
473
                // write to eeprom
474
                eeprom_write_block((void *) &Mixer, (void*)(EEPROM_ADR_MIXERTABLE), sizeof(Mixer));
475
                return 1;
476
        }
477
        else return 0;
478
}
479
 
480
/***************************************************/
481
/*    Default Values for Mixer Table               */
482
/***************************************************/
483
void MixerTable_Default(void) // Quadro
484
{
485
        uint8_t i;
486
 
487
        Mixer.Revision = EEMIXER_REVISION;
488
        // clear mixer table
489
        for(i = 0; i < 16; i++)
490
        {
491
                Mixer.Motor[i][MIX_GAS]  = 0;
492
                Mixer.Motor[i][MIX_NICK] = 0;
493
                Mixer.Motor[i][MIX_ROLL] = 0;
494
                Mixer.Motor[i][MIX_YAW]  = 0;
495
        }
496
        // default = Quadro
497
        Mixer.Motor[0][MIX_GAS] = 64; Mixer.Motor[0][MIX_NICK] = +64; Mixer.Motor[0][MIX_ROLL] =   0; Mixer.Motor[0][MIX_YAW] = +64;
498
        Mixer.Motor[1][MIX_GAS] = 64; Mixer.Motor[1][MIX_NICK] = -64; Mixer.Motor[1][MIX_ROLL] =   0; Mixer.Motor[1][MIX_YAW] = +64;
499
        Mixer.Motor[2][MIX_GAS] = 64; Mixer.Motor[2][MIX_NICK] =   0; Mixer.Motor[2][MIX_ROLL] = -64; Mixer.Motor[2][MIX_YAW] = -64;
500
        Mixer.Motor[3][MIX_GAS] = 64; Mixer.Motor[3][MIX_NICK] =   0; Mixer.Motor[3][MIX_ROLL] = +64; Mixer.Motor[3][MIX_YAW] = -64;
2266 holgerb 501
        memcpy(Mixer.Name, "Quadro\0\0\0\0\0\0", 12);
2008 holgerb 502
        Mixer.crc = RAM_Checksum((uint8_t*)(&Mixer), sizeof(Mixer) - 1);
1622 killagreg 503
}
504
 
505
/***************************************************/
506
/*       Get active parameter set                  */
507
/***************************************************/
508
uint8_t GetActiveParamSet(void)
509
{
510
        uint8_t setnumber;
511
        setnumber = eeprom_read_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + PID_ACTIVE_SET));
512
        if(setnumber > 5)
513
        {
514
                setnumber = 3;
515
                eeprom_write_byte((void*)(EEPROM_ADR_PARAM_BEGIN+PID_ACTIVE_SET), setnumber);
516
        }
2331 holgerb 517
        ActiveParamSet = setnumber;
1622 killagreg 518
        return(setnumber);
519
}
520
 
521
/***************************************************/
522
/*       Set active parameter set                  */
523
/***************************************************/
524
void SetActiveParamSet(uint8_t setnumber)
525
{
526
        if(setnumber > 5) setnumber = 5;
527
        if(setnumber < 1) setnumber = 1;
2331 holgerb 528
        ActiveParamSet = setnumber;
1622 killagreg 529
        eeprom_write_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + PID_ACTIVE_SET), setnumber);
530
}
531
 
532
/***************************************************/
1761 killagreg 533
/*       Set default parameter set                 */
534
/***************************************************/
535
void SetDefaultParameter(uint8_t set, uint8_t restore_channels)
536
{
537
 
538
        if(set > 5) set = 5;
539
        else if(set < 1) set = 1;
540
 
541
        switch(set)
542
        {
543
                case 1:
544
                        ParamSet_DefaultSet1(); // Fill ParamSet Structure to default parameter set 1 (Sport)
545
                        break;
546
                case 2:
547
                        ParamSet_DefaultSet2(); // Kamera
548
                        break;
549
                case 3:
550
                        ParamSet_DefaultSet3(); // Beginner
551
                        break;
552
                default:
553
                        ParamSet_DefaultSet3(); // Beginner
554
                        break;
555
        }
556
        if(restore_channels)
557
        {
558
                uint8_t crc;
559
                // 1st check for a valid channel backup in eeprom
560
                crc = EEProm_Checksum(EEPROM_ADR_CHANNELS, sizeof(EE_Parameter.Kanalbelegung));
561
                if(crc == eeprom_read_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung))) )
562
                {
563
                        eeprom_read_block((void *)EE_Parameter.Kanalbelegung, (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
564
                }
565
                else ParamSet_DefaultStickMapping();
566
        }
567
        else ParamSet_DefaultStickMapping();
568
        ParamSet_WriteToEEProm(set);
569
}
570
 
571
/***************************************************/
1622 killagreg 572
/*       Initialize EEPROM Parameter Sets          */
573
/***************************************************/
574
void ParamSet_Init(void)
575
{
576
        uint8_t channel_backup  = 0, bad_params = 0, ee_default = 0,i;
2336 holgerb 577
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
578
        if(PlatinenVersion != GetParamByte(PID_HARDWARE_VERSION))
579
         {
580
          if(PlatinenVersion == 22 && GetParamByte(PID_HARDWARE_VERSION) == 21 && !(PIND & 0x10)) SetParamByte(PID_EE_REVISION,0); // reset the Settings if the Version changed to V2.2
581
          SetParamByte(PID_HARDWARE_VERSION,PlatinenVersion); // Remember the Version number
582
          wdt_enable(WDTO_15MS); // Reset-Commando
583
          printf("\n\r--> Hardware Version Byte Changed <--");
584
          while(1);
585
         }
586
#endif
2323 holgerb 587
        if((EEPARAM_REVISION) != GetParamByte(PID_EE_REVISION))  
1622 killagreg 588
        {
589
                ee_default = 1; // software update or forced by mktool
590
        }
591
        // 1st check for a valid channel backup in eeprom
592
        i = EEProm_Checksum(EEPROM_ADR_CHANNELS, sizeof(EE_Parameter.Kanalbelegung));
2318 holgerb 593
        if(i == eeprom_read_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung)))) channel_backup = 1;
1622 killagreg 594
 
595
        // parameter check
596
 
597
        // check all 5 parameter settings
598
        for (i = 1;i < 6; i++)
599
        {
600
                if(ee_default || !ParamSet_ReadFromEEProm(i)) // could not read paramset from eeprom
601
                {
602
                        bad_params = 1;
603
                        printf("\n\rGenerating default Parameter Set %d",i);
604
                        switch(i)
605
                        {
606
                                case 1:
607
                                        ParamSet_DefaultSet1(); // Fill ParamSet Structure to default parameter set 1 (Sport)
608
                                        break;
609
                                case 2:
2344 holgerb 610
                                        ParamSet_DefaultSet2(); // Normal
1622 killagreg 611
                                        break;
612
                                default:
2344 holgerb 613
                                        ParamSet_DefaultSet3(); // Easy
1622 killagreg 614
                                        break;
615
                        }
616
                        if(channel_backup) // if we have an channel mapping backup in eeprom
617
                        {       // restore it from eeprom
618
                                eeprom_read_block((void *)EE_Parameter.Kanalbelegung, (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
619
                        }
620
                        else
621
                        {       // use default mapping
622
                                ParamSet_DefaultStickMapping();
623
                        }
624
                        ParamSet_WriteToEEProm(i);
625
                }
626
        }
627
        if(bad_params) // at least one of the parameter settings were invalid
628
        {
629
                // default-Setting is parameter set 3
630
                SetActiveParamSet(3);
631
        }
632
        // read active parameter set to ParamSet stucture
633
        i = GetActiveParamSet();
634
        ParamSet_ReadFromEEProm(i);
635
        printf("\n\rUsing Parameter Set %d", i);
636
 
637
        // load mixer table
1931 holgerb 638
        if(GetParamByte(PID_EE_REVISION) == 0xff || !MixerTable_ReadFromEEProm() )
1622 killagreg 639
        {
640
                printf("\n\rGenerating default Mixer Table");
641
                MixerTable_Default(); // Quadro
642
                MixerTable_WriteToEEProm();
643
        }
2323 holgerb 644
        if(ee_default)  SetParamByte(PID_EE_REVISION, (EEPARAM_REVISION));
1622 killagreg 645
        // determine motornumber
646
        RequiredMotors = 0;
647
        for(i = 0; i < 16; i++)
648
        {
649
                if(Mixer.Motor[i][MIX_GAS] > 0) RequiredMotors++;
650
        }
651
 
652
        printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name, RequiredMotors);
2030 holgerb 653
 PrintLine();// ("\n\r===================================");
654
 
1622 killagreg 655
}