Subversion Repositories FlightCtrl

Rev

Rev 2039 | Rev 2059 | 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),
1968 - 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
#ifndef EEMEM
54
#define EEMEM __attribute__ ((section (".eeprom")))
55
#endif
56
 
57
#include "eeprom.h"
58
#include "printf_P.h"
59
#include "output.h"
2055 - 60
#include <avr/wdt.h>
1960 - 61
#include <avr/eeprom.h>
1612 dongfang 62
 
63
// byte array in eeprom
1821 - 64
uint8_t EEPromArray[E2END + 1] EEMEM;
1612 dongfang 65
 
66
/***************************************************/
1960 - 67
/*       Read Parameter from EEPROM as byte        */
1612 dongfang 68
/***************************************************/
1960 - 69
uint8_t getParamByte(uint16_t param_id) {
70
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
1612 dongfang 71
}
72
 
73
/***************************************************/
1960 - 74
/*       Write Parameter to EEPROM as byte         */
1612 dongfang 75
/***************************************************/
1960 - 76
void setParamByte(uint16_t param_id, uint8_t value) {
77
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 78
}
79
 
80
/***************************************************/
1960 - 81
/*       Read Parameter from EEPROM as word        */
1612 dongfang 82
/***************************************************/
1967 - 83
/*
1960 - 84
uint16_t getParamWord(uint16_t param_id) {
85
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
86
                                                    + param_id]);
1612 dongfang 87
}
1967 - 88
*/
1612 dongfang 89
 
90
/***************************************************/
1960 - 91
/*       Write Parameter to EEPROM as word         */
1612 dongfang 92
/***************************************************/
1967 - 93
/*
1960 - 94
void setParamWord(uint16_t param_id, uint16_t value) {
95
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 96
}
1967 - 97
*/
1612 dongfang 98
 
1968 - 99
uint16_t CRC16(uint8_t* data, uint16_t length) {
100
  uint16_t crc = 0;
1960 - 101
  for (uint16_t i=0; i<length; i++) {
1968 - 102
    crc  = (uint8_t)(crc >> 8) | (crc << 8);
103
    crc ^= data[i];
104
    crc ^= (uint8_t)(crc & 0xff) >> 4;
105
    crc ^= (crc << 8) << 4;
106
    crc ^= ((crc & 0xff) << 4) << 1;
1960 - 107
  }
1968 - 108
  return crc;
1612 dongfang 109
}
110
 
1964 - 111
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
112
// length is the length of the pure data not including checksum and revision number.
113
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
1968 - 114
  uint16_t CRC = CRC16(data, length);
1969 - 115
  eeprom_write_byte(&EEPromArray[offset], CRC&0xff);
116
  eeprom_write_byte(&EEPromArray[offset+1], CRC>>8);
1968 - 117
  eeprom_write_byte(&EEPromArray[offset+2], revisionNumber);
118
  eeprom_write_block(data, &EEPromArray[offset+3], length);
1612 dongfang 119
}
120
 
1964 - 121
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
122
// length is the length of the pure data not including checksum and revision number.
123
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
1969 - 124
  uint16_t CRCRead = eeprom_read_byte(&EEPromArray[offset]) | (eeprom_read_byte(&EEPromArray[offset+1])<<8);
1968 - 125
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+2]);
126
  eeprom_read_block(target, &EEPromArray[offset+3], length);
1969 - 127
  uint16_t CRCCalculated = CRC16(target, length);
1964 - 128
 
1968 - 129
  uint8_t CRCError = (CRCRead != CRCCalculated);
1964 - 130
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
131
 
1968 - 132
  if (CRCError && revisionMismatch) printf("\n\rEEPROM CRC error and revision mismatch; ");
133
  else if (CRCError) printf("\n\rEEPROM CRC error; ");
134
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch; ");
1969 - 135
  return (CRCError || revisionMismatch);
1612 dongfang 136
}
137
 
138
/***************************************************/
139
/*       Read Parameter Set from EEPROM            */
140
/***************************************************/
1963 - 141
// setnumber [1..5]
1960 - 142
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
2039 - 143
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
144
  // output_init(); // what's that doing here??
145
  return readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
1612 dongfang 146
}
147
 
148
/***************************************************/
149
/*        Write Parameter Set to EEPROM            */
150
/***************************************************/
1960 - 151
void paramSet_writeToEEProm(uint8_t setnumber) {
2039 - 152
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
153
  writeChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
1960 - 154
  // set this parameter set to active set
155
  setActiveParamSet(setnumber);
2039 - 156
  // output_init(); // what's that doing here??
1612 dongfang 157
}
158
 
1960 - 159
void paramSet_readOrDefault() {
160
  uint8_t setnumber = getActiveParamSet();
161
  // parameter version  check
162
  if (setnumber<1 ||setnumber>5 || paramSet_readFromEEProm(setnumber)) {
163
    // if version check faild
1969 - 164
    printf("\n\rwriting default parameter sets");
1964 - 165
    for (uint8_t i=5; i>0; i--) {
166
      paramSet_default(i);
1960 - 167
      paramSet_writeToEEProm(i);
168
    }
169
    // default-Setting is parameter set 3
170
    setActiveParamSet(1);
2055 - 171
    // For some strange reason, the read will have no effect.
172
    // Lets reset...
173
    wdt_enable(WDTO_250MS);
1960 - 174
  }
175
 
1964 - 176
  printf("\n\r\rUsing Parameter Set %d", getActiveParamSet());
1612 dongfang 177
}
178
 
179
/***************************************************/
1960 - 180
/* MixerTable                                      */
1612 dongfang 181
/***************************************************/
1960 - 182
void mixerMatrix_writeToEEProm(void) {
183
  writeChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&mixerMatrix, EEPROM_ADR_MIXER_TABLE, sizeof(mixerMatrix_t));
1612 dongfang 184
}
185
 
1960 - 186
void mixerMatrix_readOrDefault(void) {
187
  // load mixer table
1969 - 188
  if (readChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&mixerMatrix, EEPROM_ADR_MIXER_TABLE, sizeof(mixerMatrix_t))) {
1964 - 189
    printf("writing default mixerMatrix");
1960 - 190
    mixerMatrix_default(); // Quadro
191
    mixerMatrix_writeToEEProm();
192
  }
193
  // determine motornumber
194
  requiredMotors = 0;
195
  for (uint8_t i=0; i<MAX_MOTORS; i++) {
196
    if (mixerMatrix.motor[i][MIX_THROTTLE])
197
      requiredMotors++;
198
  }
199
 
1969 - 200
  printf("\n\rMixer-Config: '%s' (%u Motors)",mixerMatrix.name, requiredMotors);
201
  printf("\n\r===================================");
1960 - 202
}
203
 
1612 dongfang 204
/***************************************************/
1960 - 205
/* ChannelMap                                      */
1612 dongfang 206
/***************************************************/
1960 - 207
void channelMap_writeToEEProm(void) {
208
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t));
209
}
210
 
211
void channelMap_readOrDefault(void) {
1969 - 212
  if (readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t))) {
1964 - 213
    printf("writing default channel map");
1960 - 214
    channelMap_default();
215
    channelMap_writeToEEProm();
216
  }
217
}
218
 
1961 - 219
/***************************************************/
220
/* Sensor offsets                                  */
221
/***************************************************/
1967 - 222
uint8_t gyroAmplifierOffset_readFromEEProm(void) {
223
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
224
}
225
 
226
void gyroAmplifierOffset_writeToEEProm(void) {
227
  return writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
228
}
229
 
1961 - 230
uint8_t gyroOffset_readFromEEProm(void) {
1967 - 231
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
1961 - 232
}
233
 
234
void gyroOffset_writeToEEProm(void) {
235
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
236
}
237
 
1960 - 238
uint8_t accOffset_readFromEEProm(void) {
239
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
240
}
241
 
242
void accOffset_writeToEEProm(void) {
243
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
244
}
245
 
1612 dongfang 246
/***************************************************/
1960 - 247
/*       Get active parameter set                  */
1612 dongfang 248
/***************************************************/
1960 - 249
uint8_t getActiveParamSet(void) {
250
  uint8_t setnumber;
251
  setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
252
  if (setnumber > 4) {
253
    setActiveParamSet(setnumber = 0);
254
  }
255
  return setnumber;
1612 dongfang 256
}
257
 
258
/***************************************************/
1960 - 259
/*       Set active parameter set                  */
1612 dongfang 260
/***************************************************/
1960 - 261
void setActiveParamSet(uint8_t setnumber) {
262
  eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
263
}
1821 - 264