Subversion Repositories NaviCtrl

Rev

Rev 321 | Rev 325 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 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.
1 ingob 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 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.
41 ingob 37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
1 ingob 38
// +     for non-commercial use (directly or indirectly)
41 ingob 39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 40
// +     with our written permission
41 ingob 41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 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
41 ingob 55
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 57
 
58
#include <string.h>
59
#include "91x_lib.h"
60
#include "led.h"
215 killagreg 61
#include "gps.h"
41 ingob 62
#include "uart1.h"
63
#include "spi_slave.h"
242 killagreg 64
#include "compass.h"
119 killagreg 65
#include "timer1.h"
66
#include "timer2.h"
136 killagreg 67
#include "config.h"
1 ingob 68
#include "main.h"
284 killagreg 69
#include "compass.h"
294 holgerb 70
#include "params.h"
1 ingob 71
 
41 ingob 72
#define SPI_RXSYNCBYTE1 0xAA
73
#define SPI_RXSYNCBYTE2 0x83
74
#define SPI_TXSYNCBYTE1 0x81
75
#define SPI_TXSYNCBYTE2 0x55
1 ingob 76
 
41 ingob 77
//communication packets
215 killagreg 78
FromFlightCtrl_t   FromFlightCtrl;
79
ToFlightCtrl_t     ToFlightCtrl;
183 killagreg 80
#define SPI0_TIMEOUT 500 // 500ms
146 killagreg 81
volatile u32 SPI0_Timeout = 0;
1 ingob 82
 
41 ingob 83
// tx packet buffer
84
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
85
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
86
volatile u8 SPI_TxBufferIndex = 0;
87
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
1 ingob 88
 
41 ingob 89
// rx packet buffer
90
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
91
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
92
volatile u8 SPI_RxBufferIndex = 0;
93
volatile u8 SPI_RxBuffer_Request = 0;
1 ingob 94
#define SPI_COMMAND_INDEX 0
95
 
149 killagreg 96
s32 Kalman_K = 32;
41 ingob 97
s32 Kalman_MaxDrift = 5 * 16;
98
s32 Kalman_MaxFusion = 64;
148 holgerb 99
s32 ToFcGpsZ = 0;
1 ingob 100
 
204 killagreg 101
u8 SPI_CommandSequence[] = { SPI_NCCMD_VERSION, SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_KALMAN};
41 ingob 102
u8 SPI_CommandCounter = 0;
222 holgerb 103
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
104
s32 HeadFreeStartAngle = 0;
277 killagreg 105
s16 FC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
286 killagreg 106
u32 ToFC_AltitudeRate = 0;
285 holgerb 107
s32 ToFC_AltitudeSetpoint = 0;
298 holgerb 108
u8  FromFC_VarioCharacter = ' ';
319 holgerb 109
u8 DisableFC_Sticks = 0;
323 holgerb 110
u8 NC_GPS_ModeCharacter = ' ';
58 killagreg 111
 
41 ingob 112
SPI_Version_t FC_Version;
1 ingob 113
 
41 ingob 114
//--------------------------------------------------------------
115
void SSP0_IRQHandler(void)
116
{
117
        static u8 rxchksum = 0;
118
        u8 rxdata;
1 ingob 119
 
189 killagreg 120
        #define SPI_SYNC1       0
121
        #define SPI_SYNC2       1
122
        #define SPI_DATA        2
123
        static u8 SPI_State = SPI_SYNC1;
124
 
195 killagreg 125
        IENABLE;
126
 
161 killagreg 127
        // clear pending bits
41 ingob 128
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
144 killagreg 129
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
189 killagreg 130
 
79 killagreg 131
        // while RxFIFO not empty
132
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
133
        {
41 ingob 134
                rxdata =  SSP0->DR; // catch the received byte
135
                // Fill TxFIFO while its not full or end of packet is reached
79 killagreg 136
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
161 killagreg 137
                {
79 killagreg 138
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
139
                        {
41 ingob 140
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
79 killagreg 141
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
142
                                SPI_TxBufferIndex++; // pointer to next byte
143
                        }
144
                        else // end of packet is reached reset and copy data to tx buffer
145
                        {
146
                                SPI_TxBufferIndex = 0;  // reset buffer index
147
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
148
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
149
                                BeepTime = 0; // reset local beeptime
41 ingob 150
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
79 killagreg 151
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
152
                        }
41 ingob 153
                }
154
                switch (SPI_State)
155
                {
156
                        case SPI_SYNC1:
157
                                SPI_RxBufferIndex = 0; // reset buffer index
158
                                rxchksum = rxdata;     // init checksum
159
                                if (rxdata == SPI_RXSYNCBYTE1)
160
                                {   // 1st syncbyte ok
161
                                        SPI_State = SPI_SYNC2;  // step to sync2
162
                                }
163
                                break;
79 killagreg 164
                        case SPI_SYNC2:
41 ingob 165
                                if (rxdata == SPI_RXSYNCBYTE2)
166
                                {  // 2nd Syncbyte ok
167
                                        rxchksum += rxdata;
168
                                        SPI_State = SPI_DATA;
169
                                }  // 2nd Syncbyte does not match
170
                                else
171
                                {
172
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
173
                                }
174
                                break;
175
                        case SPI_DATA:
176
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
177
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
178
                                {
179
                                        if (rxdata == rxchksum) // verify checksum byte
180
                                        {
181
                                                // copy SPI_RxBuffer -> FromFlightCtrl
182
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
183
                                                {
184
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
185
                                                        SPI_RxBuffer_Request = 1;
186
                                                }
146 killagreg 187
                                                // reset timeout counter on good packet
188
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
41 ingob 189
                                                DebugOut.Analog[13]++;
190
                                        }
191
                                        else // bad checksum byte
192
                                        {
193
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
194
                                        }
195
                                        SPI_State  = SPI_SYNC1; // reset state
196
                                }
197
                                else // end of packet not reached
198
                                {
199
                                        rxchksum += rxdata;      // update checksum
200
                                }
201
                                break;
202
                        default:
203
                                SPI_State  = SPI_SYNC1;
204
                                break;
205
                }
206
        }
195 killagreg 207
 
208
        IDISABLE;
1 ingob 209
}
210
 
211
//--------------------------------------------------------------
212
void SPI0_Init(void)
213
{
41 ingob 214
        GPIO_InitTypeDef GPIO_InitStructure;
215
        SSP_InitTypeDef   SSP_InitStructure;
1 ingob 216
 
110 killagreg 217
        UART1_PutString("\r\n SPI init...");
1 ingob 218
 
41 ingob 219
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
220
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
1 ingob 221
 
41 ingob 222
        GPIO_DeInit(GPIO2);
223
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
224
        GPIO_StructInit(&GPIO_InitStructure);
225
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
226
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
227
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
196 killagreg 228
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
41 ingob 229
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
230
        GPIO_Init (GPIO2, &GPIO_InitStructure);
1 ingob 231
 
41 ingob 232
        // SSP0_MISO pin GPIO2.6
233
        GPIO_StructInit(&GPIO_InitStructure);
234
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
235
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
236
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
196 killagreg 237
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
41 ingob 238
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
239
        GPIO_Init (GPIO2, &GPIO_InitStructure);
1 ingob 240
 
41 ingob 241
        SSP_DeInit(SSP0);
242
        SSP_StructInit(&SSP_InitStructure);
243
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
244
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
245
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
246
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
247
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
248
        SSP_InitStructure.SSP_ClockRate = 0;
1 ingob 249
 
41 ingob 250
        SSP_Init(SSP0, &SSP_InitStructure);
144 killagreg 251
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
202 killagreg 252
 
41 ingob 253
        SSP_Cmd(SSP0, ENABLE);
254
        // initialize the syncbytes in the tx buffer
255
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
256
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
257
        // set the pointer to the checksum byte in the tx buffer
258
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
259
 
215 killagreg 260
        ToFlightCtrl.GPSStick.Nick = 0;
261
        ToFlightCtrl.GPSStick.Roll = 0;
262
        ToFlightCtrl.GPSStick.Yaw = 0;
263
 
136 killagreg 264
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
41 ingob 265
        VIC_ITCmd(SSP0_ITLine, ENABLE);
266
 
146 killagreg 267
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
268
 
110 killagreg 269
        UART1_PutString("ok");
1 ingob 270
}
41 ingob 271
 
222 holgerb 272
 
1 ingob 273
//------------------------------------------------------
41 ingob 274
void SPI0_UpdateBuffer(void)
1 ingob 275
{
180 killagreg 276
        static u32 timeout = 0;
277
        static u8 counter = 50;
254 killagreg 278
        static u8 CompassCalState = 0;
268 killagreg 279
        static u8 FCCalibActive = 0;
299 killagreg 280
        s16 tmp;
202 killagreg 281
 
41 ingob 282
        if (SPI_RxBuffer_Request)
283
        {
284
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
285
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
254 killagreg 286
                ToFlightCtrl.CompassHeading = Compass_Heading;
189 killagreg 287
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
228 holgerb 288
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
286 killagreg 289
                ToFlightCtrl.MagVecX = MagVector.X;
290
                ToFlightCtrl.MagVecY = MagVector.Y;
291
                ToFlightCtrl.MagVecZ = MagVector.Z;
284 killagreg 292
                ToFlightCtrl.NCStatus = 0;
41 ingob 293
                // cycle spi commands
294
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
118 killagreg 295
                // restart command cycle at the end
41 ingob 296
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
319 holgerb 297
#define FLAG_GPS_AID 0x01
41 ingob 298
                switch (ToFlightCtrl.Command)
299
                {
202 killagreg 300
                        case  SPI_NCCMD_KALMAN:
223 killagreg 301
                                CalcHeadFree();
149 killagreg 302
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
92 killagreg 303
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
304
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
118 killagreg 305
                                ToFlightCtrl.Param.Byte[3]      = (u8) SerialLinkOkay;
149 killagreg 306
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
222 holgerb 307
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
223 killagreg 308
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
319 holgerb 309
                if(DisableFC_Sticks) ToFlightCtrl.Param.Byte[7] = FLAG_GPS_AID; else ToFlightCtrl.Param.Byte[7] = 0x00;
280 killagreg 310
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
278 killagreg 311
                                {
312
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
280 killagreg 313
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
278 killagreg 314
                                }
315
                                else
280 killagreg 316
                                {
278 killagreg 317
                                        ToFlightCtrl.Param.sInt[4] = -1;
318
                                }
294 holgerb 319
 
299 killagreg 320
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
295 killagreg 321
                                {
299 killagreg 322
                                        ToFlightCtrl.Param.sInt[5] = tmp;
323
                                }
324
                                else
325
                                {
295 killagreg 326
                                        if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) ToFlightCtrl.Param.sInt[5] = CAM_Orientation.Elevation; // only, if carefree is active
327
                                        else ToFlightCtrl.Param.sInt[5] = 0;
328
                                }
161 killagreg 329
                                break;
58 killagreg 330
 
202 killagreg 331
                        case SPI_NCCMD_VERSION:
332
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
333
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
204 killagreg 334
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
202 killagreg 335
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
264 killagreg 336
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
231 holgerb 337
                                ToFlightCtrl.Param.Byte[5] = DebugOut.Status[0];
338
                                ToFlightCtrl.Param.Byte[6] = DebugOut.Status[1];
233 holgerb 339
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;
323 holgerb 340
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
202 killagreg 341
                                break;
223 killagreg 342
 
204 killagreg 343
                        case SPI_NCCMD_GPSINFO:
344
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
345
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
346
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
232 killagreg 347
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
258 holgerb 348
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
349
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
306 holgerb 350
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;
351
                                ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
299 killagreg 352
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
353
                                {
354
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
295 killagreg 355
                                }
299 killagreg 356
                                else
357
                                {
358
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
359
                                }
360
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
361
                                {
362
                                        ToFlightCtrl.Param.sInt[5] = tmp;
363
                                }
364
                                else
365
                                {
366
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
367
                                }
300 holgerb 368
//                              DebugOut.Analog[25] = (s16)ToFlightCtrl.Param.Byte[9];
369
//                              DebugOut.Analog[20] = ToFlightCtrl.Param.sInt[5];
204 killagreg 370
                                break;
299 killagreg 371
 
41 ingob 372
                        default:
373
                                break;
285 holgerb 374
// 0 = 0,1
375
// 1 = 2,3
376
// 2 = 4,5
377
// 3 = 6,7
378
// 4 = 8,9
379
// 5 = 10,11
41 ingob 380
                }
381
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
1 ingob 382
 
383
 
41 ingob 384
                switch(FromFlightCtrl.Command)
385
                {
202 killagreg 386
                        case SPI_FCCMD_USER:
41 ingob 387
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
388
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
389
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
390
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
391
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
392
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
393
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
394
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
255 killagreg 395
                                if(ClearFCStatusFlags)
41 ingob 396
                                {
255 killagreg 397
                                        FC.StatusFlags = 0;
398
                                        ClearFCStatusFlags = 0;
41 ingob 399
                                }
255 killagreg 400
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
320 holgerb 401
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
268 killagreg 402
                                {
403
                                        Compass_Init();
404
                                        FCCalibActive = 1;
320 holgerb 405
HeadFreeStartAngle = FromFlightCtrl.GyroHeading;
268 killagreg 406
                                }
407
                                else
408
                                {
284 killagreg 409
                                        FCCalibActive = 0;
268 killagreg 410
                                }
320 holgerb 411
if(FC.StatusFlags & FC_STATUS_START) HeadFreeStartAngle = FromFlightCtrl.GyroHeading;
412
 
413
 if((Parameter.ExtraConfig & CFG_LEARNABLE_CAREFREE) && (NCFlags & NC_FLAG_GPS_OK))
414
  {
415
   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
416
    {
417
     if(NaviData.HomePositionDeviation.Distance > 100)   // nur bei ausreichender distance
418
     {
419
       HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800) % 3600; // in 0.1°
420
     }
421
         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
422
       HeadFreeStartAngle = FromFlightCtrl.GyroHeading; // in 0.1°        
423
 
424
    }
425
  }
426
 
223 killagreg 427
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
255 killagreg 428
                                DebugOut.Analog[5] = FC.StatusFlags;
294 holgerb 429
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
255 killagreg 430
                                NaviData.FCStatusFlags = FC.StatusFlags;
294 holgerb 431
                                NaviData.FCStatusFlags2 = FC.StatusFlags2;
321 holgerb 432
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[10];
41 ingob 433
                                break;
1 ingob 434
 
206 killagreg 435
                        case SPI_FCCMD_ACCU:
223 killagreg 436
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
206 killagreg 437
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
438
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
439
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[5];
298 holgerb 440
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[6];
320 holgerb 441
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[7];
442
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[8];
298 holgerb 443
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
444
                                 {
445
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
446
                                 }
206 killagreg 447
                                NaviData.UBat = FC.BAT_Voltage;
448
                                NaviData.Current = FC.BAT_Current;
449
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
450
                                break;
451
 
193 killagreg 452
#define CHK_POTI(b,a) { if(a < 248) b = a; else b = FC.Poti[255 - a]; }
190 killagreg 453
#define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max); }
454
 
202 killagreg 455
                        case SPI_FCCMD_PARAMETER1:
41 ingob 456
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
457
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
458
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
459
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
460
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
461
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
462
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
463
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
103 killagreg 464
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
41 ingob 465
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
466
                                CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255);
467
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
121 killagreg 468
                                break;
1 ingob 469
 
202 killagreg 470
                        case SPI_FCCMD_STICK:
41 ingob 471
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
472
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
473
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
474
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
190 killagreg 475
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
476
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
477
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
478
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
479
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
480
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
481
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
202 killagreg 482
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
41 ingob 483
                                break;
27 holgerb 484
 
202 killagreg 485
                        case SPI_FCCMD_MISC:
41 ingob 486
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
487
                                {       // put only new CompassCalState into queue to send via I2C
488
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
254 killagreg 489
                                        Compass_SetCalState(CompassCalState);
41 ingob 490
                                }
58 killagreg 491
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
205 killagreg 492
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
287 holgerb 493
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
494
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
160 holgerb 495
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
496
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
497
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
190 killagreg 498
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
499
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
294 holgerb 500
                                if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
501
//                              NaviData.RC_RSSI = FC.RC_RSSI;
206 killagreg 502
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
41 ingob 503
                                break;
161 killagreg 504
 
202 killagreg 505
                        case SPI_FCCMD_SERVOS:
121 killagreg 506
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
507
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
508
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
509
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
161 killagreg 510
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
121 killagreg 511
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
512
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
513
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
161 killagreg 514
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
515
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
119 killagreg 516
                                break;
1 ingob 517
 
202 killagreg 518
                        case SPI_FCCMD_VERSION:
119 killagreg 519
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
520
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
521
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
522
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
523
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
255 killagreg 524
                                FC.Error[0]                     = FromFlightCtrl.Param.Byte[5];
525
                                FC.Error[1]                     = FromFlightCtrl.Param.Byte[6];
526
                                FC.Error[2]                     = FromFlightCtrl.Param.Byte[7];
527
                                FC.Error[3]                     = FromFlightCtrl.Param.Byte[8];
528
                                FC.Error[4]                     = FromFlightCtrl.Param.Byte[9];
320 holgerb 529
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
231 holgerb 530
                                DebugOut.Status[0] |= 0x01; // status of FC Present
232 killagreg 531
                                DebugOut.Status[0] |= 0x02; // status of BL Present
255 killagreg 532
                                if(FC.Error[0] || FC.Error[1] || FC.Error[2] || FC.Error[3] || FC.Error[4]) DebugOut.Status[1] |= 0x01;
232 killagreg 533
                                else DebugOut.Status[1] &= ~0x01;
41 ingob 534
                                break;
535
                        default:
204 killagreg 536
                                break;
41 ingob 537
                }
1 ingob 538
 
41 ingob 539
                // every time we got new data from the FC via SPI call the navigation routine
215 killagreg 540
                // and update GPSStick that are returned to FC
541
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
255 killagreg 542
                ClearFCStatusFlags = 1;
202 killagreg 543
 
180 killagreg 544
                if(counter)
545
                {
546
                        counter--;                                       // count down to enable servo
202 killagreg 547
                        if(!counter) TIMER2_Init();  // enable Servo Output
180 killagreg 548
                }
1 ingob 549
 
41 ingob 550
                SPI_RxBuffer_Request = 0;
180 killagreg 551
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
1 ingob 552
 
62 killagreg 553
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
554
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
61 holgerb 555
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
556
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
62 killagreg 557
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
79 killagreg 558
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
559
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
560
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
561
        }       // EOF if(SPI_RxBuffer_Request)
180 killagreg 562
        else // no new SPI data
202 killagreg 563
        {
180 killagreg 564
                if(CheckDelay(timeout) && (counter == 0))
565
                {
566
                        TIMER2_Deinit();  // disable Servo Output
567
                        counter = 50;     // reset counter for enabling Servo Output
568
                }
569
        }
1 ingob 570
}
571
 
41 ingob 572
//------------------------------------------------------
573
void SPI0_GetFlightCtrlVersion(void)
574
{
154 killagreg 575
        u32 timeout;
576
        u8 repeat;
78 holgerb 577
        u8 msg[64];
41 ingob 578
 
297 ingob 579
        UART1_PutString("\r\n Looking for FlightControl");
41 ingob 580
        FC_Version.Major = 0xFF;
581
        FC_Version.Minor = 0xFF;
582
        FC_Version.Patch = 0xFF;
583
        FC_Version.Compatible = 0xFF;
584
 
165 killagreg 585
        // polling FC version info
154 killagreg 586
        repeat = 0;
41 ingob 587
        do
588
        {
154 killagreg 589
                timeout = SetDelay(250);
590
                do
591
                {
592
                        SPI0_UpdateBuffer();
593
                        if (FC_Version.Major != 0xFF)  break;
594
                }while (!CheckDelay(timeout));
595
                UART1_PutString(".");
596
                repeat++;
180 killagreg 597
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
41 ingob 598
        // if we got it
599
        if (FC_Version.Major != 0xFF)
600
        {
242 killagreg 601
                sprintf(msg, " FC V%d.%d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
110 killagreg 602
                UART1_PutString(msg);
41 ingob 603
        }
242 killagreg 604
        else UART1_PutString("\n\r not found!");
41 ingob 605
}
606
 
607