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