Subversion Repositories NaviCtrl

Rev

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

Rev 241 Rev 242
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
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
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
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
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.
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
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
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
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <string.h>
57
#include <string.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 "config.h"
62
#include "config.h"
63
#include "led.h"
63
#include "led.h"
64
 
64
 
65
volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
65
volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
66
 
66
 
67
// rxbuffer
67
// rxbuffer
68
volatile u8 I2C_RxBufferSize;
68
volatile u8 I2C_RxBufferSize;
69
volatile u8 I2C_RxBuffer[I2C_RXBUFFER_LEN];
69
volatile u8 I2C_RxBuffer[I2C_RXBUFFER_LEN];
70
// txbuffer
70
// txbuffer
71
volatile u8 I2C_TxBufferSize;
71
volatile u8 I2C_TxBufferSize;
72
volatile u8 I2C_TxBuffer[I2C_TXBUFFER_LEN];
72
volatile u8 I2C_TxBuffer[I2C_TXBUFFER_LEN];
73
 
73
 
74
volatile u8 I2C_Direction;
74
volatile u8 I2C_Direction;
75
volatile u8 I2C_SlaveAddress = 0x00;
75
volatile u8 I2C_SlaveAddress = 0x00;
76
I2C_pRxHandler_t I2C_pRxHandler = NULL;
76
I2C_pRxHandler_t I2C_pRxHandler = NULL;
77
 
77
 
78
volatile u32 I2C1_Timeout = 0;
78
volatile u32 I2C1_Timeout = 0;
79
 
79
 
80
//--------------------------------------------------------------
80
//--------------------------------------------------------------
81
void I2C1_Init(void)
81
void I2C1_Init(void)
82
{
82
{
83
        I2C_InitTypeDef   I2C_Struct;
83
        I2C_InitTypeDef   I2C_Struct;
84
        GPIO_InitTypeDef  GPIO_InitStructure;
84
        GPIO_InitTypeDef  GPIO_InitStructure;
85
       
85
       
86
        I2C_State = I2C_STATE_OFF;
86
        I2C_State = I2C_STATE_OFF;
87
 
87
 
88
        UART1_PutString("\r\n I2C init...");
88
        UART1_PutString("\r\n I2C init...");
89
        // enable Port 2 peripherie
89
        // enable Port 2 peripherie
90
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
90
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
91
        // disable a reset state
91
        // disable a reset state
92
        SCU_APBPeriphReset(__GPIO2, DISABLE);
92
        SCU_APBPeriphReset(__GPIO2, DISABLE);
93
 
93
 
94
        // free a busy bus
94
        // free a busy bus
95
 
95
 
96
        // At switch on I2C devices can get in a state where they
96
        // At switch on I2C devices can get in a state where they
97
        // are still waiting for a command due to all the bus lines bouncing
97
        // are still waiting for a command due to all the bus lines bouncing
98
        // around at startup have started clocking data into the device(s).
98
        // around at startup have started clocking data into the device(s).
99
        // Enable the ports as open collector port outputs
99
        // Enable the ports as open collector port outputs
100
        // and clock out at least 9 SCL pulses, then generate a stop
100
        // and clock out at least 9 SCL pulses, then generate a stop
101
        // condition and then leave the clock line high.
101
        // condition and then leave the clock line high.
102
 
102
 
103
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
103
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
104
        GPIO_StructInit(&GPIO_InitStructure);
104
        GPIO_StructInit(&GPIO_InitStructure);
105
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
105
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
106
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
106
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
107
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
107
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
108
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
108
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
109
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
109
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
110
        GPIO_Init(GPIO2, &GPIO_InitStructure);
110
        GPIO_Init(GPIO2, &GPIO_InitStructure);
111
 
111
 
112
        u8 i;
112
        u8 i;
113
        u32 delay;
113
        u32 delay;
114
        // set SCL high and then SDA to low (start condition)
114
        // set SCL high and then SDA to low (start condition)
115
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
115
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
116
        delay = SetDelay(1);
116
        delay = SetDelay(1);
117
        while (!CheckDelay(delay));
117
        while (!CheckDelay(delay));
118
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
118
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
119
        // toggle SCL at least 10 times from high to low to high
119
        // toggle SCL at least 10 times from high to low to high
120
        for(i = 0; i < 10; i++)
120
        for(i = 0; i < 10; i++)
121
        {
121
        {
122
                delay = SetDelay(1);
122
                delay = SetDelay(1);
123
                while (!CheckDelay(delay));
123
                while (!CheckDelay(delay));
124
 
124
 
125
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
125
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
126
                delay = SetDelay(1);
126
                delay = SetDelay(1);
127
                while (!CheckDelay(delay));
127
                while (!CheckDelay(delay));
128
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
128
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
129
        }
129
        }
130
        delay = SetDelay(1);
130
        delay = SetDelay(1);
131
        while (!CheckDelay(delay));
131
        while (!CheckDelay(delay));
132
        // create stop condition setting SDA HIGH when SCL is HIGH
132
        // create stop condition setting SDA HIGH when SCL is HIGH
133
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
133
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
134
 
134
 
135
 
135
 
136
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
136
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
137
        GPIO_StructInit(&GPIO_InitStructure);
137
        GPIO_StructInit(&GPIO_InitStructure);
138
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
138
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
139
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
139
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
140
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
140
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
141
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
141
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
142
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
142
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
143
        GPIO_Init(GPIO2, &GPIO_InitStructure);
143
        GPIO_Init(GPIO2, &GPIO_InitStructure);
144
 
144
 
145
        // enable I2C peripherie
145
        // enable I2C peripherie
146
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
146
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
147
        // reset I2C peripherie
147
        // reset I2C peripherie
148
        SCU_APBPeriphReset(__I2C1,ENABLE);
148
        SCU_APBPeriphReset(__I2C1,ENABLE);
149
        SCU_APBPeriphReset(__I2C1,DISABLE);
149
        SCU_APBPeriphReset(__I2C1,DISABLE);
150
 
150
 
151
        I2C_DeInit(I2C1);
151
        I2C_DeInit(I2C1);
152
        I2C_StructInit(&I2C_Struct);
152
        I2C_StructInit(&I2C_Struct);
153
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
153
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
154
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
154
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
155
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
155
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
156
        I2C_Struct.I2C_OwnAddress = 0x00;
156
        I2C_Struct.I2C_OwnAddress = 0x00;
157
        I2C_Init(I2C1, &I2C_Struct);
157
        I2C_Init(I2C1, &I2C_Struct);
158
 
158
 
159
        // empty rx and tx buffer
159
        // empty rx and tx buffer
160
        I2C_TxBufferSize = 0;
160
        I2C_TxBufferSize = 0;
161
        I2C_RxBufferSize = 0;
161
        I2C_RxBufferSize = 0;
162
 
162
 
163
        I2C_Cmd(I2C1, ENABLE);
163
        I2C_Cmd(I2C1, ENABLE);
164
        I2C_ITConfig(I2C1, ENABLE);
164
        I2C_ITConfig(I2C1, ENABLE);
165
 
165
 
166
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
166
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
167
 
167
 
168
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
168
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
169
        I2C_GenerateSTOP(I2C1, ENABLE);
169
        I2C_GenerateSTOP(I2C1, ENABLE);
170
        I2C_State = I2C_STATE_IDLE;
170
        I2C_State = I2C_STATE_IDLE;
171
 
171
 
172
        UART1_PutString("ok");
172
        UART1_PutString("ok");
173
}
173
}
174
 
174
 
175
 
175
 
176
//--------------------------------------------------------------
176
//--------------------------------------------------------------
177
void I2C1_Deinit(void)
177
void I2C1_Deinit(void)
178
{
178
{
179
        GPIO_InitTypeDef  GPIO_InitStructure;
179
        GPIO_InitTypeDef  GPIO_InitStructure;
180
        UART1_PutString("\r\n I2C deinit...");
180
        UART1_PutString("\r\n I2C deinit...");
181
        I2C_GenerateStart(I2C1, DISABLE);
181
        I2C_GenerateStart(I2C1, DISABLE);
182
        I2C_GenerateSTOP(I2C1, ENABLE);
182
        I2C_GenerateSTOP(I2C1, ENABLE);
183
        VIC_ITCmd(I2C1_ITLine, DISABLE);
183
        VIC_ITCmd(I2C1_ITLine, DISABLE);
184
        I2C_State = I2C_STATE_OFF;
184
        I2C_State = I2C_STATE_OFF;
185
        I2C_ITConfig(I2C1, DISABLE);
185
        I2C_ITConfig(I2C1, DISABLE);
186
        I2C_Cmd(I2C1, DISABLE);
186
        I2C_Cmd(I2C1, DISABLE);
187
        I2C_DeInit(I2C1);
187
        I2C_DeInit(I2C1);
188
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
188
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
189
 
189
 
190
        // set ports to input
190
        // set ports to input
191
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
191
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
192
        GPIO_StructInit(&GPIO_InitStructure);
192
        GPIO_StructInit(&GPIO_InitStructure);
193
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
193
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
194
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
194
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
195
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
195
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
196
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
196
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
197
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
197
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
198
        GPIO_Init(GPIO2, &GPIO_InitStructure);
198
        GPIO_Init(GPIO2, &GPIO_InitStructure);
199
 
199
 
200
        // empty rx and tx buffer
200
        // empty rx and tx buffer
201
        I2C_TxBufferSize = 0;
201
        I2C_TxBufferSize = 0;
202
        I2C_RxBufferSize = 0;
202
        I2C_RxBufferSize = 0;
203
 
203
 
204
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
204
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
205
 
205
 
206
        UART1_PutString("ok");
206
        UART1_PutString("ok");
207
}
207
}
208
 
208
 
209
 
209
 
210
//--------------------------------------------------------------
210
//--------------------------------------------------------------
211
void I2C1_IRQHandler(void)
211
void I2C1_IRQHandler(void)
212
{
212
{
213
        static u8 Rx_Idx = 0, Tx_Idx = 0;
213
        static u8 Rx_Idx = 0, Tx_Idx = 0;
214
        u16 status;
214
        u16 status;
215
 
215
 
216
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
216
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
217
 
217
 
218
        // detemine I2C State
218
        // detemine I2C State
219
        status = I2C_GetLastEvent(I2C1);
219
        status = I2C_GetLastEvent(I2C1);
220
 
220
 
221
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
221
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
222
        {       // Set and subsequently clear the STOP bit while BTF is set.
222
        {       // Set and subsequently clear the STOP bit while BTF is set.
223
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
223
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
224
                {
224
                {
225
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
225
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
226
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
226
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
227
                }
227
                }
228
                I2C_State = I2C_STATE_IDLE;
228
                I2C_State = I2C_STATE_IDLE;
229
                VIC_ITCmd(I2C1_ITLine, DISABLE);
229
                VIC_ITCmd(I2C1_ITLine, DISABLE);
230
                LED_GRN_OFF;
230
                LED_GRN_OFF;
231
                return;
231
                return;
232
        }
232
        }
233
        else
233
        else
234
        {       // depending on current i2c state
234
        {       // depending on current i2c state
235
                switch(status)
235
                switch(status)
236
                {
236
                {
237
                        // the start condition was initiated on the bus
237
                        // the start condition was initiated on the bus
238
                        case I2C_EVENT_MASTER_MODE_SELECT:
238
                        case I2C_EVENT_MASTER_MODE_SELECT:
239
                                LED_GRN_ON;
239
                                LED_GRN_ON;
240
                                // update current bus state variable
240
                                // update current bus state variable
241
                                // jump to rx state if there is nothing to send
241
                                // jump to rx state if there is nothing to send
242
                                switch(I2C_Direction)
242
                                switch(I2C_Direction)
243
                                {
243
                                {
244
                                        case I2C_MODE_TRANSMITTER:
244
                                        case I2C_MODE_TRANSMITTER:
245
                                                I2C_State = I2C_STATE_TX_PROGRESS;
245
                                                I2C_State = I2C_STATE_TX_PROGRESS;
246
                                                break;
246
                                                break;
247
 
247
 
248
                                        case I2C_MODE_RECEIVER:
248
                                        case I2C_MODE_RECEIVER:
249
                                                if (I2C_RxBufferSize == 0) // nothingto send?
249
                                                if (I2C_RxBufferSize == 0) // nothingto send?
250
                                                {
250
                                                {
251
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
251
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
252
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
252
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
253
                                                        LED_GRN_OFF;
253
                                                        LED_GRN_OFF;
254
                                                        I2C_State = I2C_STATE_IDLE;
254
                                                        I2C_State = I2C_STATE_IDLE;
255
                                                        return;
255
                                                        return;
256
                                                }
256
                                                }
257
                                                else
257
                                                else
258
                                                {
258
                                                {
259
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
259
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
260
                                                }
260
                                                }
261
                                                break;
261
                                                break;
262
 
262
 
263
                                        default: // invalid direction
263
                                        default: // invalid direction
264
                                                I2C_GenerateSTOP (I2C1, ENABLE);
264
                                                I2C_GenerateSTOP (I2C1, ENABLE);
265
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
265
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
266
                                                LED_GRN_OFF;
266
                                                LED_GRN_OFF;
267
                                                I2C_State = I2C_STATE_IDLE;
267
                                                I2C_State = I2C_STATE_IDLE;
268
                                                return;
268
                                                return;
269
                                }
269
                                }
270
                                // enable acknowledge
270
                                // enable acknowledge
271
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
271
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
272
                                // send address/direction byte on the bus
272
                                // send address/direction byte on the bus
273
                                I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction);
273
                                I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction);
274
                                break;
274
                                break;
275
 
275
 
276
                        // the address byte was send
276
                        // the address byte was send
277
                        case I2C_EVENT_MASTER_MODE_SELECTED:
277
                        case I2C_EVENT_MASTER_MODE_SELECTED:
278
                                // Clear EV6 by set again the PE bit
278
                                // Clear EV6 by set again the PE bit
279
                                I2C_Cmd(I2C1, ENABLE);
279
                                I2C_Cmd(I2C1, ENABLE);
280
                                switch(I2C_State)
280
                                switch(I2C_State)
281
                                {
281
                                {
282
                                        case I2C_STATE_TX_PROGRESS:
282
                                        case I2C_STATE_TX_PROGRESS:
283
                                        // send 1st data byte
283
                                        // send 1st data byte
284
                                        Tx_Idx = 0;
284
                                        Tx_Idx = 0;
285
                                        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
285
                                        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
286
                                        Tx_Idx++;
286
                                        Tx_Idx++;
287
                                        // reset timeout
287
                                        // reset timeout
288
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
288
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
289
                                        break;
289
                                        break;
290
 
290
 
291
                                        case I2C_STATE_RX_PROGRESS:
291
                                        case I2C_STATE_RX_PROGRESS:
292
                                        Rx_Idx = 0;
292
                                        Rx_Idx = 0;
293
                                        // disable acknoledge if only one byte has to be read
293
                                        // disable acknoledge if only one byte has to be read
294
                                        if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);               
294
                                        if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);               
295
                                        break;
295
                                        break;
296
 
296
 
297
                                        default: // unknown I2C state
297
                                        default: // unknown I2C state
298
                                        // should never happen
298
                                        // should never happen
299
                                        I2C_GenerateSTOP (I2C1, ENABLE);
299
                                        I2C_GenerateSTOP (I2C1, ENABLE);
300
                                        LED_GRN_OFF;
300
                                        LED_GRN_OFF;
301
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
301
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
302
                                        I2C_State = I2C_STATE_IDLE;
302
                                        I2C_State = I2C_STATE_IDLE;
303
                                        return;
303
                                        return;
304
                                        break;
304
                                        break;
305
                                }
305
                                }
306
                                break;
306
                                break;
307
 
307
 
308
                        // the master has transmitted a byte and slave has been acknowledged
308
                        // the master has transmitted a byte and slave has been acknowledged
309
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
309
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
310
 
310
 
311
                                // some bytes have to be transmitted
311
                                // some bytes have to be transmitted
312
                                if(Tx_Idx < I2C_TxBufferSize)
312
                                if(Tx_Idx < I2C_TxBufferSize)
313
                                {
313
                                {
314
                                        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
314
                                        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
315
                                        Tx_Idx++;
315
                                        Tx_Idx++;
316
                                }
316
                                }
317
                                else // last byte was send
317
                                else // last byte was send
318
                                {
318
                                {
319
                                        // generate stop or repeated start condition
319
                                        // generate stop or repeated start condition
320
                                        if (I2C_RxBufferSize > 0) // is any answer byte expected?
320
                                        if (I2C_RxBufferSize > 0) // is any answer byte expected?
321
                                        {
321
                                        {
322
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
322
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
323
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
323
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
324
                                        }
324
                                        }
325
                                        else
325
                                        else
326
                                        {   // stop communication
326
                                        {   // stop communication
327
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
327
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
328
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
328
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
329
                                                LED_GRN_OFF;
329
                                                LED_GRN_OFF;
330
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions        
330
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions        
331
                                        }
331
                                        }
332
                                }
332
                                }
333
                                break;
333
                                break;
334
 
334
 
335
                        // the master has received a byte from the slave
335
                        // the master has received a byte from the slave
336
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
336
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
337
                                // some bytes have to be received
337
                                // some bytes have to be received
338
                                if ( Rx_Idx+1 < I2C_RxBufferSize)
338
                                if ( Rx_Idx+1 < I2C_RxBufferSize)
339
                                {       // copy received byte  from the data register to the rx-buffer
339
                                {       // copy received byte  from the data register to the rx-buffer
340
                                        I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
340
                                        I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
341
                                }
341
                                }
342
                                else // if the last byte was received
342
                                else // if the last byte was received
343
                                {
343
                                {
344
                                        // generate a STOP condition on the bus before reading data register
344
                                        // generate a STOP condition on the bus before reading data register
345
                                        I2C_GenerateSTOP(I2C1, ENABLE);
345
                                        I2C_GenerateSTOP(I2C1, ENABLE);
346
                                        I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
346
                                        I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
347
                                        // call the rx handler function to process recieved data
347
                                        // call the rx handler function to process recieved data
348
                                        if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_RxBuffer, I2C_RxBufferSize);
348
                                        if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_RxBuffer, I2C_RxBufferSize);
349
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
349
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
350
                                        DebugOut.Analog[15]++;
350
                                        DebugOut.Analog[15]++;
351
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
351
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
352
                                        LED_GRN_OFF;
352
                                        LED_GRN_OFF;
353
                                        I2C_State = I2C_STATE_IDLE;
353
                                        I2C_State = I2C_STATE_IDLE;
354
                                        return;
354
                                        return;
355
                                }
355
                                }
356
                                Rx_Idx++;
356
                                Rx_Idx++;
357
                                // if the 2nd last byte was received disable acknowledge for the last one
357
                                // if the 2nd last byte was received disable acknowledge for the last one
358
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
358
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
359
                                {
359
                                {
360
                                        I2C_AcknowledgeConfig(I2C1, DISABLE);
360
                                        I2C_AcknowledgeConfig(I2C1, DISABLE);
361
                                }
361
                                }
362
                                break;
362
                                break;
363
 
363
 
364
                        default:// unknown event
364
                        default:// unknown event
365
                                // should never happen
365
                                // should never happen
366
                                I2C_GenerateSTOP (I2C1, ENABLE);
366
                                I2C_GenerateSTOP (I2C1, ENABLE);
367
                                LED_GRN_OFF;
367
                                LED_GRN_OFF;
368
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
368
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
369
                                I2C_State = I2C_STATE_IDLE;
369
                                I2C_State = I2C_STATE_IDLE;
370
                                break;
370
                                break;
371
                }
371
                }
372
        }
372
        }
373
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
373
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
374
}
374
}
375
// ----------------------------------------------------------------------------------------
375
// ----------------------------------------------------------------------------------------
376
// initate an i2c transmission
376
// initate an i2c transmission
377
u8 I2C_Transmission(u8 SlaveAddr, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
377
u8 I2C_Transmission(u8 SlaveAddr, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
378
{
378
{
379
        u8 retval = 0;
379
        u8 retval = 0;
380
 
380
 
381
        if(I2C_State == I2C_STATE_IDLE)
381
        if(I2C_State == I2C_STATE_IDLE)
382
        {
382
        {      
383
                I2C_RxBufferSize = RxBytes;
383
                I2C_RxBufferSize = RxBytes;
384
                // set direction to master transmitter
384
                // set direction to master transmitter
385
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_TXBUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
385
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_TXBUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
386
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_RXBUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
386
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_RXBUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
387
                else // nothing to send or receive
387
                else // nothing to send or receive
388
                {
388
                {
389
                        I2C_TxBufferSize = 0;
389
                        I2C_TxBufferSize = 0;
390
                        I2C_RxBufferSize = 0;
390
                        I2C_RxBufferSize = 0;
391
                        // enable I2C IRQ again
-
 
392
                        VIC_ITCmd(I2C1_ITLine, ENABLE);
-
 
393
                        return(retval);
391
                        return(retval);
394
                }
392
                }
395
                // update slave address and rx data handler     funbction pointer
393
                // update slave address and rx data handler     funbction pointer
396
                I2C_SlaveAddress = SlaveAddr;
394
                I2C_SlaveAddress = SlaveAddr;
397
                I2C_pRxHandler = pRxHandler;
395
                I2C_pRxHandler = pRxHandler;
398
                // test on busy flag and clear it
396
                // test on busy flag and clear it
399
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
397
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
-
 
398
                // enable I2C IRQ
-
 
399
                VIC_ITCmd(I2C1_ITLine, ENABLE);
400
                // initiate start condition on the bus
400
                // initiate start condition on the bus
401
                I2C_GenerateStart(I2C1, ENABLE);
401
                I2C_GenerateStart(I2C1, ENABLE);
402
                retval = 1;
402
                retval = 1;
403
         }
403
         }
404
         return(retval);
404
         return(retval);
405
}
405
}
406
 
406