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