Subversion Repositories NaviCtrl

Rev

Rev 360 | 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"
59
#include "i2c.h"
60
#include "uart1.h"
61
#include "timer1.h"
62
#include "eeprom.h"
264 killagreg 63
#include "led.h"
248 killagreg 64
 
65
 
66
// The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller.
67
// The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0.
68
 
69
#define EEPROM_I2C_ADDRESS      0xA0
70
#define EEPROM_SIZE                     65536L  // 64k bytes
71
#define EEPROM_WRITE            1
72
#define EEPROM_READ                     0
73
 
74
// timing
254 killagreg 75
#define I2C_IDLE_TIMEOUT                150 // 150 ms
76
#define I2C_TRANSFER_TIMEOUT    150 // 150 ms
248 killagreg 77
#define I2C_ACCESS_RETRYS               20      // retry 20 times
78
 
79
// globals for rx handler
254 killagreg 80
volatile u8     *EEPROM_pData = 0;
81
volatile u16 EEPROM_DataLen = 0;
248 killagreg 82
 
252 killagreg 83
// rx data handler for eeprom data read access
84
void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize)
248 killagreg 85
{       // if number of byte are matching
254 killagreg 86
        if((RxBufferSize == EEPROM_DataLen))
248 killagreg 87
        {       //copy data from primary buffer to target buffer 
254 killagreg 88
                if(EEPROM_pData) memcpy((u8*)EEPROM_pData, pRxBuffer, EEPROM_DataLen);
256 killagreg 89
        }        
248 killagreg 90
}
91
 
92
// ----------------------------------------------------------------------------------------
93
EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen)
94
{
252 killagreg 95
        u16 i, TxBytes = 0;
248 killagreg 96
        u8 retry = 0;
97
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
98
 
99
        if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1))
100
        {
101
                retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
102
                return(retval);
103
        }
104
        if((DataLen+2) > I2C_BUFFER_LEN)
105
        {
106
                retval = EEPROM_I2C_BUFFER_OVERRUN;
107
                return(retval);
108
        }
109
        do
110
        {
254 killagreg 111
                if(!I2C_LockBuffer(I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
248 killagreg 112
                // buffer is now locked
113
                TxBytes = 0;
114
                // transmitt address
115
                I2C_Buffer[TxBytes++] = (u8)(0x00FF & (Address>>8));
116
                I2C_Buffer[TxBytes++] = (u8)(0x00FF & Address);
117
                if(Direction == EEPROM_WRITE)
118
                {       // copy data to i2c transfer buffer
119
                        for(i = 0; i<DataLen;i++)
120
                        {
121
                                I2C_Buffer[TxBytes++] = pData[i];
122
                        }
123
                        // prepare pointer to rx data
124
                        EEPROM_pData = 0;
125
                        EEPROM_DataLen = 0;
126
                        // start transmission
127
                        if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, 0, 0))
128
                        {
129
                                return(retval);
130
                        }
131
                }
132
                else // Direction == EEPROM_READ
133
                {
134
                        // prepare pointer to rx data
135
                        EEPROM_pData = pData;
136
                        EEPROM_DataLen = DataLen;
137
                        // start transmission
252 killagreg 138
                        if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxDataHandler, DataLen))
248 killagreg 139
                        {
140
                                return(retval);
141
                        }
142
                }
143
                //wait for end of this transfer
144
                if(I2C_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT))
145
                {
256 killagreg 146
                        if(I2C_Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
147
                        else retval = EEPROM_DATA_TRANSFER_INCOMPLETE;
248 killagreg 148
                }
256 killagreg 149
                else// i2c transfer timed out
150
                {
151
                        return(EEPROM_ERROR_I2C_TRANSFER_TIMEOUT);     
152
                }
153
                // or retry             
248 killagreg 154
        } while(++retry < I2C_ACCESS_RETRYS);
155
        return(retval);
156
}
157
 
158
EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen)
159
{
160
        u16 len;
161
        EEPROM_Result_t retval;
162
 
163
        // there can be 32 bytes written in a single cycle if the adresses are locates in the same row
164
        // i.e. the upper 11 bits of the addres are constant
165
        #define EEPROM_WRITE_BLOCK_SIZE 32
166
 
167
        len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row
168
        if(len >= DataLen) len = DataLen; // do not write more than neccesarry
169
 
170
        do
171
        {
252 killagreg 172
                retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len);
248 killagreg 173
                Address += len; // Address of the next row
174
                pData += len;   // pointer to start of next data block
175
                DataLen -= len; // reduce the number of to be transmitted
176
                if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32
177
                else len = DataLen;
178
        }
179
        while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured
180
 
181
        return(retval);
182
}
183
 
184
// a data block can be subsequently read after setting the start address once
185
EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen)
186
{
252 killagreg 187
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
188
        u16 AdrOffset = 0;
189
        u16 RxLen;
190
 
191
        while(DataLen > 0)
192
        {
193
                if(DataLen > I2C_BUFFER_LEN) RxLen = I2C_BUFFER_LEN;
194
                else RxLen = DataLen;
195
 
196
                retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen);
197
                if(retval != EEPROM_SUCCESS) break;
198
                AdrOffset += RxLen;
199
                DataLen -= RxLen;
200
        }
201
        return(retval);
248 killagreg 202
}
264 killagreg 203
 
204
u8 EEPROM_Init(void)
205
{
206
        u8 data[20];
207
        u8 i, retval = 0;
208
        UART1_PutString("\r\n EEprom init..");
209
        // check if data can be read from eeprom
210
        for(i=0;i<10;i++)
211
        {
212
                UART1_Putchar('.');
213
                if(EEPROM_SUCCESS == EEPROM_ReadBlock(0, data, 20)) retval = 1;
214
                if(retval) break;              
215
        }
216
        if(retval)  UART1_PutString("ok");
312 holgerb 217
        else            UART1_PutString("failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
264 killagreg 218
        return(retval);
219
}