Subversion Repositories NaviCtrl

Rev

Rev 252 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 killagreg 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <string.h>
58
#include "91x_lib.h"
59
#include "i2c.h"
60
#include "uart1.h"
61
#include "timer1.h"
62
#include "eeprom.h"
63
 
64
 
65
// The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller.
66
// The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0.
67
 
68
#define EEPROM_I2C_ADDRESS      0xA0
69
#define EEPROM_SIZE                     65536L  // 64k bytes
70
#define EEPROM_WRITE            1
71
#define EEPROM_READ                     0
72
 
73
// timing
74
#define I2C_IDLE_TIMEOUT                50  // 50 ms
75
#define I2C_TRANSFER_TIMEOUT    50  // 50 ms
76
#define I2C_ACCESS_RETRYS               20      // retry 20 times
77
 
78
// globals for rx handler
79
u8      *EEPROM_pData = 0;
80
u16 EEPROM_DataLen = 0;
81
 
82
// rx data handler for eeprom data read
83
void EEPROM_RxData(u8* pRxBuffer, u8 RxBufferSize)
84
{       // if number of byte are matching
85
        if(RxBufferSize == EEPROM_DataLen)
86
        {       //copy data from primary buffer to target buffer 
87
                if(EEPROM_pData) memcpy(EEPROM_pData, pRxBuffer, sizeof(EEPROM_DataLen));
88
        }                
89
}
90
 
91
// ----------------------------------------------------------------------------------------
92
EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen)
93
{
94
        u16 i, TxBytes;
95
        u8 retry = 0;
96
        EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
97
 
98
        if(((u32)Address + (u32)DataLen) >= (EEPROM_SIZE - 1))
99
        {
100
                retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
101
                return(retval);
102
        }
103
        if((DataLen+2) > I2C_BUFFER_LEN)
104
        {
105
                retval = EEPROM_I2C_BUFFER_OVERRUN;
106
                return(retval);
107
        }
108
        do
109
        {
110
                if(!I2C_LockBuffer(I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_TRANSFER_TIMEOUT;
111
                // buffer is now locked
112
                TxBytes = 0;
113
                // transmitt address
114
                I2C_Buffer[TxBytes++] = (u8)(0x00FF & (Address>>8));
115
                I2C_Buffer[TxBytes++] = (u8)(0x00FF & Address);
116
                if(Direction == EEPROM_WRITE)
117
                {       // copy data to i2c transfer buffer
118
                        for(i = 0; i<DataLen;i++)
119
                        {
120
                                I2C_Buffer[TxBytes++] = pData[i];
121
                        }
122
                        // prepare pointer to rx data
123
                        EEPROM_pData = 0;
124
                        EEPROM_DataLen = 0;
125
                        // start transmission
126
                        if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, 0, 0))
127
                        {
128
                                return(retval);
129
                        }
130
                }
131
                else // Direction == EEPROM_READ
132
                {
133
                        // prepare pointer to rx data
134
                        EEPROM_pData = pData;
135
                        EEPROM_DataLen = DataLen;
136
                        // start transmission
137
                        if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxData, DataLen))
138
                        {
139
                                return(retval);
140
                        }
141
                }
142
                //wait for end of this transfer
143
                if(I2C_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT))
144
                {
145
                        retval = EEPROM_SUCCESS;
146
                        break;
147
                }
148
                // or retry
149
                I2C_State = I2C_STATE_IDLE;    
150
        } while(++retry < I2C_ACCESS_RETRYS);
151
        return(retval);
152
}
153
 
154
EEPROM_Result_t EEPROM_WriteBlock(u16 Address, u8 *pData, u16 DataLen)
155
{
156
        u16 len;
157
        EEPROM_Result_t retval;
158
 
159
        // there can be 32 bytes written in a single cycle if the adresses are locates in the same row
160
        // i.e. the upper 11 bits of the addres are constant
161
        #define EEPROM_WRITE_BLOCK_SIZE 32
162
 
163
        len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row
164
        if(len >= DataLen) len = DataLen; // do not write more than neccesarry
165
 
166
        do
167
        {
168
                retval = EEPROM_Transfer(I2C_MODE_TRANSMITTER, Address, pData, len);
169
                Address += len; // Address of the next row
170
                pData += len;   // pointer to start of next data block
171
                DataLen -= len; // reduce the number of to be transmitted
172
                if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32
173
                else len = DataLen;
174
        }
175
        while(DataLen && (retval == EEPROM_SUCCESS)); // repeat until all data have been sent and no error has occured
176
 
177
        return(retval);
178
}
179
 
180
// a data block can be subsequently read after setting the start address once
181
EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen)
182
{
183
        return EEPROM_Transfer(I2C_MODE_RECEIVER, Address, pData, DataLen);
184
}