Subversion Repositories NaviCtrl

Rev

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

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