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