Subversion Repositories FlightCtrl

Rev

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