Subversion Repositories FlightCtrl

Rev

Rev 1961 | Rev 1964 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
1623 - 3
// + Nur für den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
1963 - 7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1623 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1623 - 17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1623 - 22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1623 - 25
// + mit unserer Zustimmung zulässig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1623 - 47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1612 dongfang 49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 52
 
1612 dongfang 53
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
// + Contant Values
55
// + 0-250 -> normale Values
56
// + 251 -> Poti1
57
// + 252 -> Poti2
58
// + 253 -> Poti3
59
// + 254 -> Poti4
60
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
61
 
62
#ifndef EEMEM
63
#define EEMEM __attribute__ ((section (".eeprom")))
64
#endif
65
 
66
#include "eeprom.h"
67
#include "printf_P.h"
68
#include "output.h"
1960 - 69
#include <avr/eeprom.h>
1612 dongfang 70
 
71
// byte array in eeprom
1821 - 72
uint8_t EEPromArray[E2END + 1] EEMEM;
1612 dongfang 73
 
74
/***************************************************/
1960 - 75
/*       Read Parameter from EEPROM as byte        */
1612 dongfang 76
/***************************************************/
1960 - 77
uint8_t getParamByte(uint16_t param_id) {
78
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
1612 dongfang 79
}
80
 
81
/***************************************************/
1960 - 82
/*       Write Parameter to EEPROM as byte         */
1612 dongfang 83
/***************************************************/
1960 - 84
void setParamByte(uint16_t param_id, uint8_t value) {
85
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 86
}
87
 
88
/***************************************************/
1960 - 89
/*       Read Parameter from EEPROM as word        */
1612 dongfang 90
/***************************************************/
1960 - 91
uint16_t getParamWord(uint16_t param_id) {
92
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
93
                                                    + param_id]);
1612 dongfang 94
}
95
 
96
/***************************************************/
1960 - 97
/*       Write Parameter to EEPROM as word         */
1612 dongfang 98
/***************************************************/
1960 - 99
void setParamWord(uint16_t param_id, uint16_t value) {
100
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 101
}
102
 
1960 - 103
uint8_t calculateChecksum(uint8_t* data, uint16_t length) {
104
  uint8_t result = 0;
105
  for (uint16_t i=0; i<length; i++) {
106
    result += data[i];
107
  }
108
  return result;
1612 dongfang 109
}
110
 
1960 - 111
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t length, uint16_t offset) {
112
  uint8_t checksum = calculateChecksum(data+1, length-1);
113
  data[0] = checksum;
114
  data[1] = revisionNumber;
115
  eeprom_write_block(data, &EEPromArray[offset], length);
1612 dongfang 116
}
117
 
1960 - 118
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t length, uint16_t offset) {
119
  eeprom_read_block(target, &EEPromArray[offset], length);
120
  uint8_t checksum = calculateChecksum(target+1, length-1);
121
  return (checksum != target[0] || revisionNumber != target[1]);
1612 dongfang 122
}
123
 
124
/***************************************************/
125
/*       Read Parameter Set from EEPROM            */
126
/***************************************************/
1963 - 127
// setnumber [1..5]
1960 - 128
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
129
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*sizeof(paramset_t);
130
  output_init(); // what's that doing here??
131
  return readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, sizeof(paramset_t), offset);
1612 dongfang 132
}
133
 
134
/***************************************************/
135
/*        Write Parameter Set to EEPROM            */
136
/***************************************************/
1960 - 137
void paramSet_writeToEEProm(uint8_t setnumber) {
138
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*sizeof(paramset_t);
139
  writeChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, sizeof(paramset_t), offset);
140
  // set this parameter set to active set
141
  setActiveParamSet(setnumber);
142
  output_init(); // what's that doing here??
1612 dongfang 143
}
144
 
1960 - 145
void paramSet_readOrDefault() {
146
  uint8_t setnumber = getActiveParamSet();
147
  // parameter version  check
148
  if (setnumber<1 ||setnumber>5 || paramSet_readFromEEProm(setnumber)) {
149
    // if version check faild
150
    printf("\n\rInit Parameter in EEPROM");
151
    for (uint8_t i=6; i>0; i--) {
152
      paramSet_default(i); // Fill staticParams Structure to default parameter set 1 (Sport)
153
      paramSet_writeToEEProm(i);
154
    }
155
    // default-Setting is parameter set 3
156
    setActiveParamSet(1);
157
  }
158
 
159
  printf("\n\rUsing Parameter Set %d", getActiveParamSet());
1612 dongfang 160
}
161
 
162
/***************************************************/
1960 - 163
/* MixerTable                                      */
1612 dongfang 164
/***************************************************/
1960 - 165
uint8_t mixerMatrix_readFromEEProm(void) {
166
  return readChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&mixerMatrix, EEPROM_ADR_MIXER_TABLE, sizeof(mixerMatrix_t));
1612 dongfang 167
}
168
 
1960 - 169
void mixerMatrix_writeToEEProm(void) {
170
  writeChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&mixerMatrix, EEPROM_ADR_MIXER_TABLE, sizeof(mixerMatrix_t));
1612 dongfang 171
}
172
 
1960 - 173
void mixerMatrix_readOrDefault(void) {
174
  // load mixer table
175
  if (mixerMatrix_readFromEEProm()) {
176
    printf("\n\rGenerating default mixerMatrix");
177
    mixerMatrix_default(); // Quadro
178
    mixerMatrix_writeToEEProm();
179
  }
180
  // determine motornumber
181
  requiredMotors = 0;
182
  for (uint8_t i=0; i<MAX_MOTORS; i++) {
183
    if (mixerMatrix.motor[i][MIX_THROTTLE])
184
      requiredMotors++;
185
  }
186
 
187
  printf("\n\rMixer-Config: '%s' (%u Motors)",mixerMatrix.name, requiredMotors);
188
  printf("\n\r==============================");
189
}
190
 
1612 dongfang 191
/***************************************************/
1960 - 192
/* ChannelMap                                      */
1612 dongfang 193
/***************************************************/
1960 - 194
uint8_t channelMap_readFromEEProm(void) {
195
  return readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t));
1612 dongfang 196
}
197
 
1960 - 198
void channelMap_writeToEEProm(void) {
199
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t));
200
}
201
 
202
void channelMap_readOrDefault(void) {
203
  if (!channelMap_readFromEEProm()) {
204
    printf("\n\rGenerating default channel map");
205
    channelMap_default();
206
    channelMap_writeToEEProm();
207
  }
208
}
209
 
1961 - 210
/***************************************************/
211
/* Sensor offsets                                  */
212
/***************************************************/
213
uint8_t gyroOffset_readFromEEProm(void) {
214
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
215
}
216
 
217
void gyroOffset_writeToEEProm(void) {
218
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
219
}
220
 
1960 - 221
uint8_t accOffset_readFromEEProm(void) {
222
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
223
}
224
 
225
void accOffset_writeToEEProm(void) {
226
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
227
}
228
 
1612 dongfang 229
/***************************************************/
1960 - 230
/*       Get active parameter set                  */
1612 dongfang 231
/***************************************************/
1960 - 232
uint8_t getActiveParamSet(void) {
233
  uint8_t setnumber;
234
  setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
235
  if (setnumber > 4) {
236
    setActiveParamSet(setnumber = 0);
237
  }
238
  return setnumber;
1612 dongfang 239
}
240
 
241
/***************************************************/
1960 - 242
/*       Set active parameter set                  */
1612 dongfang 243
/***************************************************/
1960 - 244
void setActiveParamSet(uint8_t setnumber) {
245
  eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
246
}
1821 - 247