Rev 740 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
248 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
248 | killagreg | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
248 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
248 | killagreg | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
248 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <string.h> |
||
252 | killagreg | 57 | #include <stdio.h> |
248 | killagreg | 58 | #include "91x_lib.h" |
489 | killagreg | 59 | #include "i2c.h" |
248 | killagreg | 60 | #include "uart1.h" |
61 | #include "timer1.h" |
||
62 | #include "eeprom.h" |
||
264 | killagreg | 63 | #include "led.h" |
526 | holgerb | 64 | #include "crc16.h" |
248 | killagreg | 65 | |
66 | |||
489 | killagreg | 67 | |
248 | killagreg | 68 | // The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller. |
489 | killagreg | 69 | #define EEPROM_I2C I2C1 |
70 | |||
248 | killagreg | 71 | // The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0. |
72 | |||
73 | #define EEPROM_I2C_ADDRESS 0xA0 |
||
74 | #define EEPROM_SIZE 65536L // 64k bytes |
||
75 | #define EEPROM_WRITE 1 |
||
76 | #define EEPROM_READ 0 |
||
77 | |||
78 | // timing |
||
254 | killagreg | 79 | #define I2C_IDLE_TIMEOUT 150 // 150 ms |
80 | #define I2C_TRANSFER_TIMEOUT 150 // 150 ms |
||
248 | killagreg | 81 | #define I2C_ACCESS_RETRYS 20 // retry 20 times |
82 | |||
83 | // globals for rx handler |
||
254 | killagreg | 84 | volatile u8 *EEPROM_pData = 0; |
85 | volatile u16 EEPROM_DataLen = 0; |
||
248 | killagreg | 86 | |
252 | killagreg | 87 | // rx data handler for eeprom data read access |
88 | void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize) |
||
248 | killagreg | 89 | { // if number of byte are matching |
254 | killagreg | 90 | if((RxBufferSize == EEPROM_DataLen)) |
248 | killagreg | 91 | { //copy data from primary buffer to target buffer |
254 | killagreg | 92 | if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen); |
256 | killagreg | 93 | } |
248 | killagreg | 94 | } |
95 | |||
96 | // ---------------------------------------------------------------------------------------- |
||
97 | EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen) |
||
98 | { |
||
489 | killagreg | 99 | u16 i; |
526 | holgerb | 100 | u8 retry = 0; |
248 | killagreg | 101 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
102 | |||
103 | if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1)) |
||
104 | { |
||
105 | retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE; |
||
106 | return(retval); |
||
107 | } |
||
489 | killagreg | 108 | if((DataLen+2) > I2C_BUFFER_LEN) |
248 | killagreg | 109 | { |
110 | retval = EEPROM_I2C_BUFFER_OVERRUN; |
||
111 | return(retval); |
||
112 | } |
||
113 | do |
||
114 | { |
||
489 | killagreg | 115 | if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT; |
248 | killagreg | 116 | // buffer is now locked |
489 | killagreg | 117 | u8 TxBytes = 0; |
118 | u8 TxData[255]; |
||
248 | killagreg | 119 | // transmitt address |
489 | killagreg | 120 | TxData[TxBytes++] = (u8)(0x00FF & (Address>>8)); |
121 | TxData[TxBytes++] = (u8)(0x00FF & Address); |
||
248 | killagreg | 122 | if(Direction == EEPROM_WRITE) |
123 | { // copy data to i2c transfer buffer |
||
124 | for(i = 0; i<DataLen;i++) |
||
125 | { |
||
489 | killagreg | 126 | TxData[TxBytes++] = pData[i]; |
248 | killagreg | 127 | } |
128 | // prepare pointer to rx data |
||
129 | EEPROM_pData = 0; |
||
130 | EEPROM_DataLen = 0; |
||
131 | // start transmission |
||
489 | killagreg | 132 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0)) |
248 | killagreg | 133 | { |
134 | return(retval); |
||
135 | } |
||
136 | } |
||
137 | else // Direction == EEPROM_READ |
||
138 | { |
||
139 | // prepare pointer to rx data |
||
140 | EEPROM_pData = pData; |
||
141 | EEPROM_DataLen = DataLen; |
||
142 | // start transmission |
||
489 | killagreg | 143 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen)) |
248 | killagreg | 144 | { |
740 | holgerb | 145 | return(retval); |
248 | killagreg | 146 | } |
147 | } |
||
148 | //wait for end of this transfer |
||
489 | killagreg | 149 | if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT)) |
248 | killagreg | 150 | { |
489 | killagreg | 151 | if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS); |
256 | killagreg | 152 | else retval = EEPROM_DATA_TRANSFER_INCOMPLETE; |
248 | killagreg | 153 | } |
256 | killagreg | 154 | else// i2c transfer timed out |
155 | { |
||
156 | return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT); |
||
157 | } |
||
158 | // or retry |
||
248 | killagreg | 159 | } while(++retry < I2C_ACCESS_RETRYS); |
160 | return(retval); |
||
161 | } |
||
162 | |||
163 | EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen) |
||
164 | { |
||
165 | u16 len; |
||
166 | EEPROM_Result_t retval; |
||
167 | |||
168 | // there can be 32 bytes written in a single cycle if the adresses are locates in the same row |
||
169 | // i.e. the upper 11 bits of the addres are constant |
||
170 | #define EEPROM_WRITE_BLOCK_SIZE 32 |
||
171 | |||
172 | len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row |
||
173 | if(len >= DataLen) len = DataLen; // do not write more than neccesarry |
||
174 | |||
175 | do |
||
176 | { |
||
252 | killagreg | 177 | retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len); |
248 | killagreg | 178 | Address += len; // Address of the next row |
179 | pData += len; // pointer to start of next data block |
||
180 | DataLen -= len; // reduce the number of to be transmitted |
||
181 | if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32 |
||
182 | else len = DataLen; |
||
183 | } |
||
184 | while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured |
||
185 | |||
186 | return(retval); |
||
187 | } |
||
188 | |||
189 | // a data block can be subsequently read after setting the start address once |
||
190 | EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen) |
||
191 | { |
||
252 | killagreg | 192 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
193 | u16 AdrOffset = 0; |
||
194 | u16 RxLen; |
||
195 | |||
196 | while(DataLen > 0) |
||
197 | { |
||
526 | holgerb | 198 | if(DataLen > I2C_BUFFER_LEN - 2) RxLen = I2C_BUFFER_LEN - 2; |
252 | killagreg | 199 | else RxLen = DataLen; |
200 | |||
201 | retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen); |
||
202 | if(retval != EEPROM_SUCCESS) break; |
||
203 | AdrOffset += RxLen; |
||
204 | DataLen -= RxLen; |
||
205 | } |
||
206 | return(retval); |
||
248 | killagreg | 207 | } |
264 | killagreg | 208 | |
209 | u8 EEPROM_Init(void) |
||
210 | { |
||
211 | u8 data[20]; |
||
212 | u8 i, retval = 0; |
||
213 | UART1_PutString("\r\n EEprom init.."); |
||
214 | // check if data can be read from eeprom |
||
215 | for(i=0;i<10;i++) |
||
216 | { |
||
217 | UART1_Putchar('.'); |
||
218 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1; |
||
219 | if(retval) break; |
||
220 | } |
||
221 | if(retval) UART1_PutString("ok"); |
||
312 | holgerb | 222 | else UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
264 | killagreg | 223 | return(retval); |
224 | } |
||
526 | holgerb | 225 | |
226 | |||
227 | void WriteLicenseToEEPROM(u8 kompatibel) |
||
228 | { |
||
229 | u16 crc,crc2; |
||
230 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
||
231 | crc = kompatibel; |
||
232 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
||
233 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
||
530 | holgerb | 234 | if((crc != crc2) || (crc == 0xffff)) |
526 | holgerb | 235 | { |
236 | retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
||
530 | holgerb | 237 | if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc,2); |
526 | holgerb | 238 | } |
239 | } |
||
240 | |||
241 | void ClearLicenseText(void) |
||
242 | { |
||
243 | u32 i; |
||
244 | for(i=0; i < LICENSE_SIZE_TEXT;i++) LicensePtr[i] = 0; //Achtung: das leert die ganze Struktur, nicht nur den Namen |
||
245 | } |
||
246 | |||
247 | void DeleteLicenseInEEPROM(void) |
||
248 | { |
||
249 | ClearLicenseText(); |
||
250 | WriteLicenseToEEPROM(0); |
||
251 | } |
||
252 | |||
253 | u8 ReadLicenseFromEEPROM(void) |
||
254 | { |
||
255 | u8 retval = 0, tmp; |
||
256 | u16 crc, crc2; |
||
257 | |||
258 | tmp = EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
||
259 | if(EEPROM_SUCCESS == tmp) retval = 1; |
||
260 | |||
261 | crc = EEPROM_LICENSE_DATA_KOMPATIBEL; |
||
262 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
||
263 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
||
264 | |||
265 | if(crc != crc2) retval++; |
||
266 | if(retval && LicensePtr[0] != 0 && LicensePtr[0] != 0xff) |
||
267 | { |
||
268 | return(1); |
||
269 | } |
||
270 | else |
||
271 | { |
||
272 | ClearLicenseText(); |
||
273 | return(0); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | |||
278 | |||
279 |