Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1910 - 1
#ifndef EEMEM
2
#define EEMEM __attribute__ ((section (".eeprom")))
3
#endif
4
 
5
#include "eeprom.h"
2099 - 6
#include "printf_P.h"
1910 - 7
#include "output.h"
2099 - 8
#include <avr/wdt.h>
9
#include <avr/eeprom.h>
1910 - 10
 
11
// byte array in eeprom
12
uint8_t EEPromArray[E2END + 1] EEMEM;
13
 
14
/***************************************************/
15
/*       Read Parameter from EEPROM as byte        */
16
/***************************************************/
2099 - 17
uint8_t getParamByte(uint16_t param_id) {
18
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
1910 - 19
}
20
 
21
/***************************************************/
22
/*       Write Parameter to EEPROM as byte         */
23
/***************************************************/
2099 - 24
void setParamByte(uint16_t param_id, uint8_t value) {
25
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1910 - 26
}
27
 
28
/***************************************************/
29
/*       Read Parameter from EEPROM as word        */
30
/***************************************************/
2099 - 31
/*
32
uint16_t getParamWord(uint16_t param_id) {
33
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
34
                                                    + param_id]);
1910 - 35
}
2099 - 36
*/
1910 - 37
 
38
/***************************************************/
39
/*       Write Parameter to EEPROM as word         */
40
/***************************************************/
2099 - 41
/*
42
void setParamWord(uint16_t param_id, uint16_t value) {
43
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
1910 - 44
}
2099 - 45
*/
1910 - 46
 
2099 - 47
uint16_t CRC16(uint8_t* data, uint16_t length) {
48
  uint16_t crc = 0;
49
  for (uint16_t i=0; i<length; i++) {
50
    crc  = (uint8_t)(crc >> 8) | (crc << 8);
51
    crc ^= data[i];
52
    crc ^= (uint8_t)(crc & 0xff) >> 4;
53
    crc ^= (crc << 8) << 4;
54
    crc ^= ((crc & 0xff) << 4) << 1;
55
  }
56
  return crc;
57
}
58
 
59
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
60
// length is the length of the pure data not including checksum and revision number.
61
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
62
  uint16_t CRC = CRC16(data, length);
63
  eeprom_write_byte(&EEPromArray[offset], CRC&0xff);
64
  eeprom_write_byte(&EEPromArray[offset+1], CRC>>8);
65
  eeprom_write_byte(&EEPromArray[offset+2], revisionNumber);
66
  eeprom_write_block(data, &EEPromArray[offset+3], length);
67
}
68
 
69
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
70
// length is the length of the pure data not including checksum and revision number.
71
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
72
  uint16_t CRCRead = eeprom_read_byte(&EEPromArray[offset]) | (eeprom_read_byte(&EEPromArray[offset+1])<<8);
73
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+2]);
74
  eeprom_read_block(target, &EEPromArray[offset+3], length);
75
  uint16_t CRCCalculated = CRC16(target, length);
76
 
77
  uint8_t CRCError = (CRCRead != CRCCalculated);
78
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
79
 
80
  if (CRCError && revisionMismatch) printf("\n\rEEPROM CRC error and revision mismatch; ");
81
  else if (CRCError) printf("\n\rEEPROM CRC error; ");
82
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch; ");
83
  return (CRCError || revisionMismatch);
84
}
85
 
1910 - 86
/***************************************************/
87
/*       Read Parameter Set from EEPROM            */
88
/***************************************************/
2099 - 89
// setnumber [1..5]
90
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
91
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
92
  uint8_t result = readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
93
  configuration_paramSetDidChange();
94
  return result;
1910 - 95
}
96
 
97
/***************************************************/
98
/*        Write Parameter Set to EEPROM            */
99
/***************************************************/
2099 - 100
void paramSet_writeToEEProm(uint8_t setnumber) {
101
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
102
  writeChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
103
  // set this parameter set to active set
1910 - 104
}
105
 
2099 - 106
void paramSet_readOrDefault() {
107
  // parameter version  check
108
  if (paramSet_readFromEEProm(1)) {
109
    // if version check faild
110
    printf("\n\rwriting default parameter sets");
111
    for (uint8_t i=5; i>0; i--) {
112
      paramSet_default(i);
113
      paramSet_writeToEEProm(i);
114
    }
115
    // default-Setting is parameter set 1
116
    paramSet_readFromEEProm(1);
117
    // For some strange reason, the read will have no effect.
118
    // Lets reset...
119
    // wdt_enable(WDTO_500MS);
120
  }
121
  printf("\n\r\rUsing Parameter Set %d", 1);
1910 - 122
}
123
 
124
/***************************************************/
2099 - 125
/*          Read IMU Config from EEPROM            */
1910 - 126
/***************************************************/
2099 - 127
uint8_t IMUConfig_readFromEEprom(void) {
128
  return readChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
1910 - 129
}
130
 
131
/***************************************************/
2099 - 132
/*          Write IMU Config to EEPROM             */
1910 - 133
/***************************************************/
2099 - 134
void IMUConfig_writeToEEprom(void) {
135
  writeChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
1910 - 136
}
137
 
2099 - 138
void IMUConfig_readOrDefault(void) {
139
    if(IMUConfig_readFromEEprom()) {
140
      printf("\n\rwriting default IMU config");
141
      IMUConfig_default();
142
      IMUConfig_writeToEEprom();
143
    }
144
}
145
 
1910 - 146
/***************************************************/
2122 - 147
/* ChannelMap and R/C trim                         */
1910 - 148
/***************************************************/
2099 - 149
void channelMap_writeToEEProm(void) {
2116 - 150
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(ChannelMap_t));
1910 - 151
}
152
 
2099 - 153
void channelMap_readOrDefault(void) {
2116 - 154
  if (readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(ChannelMap_t))) {
2099 - 155
    printf("\n\rwriting default channel map");
156
    channelMap_default();
157
    channelMap_writeToEEProm();
158
  }
159
}
160
 
2122 - 161
void rcTrim_writeToEEProm(void) {
162
  writeChecksummedBlock(0, (uint8_t*)&rcTrim, EEPROM_ADR_RCTRIM, sizeof(RCTrim_t));
163
}
164
 
165
void rcTrim_readOrDefault(void) {
166
  if (readChecksummedBlock(0, (uint8_t*)&rcTrim, EEPROM_ADR_RCTRIM, sizeof(RCTrim_t))) {
167
    printf("\n\rwriting zero RC trim");
168
    RC_setZeroTrim();
169
    rcTrim_writeToEEProm();
170
  }
171
}
172
 
1910 - 173
/***************************************************/
2099 - 174
/* Sensor offsets                                  */
1910 - 175
/***************************************************/
2099 - 176
uint8_t gyroOffset_readFromEEProm(void) {
177
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
178
}
179
 
180
void gyroOffset_writeToEEProm(void) {
181
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
182
}
2105 - 183
 
184
uint8_t airpressureOffset_readFromEEProm(void) {
2106 - 185
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&airpressureOffset, EEPROM_ADR_AIRSPEEDOFFSET, sizeof(uint16_t));
2105 - 186
}
187
 
188
void airpressureOffset_writeToEEProm(void) {
2106 - 189
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&airpressureOffset, EEPROM_ADR_AIRSPEEDOFFSET, sizeof(uint16_t));
2105 - 190
}