Subversion Repositories NaviCtrl

Rev

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

Rev 241 Rev 242
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <string.h>
57
#include <string.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "mk3mag.h"
59
#include "mk3mag.h"
60
#include "i2c.h"
60
#include "i2c.h"
61
#include "timer1.h"
61
#include "timer1.h"
-
 
62
#include "led.h"
62
#include "spi_slave.h"
63
#include "spi_slave.h"
63
#include "main.h"
64
#include "main.h"
64
#include "uart1.h"
65
#include "uart1.h"
-
 
66
#include "compass.h"
65
 
67
 
66
#define MK3MAG_SLAVE_ADDRESS            0x50  // i2C slave address
68
#define MK3MAG_SLAVE_ADDRESS            0x50  // i2C slave address
67
 
69
 
-
 
70
u8 MK3MAG_Present = 0;
-
 
71
 
-
 
72
typedef struct
-
 
73
{
-
 
74
  s16 Nick;
-
 
75
  s16 Roll;
68
u8 MK3MAG_StopPolling = 1;
76
} __attribute__((packed)) MK3MAG_WriteAttitude_t;
69
 
-
 
70
// Transfer buffers
77
 
71
volatile MK3MAG_Heading_t                       MK3MAG_Heading;
-
 
72
volatile MK3MAG_WriteAttitude_t         MK3MAG_WriteAttitude;
78
// Transfer buffers
73
volatile MK3MAG_MagVector_t                     MK3MAG_MagVector;
79
volatile MK3MAG_WriteAttitude_t         MK3MAG_WriteAttitude;
74
volatile MK3MAG_Version_t                       MK3MAG_Version;
80
volatile MK3MAG_Version_t                       MK3MAG_Version;
75
volatile MK3MAG_Cal_t                           MK3MAG_WriteCal;
81
volatile MK3MAG_Cal_t                           MK3MAG_WriteCal;
76
volatile MK3MAG_Cal_t                           MK3MAG_ReadCal;
82
volatile MK3MAG_Cal_t                           MK3MAG_ReadCal;
77
 
-
 
78
 
-
 
79
 
-
 
80
void MK3MAG_Init(void)
-
 
81
{
-
 
82
        MK3MAG_Version.Major = 0xFF;
-
 
83
        MK3MAG_Version.Minor = 0xFF;
-
 
84
        MK3MAG_Version.Patch = 0xFF;
-
 
85
        MK3MAG_Version.Compatible = 0xFF;
-
 
86
 
-
 
87
        MK3MAG_Heading.Heading = -1;
-
 
88
}
83
 
89
// -------------------------------------------------------------------------------
84
// -------------------------------------------------------------------------------
90
// the calculation of the MK3MAG packet checksum
85
// the calculation of the MK3MAG packet checksum
91
 
86
 
92
// calculate the crc of  bytecount bytes in buffer
87
// calculate the crc of  bytecount bytes in buffer
93
u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount)
88
u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount)
94
{
89
{
95
        u8 i, crc = 0;
90
        u8 i, crc = 0;
96
        for(i=0; i < bytecount; i++)
91
        for(i=0; i < bytecount; i++)
97
        {
92
        {
98
                crc += pBuffer[i];
93
                crc += pBuffer[i];
99
        }
94
        }
100
        crc = ~crc;
95
        crc = ~crc;
101
        return crc;
96
        return crc;
102
}
97
}
103
 
98
 
104
// assuming the last byte in buffer is the crc byte
99
// assuming the last byte in buffer is the crc byte
105
u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen)
100
u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen)
106
{
101
{
107
        u8 crc = 0, retval = 0;
102
        u8 crc = 0, retval = 0;
108
        if(BuffLen == 0) return(retval);
103
        if(BuffLen == 0) return(retval);
109
        crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1);
104
        crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1);
110
        if(crc == pBuffer[BuffLen-1])
105
        if(crc == pBuffer[BuffLen-1])
111
        {
106
        {
112
                retval = 1;
107
                retval = 1;
113
                DebugOut.Analog[15]++; // count mk3mag ok
108
                DebugOut.Analog[15]++; // count mk3mag ok
114
        }
109
        }
115
        else
110
        else
116
        {
111
        {
117
                retval = 0;
112
                retval = 0;
118
                DebugOut.Analog[14]++; // count mk3mag crc error
113
                DebugOut.Analog[14]++; // count mk3mag crc error
119
        }
114
        }
120
        return(retval);
115
        return(retval);
121
}
116
}
122
 
117
 
123
// -------------------------------------------------------------------------------
118
// -------------------------------------------------------------------------------
124
// the I2C rx data handler functions
119
// the I2C rx data handler functions
125
 
120
 
126
// rx data handler for version info request
121
// rx data handler for version info request
127
void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize)
122
void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize)
128
{       // if crc is ok and number of byte are matching
123
{       // if crc is ok and number of byte are matching
129
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) )
124
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) )
130
        {
125
        {
131
                memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version));
126
                memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version));
132
        }                
127
        }                
133
}
128
}
134
 
129
 
135
// rx data handler for calibration request
130
// rx data handler for calibration request
136
void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize)
131
void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize)
137
{       // if crc is ok and number of byte are matching
132
{       // if crc is ok and number of byte are matching
138
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) )
133
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) )
139
        {
134
        {
140
                memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal));
135
                memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal));
141
        }                        
136
        }                        
142
}
137
}
143
 
138
 
144
// rx data handler for magnetic vector request
139
// rx data handler for magnetic vector request
145
void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
140
void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
146
{       // if crc is ok and number of byte are matching
141
{       // if crc is ok and number of byte are matching
147
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_MagVector)+1)) )
142
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) )
148
        {
143
        {
149
                memcpy((u8 *)&MK3MAG_MagVector, pRxBuffer, sizeof(MK3MAG_MagVector));
144
                memcpy((u8 *)&MagVector, pRxBuffer, sizeof(MagVector));
150
        }                        
145
        }                        
151
}
146
}
152
 
147
 
153
// rx data handler for heading request
148
// rx data handler for heading request
154
void MK3MAG_UpdateHeading(u8* pRxBuffer, u8 RxBufferSize)
149
void MK3MAG_UpdateHeading(u8* pRxBuffer, u8 RxBufferSize)
155
{       // if crc is ok and number of byte are matching
150
{       // if crc is ok and number of byte are matching
156
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Heading)+1)) )
151
        if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(CompassHeading)+1)) )
157
        {
152
        {
158
                memcpy((u8 *)&MK3MAG_Heading, pRxBuffer, sizeof(MK3MAG_Heading));
153
                memcpy((u8 *)&CompassHeading, pRxBuffer, sizeof(CompassHeading));
159
        }                        
154
        }                        
160
}
155
}
161
 
156
 
162
//----------------------------------------------------------------
157
//----------------------------------------------------------------
163
void MK3MAG_SendCommand(u8 command)
158
void MK3MAG_SendCommand(u8 command)
164
{
159
{
165
        // disable I2C IRQ to check state
-
 
166
        //VIC_ITCmd(I2C1_ITLine, DISABLE);
-
 
167
        // If I2C no transmission is in progress
160
        // If I2C no transmission is in progress
168
        if(I2C_State == I2C_STATE_IDLE)
161
        if(I2C_State == I2C_STATE_IDLE)
169
        {
162
        {
170
                I2C_TxBufferSize = 0;
163
                I2C_TxBufferSize = 0;
171
                I2C_pRxHandler_t pRxHandlerFunc = NULL;
164
                I2C_pRxHandler_t pRxHandlerFunc = NULL;
172
                u8 RxBytes = 0;
165
                u8 RxBytes = 0;
173
 
166
 
174
                // update current command id
167
                // update current command id
175
                I2C_TxBuffer[I2C_TxBufferSize++] = command;
168
                I2C_TxBuffer[I2C_TxBufferSize++] = command;
176
 
169
 
177
                // set pointers to data area with respect to the command id
170
                // set pointers to data area with respect to the command id
178
                switch (command)
171
                switch (command)
179
                {
172
                {
180
                        case MK3MAG_CMD_VERSION:
173
                        case MK3MAG_CMD_VERSION:
181
                                RxBytes = sizeof(MK3MAG_Version)+1;
174
                                RxBytes = sizeof(MK3MAG_Version)+1;
182
                                pRxHandlerFunc = &MK3MAG_UpdateVersion;
175
                                pRxHandlerFunc = &MK3MAG_UpdateVersion;
183
                                break;
176
                                break;
184
                        case MK3MAG_CMD_WRITE_CAL:
177
                        case MK3MAG_CMD_WRITE_CAL:
185
                                RxBytes = sizeof(MK3MAG_ReadCal)+1;
178
                                RxBytes = sizeof(MK3MAG_ReadCal)+1;
186
                                pRxHandlerFunc = &MK3MAG_UpdateCalibration;
179
                                pRxHandlerFunc = &MK3MAG_UpdateCalibration;
187
                                memcpy((u8*)I2C_TxBuffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
180
                                memcpy((u8*)I2C_TxBuffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
188
                                I2C_TxBufferSize += sizeof(MK3MAG_WriteCal);
181
                                I2C_TxBufferSize += sizeof(MK3MAG_WriteCal);
189
                                break;
182
                                break;
190
                        case MK3MAG_CMD_READ_MAGVECT:
183
                        case MK3MAG_CMD_READ_MAGVECT:
191
                                RxBytes = sizeof(MK3MAG_MagVector)+1;
184
                                RxBytes = sizeof(MagVector)+1;
192
                                pRxHandlerFunc = &MK3MAG_UpdateMagVector;
185
                                pRxHandlerFunc = &MK3MAG_UpdateMagVector;
193
                                break;
186
                                break;
194
                        case MK3MAG_CMD_READ_HEADING:
187
                        case MK3MAG_CMD_READ_HEADING:
195
                                RxBytes = sizeof(MK3MAG_Heading)+1;
188
                                RxBytes = sizeof(CompassHeading)+1;
196
                                pRxHandlerFunc = &MK3MAG_UpdateHeading;
189
                                pRxHandlerFunc = &MK3MAG_UpdateHeading;
197
                                // update attitude from spi rx buffer
190
                                // update attitude from spi rx buffer
198
                                VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy
191
                                VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy
199
                                MK3MAG_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
192
                                MK3MAG_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
200
                                MK3MAG_WriteAttitude.Nick = FromFlightCtrl.AngleNick;
193
                                MK3MAG_WriteAttitude.Nick = FromFlightCtrl.AngleNick;
201
                                VIC_ITCmd(SSP0_ITLine, ENABLE);
194
                                VIC_ITCmd(SSP0_ITLine, ENABLE);
202
                                memcpy((u8*)I2C_TxBuffer+1, (u8*)&MK3MAG_WriteAttitude, sizeof(MK3MAG_WriteAttitude));
195
                                memcpy((u8*)I2C_TxBuffer+1, (u8*)&MK3MAG_WriteAttitude, sizeof(MK3MAG_WriteAttitude));
203
                                I2C_TxBufferSize += sizeof(MK3MAG_WriteAttitude);
196
                                I2C_TxBufferSize += sizeof(MK3MAG_WriteAttitude);
204
                                break;
197
                                break;
205
                        default: // unknown command id
198
                        default: // unknown command id
206
                                RxBytes = 0;
199
                                RxBytes = 0;
207
                                pRxHandlerFunc = NULL;
200
                                pRxHandlerFunc = NULL;
208
                                break;
201
                                break;
209
                }
202
                }
210
                // update packet checksum
203
                // update packet checksum
211
                I2C_TxBuffer[I2C_TxBufferSize] = MK3MAG_CalcCRC((u8*)I2C_TxBuffer, I2C_TxBufferSize);
204
                I2C_TxBuffer[I2C_TxBufferSize] = MK3MAG_CalcCRC((u8*)I2C_TxBuffer, I2C_TxBufferSize);
212
                I2C_TxBufferSize++;
205
                I2C_TxBufferSize++;
213
                // enable I2C IRQ again
-
 
214
                VIC_ITCmd(I2C1_ITLine, ENABLE);
-
 
215
                // initiate I2C transmission
206
                // initiate I2C transmission
216
                I2C_Transmission(MK3MAG_SLAVE_ADDRESS, pRxHandlerFunc, RxBytes);
207
                I2C_Transmission(MK3MAG_SLAVE_ADDRESS, pRxHandlerFunc, RxBytes);
217
        } // EOF I2C_State == I2C_IDLE
208
        } // EOF I2C_State == I2C_IDLE  
218
        else // I2C_State != I2C_IDLE
-
 
219
        {
-
 
220
                // re-enable I2C IRQ again
-
 
221
                //VIC_ITCmd(I2C1_ITLine, ENABLE);
-
 
222
        }      
-
 
223
}
209
}
224
 
210
 
225
 
211
 
226
//----------------------------------------------------------------
212
//----------------------------------------------------------------
227
void MK3MAG_GetVersion(void)
213
u8 MK3MAG_Init(void)
228
{
214
{
229
        u8 msg[64];
215
        u8 msg[64];
230
        u8 repeat;
216
        u8 repeat;
231
        u32 timeout;
217
        u32 timeout;
232
       
-
 
233
        UART1_PutString("\r\n Getting Version from MK3MAG");
-
 
234
        // stop polling of other commands
218
       
235
        MK3MAG_StopPolling = 1;
219
        MK3MAG_Present = 0;
236
 
220
 
237
        MK3MAG_Version.Major = 0xFF;
221
        MK3MAG_Version.Major = 0xFF;
238
        MK3MAG_Version.Minor = 0xFF;
222
        MK3MAG_Version.Minor = 0xFF;
239
        MK3MAG_Version.Patch = 0xFF;
223
        MK3MAG_Version.Patch = 0xFF;
240
        MK3MAG_Version.Compatible = 0xFF;
224
        MK3MAG_Version.Compatible = 0xFF;
-
 
225
 
-
 
226
        CompassHeading = -1;
-
 
227
 
241
        // polling of version info
228
        // polling of version info
242
        repeat = 0;
229
        repeat = 0;
243
        do
230
        do
244
        {
231
        {
245
                MK3MAG_SendCommand(MK3MAG_CMD_VERSION);
232
                MK3MAG_SendCommand(MK3MAG_CMD_VERSION);
246
                timeout = SetDelay(250);
233
                timeout = SetDelay(250);
247
                do
234
                do
248
                {
235
                {
249
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
236
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
250
                }while (!CheckDelay(timeout));
237
                }while (!CheckDelay(timeout));
251
                UART1_PutString(".");
238
                UART1_PutString(".");
252
                repeat++;
239
                repeat++;
253
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
240
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
254
        // if we got it
241
        // if we got it
255
        if (MK3MAG_Version.Major != 0xFF)
242
        if (MK3MAG_Version.Major != 0xFF)
256
        {
243
        {
257
                sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
244
                sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
258
                UART1_PutString(msg);
-
 
259
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
-
 
260
                UART1_PutString(msg);
245
                UART1_PutString(msg);
-
 
246
                if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
-
 
247
                {
-
 
248
                        UART1_PutString("\n\r MK3MAG not compatible!");
-
 
249
                        LED_RED_ON;
-
 
250
                }
-
 
251
                else
-
 
252
                {       // version ok
-
 
253
                        MK3MAG_Present = 1;
-
 
254
                }
261
        }
255
        }
262
        else UART1_PutString("\n\r No version information from MK3Mag.");
256
        return(MK3MAG_Present);
263
 
-
 
264
        MK3MAG_StopPolling = 0; // enable polling of heading command
-
 
265
}
257
}
266
 
258
 
267
//----------------------------------------------------------------
259
//----------------------------------------------------------------
268
void MK3MAG_UpdateCompass(void)
260
void MK3MAG_UpdateCompass(void)
269
{
261
{
270
        static u32 TimerCompassUpdate = 0;
262
        static u32 TimerCompassUpdate = 0;
271
 
263
 
272
        if( (I2C_State == I2C_STATE_OFF) || MK3MAG_StopPolling ) return;
264
        if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return;
273
       
265
       
274
        if(CheckDelay(TimerCompassUpdate))
266
        if(CheckDelay(TimerCompassUpdate))
275
        {
267
        {
276
                // check for incomming compass calibration request
268
                // check for incomming compass calibration request
277
                // update CalByte from spi input queue
269
                // update CalByte from spi input queue
278
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
270
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
279
                // send new calstate
271
                // send new calstate
280
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
272
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
281
                {
273
                {
282
                        MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL);
274
                        MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL);
283
                }
275
                }
284
                else // request current heading
276
                else // request current heading
285
                {
277
                {
286
                        MK3MAG_SendCommand(MK3MAG_CMD_READ_HEADING);
278
                        MK3MAG_SendCommand(MK3MAG_CMD_READ_HEADING);
287
                }
279
                }
288
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
280
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
289
        }
281
        }
290
}
282
}
291
 
283