Subversion Repositories FlightCtrl

Rev

Rev 1967 | Rev 1969 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1967 Rev 1968
Line 2... Line 2...
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 48... Line 48...
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 52... Line -...
52
 
-
 
53
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
54
// + Contant Values
-
 
55
// + 0-250 -> normale Values
-
 
56
// + 251 -> Poti1
-
 
57
// + 252 -> Poti2
-
 
58
// + 253 -> Poti3
-
 
59
// + 254 -> Poti4
-
 
60
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
61
 
52
 
62
#ifndef EEMEM
53
#ifndef EEMEM
63
#define EEMEM __attribute__ ((section (".eeprom")))
54
#define EEMEM __attribute__ ((section (".eeprom")))
Line 64... Line 55...
64
#endif
55
#endif
Line 102... Line 93...
102
void setParamWord(uint16_t param_id, uint16_t value) {
93
void setParamWord(uint16_t param_id, uint16_t value) {
103
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
94
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
104
}
95
}
105
*/
96
*/
Line 106... Line 97...
106
 
97
 
107
uint8_t calculateChecksum(uint8_t* data, uint16_t length) {
98
uint16_t CRC16(uint8_t* data, uint16_t length) {
108
  uint8_t result = 0;
99
  uint16_t crc = 0;
-
 
100
  for (uint16_t i=0; i<length; i++) {
109
  for (uint16_t i=0; i<length; i++) {
101
    crc  = (uint8_t)(crc >> 8) | (crc << 8);
-
 
102
    crc ^= data[i];
-
 
103
    crc ^= (uint8_t)(crc & 0xff) >> 4;
-
 
104
    crc ^= (crc << 8) << 4;
110
    result += data[i];
105
    crc ^= ((crc & 0xff) << 4) << 1;
111
  }
106
  }
112
  return result;
107
  return crc;
Line 113... Line 108...
113
}
108
}
114
 
109
 
115
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
110
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
116
// length is the length of the pure data not including checksum and revision number.
111
// length is the length of the pure data not including checksum and revision number.
117
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
112
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
118
  uint8_t checksum = calculateChecksum(data, length);
113
  uint16_t CRC = CRC16(data, length);
119
  eeprom_write_byte(&EEPromArray[offset], checksum);
114
  eeprom_write_word(&EEPromArray[offset], CRC);
120
  eeprom_write_byte(&EEPromArray[offset+1], revisionNumber);
115
  eeprom_write_byte(&EEPromArray[offset+2], revisionNumber);
Line 121... Line 116...
121
  eeprom_write_block(data, &EEPromArray[offset+2], length);
116
  eeprom_write_block(data, &EEPromArray[offset+3], length);
122
}
117
}
123
 
118
 
124
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
119
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
125
// length is the length of the pure data not including checksum and revision number.
120
// length is the length of the pure data not including checksum and revision number.
126
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
121
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
127
  uint8_t checksumRead = eeprom_read_byte(&EEPromArray[offset]);
122
  uint16_t CRCRead = eeprom_read_word(&EEPromArray[offset]);
Line 128... Line 123...
128
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+1]);
123
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+2]);
129
  eeprom_read_block(target, &EEPromArray[offset+2], length);
124
  eeprom_read_block(target, &EEPromArray[offset+3], length);
Line 130... Line 125...
130
  uint8_t checksumCalculated = calculateChecksum(target, length);
125
  uint16_t CRCCalculated = CRC(target, length);
131
 
126
 
132
  uint8_t checksumError = (checksumRead != checksumCalculated);
127
  uint8_t CRCError = (CRCRead != CRCCalculated);
133
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
128
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
134
 
129
 
Line 135... Line 130...
135
  if (checksumError && revisionMismatch) printf("\n\rEEPROM checksum error and revision mismatch, ");
130
  if (CRCError && revisionMismatch) printf("\n\rEEPROM CRC error and revision mismatch; ");
136
  else if (checksumError) printf("\n\rEEPROM checksum error, ");
131
  else if (CRCError) printf("\n\rEEPROM CRC error; ");
137
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch, ");
132
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch; ");
138
  return (checksumError || revisionMismatch);
133
  return (checksumError || revisionMismatch);
139
}
134
}
140
 
135
 
141
/***************************************************/
136
/***************************************************/
142
/*       Read Parameter Set from EEPROM            */
137
/*       Read Parameter Set from EEPROM            */
143
/***************************************************/
138
/***************************************************/
Line 144... Line 139...
144
// setnumber [1..5]
139
// setnumber [1..5]
145
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
140
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
146
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(paramset_t)+2);
141
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(paramset_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
147
  output_init(); // what's that doing here??
142
  output_init(); // what's that doing here??
148
  return readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(paramset_t));
143
  return readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(paramset_t));
149
}
144
}
150
 
145
 
151
/***************************************************/
146
/***************************************************/
152
/*        Write Parameter Set to EEPROM            */
147
/*        Write Parameter Set to EEPROM            */
153
/***************************************************/
148
/***************************************************/