Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
#ifndef EEMEM
2
#define EEMEM __attribute__ ((section (".eeprom")))
3
#endif
4
 
5
#include "eeprom.h"
6
#include "printf_P.h"
7
#include "output.h"
2157 - 8
#include "configuration.h"
2055 - 9
#include <avr/wdt.h>
1960 - 10
#include <avr/eeprom.h>
2164 - 11
#include <avr/interrupt.h>
1612 dongfang 12
 
13
// byte array in eeprom
1821 - 14
uint8_t EEPromArray[E2END + 1] EEMEM;
1612 dongfang 15
 
16
/***************************************************/
1960 - 17
/*       Read Parameter from EEPROM as byte        */
1612 dongfang 18
/***************************************************/
1960 - 19
uint8_t getParamByte(uint16_t param_id) {
20
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
1612 dongfang 21
}
22
 
23
/***************************************************/
1960 - 24
/*       Write Parameter to EEPROM as byte         */
1612 dongfang 25
/***************************************************/
1960 - 26
void setParamByte(uint16_t param_id, uint8_t value) {
27
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 28
}
29
 
30
/***************************************************/
1960 - 31
/*       Read Parameter from EEPROM as word        */
1612 dongfang 32
/***************************************************/
1967 - 33
/*
1960 - 34
uint16_t getParamWord(uint16_t param_id) {
35
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
36
                                                    + param_id]);
1612 dongfang 37
}
1967 - 38
*/
1612 dongfang 39
 
40
/***************************************************/
1960 - 41
/*       Write Parameter to EEPROM as word         */
1612 dongfang 42
/***************************************************/
1967 - 43
/*
1960 - 44
void setParamWord(uint16_t param_id, uint16_t value) {
45
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1612 dongfang 46
}
1967 - 47
*/
1612 dongfang 48
 
1968 - 49
uint16_t CRC16(uint8_t* data, uint16_t length) {
50
  uint16_t crc = 0;
1960 - 51
  for (uint16_t i=0; i<length; i++) {
1968 - 52
    crc  = (uint8_t)(crc >> 8) | (crc << 8);
53
    crc ^= data[i];
54
    crc ^= (uint8_t)(crc & 0xff) >> 4;
55
    crc ^= (crc << 8) << 4;
56
    crc ^= ((crc & 0xff) << 4) << 1;
1960 - 57
  }
1968 - 58
  return crc;
1612 dongfang 59
}
60
 
1964 - 61
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
62
// length is the length of the pure data not including checksum and revision number.
63
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
1968 - 64
  uint16_t CRC = CRC16(data, length);
2164 - 65
  uint8_t sreg = SREG;
66
  cli();
1969 - 67
  eeprom_write_byte(&EEPromArray[offset], CRC&0xff);
68
  eeprom_write_byte(&EEPromArray[offset+1], CRC>>8);
1968 - 69
  eeprom_write_byte(&EEPromArray[offset+2], revisionNumber);
70
  eeprom_write_block(data, &EEPromArray[offset+3], length);
2164 - 71
  SREG = sreg;
1612 dongfang 72
}
73
 
1964 - 74
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
75
// length is the length of the pure data not including checksum and revision number.
76
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
1969 - 77
  uint16_t CRCRead = eeprom_read_byte(&EEPromArray[offset]) | (eeprom_read_byte(&EEPromArray[offset+1])<<8);
1968 - 78
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+2]);
79
  eeprom_read_block(target, &EEPromArray[offset+3], length);
1969 - 80
  uint16_t CRCCalculated = CRC16(target, length);
1964 - 81
 
1968 - 82
  uint8_t CRCError = (CRCRead != CRCCalculated);
1964 - 83
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
84
 
1968 - 85
  if (CRCError && revisionMismatch) printf("\n\rEEPROM CRC error and revision mismatch; ");
86
  else if (CRCError) printf("\n\rEEPROM CRC error; ");
87
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch; ");
1969 - 88
  return (CRCError || revisionMismatch);
1612 dongfang 89
}
90
 
91
/***************************************************/
92
/*       Read Parameter Set from EEPROM            */
93
/***************************************************/
1963 - 94
// setnumber [1..5]
1960 - 95
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
2039 - 96
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
2059 - 97
  uint8_t result = readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
98
  configuration_paramSetDidChange();
99
  return result;
1612 dongfang 100
}
101
 
102
/***************************************************/
103
/*        Write Parameter Set to EEPROM            */
104
/***************************************************/
1960 - 105
void paramSet_writeToEEProm(uint8_t setnumber) {
2039 - 106
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
107
  writeChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
1612 dongfang 108
}
109
 
1960 - 110
void paramSet_readOrDefault() {
111
  uint8_t setnumber = getActiveParamSet();
112
  // parameter version  check
113
  if (setnumber<1 ||setnumber>5 || paramSet_readFromEEProm(setnumber)) {
114
    // if version check faild
1969 - 115
    printf("\n\rwriting default parameter sets");
1964 - 116
    for (uint8_t i=5; i>0; i--) {
117
      paramSet_default(i);
1960 - 118
      paramSet_writeToEEProm(i);
119
    }
2059 - 120
    // default-Setting is parameter set 1
1960 - 121
    setActiveParamSet(1);
2059 - 122
    paramSet_readFromEEProm(getActiveParamSet());
2055 - 123
    // For some strange reason, the read will have no effect.
124
    // Lets reset...
2059 - 125
    // wdt_enable(WDTO_500MS);
1960 - 126
  }
1964 - 127
  printf("\n\r\rUsing Parameter Set %d", getActiveParamSet());
1612 dongfang 128
}
129
 
130
/***************************************************/
2092 - 131
/*          Read IMU Config from EEPROM            */
132
/***************************************************/
133
uint8_t IMUConfig_readFromEEprom(void) {
134
  return readChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
135
}
136
 
137
/***************************************************/
138
/*          Write IMU Config to EEPROM             */
139
/***************************************************/
140
void IMUConfig_writeToEEprom(void) {
141
  writeChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
142
}
143
 
144
void IMUConfig_readOrDefault(void) {
145
    if(IMUConfig_readFromEEprom()) {
146
      printf("\n\rwriting default IMU config");
147
      IMUConfig_default();
148
      IMUConfig_writeToEEprom();
149
    }
150
}
151
 
152
/***************************************************/
1960 - 153
/* MixerTable                                      */
1612 dongfang 154
/***************************************************/
2158 - 155
void motorMixer_writeToEEProm(void) {
156
  writeChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&motorMixer, EEPROM_ADR_MIXER_TABLE, sizeof(MotorMixer_t));
1612 dongfang 157
}
158
 
2158 - 159
void motorMixer_readOrDefault(void) {
1960 - 160
  // load mixer table
2158 - 161
  if (readChecksummedBlock(EEMIXER_REVISION, (uint8_t*)&motorMixer, EEPROM_ADR_MIXER_TABLE, sizeof(MotorMixer_t))) {
162
    printf("\n\rwriting default motor mixer");
163
    motorMixer_default(); // Quadro
164
    motorMixer_writeToEEProm();
1960 - 165
  }
166
  // determine motornumber
167
  requiredMotors = 0;
168
  for (uint8_t i=0; i<MAX_MOTORS; i++) {
2158 - 169
    if (motorMixer.matrix[i][MIX_THROTTLE])
1960 - 170
      requiredMotors++;
171
  }
172
 
2158 - 173
  printf("\n\rMixer-Config: '%s' (%u Motors)", motorMixer.name, requiredMotors);
1969 - 174
  printf("\n\r===================================");
1960 - 175
}
176
 
1612 dongfang 177
/***************************************************/
1960 - 178
/* ChannelMap                                      */
1612 dongfang 179
/***************************************************/
1960 - 180
void channelMap_writeToEEProm(void) {
2156 - 181
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(ChannelMap_t));
1960 - 182
}
183
 
184
void channelMap_readOrDefault(void) {
2156 - 185
  if (readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(ChannelMap_t))) {
2092 - 186
    printf("\n\rwriting default channel map");
1960 - 187
    channelMap_default();
188
    channelMap_writeToEEProm();
2059 - 189
        wdt_enable(WDTO_500MS);
1960 - 190
  }
191
}
192
 
1961 - 193
/***************************************************/
194
/* Sensor offsets                                  */
195
/***************************************************/
1967 - 196
uint8_t gyroAmplifierOffset_readFromEEProm(void) {
197
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
198
}
199
 
200
void gyroAmplifierOffset_writeToEEProm(void) {
201
  return writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
202
}
203
 
1961 - 204
uint8_t gyroOffset_readFromEEProm(void) {
1967 - 205
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
1961 - 206
}
207
 
208
void gyroOffset_writeToEEProm(void) {
209
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
210
}
211
 
1960 - 212
uint8_t accOffset_readFromEEProm(void) {
213
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
214
}
215
 
216
void accOffset_writeToEEProm(void) {
217
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&accOffset, EEPROM_ADR_ACCOFFSET, sizeof(sensorOffset_t));
218
}
219
 
1612 dongfang 220
/***************************************************/
1960 - 221
/*       Get active parameter set                  */
1612 dongfang 222
/***************************************************/
1960 - 223
uint8_t getActiveParamSet(void) {
224
  uint8_t setnumber;
225
  setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
2059 - 226
  if (setnumber > 5) {
227
    setActiveParamSet(setnumber = 1);
1960 - 228
  }
229
  return setnumber;
1612 dongfang 230
}
231
 
232
/***************************************************/
1960 - 233
/*       Set active parameter set                  */
1612 dongfang 234
/***************************************************/
1960 - 235
void setActiveParamSet(uint8_t setnumber) {
236
  eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
237
}
1821 - 238