Subversion Repositories FlightCtrl

Rev

Rev 2116 | Go to most recent revision | Details | 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"
8
#include <avr/wdt.h>
9
#include <avr/eeprom.h>
10
 
11
// byte array in eeprom
12
uint8_t EEPromArray[E2END + 1] EEMEM;
13
 
14
/***************************************************/
15
/*       Read Parameter from EEPROM as byte        */
16
/***************************************************/
17
uint8_t getParamByte(uint16_t param_id) {
18
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
19
}
20
 
21
/***************************************************/
22
/*       Write Parameter to EEPROM as byte         */
23
/***************************************************/
24
void setParamByte(uint16_t param_id, uint8_t value) {
25
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
26
}
27
 
28
/***************************************************/
29
/*       Read Parameter from EEPROM as word        */
30
/***************************************************/
31
/*
32
uint16_t getParamWord(uint16_t param_id) {
33
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
34
                                                    + param_id]);
35
}
36
*/
37
 
38
/***************************************************/
39
/*       Write Parameter to EEPROM as word         */
40
/***************************************************/
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);
44
}
45
*/
46
 
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
 
86
/***************************************************/
87
/*       Read Parameter Set from EEPROM            */
88
/***************************************************/
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;
95
}
96
 
97
/***************************************************/
98
/*        Write Parameter Set to EEPROM            */
99
/***************************************************/
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
104
}
105
 
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);
122
}
123
 
124
/***************************************************/
125
/*          Read IMU Config from EEPROM            */
126
/***************************************************/
127
uint8_t IMUConfig_readFromEEprom(void) {
128
  return readChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
129
}
130
 
131
/***************************************************/
132
/*          Write IMU Config to EEPROM             */
133
/***************************************************/
134
void IMUConfig_writeToEEprom(void) {
135
  writeChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
136
}
137
 
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
 
146
/***************************************************/
147
/* ChannelMap                                      */
148
/***************************************************/
149
void channelMap_writeToEEProm(void) {
150
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t));
151
}
152
 
153
void channelMap_readOrDefault(void) {
154
  if (readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t))) {
155
    printf("\n\rwriting default channel map");
156
    channelMap_default();
157
    channelMap_writeToEEProm();
158
        wdt_enable(WDTO_500MS);
159
  }
160
}
161
 
162
/***************************************************/
163
/* Sensor offsets                                  */
164
/***************************************************/
165
uint8_t gyroOffset_readFromEEProm(void) {
166
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
167
}
168
 
169
void gyroOffset_writeToEEProm(void) {
170
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
171
}
172
 
173
uint8_t airpressureOffset_readFromEEProm(void) {
174
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&airpressureOffset, EEPROM_ADR_AIRSPEEDOFFSET, sizeof(uint16_t));
175
}
176
 
177
void airpressureOffset_writeToEEProm(void) {
178
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&airpressureOffset, EEPROM_ADR_AIRSPEEDOFFSET, sizeof(uint16_t));
179
}