Subversion Repositories NaviCtrl

Rev

Rev 526 | Rev 740 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 526 Rev 530
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 "91x_lib.h"
58
#include "91x_lib.h"
59
#include "i2c.h"
59
#include "i2c.h"
60
#include "uart1.h"
60
#include "uart1.h"
61
#include "timer1.h"
61
#include "timer1.h"
62
#include "eeprom.h"
62
#include "eeprom.h"
63
#include "led.h"
63
#include "led.h"
64
#include "crc16.h"
64
#include "crc16.h"
65
 
65
 
66
 
66
 
67
 
67
 
68
// The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller.
68
// The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller.
69
#define EEPROM_I2C I2C1
69
#define EEPROM_I2C I2C1
70
 
70
 
71
// The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0.
71
// The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0.
72
 
72
 
73
#define EEPROM_I2C_ADDRESS      0xA0
73
#define EEPROM_I2C_ADDRESS      0xA0
74
#define EEPROM_SIZE                     65536L  // 64k bytes
74
#define EEPROM_SIZE                     65536L  // 64k bytes
75
#define EEPROM_WRITE            1
75
#define EEPROM_WRITE            1
76
#define EEPROM_READ                     0
76
#define EEPROM_READ                     0
77
 
77
 
78
// timing
78
// timing
79
#define I2C_IDLE_TIMEOUT                150 // 150 ms
79
#define I2C_IDLE_TIMEOUT                150 // 150 ms
80
#define I2C_TRANSFER_TIMEOUT    150 // 150 ms
80
#define I2C_TRANSFER_TIMEOUT    150 // 150 ms
81
#define I2C_ACCESS_RETRYS               20      // retry 20 times
81
#define I2C_ACCESS_RETRYS               20      // retry 20 times
82
 
82
 
83
// globals for rx handler
83
// globals for rx handler
84
volatile u8     *EEPROM_pData = 0;
84
volatile u8     *EEPROM_pData = 0;
85
volatile u16 EEPROM_DataLen = 0;
85
volatile u16 EEPROM_DataLen = 0;
86
 
86
 
87
// rx data handler for eeprom data read access
87
// rx data handler for eeprom data read access
88
void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize)
88
void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize)
89
{       // if number of byte are matching
89
{       // if number of byte are matching
90
        if((RxBufferSize == EEPROM_DataLen))
90
        if((RxBufferSize == EEPROM_DataLen))
91
        {       //copy data from primary buffer to target buffer 
91
        {       //copy data from primary buffer to target buffer 
92
                if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen);
92
                if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen);
93
        }        
93
        }        
94
}
94
}
95
 
95
 
96
// ----------------------------------------------------------------------------------------
96
// ----------------------------------------------------------------------------------------
97
EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen)
97
EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen)
98
{
98
{
99
        u16 i;
99
        u16 i;
100
        u8 retry = 0;  
100
        u8 retry = 0;  
101
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
101
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
102
 
102
 
103
        if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1))
103
        if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1))
104
        {
104
        {
105
                retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
105
                retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
106
                return(retval);
106
                return(retval);
107
        }
107
        }
108
        if((DataLen+2) > I2C_BUFFER_LEN)
108
        if((DataLen+2) > I2C_BUFFER_LEN)
109
        {
109
        {
110
                retval = EEPROM_I2C_BUFFER_OVERRUN;
110
                retval = EEPROM_I2C_BUFFER_OVERRUN;
111
                return(retval);
111
                return(retval);
112
        }
112
        }
113
        do
113
        do
114
        {
114
        {
115
                if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
115
                if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
116
                // buffer is now locked
116
                // buffer is now locked
117
                u8 TxBytes = 0;
117
                u8 TxBytes = 0;
118
                u8 TxData[255];
118
                u8 TxData[255];
119
                // transmitt address
119
                // transmitt address
120
                TxData[TxBytes++] = (u8)(0x00FF & (Address>>8));
120
                TxData[TxBytes++] = (u8)(0x00FF & (Address>>8));
121
                TxData[TxBytes++] = (u8)(0x00FF & Address);
121
                TxData[TxBytes++] = (u8)(0x00FF & Address);
122
                if(Direction == EEPROM_WRITE)
122
                if(Direction == EEPROM_WRITE)
123
                {       // copy data to i2c transfer buffer
123
                {       // copy data to i2c transfer buffer
124
                        for(i = 0; i<DataLen;i++)
124
                        for(i = 0; i<DataLen;i++)
125
                        {
125
                        {
126
                                TxData[TxBytes++] = pData[i];
126
                                TxData[TxBytes++] = pData[i];
127
                        }
127
                        }
128
                        // prepare pointer to rx data
128
                        // prepare pointer to rx data
129
                        EEPROM_pData = 0;
129
                        EEPROM_pData = 0;
130
                        EEPROM_DataLen = 0;
130
                        EEPROM_DataLen = 0;
131
                        // start transmission
131
                        // start transmission
132
                        if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0))
132
                        if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0))
133
                        {
133
                        {
134
                                return(retval);
134
                                return(retval);
135
                        }
135
                        }
136
                }
136
                }
137
                else // Direction == EEPROM_READ
137
                else // Direction == EEPROM_READ
138
                {
138
                {
139
                        // prepare pointer to rx data
139
                        // prepare pointer to rx data
140
                        EEPROM_pData = pData;
140
                        EEPROM_pData = pData;
141
                        EEPROM_DataLen = DataLen;
141
                        EEPROM_DataLen = DataLen;
142
                        // start transmission
142
                        // start transmission
143
                        if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen))
143
                        if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen))
144
                        {
144
                        {
145
                                return(retval);
145
                                return(retval);
146
                        }
146
                        }
147
                }
147
                }
148
                //wait for end of this transfer
148
                //wait for end of this transfer
149
                if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT))
149
                if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT))
150
                {
150
                {
151
                        if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
151
                        if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
152
                        else retval = EEPROM_DATA_TRANSFER_INCOMPLETE;
152
                        else retval = EEPROM_DATA_TRANSFER_INCOMPLETE;
153
                }
153
                }
154
                else// i2c transfer timed out
154
                else// i2c transfer timed out
155
                {
155
                {
156
                        return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT);     
156
                        return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT);     
157
                }
157
                }
158
                // or retry             
158
                // or retry             
159
        } while(++retry < I2C_ACCESS_RETRYS);
159
        } while(++retry < I2C_ACCESS_RETRYS);
160
        return(retval);
160
        return(retval);
161
}
161
}
162
 
162
 
163
EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen)
163
EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen)
164
{
164
{
165
        u16 len;
165
        u16 len;
166
        EEPROM_Result_t retval;
166
        EEPROM_Result_t retval;
167
 
167
 
168
        // there can be 32 bytes written in a single cycle if the adresses are locates in the same row
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
169
        // i.e. the upper 11 bits of the addres are constant
170
        #define EEPROM_WRITE_BLOCK_SIZE 32
170
        #define EEPROM_WRITE_BLOCK_SIZE 32
171
 
171
 
172
        len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row
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
173
        if(len >= DataLen) len = DataLen; // do not write more than neccesarry
174
 
174
 
175
        do
175
        do
176
        {
176
        {
177
                retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len);
177
                retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len);
178
                Address += len; // Address of the next row
178
                Address += len; // Address of the next row
179
                pData += len;   // pointer to start of next data block
179
                pData += len;   // pointer to start of next data block
180
                DataLen -= len; // reduce the number of to be transmitted
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
181
                if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32
182
                else len = DataLen;
182
                else len = DataLen;
183
        }
183
        }
184
        while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured
184
        while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured
185
 
185
 
186
        return(retval);
186
        return(retval);
187
}
187
}
188
 
188
 
189
// a data block can be subsequently read after setting the start address once
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)
190
EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen)
191
{
191
{
192
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
192
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
193
        u16 AdrOffset = 0;
193
        u16 AdrOffset = 0;
194
        u16 RxLen;
194
        u16 RxLen;
195
 
195
 
196
        while(DataLen > 0)
196
        while(DataLen > 0)
197
        {
197
        {
198
                if(DataLen > I2C_BUFFER_LEN - 2) RxLen = I2C_BUFFER_LEN - 2;
198
                if(DataLen > I2C_BUFFER_LEN - 2) RxLen = I2C_BUFFER_LEN - 2;
199
                else RxLen = DataLen;
199
                else RxLen = DataLen;
200
           
200
           
201
                retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen);
201
                retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen);
202
                if(retval != EEPROM_SUCCESS) break;
202
                if(retval != EEPROM_SUCCESS) break;
203
                AdrOffset += RxLen;
203
                AdrOffset += RxLen;
204
                DataLen -= RxLen;
204
                DataLen -= RxLen;
205
        }
205
        }
206
        return(retval);
206
        return(retval);
207
}
207
}
208
 
208
 
209
u8 EEPROM_Init(void)
209
u8 EEPROM_Init(void)
210
{
210
{
211
        u8 data[20];
211
        u8 data[20];
212
        u8 i, retval = 0;
212
        u8 i, retval = 0;
213
        UART1_PutString("\r\n EEprom init..");
213
        UART1_PutString("\r\n EEprom init..");
214
        // check if data can be read from eeprom
214
        // check if data can be read from eeprom
215
        for(i=0;i<10;i++)
215
        for(i=0;i<10;i++)
216
        {
216
        {
217
                UART1_Putchar('.');
217
                UART1_Putchar('.');
218
                if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1;
218
                if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1;
219
                if(retval) break;              
219
                if(retval) break;              
220
        }
220
        }
221
        if(retval)  UART1_PutString("ok");
221
        if(retval)  UART1_PutString("ok");
222
        else            UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
222
        else            UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
223
        return(retval);
223
        return(retval);
224
}
224
}
225
 
225
 
226
 
226
 
227
void WriteLicenseToEEPROM(u8 kompatibel)
227
void WriteLicenseToEEPROM(u8 kompatibel)
228
{
228
{
229
//      u8 msg[150];    
-
 
230
        u16 crc,crc2;
229
        u16 crc,crc2;
231
    EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
230
    EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
232
        crc = kompatibel;
231
        crc = kompatibel;
233
        crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT);
232
        crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT);
234
        EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2);
233
        EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2);
235
   if(crc != crc2)
234
   if((crc != crc2) || (crc == 0xffff))
236
   {
235
   {
237
//      UART1_PutString("\r\n Copy License to EEPROM...");
-
 
238
        retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT);
236
        retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT);
239
        if(retval == EEPROM_SUCCESS) retval = EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc,2);
237
        if(retval == EEPROM_SUCCESS) EEPROM_WriteBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc,2);
240
/*
-
 
241
        if(retval == EEPROM_SUCCESS)  UART1_PutString("ok");
-
 
242
        else            UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-
 
243
        sprintf(msg, " (0x%04x)",crc);
-
 
244
        UART1_PutString(msg);
-
 
245
*/
-
 
246
   }
238
   }
247
//   else UART1_PutString("\r\n Copy of License in EEPROM ");
-
 
248
}
239
}
249
 
240
 
250
void ClearLicenseText(void)
241
void ClearLicenseText(void)
251
{
242
{
252
 u32 i;
243
 u32 i;
253
 for(i=0; i < LICENSE_SIZE_TEXT;i++) LicensePtr[i] = 0; //Achtung: das leert die ganze Struktur, nicht nur den Namen
244
 for(i=0; i < LICENSE_SIZE_TEXT;i++) LicensePtr[i] = 0; //Achtung: das leert die ganze Struktur, nicht nur den Namen
254
}
245
}
255
 
246
 
256
void DeleteLicenseInEEPROM(void)
247
void DeleteLicenseInEEPROM(void)
257
{
248
{
258
 ClearLicenseText();
249
 ClearLicenseText();
259
 WriteLicenseToEEPROM(0);
250
 WriteLicenseToEEPROM(0);
260
}
251
}
261
 
252
 
262
u8 ReadLicenseFromEEPROM(void)
253
u8 ReadLicenseFromEEPROM(void)
263
{
254
{
264
        u8 retval = 0, tmp;
255
        u8 retval = 0, tmp;
265
//      u8 msg[150];    
-
 
266
        u16 crc, crc2;
256
        u16 crc, crc2;
267
 
-
 
268
//      UART1_PutString("\r\n Read License from EEPROM...");
257
 
269
        tmp = EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT);
258
        tmp = EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA,LicensePtr,LICENSE_SIZE_TEXT);
270
        if(EEPROM_SUCCESS == tmp) retval = 1;
259
        if(EEPROM_SUCCESS == tmp) retval = 1;
271
 
260
 
272
        crc = EEPROM_LICENSE_DATA_KOMPATIBEL;
261
        crc = EEPROM_LICENSE_DATA_KOMPATIBEL;
273
        crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT);
262
        crc += CRC16(LicensePtr,LICENSE_SIZE_TEXT);
274
        EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2);
263
        EEPROM_ReadBlock(EEPROM_ADR_LICENSE_DATA_CRC,(u8 *)&crc2,2);
275
 
264
 
276
        if(crc != crc2) retval++;
265
        if(crc != crc2) retval++;
277
        if(retval && LicensePtr[0] != 0 && LicensePtr[0] != 0xff)  
266
        if(retval && LicensePtr[0] != 0 && LicensePtr[0] != 0xff)  
278
         {
267
         {
279
//      sprintf(msg, "ok (0x%04x)",crc);
-
 
280
//        UART1_PutString(msg);
-
 
281
          return(1);
268
          return(1);
282
         }
269
         }
283
        else           
270
        else           
284
         {
271
         {
285
          UART1_PutString("not found !");
-
 
286
          ClearLicenseText();
272
          ClearLicenseText();
287
          return(0);
273
          return(0);
288
         }
274
         }
289
}
275
}
290
 
276
 
291
 
277
 
292
 
278
 
293
 
279
 
294
 
280