Rev 819 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 819 | Rev 865 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Software Nutzungsbedingungen (english version: see below) |
7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
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 |
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 |
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. |
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. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
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. |
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 |
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. |
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 |
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. |
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 |
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 |
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. |
22 | // + des Mitverschuldens offen. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
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. |
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. |
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 |
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. |
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. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
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. |
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. |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Software LICENSING TERMS |
32 | // + Software LICENSING TERMS |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
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 |
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*. |
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. |
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 |
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 |
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. |
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 |
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. |
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 |
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 |
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 |
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 |
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. |
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 |
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. |
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 |
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. |
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. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
53 | // + #### END OF LICENSING TERMS #### |
53 | // + #### END OF LICENSING TERMS #### |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <string.h> |
56 | #include <string.h> |
57 | #include <stdio.h> |
57 | #include <stdio.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
60 | #include "i2c.h" |
60 | #include "i2c.h" |
61 | #include "uart1.h" |
61 | #include "uart1.h" |
62 | #include "timer1.h" |
62 | #include "timer1.h" |
63 | #include "eeprom.h" |
63 | #include "eeprom.h" |
64 | #include "led.h" |
64 | #include "led.h" |
65 | #include "crc16.h" |
65 | #include "crc16.h" |
66 | #include "main.h" |
66 | #include "main.h" |
67 | #include "spi_slave.h" |
67 | #include "spi_slave.h" |
68 | #include "params.h" |
68 | #include "params.h" |
69 | 69 | ||
70 | 70 | ||
71 | // The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller. |
71 | // The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller. |
72 | #define EEPROM_I2C I2C1 |
72 | #define EEPROM_I2C I2C1 |
73 | 73 | ||
74 | // The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0. |
74 | // The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0. |
75 | 75 | ||
76 | #define EEPROM_I2C_ADDRESS 0xA0 |
76 | #define EEPROM_I2C_ADDRESS 0xA0 |
77 | #define EEPROM_SIZE 65536L // 64k bytes |
77 | #define EEPROM_SIZE 65536L // 64k bytes |
78 | #define EEPROM_WRITE 1 |
78 | #define EEPROM_WRITE 1 |
79 | #define EEPROM_READ 0 |
79 | #define EEPROM_READ 0 |
80 | 80 | ||
81 | // timing |
81 | // timing |
82 | #define I2C_IDLE_TIMEOUT 150 // 150 ms |
82 | #define I2C_IDLE_TIMEOUT 150 // 150 ms |
83 | #define I2C_TRANSFER_TIMEOUT 150 // 150 ms |
83 | #define I2C_TRANSFER_TIMEOUT 150 // 150 ms |
84 | #define I2C_ACCESS_RETRYS 20 // retry 20 times |
84 | #define I2C_ACCESS_RETRYS 20 // retry 20 times |
85 | 85 | ||
86 | // globals for rx handler |
86 | // globals for rx handler |
87 | volatile u8 *EEPROM_pData = 0; |
87 | volatile u8 *EEPROM_pData = 0; |
88 | volatile u16 EEPROM_DataLen = 0; |
88 | volatile u16 EEPROM_DataLen = 0; |
89 | 89 | ||
90 | // rx data handler for eeprom data read access |
90 | // rx data handler for eeprom data read access |
91 | void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize) |
91 | void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize) |
92 | { // if number of byte are matching |
92 | { // if number of byte are matching |
93 | if((RxBufferSize == EEPROM_DataLen)) |
93 | if((RxBufferSize == EEPROM_DataLen)) |
94 | { //copy data from primary buffer to target buffer |
94 | { //copy data from primary buffer to target buffer |
95 | if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen); |
95 | if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen); |
96 | } |
96 | } |
97 | } |
97 | } |
98 | 98 | ||
99 | // ---------------------------------------------------------------------------------------- |
99 | // ---------------------------------------------------------------------------------------- |
100 | EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen) |
100 | EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen) |
101 | { |
101 | { |
102 | u16 i; |
102 | u16 i; |
103 | u8 retry = 0; |
103 | u8 retry = 0; |
104 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
104 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
105 | 105 | ||
106 | if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1)) |
106 | if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1)) |
107 | { |
107 | { |
108 | retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE; |
108 | retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE; |
109 | return(retval); |
109 | return(retval); |
110 | } |
110 | } |
111 | if((DataLen+2) > I2C_BUFFER_LEN) |
111 | if((DataLen+2) > I2C_BUFFER_LEN) |
112 | { |
112 | { |
113 | retval = EEPROM_I2C_BUFFER_OVERRUN; |
113 | retval = EEPROM_I2C_BUFFER_OVERRUN; |
114 | return(retval); |
114 | return(retval); |
115 | } |
115 | } |
116 | do |
116 | do |
117 | { |
117 | { |
118 | if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT; |
118 | if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT; |
119 | // buffer is now locked |
119 | // buffer is now locked |
120 | u8 TxBytes = 0; |
120 | u8 TxBytes = 0; |
121 | u8 TxData[255]; |
121 | u8 TxData[255]; |
122 | // transmitt address |
122 | // transmitt address |
123 | TxData[TxBytes++] = (u8)(0x00FF & (Address>>8)); |
123 | TxData[TxBytes++] = (u8)(0x00FF & (Address>>8)); |
124 | TxData[TxBytes++] = (u8)(0x00FF & Address); |
124 | TxData[TxBytes++] = (u8)(0x00FF & Address); |
125 | if(Direction == EEPROM_WRITE) |
125 | if(Direction == EEPROM_WRITE) |
126 | { // copy data to i2c transfer buffer |
126 | { // copy data to i2c transfer buffer |
127 | for(i = 0; i<DataLen;i++) |
127 | for(i = 0; i<DataLen;i++) |
128 | { |
128 | { |
129 | TxData[TxBytes++] = pData[i]; |
129 | TxData[TxBytes++] = pData[i]; |
130 | } |
130 | } |
131 | // prepare pointer to rx data |
131 | // prepare pointer to rx data |
132 | EEPROM_pData = 0; |
132 | EEPROM_pData = 0; |
133 | EEPROM_DataLen = 0; |
133 | EEPROM_DataLen = 0; |
134 | // start transmission |
134 | // start transmission |
135 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0)) |
135 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0)) |
136 | { |
136 | { |
137 | return(retval); |
137 | return(retval); |
138 | } |
138 | } |
139 | } |
139 | } |
140 | else // Direction == EEPROM_READ |
140 | else // Direction == EEPROM_READ |
141 | { |
141 | { |
142 | // prepare pointer to rx data |
142 | // prepare pointer to rx data |
143 | EEPROM_pData = pData; |
143 | EEPROM_pData = pData; |
144 | EEPROM_DataLen = DataLen; |
144 | EEPROM_DataLen = DataLen; |
145 | // start transmission |
145 | // start transmission |
146 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen)) |
146 | if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen)) |
147 | { |
147 | { |
148 | return(retval); |
148 | return(retval); |
149 | } |
149 | } |
150 | } |
150 | } |
151 | //wait for end of this transfer |
151 | //wait for end of this transfer |
152 | if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT)) |
152 | if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT)) |
153 | { |
153 | { |
154 | if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS); |
154 | if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS); |
155 | else retval = EEPROM_DATA_TRANSFER_INCOMPLETE; |
155 | else retval = EEPROM_DATA_TRANSFER_INCOMPLETE; |
156 | } |
156 | } |
157 | else// i2c transfer timed out |
157 | else// i2c transfer timed out |
158 | { |
158 | { |
159 | return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT); |
159 | return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT); |
160 | } |
160 | } |
161 | // or retry |
161 | // or retry |
162 | } while(++retry < I2C_ACCESS_RETRYS); |
162 | } while(++retry < I2C_ACCESS_RETRYS); |
163 | return(retval); |
163 | return(retval); |
164 | } |
164 | } |
165 | 165 | ||
166 | EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen) |
166 | EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen) |
167 | { |
167 | { |
168 | u16 len; |
168 | u16 len; |
169 | EEPROM_Result_t retval; |
169 | EEPROM_Result_t retval; |
170 | 170 | ||
171 | // there can be 32 bytes written in a single cycle if the adresses are locates in the same row |
171 | // there can be 32 bytes written in a single cycle if the adresses are locates in the same row |
172 | // i.e. the upper 11 bits of the addres are constant |
172 | // i.e. the upper 11 bits of the addres are constant |
173 | #define EEPROM_WRITE_BLOCK_SIZE 32 |
173 | #define EEPROM_WRITE_BLOCK_SIZE 32 |
174 | 174 | ||
175 | len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row |
175 | len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row |
176 | if(len >= DataLen) len = DataLen; // do not write more than neccesarry |
176 | if(len >= DataLen) len = DataLen; // do not write more than neccesarry |
177 | 177 | ||
178 | do |
178 | do |
179 | { |
179 | { |
180 | retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len); |
180 | retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len); |
181 | Address += len; // Address of the next row |
181 | Address += len; // Address of the next row |
182 | pData += len; // pointer to start of next data block |
182 | pData += len; // pointer to start of next data block |
183 | DataLen -= len; // reduce the number of to be transmitted |
183 | DataLen -= len; // reduce the number of to be transmitted |
184 | if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32 |
184 | if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32 |
185 | else len = DataLen; |
185 | else len = DataLen; |
186 | } |
186 | } |
187 | while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured |
187 | while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured |
188 | 188 | ||
189 | return(retval); |
189 | return(retval); |
190 | } |
190 | } |
191 | 191 | ||
192 | // a data block can be subsequently read after setting the start address once |
192 | // a data block can be subsequently read after setting the start address once |
193 | EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen) |
193 | EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen) |
194 | { |
194 | { |
195 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
195 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
196 | u16 AdrOffset = 0; |
196 | u16 AdrOffset = 0; |
197 | u16 RxLen; |
197 | u16 RxLen; |
198 | 198 | ||
199 | while(DataLen > 0) |
199 | while(DataLen > 0) |
200 | { |
200 | { |
201 | if(DataLen > I2C_BUFFER_LEN - 2) RxLen = I2C_BUFFER_LEN - 2; |
201 | if(DataLen > I2C_BUFFER_LEN - 2) RxLen = I2C_BUFFER_LEN - 2; |
202 | else RxLen = DataLen; |
202 | else RxLen = DataLen; |
203 | 203 | ||
204 | retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen); |
204 | retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen); |
205 | if(retval != EEPROM_SUCCESS) break; |
205 | if(retval != EEPROM_SUCCESS) break; |
206 | AdrOffset += RxLen; |
206 | AdrOffset += RxLen; |
207 | DataLen -= RxLen; |
207 | DataLen -= RxLen; |
208 | } |
208 | } |
209 | return(retval); |
209 | return(retval); |
210 | } |
210 | } |
211 | 211 | ||
212 | u8 EEPROM_Init(void) |
212 | u8 EEPROM_Init(void) |
213 | { |
213 | { |
214 | u8 data[20]; |
214 | u8 data[20]; |
215 | u8 i, retval = 0; |
215 | u8 i, retval = 0; |
216 | UART1_PutString("\r\n EEprom init.."); |
216 | UART1_PutString("\r\n EEprom init.."); |
217 | // check if data can be read from eeprom |
217 | // check if data can be read from eeprom |
218 | for(i=0;i<10;i++) |
218 | for(i=0;i<10;i++) |
219 | { |
219 | { |
220 | UART1_Putchar('.'); |
220 | UART1_Putchar('.'); |
221 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1; |
221 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1; |
222 | if(retval) break; |
222 | if(retval) break; |
223 | } |
223 | } |
224 | if(retval) UART1_PutString("ok"); |
224 | if(retval) UART1_PutString("ok"); |
225 | else UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
225 | else UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
226 | return(retval); |
226 | return(retval); |
227 | } |
227 | } |
228 | 228 | ||
229 | void WriteOemNameToEEPROM(void) |
229 | void WriteOemNameToEEPROM(void) |
230 | { |
230 | { |
231 | char msg[100]; |
231 | char msg[100]; |
232 | u16 crc,crc2; |
232 | u16 crc,crc2; |
233 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
233 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
234 | memcpy(OEM_String,&LicensePtr[OEM_IN_LICENSE], OEM_NAME_LENGHT); |
234 | memcpy(OEM_String,&LicensePtr[OEM_IN_LICENSE], OEM_NAME_LENGHT); |
235 | if(LicensePtr[OEM_IN_LICENSE] == 0) |
235 | if(LicensePtr[OEM_IN_LICENSE] == 0) |
236 | { |
236 | { |
237 | OEM_String[0] = 255; // that is a command to erease the OEM-string |
237 | OEM_String[0] = 255; // that is a command to erease the OEM-string |
238 | LicensePtr[OEM_IN_LICENSE] = 255; |
238 | LicensePtr[OEM_IN_LICENSE] = 255; |
239 | UART1_PutString("\r\n Erase OEM-String\r\n "); |
239 | UART1_PutString("\r\n Erase OEM-String\r\n "); |
240 | } |
240 | } |
241 | else |
241 | else |
242 | { |
242 | { |
243 | if(UART_VersionInfo.HWMajor >= 30) SendOemName = 1; |
243 | if(UART_VersionInfo.HWMajor >= 30) SendOemName = 1; |
244 | UART1_PutString("\r\n Write OEM-String: ");sprintf(msg, "(%s)\r\n",OEM_String);UART1_PutString(msg); |
244 | UART1_PutString("\r\n Write OEM-String: ");sprintf(msg, "(%s)\r\n",OEM_String);UART1_PutString(msg); |
245 | //sprintf(msg, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n\r",OEM_String[0],OEM_String[1],OEM_String[2],OEM_String[3],OEM_String[4],OEM_String[5],OEM_String[6],OEM_String[7],OEM_String[8],OEM_String[9],OEM_String[10],OEM_String[11],OEM_String[12],OEM_String[13],OEM_String[14],OEM_String[15]); UART1_PutString(msg); |
245 | //sprintf(msg, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n\r",OEM_String[0],OEM_String[1],OEM_String[2],OEM_String[3],OEM_String[4],OEM_String[5],OEM_String[6],OEM_String[7],OEM_String[8],OEM_String[9],OEM_String[10],OEM_String[11],OEM_String[12],OEM_String[13],OEM_String[14],OEM_String[15]); UART1_PutString(msg); |
246 | } |
246 | } |
247 | crc = 2; // Kompatibliaet |
247 | crc = 2; // Kompatibliaet |
248 | crc += CRC16(OEM_String,16); |
248 | crc += CRC16(OEM_String,16); |
249 | EEPROM_ReadBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc2,2); |
249 | EEPROM_ReadBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc2,2); |
250 | if((crc != crc2) || (crc == 0xffff)) |
250 | if((crc != crc2) || (crc == 0xffff)) |
251 | { |
251 | { |
252 | retval = EEPROM_WriteBlock(EEPROM_ADR_OEM_Name,OEM_String,OEM_NAME_LENGHT); |
252 | retval = EEPROM_WriteBlock(EEPROM_ADR_OEM_Name,OEM_String,OEM_NAME_LENGHT); |
253 | if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc,2); |
253 | if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc,2); |
254 | } |
254 | } |
255 | } |
255 | } |
256 | 256 | ||
257 | u8 ReadOemNameFromEEPROM(void) |
257 | u8 ReadOemNameFromEEPROM(void) |
258 | { |
258 | { |
259 | u8 retval = 0, tmp; |
259 | u8 retval = 0, tmp; |
260 | u16 crc, crc2; |
260 | u16 crc, crc2; |
261 | tmp = EEPROM_ReadBlock(EEPROM_ADR_OEM_Name,OEM_String,OEM_NAME_LENGHT); |
261 | tmp = EEPROM_ReadBlock(EEPROM_ADR_OEM_Name,OEM_String,OEM_NAME_LENGHT); |
262 | if(EEPROM_SUCCESS == tmp) retval = 1; |
262 | if(EEPROM_SUCCESS == tmp) retval = 1; |
263 | crc = 2; |
263 | crc = 2; |
264 | crc += CRC16(OEM_String,16); |
264 | crc += CRC16(OEM_String,16); |
265 | EEPROM_ReadBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc2,2); |
265 | EEPROM_ReadBlock(EEPROM_ADR_OEM_Name_CRC,(u8 *)&crc2,2); |
266 | if((crc == crc2) && OEM_String[0] != 0 && OEM_String[0] != 0xff) |
266 | if((crc == crc2) && OEM_String[0] != 0 && OEM_String[0] != 0xff) |
267 | { |
267 | { |
268 | memcpy(&LicensePtr[OEM_IN_LICENSE],OEM_String, OEM_NAME_LENGHT); |
268 | memcpy(&LicensePtr[OEM_IN_LICENSE],OEM_String, OEM_NAME_LENGHT); |
269 | SendOemName = 1; |
269 | SendOemName = 1; |
270 | return(1); |
270 | return(1); |
271 | } |
271 | } |
272 | else |
272 | else |
273 | { |
273 | { |
274 | OEM_String[0] = 255; // delete |
274 | OEM_String[0] = 255; // delete |
275 | return(0); |
275 | return(0); |
276 | } |
276 | } |
277 | } |
277 | } |
278 | 278 | ||
279 | 279 | ||
280 | void WriteLicenseToEEPROM(u8 kompatibel) |
280 | void WriteLicenseToEEPROM(u8 kompatibel) |
281 | { |
281 | { |
282 | u16 crc,crc2; |
282 | u16 crc,crc2; |
283 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
283 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
284 | if(LicensePtr[OEM_IN_LICENSE] != 255) WriteOemNameToEEPROM(); // then we have a OEM name to write |
284 | if(LicensePtr[OEM_IN_LICENSE] != 255) WriteOemNameToEEPROM(); // then we have a OEM name to write |
285 | crc = kompatibel; |
285 | crc = kompatibel; |
286 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
286 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
287 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
287 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
288 | if((crc != crc2) || (crc == 0xffff)) |
288 | if((crc != crc2) || (crc == 0xffff)) |
289 | { |
289 | { |
290 | retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
290 | retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
291 | if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc,2); |
291 | if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc,2); |
292 | } |
292 | } |
293 | } |
293 | } |
294 | 294 | ||
295 | void ClearLicenseText(void) |
295 | void ClearLicenseText(void) |
296 | { |
296 | { |
297 | u32 i; |
297 | u32 i; |
298 | for(i=0; i < LICENSE_SIZE_TEXT;i++) LicensePtr[i] = 0; //Achtung: das leert die ganze Struktur, nicht nur den Namen |
298 | for(i=0; i < LICENSE_SIZE_TEXT;i++) LicensePtr[i] = 0; //Achtung: das leert die ganze Struktur, nicht nur den Namen |
299 | } |
299 | } |
300 | 300 | ||
301 | void DeleteLicenseInEEPROM(void) |
301 | void DeleteLicenseInEEPROM(void) |
302 | { |
302 | { |
303 | ClearLicenseText(); |
303 | ClearLicenseText(); |
304 | WriteLicenseToEEPROM(0); |
304 | WriteLicenseToEEPROM(0); |
305 | } |
305 | } |
306 | 306 | ||
307 | u8 ReadLicenseFromEEPROM(void) |
307 | u8 ReadLicenseFromEEPROM(void) |
308 | { |
308 | { |
309 | u8 retval = 0, tmp; |
309 | u8 retval = 0, tmp; |
310 | u16 crc, crc2; |
310 | u16 crc, crc2; |
311 | 311 | ||
312 | tmp = EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
312 | tmp = EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT); |
313 | if(EEPROM_SUCCESS == tmp) retval = 1; |
313 | if(EEPROM_SUCCESS == tmp) retval = 1; |
314 | 314 | ||
315 | crc = EEPROM_LICENSE_DATA_KOMPATIBEL; |
315 | crc = EEPROM_LICENSE_DATA_KOMPATIBEL; |
316 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
316 | crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT); |
317 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
317 | EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2); |
318 | if(crc != crc2) retval++; |
318 | if(crc != crc2) retval++; |
319 | if(retval && LicensePtr[0] != 0 && LicensePtr[0] != 0xff) |
319 | if(retval && LicensePtr[0] != 0 && LicensePtr[0] != 0xff) |
320 | { |
320 | { |
321 | return(1); |
321 | return(1); |
322 | } |
322 | } |
323 | else |
323 | else |
324 | { |
324 | { |
325 | ClearLicenseText(); |
325 | ClearLicenseText(); |
326 | return(0); |
326 | return(0); |
327 | } |
327 | } |
328 | } |
328 | } |
329 | 329 | ||
330 | void WriteBaroCalibrationToEEprom(void) |
330 | void WriteBaroCalibrationToEEprom(void) |
331 | { |
331 | { |
332 | #define BARO_KOMPATIBLE 1 |
332 | #define BARO_KOMPATIBLE 1 |
333 | s16 data[2]; |
333 | s16 data[2]; |
334 | data[0] = LuftdruckTemperaturKompensation; |
334 | data[0] = LuftdruckTemperaturKompensation; |
335 | data[1] = data[0] + BARO_KOMPATIBLE; |
335 | data[1] = data[0] + BARO_KOMPATIBLE; |
336 | EEPROM_WriteBlock(EEPROM_ADR_BARO_KALIBRATION,(u8 *)&data,4); |
336 | EEPROM_WriteBlock(EEPROM_ADR_BARO_KALIBRATION,(u8 *)&data,4); |
337 | NCParams[NCPARAMS_BARO_KOMPENSATION] = LuftdruckTemperaturKompensation; |
337 | NCParams[NCPARAMS_BARO_KOMPENSATION] = LuftdruckTemperaturKompensation; |
338 | NCParamState[NCPARAMS_BARO_KOMPENSATION] = NCRARAM_STATE_VALID; |
338 | NCParamState[NCPARAMS_BARO_KOMPENSATION] = NCRARAM_STATE_VALID; |
339 | } |
339 | } |
340 | 340 | ||
341 | void ReadBaroCalibrationfromEEprom(void) |
341 | void ReadBaroCalibrationfromEEprom(void) |
342 | { |
342 | { |
343 | s16 data[2]; |
343 | s16 data[2]; |
344 | EEPROM_ReadBlock(EEPROM_ADR_BARO_KALIBRATION,(u8 *)&data,4); |
344 | EEPROM_ReadBlock(EEPROM_ADR_BARO_KALIBRATION,(u8 *)&data,4); |
345 | if(data[0] + BARO_KOMPATIBLE == data[1]) |
345 | if(data[0] + BARO_KOMPATIBLE == data[1]) |
346 | { |
346 | { |
347 | LuftdruckTemperaturKompensation = data[0]; |
347 | LuftdruckTemperaturKompensation = data[0]; |
348 | if(abs(LuftdruckTemperaturKompensation) > 250) LuftdruckTemperaturKompensation = 0; |
348 | if(abs(LuftdruckTemperaturKompensation) > 250) LuftdruckTemperaturKompensation = 0; |
349 | } |
349 | } |
350 | 350 | ||
351 | NCParams[NCPARAMS_BARO_KOMPENSATION] = LuftdruckTemperaturKompensation; |
351 | NCParams[NCPARAMS_BARO_KOMPENSATION] = LuftdruckTemperaturKompensation; |
352 | NCParamState[NCPARAMS_BARO_KOMPENSATION] = NCRARAM_STATE_VALID; |
352 | NCParamState[NCPARAMS_BARO_KOMPENSATION] = NCRARAM_STATE_VALID; |
353 | if(LuftdruckTemperaturKompensation) UART1_PutString("\r\n Baro Temperature Calibration active\r\n"); |
353 | if(LuftdruckTemperaturKompensation) UART1_PutString(" Baro Temperature Calibration active\r\n"); |
354 | } |
354 | } |
355 | 355 | ||
356 | void WriteBaudrateIndexToEEprom(void) |
356 | void WriteBaudrateIndexToEEprom(void) |
357 | { |
357 | { |
358 | u8 data[2]; |
358 | u8 data[2]; |
359 | #define BAUD_KOMPATIBLE 1 |
359 | #define BAUD_KOMPATIBLE 1 |
360 | data[0] = ToFcBaudrateIndex; |
360 | data[0] = ToFcBaudrateIndex; |
361 | data[1] = ToFcBaudrateIndex + BAUD_KOMPATIBLE; |
361 | data[1] = ToFcBaudrateIndex + BAUD_KOMPATIBLE; |
362 | EEPROM_WriteBlock(EEPROM_ADR_BAUDRATE_INDEX,(u8 *)&data,2); |
362 | EEPROM_WriteBlock(EEPROM_ADR_BAUDRATE_INDEX,(u8 *)&data,2); |
363 | // UART1_PutString(" EE-BD-WRITE:"); UART1_Putchar('0'+ToFcBaudrateIndex); |
363 | // UART1_PutString(" EE-BD-WRITE:"); UART1_Putchar('0'+ToFcBaudrateIndex); |
364 | } |
364 | } |
365 | 365 | ||
366 | void ReadBaudrateIndexfromEEprom(void) |
366 | void ReadBaudrateIndexfromEEprom(void) |
367 | { |
367 | { |
368 | u8 data[2]; |
368 | u8 data[2]; |
369 | EEPROM_ReadBlock(EEPROM_ADR_BAUDRATE_INDEX,(u8 *)&data,2); |
369 | EEPROM_ReadBlock(EEPROM_ADR_BAUDRATE_INDEX,(u8 *)&data,2); |
370 | if(data[0] + BAUD_KOMPATIBLE == data[1]) |
370 | if(data[0] + BAUD_KOMPATIBLE == data[1]) |
371 | { |
371 | { |
372 | ToFcBaudrateIndex = data[0]; |
372 | ToFcBaudrateIndex = data[0]; |
373 | Uart1Baudrate = BAUDRATES[ToFcBaudrateIndex]; |
373 | Uart1Baudrate = BAUDRATES[ToFcBaudrateIndex]; |
374 | // UART1_PutString(" EE-BD-OK:"); UART1_Putchar('0'+ToFcBaudrateIndex); |
374 | // UART1_PutString(" EE-BD-OK:"); UART1_Putchar('0'+ToFcBaudrateIndex); |
375 | 375 | ||
376 | } |
376 | } |
377 | else UART1_PutString(" EE-BD-ERR "); |
377 | else UART1_PutString(" EE-BD-ERR "); |
378 | } |
378 | } |
379 | 379 | ||
380 | 380 | ||
381 | 381 | ||
382 | 382 | ||
383 | 383 | ||
384 | 384 |