Subversion Repositories NaviCtrl

Rev

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

Rev 228 Rev 229
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 "main.h"
63
#include "main.h"
64
#include "led.h"
64
#include "led.h"
65
#include "spi_slave.h"
65
#include "spi_slave.h"
66
 
66
 
67
#define I2C_SLAVE_ADDRESS               0x50
67
#define I2C_SLAVE_ADDRESS               0x50
68
 
68
 
69
// I2C states
69
// I2C states
70
#define I2C_UNDEF               0
70
#define I2C_UNDEF               0
71
#define I2C_IDLE                1
71
#define I2C_IDLE                1
72
#define I2C_TX_PROGRESS 2
72
#define I2C_TX_PROGRESS 2
73
#define I2C_RX_PENDING  3
73
#define I2C_RX_PENDING  3
74
#define I2C_RX_PROGRESS 4
74
#define I2C_RX_PROGRESS 4
75
#define I2C_OFF                 5
75
#define I2C_OFF                 5
76
 
76
 
77
volatile u8 I2C_State = I2C_OFF; // only on byte! because of sync by nesting irqs
77
volatile u8 I2C_State = I2C_OFF; // only on byte! because of sync by nesting irqs
78
u8 I2C_StopPolling = 1;
78
u8 I2C_StopPolling = 1;
79
 
79
 
80
// rxbuffer
80
// rxbuffer
81
volatile u8 I2C_RxBufferSize;
81
volatile u8 I2C_RxBufferSize;
82
volatile u8 *I2C_RxBuffer;
82
volatile u8 *I2C_RxBuffer;
83
// txbuffer
83
// txbuffer
84
volatile u8 I2C_TxBufferSize;
84
volatile u8 I2C_TxBufferSize;
85
volatile u8 *I2C_TxBuffer;
85
volatile u8 *I2C_TxBuffer;
86
 
86
 
87
volatile u8 I2C_Direction;
87
volatile u8 I2C_Direction;
88
volatile u8 I2C_Command;
88
volatile u8 I2C_Command;
89
// I2C Transfer buffers
89
// I2C Transfer buffers
90
volatile I2C_Heading_t                  I2C_Heading;
90
volatile I2C_Heading_t                  I2C_Heading;
91
volatile I2C_WriteAttitude_t    I2C_WriteAttitude;
91
volatile I2C_WriteAttitude_t    I2C_WriteAttitude;
92
volatile I2C_Mag_t                              I2C_Mag;
92
volatile I2C_Mag_t                              I2C_Mag;
93
volatile I2C_Version_t                  MK3MAG_Version;
93
volatile I2C_Version_t                  MK3MAG_Version;
94
volatile I2C_Cal_t                              I2C_WriteCal;
94
volatile I2C_Cal_t                              I2C_WriteCal;
95
volatile I2C_Cal_t                              I2C_ReadCal;
95
volatile I2C_Cal_t                              I2C_ReadCal;
96
 
96
 
97
#define I2C1_TIMEOUT 500 // 500 ms
97
#define I2C1_TIMEOUT 500 // 500 ms
98
volatile u32 I2C1_Timeout = 0;
98
volatile u32 I2C1_Timeout = 0;
99
 
99
 
100
//--------------------------------------------------------------
100
//--------------------------------------------------------------
101
void I2C1_Init(void)
101
void I2C1_Init(void)
102
{
102
{
103
        I2C_InitTypeDef   I2C_Struct;
103
        I2C_InitTypeDef   I2C_Struct;
104
        GPIO_InitTypeDef  GPIO_InitStructure;
104
        GPIO_InitTypeDef  GPIO_InitStructure;
105
       
105
       
106
        I2C_State = I2C_OFF;
106
        I2C_State = I2C_OFF;
107
 
107
 
108
        UART1_PutString("\r\n I2C init...");
108
        UART1_PutString("\r\n I2C init...");
109
        // enable Port 2 peripherie
109
        // enable Port 2 peripherie
110
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
110
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
111
        // disable a reset state
111
        // disable a reset state
112
        SCU_APBPeriphReset(__GPIO2, DISABLE);
112
        SCU_APBPeriphReset(__GPIO2, DISABLE);
113
 
113
 
114
        // free a busy bus
114
        // free a busy bus
115
 
115
 
116
        // At switch on I2C devices can get in a state where they
116
        // At switch on I2C devices can get in a state where they
117
        // are still waiting for a command due to all the bus lines bouncing
117
        // are still waiting for a command due to all the bus lines bouncing
118
        // around at startup have started clocking data into the device(s).
118
        // around at startup have started clocking data into the device(s).
119
        // Enable the ports as open collector port outputs
119
        // Enable the ports as open collector port outputs
120
        // and clock out at least 9 SCL pulses, then generate a stop
120
        // and clock out at least 9 SCL pulses, then generate a stop
121
        // condition and then leave the clock line high.
121
        // condition and then leave the clock line high.
122
 
122
 
123
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
123
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
124
        GPIO_StructInit(&GPIO_InitStructure);
124
        GPIO_StructInit(&GPIO_InitStructure);
125
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
125
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
126
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
126
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
127
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
127
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
128
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
128
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
129
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
129
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
130
        GPIO_Init(GPIO2, &GPIO_InitStructure);
130
        GPIO_Init(GPIO2, &GPIO_InitStructure);
131
 
131
 
132
        u8 i;
132
        u8 i;
133
        u32 delay;
133
        u32 delay;
134
        // set SCL high and then SDA to low (start condition)
134
        // set SCL high and then SDA to low (start condition)
135
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
135
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
136
        delay = SetDelay(1);
136
        delay = SetDelay(1);
137
        while (!CheckDelay(delay));
137
        while (!CheckDelay(delay));
138
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
138
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
139
        // toggle SCL at least 10 times from high to low to high
139
        // toggle SCL at least 10 times from high to low to high
140
        for(i = 0; i < 10; i++)
140
        for(i = 0; i < 10; i++)
141
        {
141
        {
142
                delay = SetDelay(1);
142
                delay = SetDelay(1);
143
                while (!CheckDelay(delay));
143
                while (!CheckDelay(delay));
144
 
144
 
145
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
145
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
146
                delay = SetDelay(1);
146
                delay = SetDelay(1);
147
                while (!CheckDelay(delay));
147
                while (!CheckDelay(delay));
148
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
148
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
149
        }
149
        }
150
        delay = SetDelay(1);
150
        delay = SetDelay(1);
151
        while (!CheckDelay(delay));
151
        while (!CheckDelay(delay));
152
        // create stop condition setting SDA HIGH when SCL is HIGH
152
        // create stop condition setting SDA HIGH when SCL is HIGH
153
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
153
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
154
 
154
 
155
 
155
 
156
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
156
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
157
        GPIO_StructInit(&GPIO_InitStructure);
157
        GPIO_StructInit(&GPIO_InitStructure);
158
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
158
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
159
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
159
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
160
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
160
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
161
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
161
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
162
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
162
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
163
        GPIO_Init(GPIO2, &GPIO_InitStructure);
163
        GPIO_Init(GPIO2, &GPIO_InitStructure);
164
 
164
 
165
        // enable I2C peripherie
165
        // enable I2C peripherie
166
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
166
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
167
        // reset I2C peripherie
167
        // reset I2C peripherie
168
        SCU_APBPeriphReset(__I2C1,ENABLE);
168
        SCU_APBPeriphReset(__I2C1,ENABLE);
169
        SCU_APBPeriphReset(__I2C1,DISABLE);
169
        SCU_APBPeriphReset(__I2C1,DISABLE);
170
 
170
 
171
        I2C_DeInit(I2C1);
171
        I2C_DeInit(I2C1);
172
        I2C_StructInit(&I2C_Struct);
172
        I2C_StructInit(&I2C_Struct);
173
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
173
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
174
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
174
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
175
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
175
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
176
        I2C_Struct.I2C_OwnAddress = 0x00;
176
        I2C_Struct.I2C_OwnAddress = 0x00;
177
        I2C_Init(I2C1, &I2C_Struct);
177
        I2C_Init(I2C1, &I2C_Struct);
178
 
178
 
179
        I2C_TxBuffer = NULL;
179
        I2C_TxBuffer = NULL;
180
        I2C_TxBufferSize = 0;
180
        I2C_TxBufferSize = 0;
181
 
181
 
182
        I2C_RxBuffer = NULL;
182
        I2C_RxBuffer = NULL;
183
        I2C_RxBufferSize = 0;
183
        I2C_RxBufferSize = 0;
184
 
184
 
185
        I2C_Cmd(I2C1, ENABLE);
185
        I2C_Cmd(I2C1, ENABLE);
186
        I2C_ITConfig(I2C1, ENABLE);
186
        I2C_ITConfig(I2C1, ENABLE);
187
 
187
 
188
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
188
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
-
 
189
        I2C_Heading.Heading = -1;
189
 
190
 
190
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
191
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
191
        I2C_GenerateSTOP(I2C1, ENABLE);
192
        I2C_GenerateSTOP(I2C1, ENABLE);
192
        I2C_State = I2C_IDLE;
193
        I2C_State = I2C_IDLE;
193
 
194
 
194
        I2C_StopPolling = 0; // start polling
195
        I2C_StopPolling = 0; // start polling
195
 
196
 
196
        UART1_PutString("ok");
197
        UART1_PutString("ok");
197
}
198
}
198
 
199
 
199
 
200
 
200
//--------------------------------------------------------------
201
//--------------------------------------------------------------
201
void I2C1_Deinit(void)
202
void I2C1_Deinit(void)
202
{
203
{
203
        GPIO_InitTypeDef  GPIO_InitStructure;
204
        GPIO_InitTypeDef  GPIO_InitStructure;
204
        I2C_StopPolling = 1;// stop polling
205
        I2C_StopPolling = 1;// stop polling
205
        UART1_PutString("\r\n I2C deinit...");
206
        UART1_PutString("\r\n I2C deinit...");
206
        I2C_GenerateStart(I2C1, DISABLE);
207
        I2C_GenerateStart(I2C1, DISABLE);
207
        I2C_GenerateSTOP(I2C1, ENABLE);
208
        I2C_GenerateSTOP(I2C1, ENABLE);
208
        VIC_ITCmd(I2C1_ITLine, DISABLE);
209
        VIC_ITCmd(I2C1_ITLine, DISABLE);
209
        I2C_State = I2C_OFF;
210
        I2C_State = I2C_OFF;
210
        I2C_ITConfig(I2C1, DISABLE);
211
        I2C_ITConfig(I2C1, DISABLE);
211
        I2C_Cmd(I2C1, DISABLE);
212
        I2C_Cmd(I2C1, DISABLE);
212
        I2C_DeInit(I2C1);
213
        I2C_DeInit(I2C1);
213
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
214
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
214
 
215
 
215
        // set ports to input
216
        // set ports to input
216
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
217
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
217
        GPIO_StructInit(&GPIO_InitStructure);
218
        GPIO_StructInit(&GPIO_InitStructure);
218
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
219
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
219
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
220
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
220
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
221
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
221
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
222
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
222
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
223
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
223
        GPIO_Init(GPIO2, &GPIO_InitStructure);
224
        GPIO_Init(GPIO2, &GPIO_InitStructure);
224
 
225
 
225
        I2C_TxBuffer = NULL;
226
        I2C_TxBuffer = NULL;
226
        I2C_TxBufferSize = 0;
227
        I2C_TxBufferSize = 0;
227
 
228
 
228
        I2C_RxBuffer = NULL;
229
        I2C_RxBuffer = NULL;
229
        I2C_RxBufferSize = 0;
230
        I2C_RxBufferSize = 0;
230
 
231
 
231
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
232
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
232
        I2C_Heading.Heading = -2;
233
        I2C_Heading.Heading = -1;
233
 
234
 
234
        UART1_PutString("ok");
235
        UART1_PutString("ok");
235
}
236
}
236
 
237
 
237
 
238
 
238
//--------------------------------------------------------------
239
//--------------------------------------------------------------
239
void I2C1_IRQHandler(void)
240
void I2C1_IRQHandler(void)
240
{
241
{
241
        static u8 Rx_Idx = 0, Tx_Idx = 0, crc = 0;
242
        static u8 Rx_Idx = 0, Tx_Idx = 0, crc = 0;
242
        static u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
243
        static u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
243
        u16 status;
244
        u16 status;
244
 
245
 
245
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
246
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
246
 
247
 
247
        // detemine I2C State
248
        // detemine I2C State
248
        status = I2C_GetLastEvent(I2C1);
249
        status = I2C_GetLastEvent(I2C1);
249
 
250
 
250
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
251
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
251
        {       // Set and subsequently clear the STOP bit while BTF is set.
252
        {       // Set and subsequently clear the STOP bit while BTF is set.
252
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
253
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
253
                {
254
                {
254
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
255
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
255
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
256
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
256
                }
257
                }
257
                I2C_State = I2C_IDLE;
258
                I2C_State = I2C_IDLE;
258
                VIC_ITCmd(I2C1_ITLine, DISABLE);
259
                VIC_ITCmd(I2C1_ITLine, DISABLE);
259
                LED_GRN_OFF;
260
                LED_GRN_OFF;
260
                return;
261
                return;
261
        }
262
        }
262
        else
263
        else
263
        {       // depending on current i2c state
264
        {       // depending on current i2c state
264
                switch (status)
265
                switch (status)
265
                {
266
                {
266
                        // the start condition was initiated on the bus
267
                        // the start condition was initiated on the bus
267
                        case I2C_EVENT_MASTER_MODE_SELECT:
268
                        case I2C_EVENT_MASTER_MODE_SELECT:
268
                                LED_GRN_ON;
269
                                LED_GRN_ON;
269
                                // update current bus state variable
270
                                // update current bus state variable
270
                                switch(I2C_Direction)
271
                                switch(I2C_Direction)
271
                                {
272
                                {
272
                                        case I2C_MODE_TRANSMITTER:
273
                                        case I2C_MODE_TRANSMITTER:
273
                                                I2C_State = I2C_TX_PROGRESS;
274
                                                I2C_State = I2C_TX_PROGRESS;
274
                                                break;
275
                                                break;
275
 
276
 
276
                                        case I2C_MODE_RECEIVER:
277
                                        case I2C_MODE_RECEIVER:
277
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
278
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
278
                                                {
279
                                                {
279
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
280
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
280
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
281
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
281
                                                        LED_GRN_OFF;
282
                                                        LED_GRN_OFF;
282
                                                        I2C_State = I2C_IDLE;
283
                                                        I2C_State = I2C_IDLE;
283
                                                        return;
284
                                                        return;
284
                                                }
285
                                                }
285
                                                else
286
                                                else
286
                                                {
287
                                                {
287
                                                        I2C_State = I2C_RX_PROGRESS;
288
                                                        I2C_State = I2C_RX_PROGRESS;
288
                                                }
289
                                                }
289
                                                break;
290
                                                break;
290
 
291
 
291
                                        default: // invalid direction
292
                                        default: // invalid direction
292
                                                I2C_GenerateSTOP (I2C1, ENABLE);
293
                                                I2C_GenerateSTOP (I2C1, ENABLE);
293
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
294
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
294
                                                LED_GRN_OFF;
295
                                                LED_GRN_OFF;
295
                                                I2C_State = I2C_IDLE;
296
                                                I2C_State = I2C_IDLE;
296
                                                return;
297
                                                return;
297
                                }
298
                                }
298
                                // enable acknowledge
299
                                // enable acknowledge
299
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
300
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
300
                                // send address/direction byte on the bus
301
                                // send address/direction byte on the bus
301
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
302
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
302
                                break;
303
                                break;
303
 
304
 
304
                        // the address byte was send
305
                        // the address byte was send
305
                        case I2C_EVENT_MASTER_MODE_SELECTED:
306
                        case I2C_EVENT_MASTER_MODE_SELECTED:
306
                                // Clear EV6 by set again the PE bit
307
                                // Clear EV6 by set again the PE bit
307
                                I2C_Cmd(I2C1, ENABLE);
308
                                I2C_Cmd(I2C1, ENABLE);
308
                                // reset checksum
309
                                // reset checksum
309
                                crc = 0;
310
                                crc = 0;
310
                                switch(I2C_State)
311
                                switch(I2C_State)
311
                                {
312
                                {
312
                                        case I2C_TX_PROGRESS:
313
                                        case I2C_TX_PROGRESS:
313
                                        // send command 1st data byte (allways the command id)
314
                                        // send command 1st data byte (allways the command id)
314
                                        I2C_SendData(I2C1, I2C_Command);
315
                                        I2C_SendData(I2C1, I2C_Command);
315
                                        crc += I2C_Command;
316
                                        crc += I2C_Command;
316
                                        Tx_Idx = 0;
317
                                        Tx_Idx = 0;
317
                                        // reset timeout
318
                                        // reset timeout
318
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
319
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
319
                                        break;
320
                                        break;
320
 
321
 
321
                                        case I2C_RX_PROGRESS:
322
                                        case I2C_RX_PROGRESS:
322
                                        Rx_Idx = 0;
323
                                        Rx_Idx = 0;
323
                                        break;
324
                                        break;
324
 
325
 
325
                                        default: // unknown I2C state
326
                                        default: // unknown I2C state
326
                                        // should never happen
327
                                        // should never happen
327
                                        I2C_GenerateSTOP (I2C1, ENABLE);
328
                                        I2C_GenerateSTOP (I2C1, ENABLE);
328
                                        LED_GRN_OFF;
329
                                        LED_GRN_OFF;
329
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
330
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
330
                                        I2C_State = I2C_IDLE;
331
                                        I2C_State = I2C_IDLE;
331
                                        return;
332
                                        return;
332
                                        break;
333
                                        break;
333
                                }
334
                                }
334
                                break;
335
                                break;
335
 
336
 
336
                        // the master has transmitted a byte and slave has been acknowledged
337
                        // the master has transmitted a byte and slave has been acknowledged
337
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
338
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
338
 
339
 
339
                                // some bytes have to be transmitted
340
                                // some bytes have to be transmitted
340
                                if(Tx_Idx < I2C_TxBufferSize)
341
                                if(Tx_Idx < I2C_TxBufferSize)
341
                                {
342
                                {
342
                                        if(I2C_TxBuffer != NULL)
343
                                        if(I2C_TxBuffer != NULL)
343
                                        {
344
                                        {
344
                                                I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
345
                                                I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
345
                                                crc += I2C_TxBuffer[Tx_Idx];
346
                                                crc += I2C_TxBuffer[Tx_Idx];
346
                                        }
347
                                        }
347
                                        else
348
                                        else
348
                                        {
349
                                        {
349
                                                I2C_SendData(I2C1, 0x00);
350
                                                I2C_SendData(I2C1, 0x00);
350
                                        }
351
                                        }
351
                                }
352
                                }
352
                                else if(Tx_Idx == I2C_TxBufferSize) // the last tx buffer byte was send
353
                                else if(Tx_Idx == I2C_TxBufferSize) // the last tx buffer byte was send
353
                                {
354
                                {
354
                                        // send crc byte at the end
355
                                        // send crc byte at the end
355
                                        crc = ~crc; // flip all bits in the checksum
356
                                        crc = ~crc; // flip all bits in the checksum
356
                                        I2C_SendData(I2C1, crc);
357
                                        I2C_SendData(I2C1, crc);
357
                                }
358
                                }
358
                                else if(Tx_Idx == (I2C_TxBufferSize+1) )
359
                                else if(Tx_Idx == (I2C_TxBufferSize+1) )
359
                                {
360
                                {
360
                                        I2C_SendData(I2C1, 0xAA); // send a dummybyte
361
                                        I2C_SendData(I2C1, 0xAA); // send a dummybyte
361
                                }
362
                                }
362
                                else // last byte was send
363
                                else // last byte was send
363
                                {
364
                                {
364
                                        // generate stop or repeated start condition
365
                                        // generate stop or repeated start condition
365
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
366
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
366
                                        {
367
                                        {
367
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
368
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
368
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
369
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
369
                                        }
370
                                        }
370
                                        else
371
                                        else
371
                                        {   // stop communication
372
                                        {   // stop communication
372
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
373
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
373
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
374
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
374
                                                LED_GRN_OFF;
375
                                                LED_GRN_OFF;
375
                                                DebugOut.Analog[15]++;
376
                                                DebugOut.Analog[15]++;
376
                                                I2C_State = I2C_IDLE;                   // ready for new actions
377
                                                I2C_State = I2C_IDLE;                   // ready for new actions
377
                                               
378
                                               
378
                                        }
379
                                        }
379
                                }
380
                                }
380
                                Tx_Idx++;
381
                                Tx_Idx++;
381
                                break;
382
                                break;
382
 
383
 
383
                        // the master has received a byte from the slave
384
                        // the master has received a byte from the slave
384
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
385
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
385
                                // some bytes have to be received
386
                                // some bytes have to be received
386
                                if (Rx_Idx < I2C_RxBufferSize)
387
                                if (Rx_Idx < I2C_RxBufferSize)
387
                                {       // copy received byte  from the data register to the rx-buffer
388
                                {       // copy received byte  from the data register to the rx-buffer
388
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
389
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
389
                                        // update checksum
390
                                        // update checksum
390
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
391
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
391
                                }
392
                                }
392
                                // if the last byte (crc) was received
393
                                // if the last byte (crc) was received
393
                                else if ( Rx_Idx == I2C_RxBufferSize)
394
                                else if ( Rx_Idx == I2C_RxBufferSize)
394
                                {
395
                                {
395
                                        // generate a STOP condition on the bus before reading data register
396
                                        // generate a STOP condition on the bus before reading data register
396
                                        I2C_GenerateSTOP(I2C1, ENABLE);
397
                                        I2C_GenerateSTOP(I2C1, ENABLE);
397
                                        // compare last byte with checksum
398
                                        // compare last byte with checksum
398
                                        crc = ~crc;// flip all bits in calulated checksum
399
                                        crc = ~crc;// flip all bits in calulated checksum
399
                                        if(crc == I2C_ReceiveData(I2C1))
400
                                        if(crc == I2C_ReceiveData(I2C1))
400
                                        {      
401
                                        {      
401
                                                // copy primary rx buffer content to rx buffer if exist
402
                                                // copy primary rx buffer content to rx buffer if exist
402
                                                if(I2C_RxBuffer != NULL)
403
                                                if(I2C_RxBuffer != NULL)
403
                                                {
404
                                                {
404
                                                        memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
405
                                                        memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
405
                                                }
406
                                                }
406
                                                I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
407
                                                I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
407
                                                DebugOut.Analog[15]++;
408
                                                DebugOut.Analog[15]++;
408
                                        }
409
                                        }
409
                                        else // checksum error detected
410
                                        else // checksum error detected
410
                                        {
411
                                        {
411
                                                DebugOut.Analog[14]++;
412
                                                DebugOut.Analog[14]++;
412
                                        }
413
                                        }
413
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
414
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
414
                                        LED_GRN_OFF;
415
                                        LED_GRN_OFF;
415
                                        I2C_State = I2C_IDLE;
416
                                        I2C_State = I2C_IDLE;
416
                                        return;
417
                                        return;
417
                                }
418
                                }
418
                                Rx_Idx++;
419
                                Rx_Idx++;
419
                                // if the 2nd last byte was received disable acknowledge for the last one
420
                                // if the 2nd last byte was received disable acknowledge for the last one
420
                                if ( Rx_Idx == I2C_RxBufferSize )
421
                                if ( Rx_Idx == I2C_RxBufferSize )
421
                                {
422
                                {
422
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
423
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
423
                                }
424
                                }
424
                                break;
425
                                break;
425
 
426
 
426
                        default:
427
                        default:
427
                                break;
428
                                break;
428
                }
429
                }
429
        }
430
        }
430
 
431
 
431
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
432
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
432
}
433
}
433
//----------------------------------------------------------------
434
//----------------------------------------------------------------
434
void I2C1_SendCommand(u8 command)
435
void I2C1_SendCommand(u8 command)
435
{
436
{
436
        // disable I2C IRQ to check state
437
        // disable I2C IRQ to check state
437
        VIC_ITCmd(I2C1_ITLine, DISABLE);
438
        VIC_ITCmd(I2C1_ITLine, DISABLE);
438
        // If I2C transmission is in progress
439
        // If I2C transmission is in progress
439
        if(I2C_State == I2C_IDLE)
440
        if(I2C_State == I2C_IDLE)
440
        {
441
        {
441
                // update current command id
442
                // update current command id
442
                I2C_Command = command;
443
                I2C_Command = command;
443
                // set pointers to data area with respect to the command id
444
                // set pointers to data area with respect to the command id
444
                switch (command)
445
                switch (command)
445
                {
446
                {
446
                        case I2C_CMD_VERSION:
447
                        case I2C_CMD_VERSION:
447
                                I2C_RxBuffer = (u8 *)&MK3MAG_Version;
448
                                I2C_RxBuffer = (u8 *)&MK3MAG_Version;
448
                                I2C_RxBufferSize = sizeof(MK3MAG_Version);
449
                                I2C_RxBufferSize = sizeof(MK3MAG_Version);
449
                                I2C_TxBuffer = NULL;
450
                                I2C_TxBuffer = NULL;
450
                                I2C_TxBufferSize = 0;
451
                                I2C_TxBufferSize = 0;
451
                                break;
452
                                break;
452
                        case I2C_CMD_WRITE_CAL:
453
                        case I2C_CMD_WRITE_CAL:
453
                                I2C_RxBuffer = (u8 *)&I2C_ReadCal;
454
                                I2C_RxBuffer = (u8 *)&I2C_ReadCal;
454
                                I2C_RxBufferSize = sizeof(I2C_ReadCal);
455
                                I2C_RxBufferSize = sizeof(I2C_ReadCal);
455
                                I2C_TxBuffer = (u8 *)&I2C_WriteCal;
456
                                I2C_TxBuffer = (u8 *)&I2C_WriteCal;
456
                                I2C_TxBufferSize = sizeof(I2C_WriteCal);
457
                                I2C_TxBufferSize = sizeof(I2C_WriteCal);
457
                                break;
458
                                break;
458
                        case I2C_CMD_READ_MAG:
459
                        case I2C_CMD_READ_MAG:
459
                                I2C_RxBuffer = (u8 *)&I2C_Mag;
460
                                I2C_RxBuffer = (u8 *)&I2C_Mag;
460
                                I2C_RxBufferSize = sizeof(I2C_Mag);
461
                                I2C_RxBufferSize = sizeof(I2C_Mag);
461
                                I2C_TxBuffer = NULL;
462
                                I2C_TxBuffer = NULL;
462
                                I2C_TxBufferSize = 0;
463
                                I2C_TxBufferSize = 0;
463
                                break;
464
                                break;
464
                        case I2C_CMD_READ_HEADING:
465
                        case I2C_CMD_READ_HEADING:
465
                                I2C_RxBuffer = (u8 *)&I2C_Heading;
466
                                I2C_RxBuffer = (u8 *)&I2C_Heading;
466
                                I2C_RxBufferSize = sizeof(I2C_Heading);
467
                                I2C_RxBufferSize = sizeof(I2C_Heading);
467
                                I2C_TxBuffer =  (u8 *)&I2C_WriteAttitude;
468
                                I2C_TxBuffer =  (u8 *)&I2C_WriteAttitude;
468
                                I2C_TxBufferSize = sizeof(I2C_WriteAttitude);
469
                                I2C_TxBufferSize = sizeof(I2C_WriteAttitude);
469
                                // update attitude from spi rx buffer
470
                                // update attitude from spi rx buffer
470
                                VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy
471
                                VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy
471
                                I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
472
                                I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
472
                                I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick;
473
                                I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick;
473
                                VIC_ITCmd(SSP0_ITLine, ENABLE);
474
                                VIC_ITCmd(SSP0_ITLine, ENABLE);
474
                                break;
475
                                break;
475
                        default: // unknown command id
476
                        default: // unknown command id
476
                                I2C_RxBuffer = NULL;
477
                                I2C_RxBuffer = NULL;
477
                                I2C_RxBufferSize = 0;
478
                                I2C_RxBufferSize = 0;
478
                                I2C_TxBuffer =  NULL;
479
                                I2C_TxBuffer =  NULL;
479
                                I2C_TxBufferSize = 0;
480
                                I2C_TxBufferSize = 0;
480
                                break;
481
                                break;
481
                }
482
                }
482
                // set direction to master transmitter
483
                // set direction to master transmitter
483
                I2C_Direction = I2C_MODE_TRANSMITTER;
484
                I2C_Direction = I2C_MODE_TRANSMITTER;
484
                // test on busy flag and clear it
485
                // test on busy flag and clear it
485
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
486
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
486
                // enable I2C IRQ again
487
                // enable I2C IRQ again
487
                VIC_ITCmd(I2C1_ITLine, ENABLE);
488
                VIC_ITCmd(I2C1_ITLine, ENABLE);
488
                // initiate start condition on the bus
489
                // initiate start condition on the bus
489
                I2C_GenerateStart(I2C1, ENABLE);
490
                I2C_GenerateStart(I2C1, ENABLE);
490
                // to be continued in the I2C1_IRQHandler() above
491
                // to be continued in the I2C1_IRQHandler() above
491
        } // EOF I2C_State == I2C_IDLE
492
        } // EOF I2C_State == I2C_IDLE
492
        else // I2C_State != I2C_IDLE
493
        else // I2C_State != I2C_IDLE
493
        {
494
        {
494
                // re-enable I2C IRQ again
495
                // re-enable I2C IRQ again
495
                VIC_ITCmd(I2C1_ITLine, ENABLE);
496
                VIC_ITCmd(I2C1_ITLine, ENABLE);
496
        }      
497
        }      
497
}
498
}
498
 
499
 
499
//----------------------------------------------------------------
500
//----------------------------------------------------------------
500
void I2C1_GetMK3MagVersion(void)
501
void I2C1_GetMK3MagVersion(void)
501
{
502
{
502
        u8 msg[64];
503
        u8 msg[64];
503
        u8 repeat;
504
        u8 repeat;
504
        u32 timeout;
505
        u32 timeout;
505
       
506
       
506
        UART1_PutString("\r\n Getting Version from MK3MAG");
507
        UART1_PutString("\r\n Getting Version from MK3MAG");
507
        // stop polling of other commands
508
        // stop polling of other commands
508
        I2C_StopPolling = 1;
509
        I2C_StopPolling = 1;
509
 
510
 
510
        MK3MAG_Version.Major = 0xFF;
511
        MK3MAG_Version.Major = 0xFF;
511
        MK3MAG_Version.Minor = 0xFF;
512
        MK3MAG_Version.Minor = 0xFF;
512
        MK3MAG_Version.Patch = 0xFF;
513
        MK3MAG_Version.Patch = 0xFF;
513
        MK3MAG_Version.Compatible = 0xFF;
514
        MK3MAG_Version.Compatible = 0xFF;
514
        // polling of version info
515
        // polling of version info
515
        repeat = 0;
516
        repeat = 0;
516
        do
517
        do
517
        {
518
        {
518
                I2C1_SendCommand(I2C_CMD_VERSION);
519
                I2C1_SendCommand(I2C_CMD_VERSION);
519
                timeout = SetDelay(250);
520
                timeout = SetDelay(250);
520
                do
521
                do
521
                {
522
                {
522
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
523
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
523
                }while (!CheckDelay(timeout));
524
                }while (!CheckDelay(timeout));
524
                UART1_PutString(".");
525
                UART1_PutString(".");
525
                repeat++;
526
                repeat++;
526
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
527
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
527
        // if we got it
528
        // if we got it
528
        if (MK3MAG_Version.Major != 0xFF)
529
        if (MK3MAG_Version.Major != 0xFF)
529
        {
530
        {
530
                sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
531
                sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
531
                UART1_PutString(msg);
532
                UART1_PutString(msg);
532
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
533
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
533
                UART1_PutString(msg);
534
                UART1_PutString(msg);
534
        }
535
        }
535
        else UART1_PutString("\n\r No version information from MK3Mag.");
536
        else UART1_PutString("\n\r No version information from MK3Mag.");
536
 
537
 
537
        I2C_StopPolling = 0; // enable polling of heading command
538
        I2C_StopPolling = 0; // enable polling of heading command
538
}
539
}
539
 
540
 
540
 
541
 
541
//----------------------------------------------------------------
542
//----------------------------------------------------------------
542
void I2C1_UpdateCompass(void)
543
void I2C1_UpdateCompass(void)
543
{
544
{
544
        static u32 TimerCompassUpdate = 0;
545
        static u32 TimerCompassUpdate = 0;
545
 
546
 
546
        if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return;
547
        if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return;
547
       
548
       
548
        if(CheckDelay(TimerCompassUpdate))
549
        if(CheckDelay(TimerCompassUpdate))
549
        {
550
        {
550
                // check for incomming compass calibration request
551
                // check for incomming compass calibration request
551
                // update CalByte from spi input queue
552
                // update CalByte from spi input queue
552
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
553
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
553
                // send new calstate
554
                // send new calstate
554
                if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
555
                if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
555
                {
556
                {
556
                        I2C1_SendCommand(I2C_CMD_WRITE_CAL);
557
                        I2C1_SendCommand(I2C_CMD_WRITE_CAL);
557
                }
558
                }
558
                else // request current heading
559
                else // request current heading
559
                {
560
                {
560
                        I2C1_SendCommand(I2C_CMD_READ_HEADING);
561
                        I2C1_SendCommand(I2C_CMD_READ_HEADING);
561
                }
562
                }
562
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
563
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
563
        }
564
        }
564
}
565
}
565
 
566