Subversion Repositories NaviCtrl

Rev

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

Rev 37 Rev 41
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
6
// + Nur für den privaten Gebrauch
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 der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die PORTIERUNG 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 this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
43
// +   * PORTING this software (or part of it) to systems (other than 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 <stdlib.h>
-
 
58
#include <string.h>
-
 
59
#include "91x_lib.h"
-
 
60
#include "i2c.h"
-
 
61
#include "uart1.h"
-
 
62
#include "timer.h"
57
#include "main.h"
63
#include "main.h"
-
 
64
#include "led.h"
-
 
65
#include "spi_slave.h"
-
 
66
 
-
 
67
 
-
 
68
volatile I2C_State_t I2C_State = I2C_OFF;
-
 
69
 
-
 
70
// rxbuffer
-
 
71
volatile u8 I2C_RxBufferSize;
-
 
72
volatile u8 *I2C_RxBuffer;
-
 
73
volatile u8 Rx_Idx = 0;
-
 
74
// txbuffer
-
 
75
volatile u8 I2C_TxBufferSize;
-
 
76
volatile u8 *I2C_TxBuffer;
-
 
77
volatile u8 Tx_Idx = 0;
-
 
78
 
-
 
79
volatile u8 I2C_Direction;
-
 
80
volatile u8 I2C_Command;
-
 
81
 
-
 
82
volatile I2C_Heading_t                  I2C_Heading;
-
 
83
volatile I2C_WriteAttitude_t    I2C_WriteAttitude;
-
 
84
volatile I2C_Mag_t                              I2C_Mag;
-
 
85
volatile I2C_Version_t                  MK3MAG_Version;
-
 
86
volatile I2C_Cal_t                              I2C_WriteCal;
-
 
87
volatile I2C_Cal_t                              I2C_ReadCal;
58
 
-
 
59
u8 I2C_RxBufferSize, I2C_TxBufferSize;
-
 
60
u8 *I2C_TxBuffer, *I2C_RxBuffer;
-
 
61
u8 Tx_Idx=0, Rx_Idx=0, I2C_Direction;
-
 
62
u8 I2C_Command;
-
 
63
 
-
 
64
struct str_I2C_Heading       I2C_Heading;
-
 
65
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
-
 
66
struct str_I2C_Mag           I2C_Mag;
-
 
67
struct str_I2C_EEPROM        I2C_ReadEEPROM, I2C_WriteEEPROM;
-
 
68
struct str_I2C_Version       I2C_Version;
-
 
69
struct str_I2C_WriteCal      I2C_WriteCal;
-
 
70
u8 CompassUpdateActiv = 0;
-
 
71
u8 CompassCalState = 0;
88
 
-
 
89
volatile u8 I2C_ReadRequest = 0;
-
 
90
volatile u32 I2C1_Timeout = 0;
-
 
91
volatile u32 CheckI2COkay = 0;
-
 
92
volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
72
volatile u8 I2C_ReadRequest = 0;
93
 
73
 
94
 
74
//--------------------------------------------------------------
95
//--------------------------------------------------------------
75
void I2C1_Init(void)
96
void I2C1_Init(void)
76
{
97
{
77
    I2C_InitTypeDef   I2C_Struct;
98
        I2C_InitTypeDef   I2C_Struct;
78
  GPIO_InitTypeDef  GPIO_Struct;
99
        GPIO_InitTypeDef  GPIO_InitStructure;
79
 
100
 
-
 
101
        SerialPutString("\r\n I2C init...");
80
  SerialPutString("I2C init...");
102
        // enable Port 2 peripherie
-
 
103
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
-
 
104
        // disable a reset state
-
 
105
        SCU_APBPeriphReset(__GPIO2, DISABLE);
81
  SCU_APBPeriphClockConfig(__I2C1,ENABLE);
106
 
-
 
107
        // free a busy bus
-
 
108
 
-
 
109
        // At switch on I2C devices can get in a state where they
-
 
110
        // are still waiting for a command due to all the bus lines bouncing
-
 
111
        // around at startup have started clocking data into the device(s).
-
 
112
        // Enable the ports as open collector port outputs
-
 
113
        // and clock out at least 9 SCL pulses, then generate a stop
82
  I2C_DeInit(I2C1);
114
        // condition and then leave the clock line high.
-
 
115
 
-
 
116
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
-
 
117
        GPIO_StructInit(&GPIO_InitStructure);
83
 
118
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
84
  GPIO_Struct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
119
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
85
  GPIO_Struct.GPIO_Type = GPIO_Type_OpenCollector;
120
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
86
  GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Enable;
121
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
-
 
122
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
-
 
123
        GPIO_Init(GPIO2, &GPIO_InitStructure);
-
 
124
 
-
 
125
        u8 i;
-
 
126
        u32 delay;
-
 
127
        // set SCL high and then SDA to low (start condition)
-
 
128
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
-
 
129
        delay = SetDelay(1);
-
 
130
        while (!CheckDelay(delay));
-
 
131
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
-
 
132
        // toggle SCL at least 10 times from high to low to high
-
 
133
        for(i = 0; i < 10; i++)
-
 
134
        {
-
 
135
                delay = SetDelay(1);
-
 
136
                while (!CheckDelay(delay));
-
 
137
 
-
 
138
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
-
 
139
                delay = SetDelay(1);
-
 
140
                while (!CheckDelay(delay));
-
 
141
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
-
 
142
        }
-
 
143
        delay = SetDelay(1);
-
 
144
        while (!CheckDelay(delay));
-
 
145
        // create stop condition setting SDA HIGH when SCL is HIGH
-
 
146
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
-
 
147
 
-
 
148
 
-
 
149
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
-
 
150
        GPIO_StructInit(&GPIO_InitStructure);
-
 
151
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
-
 
152
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
-
 
153
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
-
 
154
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
87
  GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2;
155
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
-
 
156
        GPIO_Init(GPIO2, &GPIO_InitStructure);
-
 
157
 
-
 
158
        // enable I2C peripherie
-
 
159
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
-
 
160
        // reset I2C peripherie
-
 
161
        SCU_APBPeriphReset(__I2C1,ENABLE);
88
  GPIO_Init(GPIO2, &GPIO_Struct);
162
        SCU_APBPeriphReset(__I2C1,DISABLE);
-
 
163
 
-
 
164
        I2C_DeInit(I2C1);
89
 
165
        I2C_StructInit(&I2C_Struct);
90
  I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
166
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
91
  I2C_Struct.I2C_Ack = I2C_Ack_Enable;
167
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
92
  I2C_Struct.I2C_CLKSpeed = 20000;
168
        I2C_Struct.I2C_CLKSpeed = 20000;
93
  I2C_Struct.I2C_OwnAddress = I2C_SLAVE_ADDRESS;
169
        I2C_Struct.I2C_OwnAddress = 0x00;
-
 
170
        I2C_Init(I2C1, &I2C_Struct);
-
 
171
 
-
 
172
        I2C_TxBuffer = NULL;
-
 
173
        Tx_Idx = 0;
-
 
174
        I2C_TxBufferSize = 0;
-
 
175
 
-
 
176
        I2C_RxBuffer = NULL;
-
 
177
        Rx_Idx = 0;
-
 
178
        I2C_RxBufferSize = 0;
-
 
179
 
-
 
180
        I2C_Cmd(I2C1, ENABLE);
-
 
181
        I2C_ITConfig(I2C1, ENABLE);
-
 
182
 
-
 
183
        VIC_Config(I2C1_ITLine, VIC_IRQ , 0);
-
 
184
        VIC_ITCmd(I2C1_ITLine, ENABLE);
-
 
185
 
-
 
186
        I2C1_Timeout = SetDelay(1000);
-
 
187
        I2C_Heading.Heading = -1;
-
 
188
        I2C_GenerateSTOP(I2C1, ENABLE);
94
  I2C_Init(I2C1, &I2C_Struct);
189
        I2C_State = I2C_IDLE;
-
 
190
 
-
 
191
        SerialPutString("ok");
-
 
192
}
-
 
193
 
95
 
194
 
-
 
195
//--------------------------------------------------------------
-
 
196
void I2C1_Deinit(void)
-
 
197
{
96
  I2C_Cmd(I2C1, ENABLE);
198
        GPIO_InitTypeDef  GPIO_InitStructure;  
-
 
199
 
97
  I2C_ITConfig(I2C1, ENABLE);
200
        SerialPutString("\r\n I2C deinit...");
-
 
201
        I2C_GenerateStart(I2C1, DISABLE);
-
 
202
        I2C_GenerateSTOP(I2C1, ENABLE);
-
 
203
        VIC_ITCmd(I2C1_ITLine, DISABLE);
-
 
204
        I2C_ITConfig(I2C1, DISABLE);
-
 
205
        I2C_Cmd(I2C1, DISABLE);
-
 
206
        I2C_DeInit(I2C1);
-
 
207
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
-
 
208
       
-
 
209
        // set ports to input
-
 
210
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
-
 
211
        GPIO_StructInit(&GPIO_InitStructure);
-
 
212
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
-
 
213
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2 | GPIO_Pin_3;
-
 
214
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
-
 
215
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
-
 
216
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
-
 
217
    GPIO_Init(GPIO2, &GPIO_InitStructure);
-
 
218
 
-
 
219
        I2C_TxBuffer = NULL;
-
 
220
        Tx_Idx = 0;
-
 
221
        I2C_TxBufferSize = 0;
-
 
222
 
98
 
223
        I2C_RxBuffer = NULL;
99
  VIC_Config(I2C1_ITLine,VIC_IRQ , 8);
-
 
-
 
224
        Rx_Idx = 0;
-
 
225
        I2C_RxBufferSize = 0;
-
 
226
 
100
  VIC_ITCmd(I2C1_ITLine, ENABLE);
227
        I2C_State = I2C_OFF;
-
 
228
 
101
 
229
        I2C1_Timeout = 0;
102
  I2C_Heading.Heading = 0;
230
        I2C_Heading.Heading = -1;
103
  SerialPutString("ok\n\r");
231
 
104
 
232
        SerialPutString("ok");
105
 
233
}
106
}
234
 
107
 
235
 
108
//--------------------------------------------------------------
236
//--------------------------------------------------------------
109
void I2C1_IRQHandler(void)
237
void I2C1_IRQHandler(void)
110
{
238
{
111
 
-
 
112
  switch (I2C_GetLastEvent(I2C1))
-
 
113
  {
-
 
114
    case I2C_EVENT_MASTER_MODE_SELECT:             // EV5
-
 
115
      Tx_Idx = 0;
239
        u16 status;
116
          Rx_Idx = 0;
240
        static u8 crc;
117
          I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
-
 
118
//        DebugOut.Analog[16]++;
241
        // detemine I2C State
119
      break;
242
        status = I2C_GetLastEvent(I2C1);
120
 
243
 
121
    case I2C_EVENT_MASTER_MODE_SELECTED:           // EV6
244
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
122
      // Clear EV6 by set again the PE bit
245
        {   // Set and subsequently clear the STOP bit while BTF is set.
123
      I2C1->CR |=  0x20;
-
 
124
      if (I2C_Direction == I2C_MODE_TRANSMITTER)
246
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
125
      {
247
                {
-
 
248
                        I2C_GenerateSTOP (I2C1, ENABLE); // free the bus
126
             I2C_SendData(I2C1, I2C_Command);//EV8 just after EV6
249
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
127
          }
250
                }
-
 
251
                I2C_State = I2C_IDLE;
128
      break;
252
                LED_GRN_OFF;
129
 
253
        }
130
    case I2C_EVENT_MASTER_BYTE_TRANSMITTED:         // EV8
-
 
131
//      DebugOut.Analog[17]++;
-
 
132
          if ( Tx_Idx >= I2C_TxBufferSize )
-
 
133
      {
254
        else
134
        I2C_GenerateSTOP (I2C1, ENABLE);
255
        {       // depending on current i2c state
135
        Tx_Idx = 0;
256
                switch (status)
136
                if (I2C_RxBufferSize > 0)                  // wird Antwort erwartet ?
-
 
137
                {
257
                {
-
 
258
                        // the start condition was initiated on the bus
-
 
259
                        case I2C_EVENT_MASTER_MODE_SELECT:
-
 
260
                                LED_GRN_ON;
-
 
261
                                // update current bus state variable
-
 
262
                                switch(I2C_Direction)
-
 
263
                                {
-
 
264
                                        case I2C_MODE_TRANSMITTER:
-
 
265
                                                I2C_State = I2C_TX_PROGRESS;
-
 
266
                                                break;
-
 
267
       
-
 
268
                                        case I2C_MODE_RECEIVER:
-
 
269
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
-
 
270
                                                {
-
 
271
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
-
 
272
                                                        I2C_State = I2C_IDLE;
-
 
273
                                                        return;
-
 
274
                                                }
-
 
275
                                                else
-
 
276
                                                {
-
 
277
                                                        I2C_State = I2C_RX_PROGRESS;
-
 
278
                                                }
-
 
279
                                                break;
-
 
280
       
-
 
281
                                        default: // invalid direction
-
 
282
                                                I2C_GenerateSTOP (I2C1, ENABLE);
-
 
283
                                                I2C_State = I2C_IDLE;
-
 
284
                                                LED_GRN_OFF;
-
 
285
                                                return;
-
 
286
                                }
-
 
287
                                // enable acknowledge
-
 
288
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
-
 
289
                                // send address/direction byte on the bus
-
 
290
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
-
 
291
                                break;
-
 
292
       
-
 
293
                        // the address byte was send
-
 
294
                        case I2C_EVENT_MASTER_MODE_SELECTED:
-
 
295
                                // Clear EV6 by set again the PE bit
-
 
296
                                I2C_Cmd(I2C1, ENABLE);
-
 
297
                                // reset checksum
-
 
298
                                crc = 0;
-
 
299
                                switch(I2C_State)
-
 
300
                                {
-
 
301
                                        case I2C_TX_PROGRESS:
-
 
302
                                        // send command 1st data byte (allways the command id) 
-
 
303
                                        I2C_SendData(I2C1, I2C_Command);
-
 
304
                                        crc += I2C_Command;
-
 
305
                                        Tx_Idx = 0;
-
 
306
                                        // reset timeout
-
 
307
                                        I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset
-
 
308
                                        break;
-
 
309
       
-
 
310
                                        case I2C_RX_PROGRESS:
-
 
311
                                        Rx_Idx = 0;
-
 
312
                                        break;
-
 
313
       
-
 
314
                                        default: // unknown I2C state
-
 
315
                                        // should never happen
-
 
316
                                        I2C_GenerateSTOP (I2C1, ENABLE);
138
                  I2C_ReadRequest = 1;
317
                                        I2C_State = I2C_IDLE;
-
 
318
                                        break;
-
 
319
                                }
-
 
320
                                break;
-
 
321
       
-
 
322
                        // the master has transmitted a byte and slave has been acknowledged
-
 
323
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
-
 
324
                                       
-
 
325
                                // some bytes have to be transmitted
-
 
326
                                if(Tx_Idx < I2C_TxBufferSize)
-
 
327
                                {
-
 
328
                                        if(I2C_TxBuffer != NULL)
-
 
329
                                        {
-
 
330
                                                I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
-
 
331
                                                crc += I2C_TxBuffer[Tx_Idx];
-
 
332
                                        }
-
 
333
                                        else
-
 
334
                                        {
139
                  TimerI2CReadDelay = SetDelay(10);
335
                                                I2C_SendData(I2C1, 0x00);
-
 
336
                                        }
-
 
337
                                }
-
 
338
                                else // the last tx buffer byte was send
-
 
339
                                {
-
 
340
                                        // send crc byte at the end
-
 
341
                                        I2C_SendData(I2C1, crc);
-
 
342
                                        // generate stop or repeated start condition
-
 
343
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
-
 
344
                                        {
-
 
345
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition 
-
 
346
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
-
 
347
                                        }
-
 
348
                                        else
-
 
349
                                        {   // stop communication
-
 
350
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
-
 
351
                                                I2C_State = I2C_IDLE;                   // ready for new actions
-
 
352
                                                LED_GRN_OFF;
-
 
353
                                                DebugOut.Analog[15]++;
-
 
354
                                        }
-
 
355
                                }
-
 
356
                                Tx_Idx++;      
-
 
357
                                break;
-
 
358
       
-
 
359
                        // the master has received a byte from the slave
-
 
360
                case I2C_EVENT_MASTER_BYTE_RECEIVED:
-
 
361
                                // some bytes have to be received
-
 
362
                                if (Rx_Idx < I2C_RxBufferSize)
-
 
363
                                {       // copy received byte  from the data register to the rx-buffer
-
 
364
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
-
 
365
                                        // update checksum
-
 
366
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
-
 
367
                                }
-
 
368
                                // if the last byte (crc) was received
-
 
369
                                else if ( Rx_Idx == I2C_RxBufferSize)
-
 
370
                                {
-
 
371
                                        // generate a STOP condition on the bus before reading data register
-
 
372
                                        I2C_GenerateSTOP(I2C1, ENABLE);
-
 
373
                                        // compare last byte with checksum
-
 
374
                                        if(crc == I2C_ReceiveData(I2C1))
-
 
375
                                        {       // copy primary rx buffer content to rx buffer if exist
-
 
376
                                                if(I2C_RxBuffer != NULL)
-
 
377
                                                {
-
 
378
                                                        memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
-
 
379
                                                }
-
 
380
                                                DebugOut.Analog[15]++;
-
 
381
                                                CheckI2COkay++;
-
 
382
                                        }
-
 
383
                                        else // checksum error detected
-
 
384
                                        {
-
 
385
                                                DebugOut.Analog[14]++;
-
 
386
                                        }
-
 
387
                                        I2C_State = I2C_IDLE;
-
 
388
                                        LED_GRN_OFF;   
-
 
389
                                }
-
 
390
                                Rx_Idx++;
-
 
391
                                // if the 2nd last byte was received disable acknowledge for the last one
-
 
392
                                if ( Rx_Idx == I2C_RxBufferSize )
-
 
393
                                {
-
 
394
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
-
 
395
                                }
-
 
396
                                break;
-
 
397
       
-
 
398
                        default:
-
 
399
                                break;
140
                }
400
                }
141
      }
-
 
142
      else
-
 
143
      {
-
 
144
        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
-
 
145
        Tx_Idx++;
-
 
146
      }
-
 
147
      break;
-
 
148
 
-
 
149
    case I2C_EVENT_MASTER_BYTE_RECEIVED:           // EV7
-
 
150
          DebugOut.Analog[16]++;
-
 
151
          //if (Rx_Idx == 1) DebugOut.Analog[19] = I2C_RxBuffer[0];
-
 
152
          if (Rx_Idx < I2C_RxBufferSize)
-
 
153
          { I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
-
 
154
 
-
 
155
          }     else I2C_ReceiveData(I2C1);
-
 
156
 
-
 
157
          Rx_Idx++;
-
 
158
          if ( Rx_Idx == I2C_RxBufferSize+1 )
-
 
159
      {
-
 
160
        I2C_AcknowledgeConfig (I2C1, DISABLE);
-
 
161
      }
-
 
162
      if ( Rx_Idx == I2C_RxBufferSize+2 )
-
 
163
      {
-
 
164
        I2C_GenerateSTOP(I2C1, ENABLE);
-
 
165
                if (I2C_Command == I2C_CMD_READ_HEADING) CompassUpdateActiv = 0;
-
 
166
      }
-
 
167
      break;
-
 
168
 
-
 
169
    default:
-
 
170
      break;
-
 
171
  }
401
        }
172
}
402
}
173
//----------------------------------------------------------------
403
//----------------------------------------------------------------
174
void SendI2C_Command(u8 command)
404
void I2C1_SendCommand(u8 command)
175
{
405
{
-
 
406
        // If I2C transmission is in progress
-
 
407
        while (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress
-
 
408
        // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during
-
 
409
        // update of that buffer pointers and length
-
 
410
        I2C_ITConfig(I2C1, DISABLE);
-
 
411
        // update current command id
176
  I2C_Command = command;
412
        I2C_Command = command;
-
 
413
        // set pointers to data area with respect to the command id
-
 
414
        switch (command)
-
 
415
        {
-
 
416
            case I2C_CMD_VERSION:
-
 
417
                        I2C_RxBuffer = (u8 *)&MK3MAG_Version;
-
 
418
                        I2C_RxBufferSize = sizeof(MK3MAG_Version);
-
 
419
                        I2C_TxBuffer = NULL;
-
 
420
                        I2C_TxBufferSize = 0;
-
 
421
                        break;
-
 
422
                case I2C_CMD_WRITE_CAL:
-
 
423
                        I2C_RxBuffer = (u8 *)&I2C_ReadCal;
-
 
424
                        I2C_RxBufferSize = sizeof(I2C_ReadCal);
-
 
425
                        I2C_TxBuffer = (u8 *)&I2C_WriteCal;
-
 
426
                        I2C_TxBufferSize = sizeof(I2C_WriteCal);
-
 
427
                        break;
-
 
428
            case I2C_CMD_READ_MAG:
-
 
429
                        I2C_RxBuffer = (u8 *)&I2C_Mag;
-
 
430
                        I2C_RxBufferSize = sizeof(I2C_Mag);
-
 
431
                        I2C_TxBuffer = NULL;
-
 
432
                        I2C_TxBufferSize = 0;
-
 
433
                        break;
-
 
434
            case I2C_CMD_READ_HEADING:
-
 
435
                        DebugOut.Analog[26] = I2C_Heading.Heading;
-
 
436
                        I2C_RxBuffer = (u8 *)&I2C_Heading;
-
 
437
                        I2C_RxBufferSize = sizeof(I2C_Heading);
-
 
438
                        // updat atitude from spi rx buffer
-
 
439
                        I2C_WriteAttitude.Roll = FromFlightCtrl.IntegralRoll;
-
 
440
                        I2C_WriteAttitude.Nick = FromFlightCtrl.IntegralNick;
-
 
441
                        I2C_TxBuffer =  (u8 *)&I2C_WriteAttitude;
-
 
442
                        I2C_TxBufferSize = sizeof(I2C_WriteAttitude);
-
 
443
                        break;
-
 
444
                default: // unknown command id
-
 
445
                        I2C_RxBuffer = NULL;
-
 
446
                        I2C_RxBufferSize = 0;
-
 
447
                        I2C_TxBuffer =  NULL;
-
 
448
                        I2C_TxBufferSize = 0;
-
 
449
                        break;
-
 
450
        }
-
 
451
        // enable I2C IRQ again
-
 
452
        I2C_ITConfig(I2C1, ENABLE);
-
 
453
        // set direction to master transmitter
-
 
454
        I2C_Direction = I2C_MODE_TRANSMITTER;
-
 
455
        // test on busy flag and clear it 
-
 
456
        I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
-
 
457
        // initiale start condition on the bus 
177
  //I2C_GenerateSTOP(I2C1, ENABLE);
458
        I2C_GenerateStart(I2C1, ENABLE);
-
 
459
        // to be continued in the I2C1_IRQHandler() above
178
 
-
 
179
  switch (command)
460
 
180
  {
-
 
181
    case I2C_CMD_VERSION:
-
 
182
           I2C_RxBuffer = (u8 *)&I2C_Version;
-
 
183
                   I2C_RxBufferSize = sizeof(I2C_Version);
-
 
184
                   I2C_TxBufferSize = 0;
-
 
185
          break;
-
 
186
        case I2C_CMD_WRITE_EEPROM:
-
 
187
                   I2C_RxBufferSize = 0;
-
 
188
                   I2C_TxBuffer = (u8 *)&I2C_WriteEEPROM;
-
 
189
                   I2C_TxBufferSize = sizeof(I2C_WriteEEPROM);
-
 
190
          break;
-
 
191
        case I2C_CMD_WRITE_CAL:
-
 
192
                   I2C_RxBufferSize = 0;
-
 
193
                   I2C_WriteCal.CalByte = CompassCalState;
-
 
194
                   I2C_TxBuffer = (u8 *)&I2C_WriteCal;
-
 
195
                   I2C_TxBufferSize = sizeof(I2C_WriteCal);
-
 
196
          break;
-
 
197
    case I2C_CMD_READ_EEPROM:
-
 
198
           I2C_RxBuffer = (u8 *)&I2C_ReadEEPROM.Inhalt;
-
 
199
                   I2C_RxBufferSize = 2;
-
 
200
                   I2C_TxBuffer = (u8 *)&I2C_ReadEEPROM;
-
 
201
                   I2C_TxBufferSize = 1;
-
 
202
          break;
-
 
203
    case I2C_CMD_READ_MAG:
-
 
204
           I2C_RxBuffer = (u8 *)&I2C_Mag;
-
 
205
                   I2C_RxBufferSize = sizeof(I2C_Mag);
-
 
206
                   I2C_TxBufferSize = 0;
-
 
207
          break;
-
 
208
    case I2C_CMD_READ_HEADING:
-
 
209
               CompassUpdateActiv = 1;
-
 
210
           I2C_RxBuffer = (u8 *)&I2C_Heading;
-
 
211
                   I2C_RxBufferSize = sizeof(I2C_Heading);
-
 
212
                   I2C_TxBuffer =  (u8 *)&I2C_WriteNickRoll;
-
 
213
                   I2C_TxBufferSize = sizeof(I2C_WriteNickRoll);
-
 
214
          break;
461
       
215
 
462
   
216
  }
-
 
217
  /*
-
 
218
  if (I2C_RxBufferSize > 0)
-
 
219
  {
-
 
220
    I2C_RxBufferSize++;
-
 
221
        if (I2C_RxBufferSize < 3) I2C_RxBufferSize = 3;
-
 
-
 
463
}
222
  }*/
464
 
-
 
465
//----------------------------------------------------------------
-
 
466
void I2C1_GetMK3MagVersion(void)
223
 
467
{
-
 
468
        u8 msg[20];
-
 
469
    u8 repeat = 10;
-
 
470
 
-
 
471
        MK3MAG_Version.Major = 0xFF;
-
 
472
        MK3MAG_Version.Minor = 0xFF;
-
 
473
        MK3MAG_Version.Patch = 0xFF;
-
 
474
        MK3MAG_Version.Compatible = 0xFF;
224
  I2C_AcknowledgeConfig (I2C1, ENABLE);
475
 
-
 
476
        while ((MK3MAG_Version.Major == 0xFF)  && (repeat != 0))
-
 
477
        {
-
 
478
                I2C1_SendCommand(I2C_CMD_VERSION);     
-
 
479
                I2C1_Timeout = SetDelay(500);
-
 
480
                while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break;
-
 
481
                repeat--;
-
 
482
        }
-
 
483
   
-
 
484
        if (MK3MAG_Version.Major != 0xFF)
-
 
485
        {
-
 
486
                sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
-
 
487
                SerialPutString(msg);
-
 
488
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
225
  I2C_Direction = I2C_MODE_TRANSMITTER;
489
                SerialPutString(msg);
226
 
490
        }
227
  I2C_GenerateStart(I2C1, ENABLE);
491
        else SerialPutString("\n\rNo version information from MK3Mag.");
228
 
492
 
229
}
493
}
230
 
494