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
// + Copyright (c) Holger Buss, Ingo Busker
3
// + Nur für den privaten Gebrauch
4
// + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Verkauf von Luftbildaufnahmen, usw.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
15
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
18
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
19
// + eindeutig als Ursprung verlinkt werden
20
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
22
// + Benutzung auf eigene Gefahr
23
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
24
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
26
// + mit unserer Zustimmung zulässig
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
31
// + this list of conditions and the following disclaimer.
32
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
33
// +     from this software without specific prior written permission.
34
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
35
// +     for non-commercial use (directly or indirectly)
36
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
37
// +     with our written permission
38
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
39
// +     clearly linked as origin
40
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
41
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
 
53
 
54
#ifndef EEMEM
55
#define EEMEM __attribute__ ((section (".eeprom")))
56
#endif
57
 
58
 
59
#include <avr/eeprom.h>
60
#include <string.h>
61
#include "eeprom.h"
62
#include "uart.h"
63
#include "led.h"
64
#include "main.h"
65
#include "fc.h"
1657 killagreg 66
#include "twimaster.h"
1622 killagreg 67
 
68
paramset_t              EE_Parameter;
69
MixerTable_t    Mixer;
1654 killagreg 70
uint8_t RequiredMotors;
1622 killagreg 71
 
72
 
73
uint8_t RAM_Checksum(uint8_t* pBuffer, uint16_t len)
395 hbuss 74
{
1622 killagreg 75
        uint8_t crc = 0xAA;
76
        uint16_t i;
77
 
78
        for(i=0; i<len; i++)
79
        {
80
                crc += pBuffer[i];
81
        }
82
        return crc;
993 hbuss 83
}
84
 
1622 killagreg 85
uint8_t EEProm_Checksum(uint16_t EEAddr, uint16_t len)
993 hbuss 86
{
1622 killagreg 87
        uint8_t crc = 0xAA;
88
        uint16_t off;
89
 
90
        for(off=0; off<len; off++)
91
        {
92
                crc += eeprom_read_byte((uint8_t*)(EEAddr + off));;
93
        }
94
        return crc;
395 hbuss 95
}
1622 killagreg 96
 
97
void ParamSet_DefaultStickMapping(void)
395 hbuss 98
{
1622 killagreg 99
        EE_Parameter.Kanalbelegung[K_GAS]   = 1;
100
        EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
101
        EE_Parameter.Kanalbelegung[K_NICK]  = 3;
102
        EE_Parameter.Kanalbelegung[K_GIER]  = 4;
103
        EE_Parameter.Kanalbelegung[K_POTI1] = 5;
104
        EE_Parameter.Kanalbelegung[K_POTI2] = 6;
105
        EE_Parameter.Kanalbelegung[K_POTI3] = 7;
106
        EE_Parameter.Kanalbelegung[K_POTI4] = 8;
107
        EE_Parameter.Kanalbelegung[K_POTI5] = 9;
108
        EE_Parameter.Kanalbelegung[K_POTI6] = 10;
109
        EE_Parameter.Kanalbelegung[K_POTI7] = 11;
110
        EE_Parameter.Kanalbelegung[K_POTI8] = 12;
395 hbuss 111
}
112
 
1622 killagreg 113
 
114
/***************************************************/
115
/*    Default Values for parameter set 1           */
116
/***************************************************/
1760 holgerb 117
void CommonDefaults(void)
395 hbuss 118
{
1622 killagreg 119
        EE_Parameter.Revision = EEPARAM_REVISION;
120
 
121
        if(PlatinenVersion >= 20)
122
        {
1676 holgerb 123
                EE_Parameter.Gyro_D = 10;
1622 killagreg 124
                EE_Parameter.Driftkomp = 0;
125
                EE_Parameter.GyroAccFaktor = 27;
126
                EE_Parameter.WinkelUmschlagNick = 78;
127
                EE_Parameter.WinkelUmschlagRoll = 78;
128
        }
129
        else
130
        {
131
                EE_Parameter.Gyro_D = 3;
132
                EE_Parameter.Driftkomp = 32;
133
                EE_Parameter.GyroAccFaktor = 30;
134
                EE_Parameter.WinkelUmschlagNick = 85;
135
                EE_Parameter.WinkelUmschlagRoll = 85;
136
        }
1957 holgerb 137
        EE_Parameter.GyroAccAbgleich = 32;        // 1/k
2008 holgerb 138
        EE_Parameter.BitConfig = 0;              // Looping usw.
1762 holgerb 139
        EE_Parameter.GlobalConfig = CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER;
1916 holgerb 140
        EE_Parameter.ExtraConfig = CFG_GPS_AID | CFG2_VARIO_BEEP;
2028 holgerb 141
        EE_Parameter.GlobalConfig3 = CFG3_VARIO_FAILSAFE;
1850 holgerb 142
        EE_Parameter.Receiver = RECEIVER_JETI;
1762 holgerb 143
        EE_Parameter.MotorSafetySwitch = 0;
1769 holgerb 144
        EE_Parameter.ExternalControl = 0;
1760 holgerb 145
 
1762 holgerb 146
        EE_Parameter.Gas_Min = 8;             // Wert : 0-32
147
        EE_Parameter.Gas_Max = 230;           // Wert : 33-247
1839 holgerb 148
        EE_Parameter.KompassWirkung = 64;    // Wert : 0-247
1762 holgerb 149
 
150
        EE_Parameter.Hoehe_MinGas = 30;
151
        EE_Parameter.MaxHoehe     = 255;         // Wert : 0-247   255 -> Poti1
152
        EE_Parameter.Hoehe_P      = 15;          // Wert : 0-32
153
        EE_Parameter.Luftdruck_D  = 30;          // Wert : 0-247
154
        EE_Parameter.Hoehe_ACC_Wirkung = 0;     // Wert : 0-247
155
        EE_Parameter.Hoehe_HoverBand = 8;         // Wert : 0-247
156
        EE_Parameter.Hoehe_GPS_Z = 64;           // Wert : 0-247
157
        EE_Parameter.Hoehe_StickNeutralPoint = 0;// Wert : 0-247 (0 = Hover-Estimation)
1942 holgerb 158
        EE_Parameter.Hoehe_Verstaerkung = 15;    // Wert : 0-50 (15 -> ca. +/- 5m/sek bei Stick-Voll-Ausschlag)
1762 holgerb 159
 
1622 killagreg 160
        EE_Parameter.UserParam1 =   0;           // zur freien Verwendung
161
        EE_Parameter.UserParam2 =   0;           // zur freien Verwendung
162
        EE_Parameter.UserParam3 =   0;           // zur freien Verwendung
163
        EE_Parameter.UserParam4 =   0;           // zur freien Verwendung
164
        EE_Parameter.UserParam5 =   0;           // zur freien Verwendung
165
        EE_Parameter.UserParam6 =   0;           // zur freien Verwendung
166
        EE_Parameter.UserParam7 = 0;             // zur freien Verwendung
167
        EE_Parameter.UserParam8 = 0;             // zur freien Verwendung
1762 holgerb 168
 
1848 holgerb 169
        EE_Parameter.ServoNickControl = 128;     // Wert : 0-247     // Stellung des Servos
170
        EE_Parameter.ServoNickComp = 50;         // Wert : 0-247     // Einfluss Gyro/Servo
1771 holgerb 171
        EE_Parameter.ServoCompInvert = 2;        // Wert : 0-247     // Richtung Einfluss Gyro/Servo
1787 holgerb 172
        EE_Parameter.ServoNickMin = 15;          // Wert : 0-247     // Anschlag
1848 holgerb 173
        EE_Parameter.ServoNickMax = 230;         // Wert : 0-247     // Anschlag
174
        EE_Parameter.ServoNickRefresh = 4;
1622 killagreg 175
        EE_Parameter.Servo3 = 125;
176
        EE_Parameter.Servo4 = 125;
177
        EE_Parameter.Servo5 = 125;
1848 holgerb 178
        EE_Parameter.ServoRollControl = 128;     // Wert : 0-247     // Stellung des Servos
179
        EE_Parameter.ServoRollComp = 85;         // Wert : 0-247     // Einfluss Gyro/Servo
1876 holgerb 180
        EE_Parameter.ServoRollMin = 70;          // Wert : 0-247     // Anschlag
1848 holgerb 181
        EE_Parameter.ServoRollMax = 220;         // Wert : 0-247     // Anschlag
182
        EE_Parameter.ServoManualControlSpeed = 60;
1876 holgerb 183
        EE_Parameter.CamOrientation = 0;         // Wert : 0-24 -> 0-360 -> 15° steps
1762 holgerb 184
 
1622 killagreg 185
        EE_Parameter.J16Bitmask = 95;
186
        EE_Parameter.J17Bitmask = 243;
187
        EE_Parameter.WARN_J16_Bitmask = 0xAA;
188
        EE_Parameter.WARN_J17_Bitmask = 0xAA;
1760 holgerb 189
        EE_Parameter.J16Timing = 20;
190
        EE_Parameter.J17Timing = 20;
1762 holgerb 191
 
192
        EE_Parameter.LoopGasLimit = 50;
193
        EE_Parameter.LoopThreshold = 90;         // Wert: 0-247  Schwelle für Stickausschlag
194
        EE_Parameter.LoopHysterese = 50;
195
 
1622 killagreg 196
        EE_Parameter.NaviGpsModeControl = 254; // 254 -> Poti 2
197
        EE_Parameter.NaviGpsGain = 100;
198
        EE_Parameter.NaviGpsP = 90;
199
        EE_Parameter.NaviGpsI = 90;
200
        EE_Parameter.NaviGpsD = 90;
201
        EE_Parameter.NaviGpsPLimit = 75;
1931 holgerb 202
        EE_Parameter.NaviGpsILimit = 85;
1622 killagreg 203
        EE_Parameter.NaviGpsDLimit = 75;
204
        EE_Parameter.NaviGpsACC = 0;
205
        EE_Parameter.NaviGpsMinSat = 6;
206
        EE_Parameter.NaviStickThreshold = 8;
207
        EE_Parameter.NaviWindCorrection = 90;
1936 holgerb 208
        EE_Parameter.NaviAccCompensation = 42;
1857 holgerb 209
        EE_Parameter.NaviOperatingRadius = 245;
1931 holgerb 210
        EE_Parameter.NaviAngleLimitation = 140;
1936 holgerb 211
        EE_Parameter.NaviPH_LoginTime = 5;
1668 holgerb 212
        EE_Parameter.OrientationAngle = 0;
1921 holgerb 213
        EE_Parameter.CareFreeModeControl = 0;
1760 holgerb 214
        EE_Parameter.UnterspannungsWarnung = 33; // Wert : 0-247 ( Automatische Zellenerkennung bei < 50)
2028 holgerb 215
        EE_Parameter.NotGas = 80;                // Wert : 0-247     // Gaswert bei Empangsverlust (in Prozent)
1760 holgerb 216
        EE_Parameter.NotGasZeit = 90;            // Wert : 0-247     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
1915 holgerb 217
        EE_Parameter.MotorSmooth = 0;          
218
        EE_Parameter.ComingHomeAltitude = 0;      // 0 = don't change 
1916 holgerb 219
        EE_Parameter.FailSafeTime = 0;            // 0 = off
1936 holgerb 220
        EE_Parameter.MaxAltitude = 150;           // 0 = off
1944 holgerb 221
        EE_Parameter.AchsKopplung1 = 90;
222
        EE_Parameter.AchsKopplung2 = 55;
2008 holgerb 223
        EE_Parameter.FailsafeChannel = 0;
2012 holgerb 224
        EE_Parameter.ServoFilterNick = 0;
225
        EE_Parameter.ServoFilterRoll = 0;
1760 holgerb 226
}
1944 holgerb 227
/*
1760 holgerb 228
void ParamSet_DefaultSet1(void) // sport
229
{
230
        CommonDefaults();
231
        EE_Parameter.Stick_P = 14;            // Wert : 1-20
232
        EE_Parameter.Stick_D = 16;            // Wert : 0-20
1944 holgerb 233
        EE_Parameter.StickGier_P = 12;             // Wert : 1-20
1760 holgerb 234
        EE_Parameter.Gyro_P = 80;             // Wert : 0-247
235
        EE_Parameter.Gyro_I = 150;            // Wert : 0-247
236
        EE_Parameter.Gyro_Gier_P = 80;        // Wert : 0-247
237
        EE_Parameter.Gyro_Gier_I = 150;       // Wert : 0-247
238
        EE_Parameter.Gyro_Stability = 6;          // Wert : 1-8
239
        EE_Parameter.I_Faktor = 32;
240
        EE_Parameter.CouplingYawCorrection = 1;
241
        EE_Parameter.GyroAccAbgleich = 16;        // 1/k;
242
        EE_Parameter.DynamicStability = 100;
1622 killagreg 243
        memcpy(EE_Parameter.Name, "Sport\0", 12);
244
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
395 hbuss 245
}
1944 holgerb 246
*/
1622 killagreg 247
 
248
/***************************************************/
1944 holgerb 249
/*    Default Values for parameter set 1           */
1622 killagreg 250
/***************************************************/
1944 holgerb 251
void ParamSet_DefaultSet1(void) // normal
1622 killagreg 252
{
1760 holgerb 253
        CommonDefaults();
1664 holgerb 254
        EE_Parameter.Stick_P = 10;               // Wert : 1-20
255
        EE_Parameter.Stick_D = 16;               // Wert : 0-20
1944 holgerb 256
        EE_Parameter.StickGier_P = 6;                 // Wert : 1-20
1622 killagreg 257
        EE_Parameter.Gyro_P = 90;                // Wert : 0-247
258
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
259
        EE_Parameter.Gyro_Gier_P = 90;           // Wert : 0-247
260
        EE_Parameter.Gyro_Gier_I = 120;          // Wert : 0-247
1685 holgerb 261
        EE_Parameter.Gyro_Stability = 6;                  // Wert : 1-8
1622 killagreg 262
        EE_Parameter.I_Faktor = 32;
263
        EE_Parameter.CouplingYawCorrection = 60;
264
        EE_Parameter.DynamicStability = 75;
1944 holgerb 265
        memcpy(EE_Parameter.Name, "Fast\0", 12);
1622 killagreg 266
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
267
}
268
 
269
 
270
/***************************************************/
1944 holgerb 271
/*    Default Values for parameter set 2           */
1622 killagreg 272
/***************************************************/
1944 holgerb 273
void ParamSet_DefaultSet2(void) // beginner
1622 killagreg 274
{
1762 holgerb 275
        CommonDefaults();
1664 holgerb 276
        EE_Parameter.Stick_P = 8;                // 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 = 100;               // Wert : 0-247
280
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
281
        EE_Parameter.Gyro_Gier_P = 100;          // 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 = 16;
285
        EE_Parameter.CouplingYawCorrection = 70;
1690 holgerb 286
        EE_Parameter.DynamicStability = 70;
1944 holgerb 287
        memcpy(EE_Parameter.Name, "Normal\0", 12);
1622 killagreg 288
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
289
}
290
 
291
/***************************************************/
1944 holgerb 292
/*    Default Values for parameter set 3           */
293
/***************************************************/
294
void ParamSet_DefaultSet3(void) // beginner
295
{
296
        CommonDefaults();
297
        EE_Parameter.Stick_P = 6;                // Wert : 1-20
298
        EE_Parameter.Stick_D = 10;               // Wert : 0-20
299
        EE_Parameter.StickGier_P  = 4;           // Wert : 1-20
300
        EE_Parameter.Gyro_P = 100;               // Wert : 0-247
301
        EE_Parameter.Gyro_I = 120;               // Wert : 0-247
302
        EE_Parameter.Gyro_Gier_P = 100;          // Wert : 0-247
303
        EE_Parameter.Gyro_Gier_I = 120;          // Wert : 0-247
304
        EE_Parameter.Gyro_Stability = 6;              // Wert : 1-8
305
        EE_Parameter.I_Faktor = 16;
306
        EE_Parameter.CouplingYawCorrection = 70;
307
        EE_Parameter.DynamicStability = 70;
308
        memcpy(EE_Parameter.Name, "Easy\0", 12);
309
        EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
310
}
311
 
312
 
313
/***************************************************/
1622 killagreg 314
/*       Read Parameter from EEPROM as byte        */
315
/***************************************************/
316
uint8_t GetParamByte(uint16_t param_id)
317
{
318
        return eeprom_read_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + param_id));
319
}
320
 
321
/***************************************************/
322
/*       Write Parameter to EEPROM as byte         */
323
/***************************************************/
324
void SetParamByte(uint16_t param_id, uint8_t value)
325
{
326
        eeprom_write_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + param_id), value);
327
}
328
 
329
/***************************************************/
330
/*       Read Parameter from EEPROM as word        */
331
/***************************************************/
332
uint16_t GetParamWord(uint16_t param_id)
333
{
334
        return eeprom_read_word((uint16_t *)(EEPROM_ADR_PARAM_BEGIN + param_id));
335
}
336
 
337
/***************************************************/
338
/*       Write Parameter to EEPROM as word         */
339
/***************************************************/
340
void SetParamWord(uint16_t param_id, uint16_t value)
341
{
342
        eeprom_write_word((uint16_t*)(EEPROM_ADR_PARAM_BEGIN + param_id), value);
343
}
344
 
345
/***************************************************/
346
/*       Read Parameter Set from EEPROM            */
347
/***************************************************/
348
// number [1..5]
349
uint8_t ParamSet_ReadFromEEProm(uint8_t setnumber)
350
{
351
        uint8_t crc;
352
        uint16_t eeaddr;
353
 
354
        // range the setnumber
355
        if((1 > setnumber) || (setnumber > 5)) setnumber = 3;
356
 
357
        // calculate eeprom addr
358
        eeaddr = EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1);
359
 
360
        // calculate checksum from eeprom
361
        crc = EEProm_Checksum(eeaddr, PARAMSET_STRUCT_LEN - 1);
362
 
363
        // check crc
364
        if(crc != eeprom_read_byte((uint8_t*)(eeaddr + PARAMSET_STRUCT_LEN - 1))) return 0;
365
 
366
        // check revision
367
        if(eeprom_read_byte((uint8_t*)(eeaddr)) != EEPARAM_REVISION) return 0;
368
 
369
        // read paramset from eeprom
370
        eeprom_read_block((void *) &EE_Parameter, (void*)(EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1)), PARAMSET_STRUCT_LEN);
371
        LED_Init();
1925 holgerb 372
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1921 holgerb 373
        LIBFC_HoTT_Clear();
1925 holgerb 374
#endif
1622 killagreg 375
        return 1;
376
}
377
 
378
/***************************************************/
379
/*        Write Parameter Set to EEPROM            */
380
/***************************************************/
381
// number [1..5]
382
uint8_t ParamSet_WriteToEEProm(uint8_t setnumber)
383
{
384
        uint8_t crc;
385
 
386
        if(EE_Parameter.Revision == EEPARAM_REVISION) // write only the right revision to eeprom
387
        {
388
                if(setnumber > 5) setnumber = 5;
389
                if(setnumber < 1) return 0;
1916 holgerb 390
        LIBFC_CheckSettings();
2028 holgerb 391
                if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE)         // check the Setting: Not more than 100% emergency gas
392
                        {
393
                         if(EE_Parameter.NotGas > 99) EE_Parameter.NotGas = 80; // i.e. 80% of Hovergas
394
                        }
1622 killagreg 395
                // update checksum
396
                EE_Parameter.crc = RAM_Checksum((uint8_t*)(&EE_Parameter), sizeof(EE_Parameter)-1);
397
 
398
                // write paramset to eeprom
399
                eeprom_write_block((void *) &EE_Parameter, (void*)(EEPROM_ADR_PARAMSET + PARAMSET_STRUCT_LEN * (setnumber - 1)), PARAMSET_STRUCT_LEN);
400
 
401
                // backup channel settings to separate block in eeprom
402
                eeprom_write_block( (void*)(EE_Parameter.Kanalbelegung), (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
403
 
404
                // write crc of channel block to eeprom
405
                crc = RAM_Checksum((uint8_t*)(EE_Parameter.Kanalbelegung), sizeof(EE_Parameter.Kanalbelegung));
406
                eeprom_write_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung)), crc);
407
 
408
                // update active settings number
409
                SetActiveParamSet(setnumber);
410
                LED_Init();
1925 holgerb 411
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1921 holgerb 412
                LIBFC_HoTT_Clear();
1925 holgerb 413
#endif
1622 killagreg 414
                return 1;
415
        }
416
        // wrong revision
417
        return 0;
418
}
419
 
420
/***************************************************/
421
/*          Read MixerTable from EEPROM            */
422
/***************************************************/
423
uint8_t MixerTable_ReadFromEEProm(void)
424
{
425
        uint8_t crc;
426
 
427
        // calculate checksum in eeprom
428
        crc = EEProm_Checksum(EEPROM_ADR_MIXERTABLE, sizeof(Mixer) - 1);
429
 
430
        // check crc
431
        if( crc != eeprom_read_byte((uint8_t*)(EEPROM_ADR_MIXERTABLE + sizeof(Mixer) - 1)) ) return 0;
432
 
433
        // check revision
434
        if(eeprom_read_byte((uint8_t*)(EEPROM_ADR_MIXERTABLE)) != EEMIXER_REVISION) return 0;
435
 
436
        // read mixer table
437
        eeprom_read_block((void *) &Mixer, (void*)(EEPROM_ADR_MIXERTABLE), sizeof(Mixer));
438
        return 1;
439
}
440
 
441
/***************************************************/
442
/*          Write Mixer Table to EEPROM            */
443
/***************************************************/
444
uint8_t MixerTable_WriteToEEProm(void)
445
{
446
        if(Mixer.Revision == EEMIXER_REVISION)
447
        {
448
                // update crc
449
                Mixer.crc = RAM_Checksum((uint8_t*)(&Mixer), sizeof(Mixer) - 1);
450
 
451
                // write to eeprom
452
                eeprom_write_block((void *) &Mixer, (void*)(EEPROM_ADR_MIXERTABLE), sizeof(Mixer));
453
                return 1;
454
        }
455
        else return 0;
456
}
457
 
458
/***************************************************/
459
/*    Default Values for Mixer Table               */
460
/***************************************************/
461
void MixerTable_Default(void) // Quadro
462
{
463
        uint8_t i;
464
 
465
        Mixer.Revision = EEMIXER_REVISION;
466
        // clear mixer table
467
        for(i = 0; i < 16; i++)
468
        {
469
                Mixer.Motor[i][MIX_GAS]  = 0;
470
                Mixer.Motor[i][MIX_NICK] = 0;
471
                Mixer.Motor[i][MIX_ROLL] = 0;
472
                Mixer.Motor[i][MIX_YAW]  = 0;
473
        }
474
        // default = Quadro
475
        Mixer.Motor[0][MIX_GAS] = 64; Mixer.Motor[0][MIX_NICK] = +64; Mixer.Motor[0][MIX_ROLL] =   0; Mixer.Motor[0][MIX_YAW] = +64;
476
        Mixer.Motor[1][MIX_GAS] = 64; Mixer.Motor[1][MIX_NICK] = -64; Mixer.Motor[1][MIX_ROLL] =   0; Mixer.Motor[1][MIX_YAW] = +64;
477
        Mixer.Motor[2][MIX_GAS] = 64; Mixer.Motor[2][MIX_NICK] =   0; Mixer.Motor[2][MIX_ROLL] = -64; Mixer.Motor[2][MIX_YAW] = -64;
478
        Mixer.Motor[3][MIX_GAS] = 64; Mixer.Motor[3][MIX_NICK] =   0; Mixer.Motor[3][MIX_ROLL] = +64; Mixer.Motor[3][MIX_YAW] = -64;
479
        memcpy(Mixer.Name, "Quadro\0", 7);
2008 holgerb 480
        Mixer.crc = RAM_Checksum((uint8_t*)(&Mixer), sizeof(Mixer) - 1);
1622 killagreg 481
}
482
 
483
/***************************************************/
484
/*       Get active parameter set                  */
485
/***************************************************/
486
uint8_t GetActiveParamSet(void)
487
{
488
        uint8_t setnumber;
489
        setnumber = eeprom_read_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + PID_ACTIVE_SET));
490
        if(setnumber > 5)
491
        {
492
                setnumber = 3;
493
                eeprom_write_byte((void*)(EEPROM_ADR_PARAM_BEGIN+PID_ACTIVE_SET), setnumber);
494
        }
495
        return(setnumber);
496
}
497
 
498
/***************************************************/
499
/*       Set active parameter set                  */
500
/***************************************************/
501
void SetActiveParamSet(uint8_t setnumber)
502
{
503
        if(setnumber > 5) setnumber = 5;
504
        if(setnumber < 1) setnumber = 1;
505
        eeprom_write_byte((uint8_t*)(EEPROM_ADR_PARAM_BEGIN + PID_ACTIVE_SET), setnumber);
506
}
507
 
508
/***************************************************/
1761 killagreg 509
/*       Set default parameter set                 */
510
/***************************************************/
511
void SetDefaultParameter(uint8_t set, uint8_t restore_channels)
512
{
513
 
514
        if(set > 5) set = 5;
515
        else if(set < 1) set = 1;
516
 
517
        switch(set)
518
        {
519
                case 1:
520
                        ParamSet_DefaultSet1(); // Fill ParamSet Structure to default parameter set 1 (Sport)
521
                        break;
522
                case 2:
523
                        ParamSet_DefaultSet2(); // Kamera
524
                        break;
525
                case 3:
526
                        ParamSet_DefaultSet3(); // Beginner
527
                        break;
528
                default:
529
                        ParamSet_DefaultSet3(); // Beginner
530
                        break;
531
        }
532
        if(restore_channels)
533
        {
534
                uint8_t crc;
535
                // 1st check for a valid channel backup in eeprom
536
                crc = EEProm_Checksum(EEPROM_ADR_CHANNELS, sizeof(EE_Parameter.Kanalbelegung));
537
                if(crc == eeprom_read_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung))) )
538
                {
539
                        eeprom_read_block((void *)EE_Parameter.Kanalbelegung, (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
540
                }
541
                else ParamSet_DefaultStickMapping();
542
        }
543
        else ParamSet_DefaultStickMapping();
544
        ParamSet_WriteToEEProm(set);
545
}
546
 
547
/***************************************************/
1622 killagreg 548
/*       Initialize EEPROM Parameter Sets          */
549
/***************************************************/
550
void ParamSet_Init(void)
551
{
552
        uint8_t channel_backup  = 0, bad_params = 0, ee_default = 0,i;
553
 
554
 
555
        if(EEPARAM_REVISION != GetParamByte(PID_EE_REVISION) )
556
        {
557
                ee_default = 1; // software update or forced by mktool
558
        }
559
 
560
 
561
        // 1st check for a valid channel backup in eeprom
562
        i = EEProm_Checksum(EEPROM_ADR_CHANNELS, sizeof(EE_Parameter.Kanalbelegung));
563
        if(i == eeprom_read_byte((uint8_t*)(EEPROM_ADR_CHANNELS + sizeof(EE_Parameter.Kanalbelegung))) ) channel_backup = 1;
564
 
565
 
566
        // parameter check
567
 
568
        // check all 5 parameter settings
569
        for (i = 1;i < 6; i++)
570
        {
571
                if(ee_default || !ParamSet_ReadFromEEProm(i)) // could not read paramset from eeprom
572
                {
573
                        bad_params = 1;
574
                        printf("\n\rGenerating default Parameter Set %d",i);
575
                        switch(i)
576
                        {
577
                                case 1:
578
                                        ParamSet_DefaultSet1(); // Fill ParamSet Structure to default parameter set 1 (Sport)
579
                                        break;
580
                                case 2:
581
                                        ParamSet_DefaultSet2(); // Kamera
582
                                        break;
583
                                case 3:
584
                                        ParamSet_DefaultSet3(); // Beginner
585
                                        break;
586
                                default:
1668 holgerb 587
                                        ParamSet_DefaultSet3(); // Kamera
1622 killagreg 588
                                        break;
589
                        }
590
                        if(channel_backup) // if we have an channel mapping backup in eeprom
591
                        {       // restore it from eeprom
592
                                eeprom_read_block((void *)EE_Parameter.Kanalbelegung, (void*)(EEPROM_ADR_CHANNELS), sizeof(EE_Parameter.Kanalbelegung));
593
                        }
594
                        else
595
                        {       // use default mapping
596
                                ParamSet_DefaultStickMapping();
597
                        }
598
                        ParamSet_WriteToEEProm(i);
599
                }
600
        }
601
        if(bad_params) // at least one of the parameter settings were invalid
602
        {
603
                // default-Setting is parameter set 3
604
                SetActiveParamSet(3);
605
        }
606
 
607
 
608
        // read active parameter set to ParamSet stucture
609
        i = GetActiveParamSet();
610
        ParamSet_ReadFromEEProm(i);
611
        printf("\n\rUsing Parameter Set %d", i);
612
 
613
        // load mixer table
1931 holgerb 614
        if(GetParamByte(PID_EE_REVISION) == 0xff || !MixerTable_ReadFromEEProm() )
1622 killagreg 615
        {
616
                printf("\n\rGenerating default Mixer Table");
617
                MixerTable_Default(); // Quadro
618
                MixerTable_WriteToEEProm();
619
        }
1931 holgerb 620
        if(ee_default)  SetParamByte(PID_EE_REVISION, EEPARAM_REVISION);
1622 killagreg 621
        // determine motornumber
622
        RequiredMotors = 0;
623
        for(i = 0; i < 16; i++)
624
        {
625
                if(Mixer.Motor[i][MIX_GAS] > 0) RequiredMotors++;
626
        }
627
 
628
        printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name, RequiredMotors);
629
        printf("\n\r==============================");
630
}