Subversion Repositories FlightCtrl

Rev

Rev 2132 | Details | Compare with Previous | Last modification | View Log | RSS feed

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