Subversion Repositories NaviCtrl

Rev

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

Rev 462 Rev 463
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
 
56
 
57
#include <string.h>
57
#include <string.h>
58
#include <math.h>
58
#include <math.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "led.h"
60
#include "led.h"
61
#include "gps.h"
61
#include "gps.h"
62
#include "uart1.h"
62
#include "uart1.h"
63
#include "spi_slave.h"
63
#include "spi_slave.h"
64
#include "compass.h"
64
#include "compass.h"
65
#include "timer1.h"
65
#include "timer1.h"
66
#include "timer2.h"
66
#include "timer2.h"
67
#include "config.h"
67
#include "config.h"
68
#include "main.h"
68
#include "main.h"
69
#include "compass.h"
69
#include "compass.h"
70
#include "params.h"
70
#include "params.h"
71
#include "stdlib.h"
71
#include "stdlib.h"
72
#include "settings.h"
72
#include "settings.h"
73
 
73
 
74
#define SPI_RXSYNCBYTE1 0xAA
74
#define SPI_RXSYNCBYTE1 0xAA
75
#define SPI_RXSYNCBYTE2 0x83
75
#define SPI_RXSYNCBYTE2 0x83
76
#define SPI_TXSYNCBYTE1 0x81
76
#define SPI_TXSYNCBYTE1 0x81
77
#define SPI_TXSYNCBYTE2 0x55
77
#define SPI_TXSYNCBYTE2 0x55
78
 
78
 
79
//communication packets
79
//communication packets
80
FromFlightCtrl_t   FromFlightCtrl;
80
FromFlightCtrl_t   FromFlightCtrl;
81
ToFlightCtrl_t     ToFlightCtrl;
81
ToFlightCtrl_t     ToFlightCtrl;
82
#define SPI0_TIMEOUT     2500  //ms
82
#define SPI0_TIMEOUT     2500  //ms
83
volatile u32 SPI0_Timeout = 0;
83
volatile u32 SPI0_Timeout = 0;
84
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
84
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
85
u8 SpeakHoTT = 0;
85
u8 SpeakHoTT = 0;
86
 
86
 
87
// tx packet buffer
87
// tx packet buffer
88
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
88
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
89
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN + 10];
89
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN + 10];
90
volatile u8 SPI_TxBufferIndex = 0;
90
volatile u8 SPI_TxBufferIndex = 0;
91
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
91
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
92
 
92
 
93
// rx packet buffer
93
// rx packet buffer
94
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
94
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
95
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN+10];
95
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN+10];
96
volatile u8 SPI_RxBufferIndex = 0;
96
volatile u8 SPI_RxBufferIndex = 0;
97
volatile u8 SPI_RxBuffer_Request = 0;
97
volatile u8 SPI_RxBuffer_Request = 0;
98
#define SPI_COMMAND_INDEX 0
98
#define SPI_COMMAND_INDEX 0
99
 
99
 
100
s32 Kalman_K = 32;
100
s32 Kalman_K = 32;
101
s32 Kalman_MaxDrift = 5 * 16;
101
s32 Kalman_MaxDrift = 5 * 16;
102
s32 Kalman_MaxFusion = 64;
102
s32 Kalman_MaxFusion = 64;
103
s32 Kalman_Kompass = 32;
103
s32 Kalman_Kompass = 32;
104
s32 ToFcGpsZ = 0;
104
s32 ToFcGpsZ = 0;
105
u8 CompassCalState = 0;
105
u8 CompassCalState = 0;
106
 
106
 
107
u8 SPI_CommandSequence[] = { SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO, SPI_NCCMD_KALMAN, SPI_MISC, SPI_NCCMD_KALMAN, SPI_NCCMD_VERSION };
107
u8 SPI_CommandSequence[] = { SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO, SPI_NCCMD_KALMAN, SPI_MISC, SPI_NCCMD_KALMAN, SPI_NCCMD_VERSION };
108
u8 SPI_CommandCounter = 0;
108
u8 SPI_CommandCounter = 0;
109
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
109
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
110
s32 HeadFreeStartAngle = 0;
110
s32 HeadFreeStartAngle = 0;
111
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
111
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
112
u32 ToFC_AltitudeRate = 0;
112
u32 ToFC_AltitudeRate = 0;
113
s32 ToFC_AltitudeSetpoint = 0;
113
s32 ToFC_AltitudeSetpoint = 0;
114
u8  FromFC_VarioCharacter = ' ';
114
u8  FromFC_VarioCharacter = ' ';
115
u8 GPS_Aid_StickMultiplikator = 0;
115
u8 GPS_Aid_StickMultiplikator = 0;
116
u8 NC_GPS_ModeCharacter = ' ';
116
u8 NC_GPS_ModeCharacter = ' ';
117
u8 FCCalibActive = 0;
117
u8 FCCalibActive = 0;
118
u8 FC_is_Calibrated = 0;
118
u8 FC_is_Calibrated = 0;
119
Motor_t Motor[12];
119
Motor_t Motor[12];
120
u8 NC_To_FC_Flags = 0;
120
u8 NC_To_FC_Flags = 0;
121
u8 BL_MinOfMaxPWM;  // indication if all BL-controllers run on full power
121
u8 BL_MinOfMaxPWM;  // indication if all BL-controllers run on full power
122
u32 FC_I2C_ErrorConter;
122
u32 FC_I2C_ErrorConter;
123
SPI_Version_t FC_Version;
123
SPI_Version_t FC_Version;
124
s16 POI_KameraNick = 0;
124
s16 POI_KameraNick = 0;
125
u8 NC_Wait_for_LED = 0;
125
u8 NC_Wait_for_LED = 0;
-
 
126
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
126
 
127
 
127
//--------------------------------------------------------------
128
//--------------------------------------------------------------
128
void SSP0_IRQHandler(void)
129
void SSP0_IRQHandler(void)
129
{
130
{
130
        static u8 rxchksum = 0;
131
        static u8 rxchksum = 0;
131
        u8 rxdata;
132
        u8 rxdata;
132
 
133
 
133
        #define SPI_SYNC1       0
134
        #define SPI_SYNC1       0
134
        #define SPI_SYNC2       1
135
        #define SPI_SYNC2       1
135
        #define SPI_DATA        2
136
        #define SPI_DATA        2
136
        static u8 SPI_State = SPI_SYNC1;
137
        static u8 SPI_State = SPI_SYNC1;
137
        //IENABLE;
138
        //IENABLE;
138
 
139
 
139
        // clear pending bits
140
        // clear pending bits
140
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
141
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
141
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
142
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
142
 
143
 
143
        // while RxFIFO not empty
144
        // while RxFIFO not empty
144
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
145
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
145
        {
146
        {
146
                rxdata =  SSP0->DR; // catch the received byte
147
                rxdata =  SSP0->DR; // catch the received byte
147
                // Fill TxFIFO while its not full or end of packet is reached
148
                // Fill TxFIFO while its not full or end of packet is reached
148
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
149
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
149
                {
150
                {
150
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
151
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
151
                        {
152
                        {
152
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
153
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
153
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
154
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
154
                                if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
155
                                if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
155
                                SPI_TxBufferIndex++; // pointer to next byte
156
                                SPI_TxBufferIndex++; // pointer to next byte
156
                        }
157
                        }
157
                        else // end of packet is reached reset and copy data to tx buffer
158
                        else // end of packet is reached reset and copy data to tx buffer
158
                        {
159
                        {
159
                                SPI_TxBufferIndex = 0;  // reset buffer index
160
                                SPI_TxBufferIndex = 0;  // reset buffer index
160
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
161
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
161
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
162
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
162
                                BeepTime = 0; // reset local beeptime
163
                                BeepTime = 0; // reset local beeptime
163
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
164
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
164
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
165
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
165
                        }
166
                        }
166
                }
167
                }
167
                switch (SPI_State)
168
                switch (SPI_State)
168
                {
169
                {
169
                        case SPI_SYNC1:
170
                        case SPI_SYNC1:
170
                                SPI_RxBufferIndex = 0; // reset buffer index
171
                                SPI_RxBufferIndex = 0; // reset buffer index
171
                                rxchksum = rxdata;     // init checksum
172
                                rxchksum = rxdata;     // init checksum
172
                                if (rxdata == SPI_RXSYNCBYTE1)
173
                                if (rxdata == SPI_RXSYNCBYTE1)
173
                                {   // 1st syncbyte ok
174
                                {   // 1st syncbyte ok
174
                                        SPI_State = SPI_SYNC2;  // step to sync2
175
                                        SPI_State = SPI_SYNC2;  // step to sync2
175
                                }
176
                                }
176
                                break;
177
                                break;
177
                        case SPI_SYNC2:
178
                        case SPI_SYNC2:
178
                                if (rxdata == SPI_RXSYNCBYTE2)
179
                                if (rxdata == SPI_RXSYNCBYTE2)
179
                                {  // 2nd Syncbyte ok
180
                                {  // 2nd Syncbyte ok
180
                                        rxchksum += rxdata;
181
                                        rxchksum += rxdata;
181
                                        SPI_State = SPI_DATA;
182
                                        SPI_State = SPI_DATA;
182
                                }  // 2nd Syncbyte does not match
183
                                }  // 2nd Syncbyte does not match
183
                                else
184
                                else
184
                                {
185
                                {
185
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
186
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
186
                                }
187
                                }
187
                                break;
188
                                break;
188
                        case SPI_DATA:
189
                        case SPI_DATA:
189
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
190
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
190
                                if(SPI_RxBufferIndex >= SPI_RXBUFFER_LEN)  // end of packet is reached
191
                                if(SPI_RxBufferIndex >= SPI_RXBUFFER_LEN)  // end of packet is reached
191
                                {
192
                                {
192
                                        if (rxdata == rxchksum) // verify checksum byte
193
                                        if (rxdata == rxchksum) // verify checksum byte
193
                                        {
194
                                        {
194
                                                // copy SPI_RxBuffer -> FromFlightCtrl
195
                                                // copy SPI_RxBuffer -> FromFlightCtrl
195
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
196
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
196
                                                {
197
                                                {
197
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
198
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
198
                                                        SPI_RxBuffer_Request = 1;
199
                                                        SPI_RxBuffer_Request = 1;
199
                                                }
200
                                                }
200
                                                // reset timeout counter on good packet
201
                                                // reset timeout counter on good packet
201
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
202
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
202
                                                DebugOut.Analog[13]++;
203
                                                DebugOut.Analog[13]++;
203
                                        }
204
                                        }
204
                                        else // bad checksum byte
205
                                        else // bad checksum byte
205
                                        {
206
                                        {
206
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
207
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
207
                                        }
208
                                        }
208
                                        SPI_State  = SPI_SYNC1; // reset state
209
                                        SPI_State  = SPI_SYNC1; // reset state
209
                                }
210
                                }
210
                                else // end of packet not reached
211
                                else // end of packet not reached
211
                                {
212
                                {
212
                                        rxchksum += rxdata;      // update checksum
213
                                        rxchksum += rxdata;      // update checksum
213
                                }
214
                                }
214
                                break;
215
                                break;
215
                        default:
216
                        default:
216
                                SPI_State  = SPI_SYNC1;
217
                                SPI_State  = SPI_SYNC1;
217
                                break;
218
                                break;
218
                }
219
                }
219
        }
220
        }
220
 
221
 
221
//      IDISABLE;
222
//      IDISABLE;
222
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
223
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
223
}
224
}
224
 
225
 
225
//--------------------------------------------------------------
226
//--------------------------------------------------------------
226
void SPI0_Init(void)
227
void SPI0_Init(void)
227
{
228
{
228
        GPIO_InitTypeDef GPIO_InitStructure;
229
        GPIO_InitTypeDef GPIO_InitStructure;
229
        SSP_InitTypeDef   SSP_InitStructure;
230
        SSP_InitTypeDef   SSP_InitStructure;
230
 
231
 
231
        UART1_PutString("\r\n SPI init...");
232
        UART1_PutString("\r\n SPI init...");
232
 
233
 
233
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
234
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
234
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
235
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
235
 
236
 
236
        GPIO_DeInit(GPIO2);
237
        GPIO_DeInit(GPIO2);
237
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
238
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
238
        GPIO_StructInit(&GPIO_InitStructure);
239
        GPIO_StructInit(&GPIO_InitStructure);
239
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
240
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
240
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
241
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
241
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
242
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
242
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
243
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
243
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
244
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
244
        GPIO_Init (GPIO2, &GPIO_InitStructure);
245
        GPIO_Init (GPIO2, &GPIO_InitStructure);
245
 
246
 
246
        // SSP0_MISO pin GPIO2.6
247
        // SSP0_MISO pin GPIO2.6
247
        GPIO_StructInit(&GPIO_InitStructure);
248
        GPIO_StructInit(&GPIO_InitStructure);
248
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
249
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
249
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
250
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
250
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
251
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
251
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
252
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
252
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
253
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
253
        GPIO_Init (GPIO2, &GPIO_InitStructure);
254
        GPIO_Init (GPIO2, &GPIO_InitStructure);
254
 
255
 
255
        SSP_DeInit(SSP0);
256
        SSP_DeInit(SSP0);
256
        SSP_StructInit(&SSP_InitStructure);
257
        SSP_StructInit(&SSP_InitStructure);
257
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
258
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
258
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
259
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
259
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
260
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
260
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
261
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
261
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
262
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
262
        SSP_InitStructure.SSP_ClockRate = 0;
263
        SSP_InitStructure.SSP_ClockRate = 0;
263
 
264
 
264
        SSP_Init(SSP0, &SSP_InitStructure);
265
        SSP_Init(SSP0, &SSP_InitStructure);
265
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
266
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
266
        SSP_Cmd(SSP0, ENABLE);
267
        SSP_Cmd(SSP0, ENABLE);
267
        // initialize the syncbytes in the tx buffer
268
        // initialize the syncbytes in the tx buffer
268
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
269
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
269
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
270
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
270
        // set the pointer to the checksum byte in the tx buffer
271
        // set the pointer to the checksum byte in the tx buffer
271
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
272
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
272
 
273
 
273
        ToFlightCtrl.GPSStick.Nick = 0;
274
        ToFlightCtrl.GPSStick.Nick = 0;
274
        ToFlightCtrl.GPSStick.Roll = 0;
275
        ToFlightCtrl.GPSStick.Roll = 0;
275
//      ToFlightCtrl.GPSStick.Yaw = 0;
276
//      ToFlightCtrl.GPSStick.Yaw = 0;
276
 
277
 
277
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
278
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
278
        VIC_ITCmd(SSP0_ITLine, ENABLE);
279
        VIC_ITCmd(SSP0_ITLine, ENABLE);
279
 
280
 
280
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
281
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
281
 
282
 
282
        UART1_PutString("ok");
283
        UART1_PutString("ok");
283
}
284
}
284
 
285
 
285
 
286
 
286
//------------------------------------------------------
287
//------------------------------------------------------
287
void SPI0_UpdateBuffer(void)
288
void SPI0_UpdateBuffer(void)
288
{
289
{
289
        static u32 timeout = 0;
290
        static u32 timeout = 0;
290
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
291
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
291
        static s16 last_wp_event = 0;
292
        static s16 last_wp_event = 0;
292
        s16 tmp;
293
        s16 tmp;
293
        s32 i1,i2;
294
        s32 i1,i2;
294
/*
295
/*
295
        union
296
        union
296
        {
297
        {
297
                unsigned char Byte[4];
298
                unsigned char Byte[4];
298
                unsigned int Int[2];
299
                unsigned int Int[2];
299
                unsigned long Long;
300
                unsigned long Long;
300
        } Temp;
301
        } Temp;
301
*/     
302
*/     
302
    SPIWatchDog = 3500;    // stop communication to FC after this timeout
303
    SPIWatchDog = 3500;    // stop communication to FC after this timeout
303
        if(SPI_RxBuffer_Request)
304
        if(SPI_RxBuffer_Request)
304
        {
305
        {
305
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
306
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
306
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
307
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
307
                ToFlightCtrl.CompassHeading = Compass_Heading;
308
                ToFlightCtrl.CompassHeading = Compass_Heading;
308
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
309
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
-
 
310
GyroCompassCorrected = (3600 + FromFlightCtrl.GyroHeading - GeoMagDec) % 3600;
309
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
311
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
310
                ToFlightCtrl.MagVecX = MagVector.X;
312
                ToFlightCtrl.MagVecX = MagVector.X;
311
                ToFlightCtrl.MagVecY = MagVector.Y;
313
                ToFlightCtrl.MagVecY = MagVector.Y;
312
                ToFlightCtrl.MagVecZ = MagVector.Z;
314
                ToFlightCtrl.MagVecZ = MagVector.Z;
313
//              ToFlightCtrl.NCStatus = 0;
315
//              ToFlightCtrl.NCStatus = 0;
314
                // cycle spi commands
316
                // cycle spi commands
315
                if(ErrorCode != last_error_code && enable_injecting)
317
                if(ErrorCode != last_error_code && enable_injecting)
316
                 {
318
                 {
317
                  ToFlightCtrl.Command = SPI_NCCMD_VERSION;
319
                  ToFlightCtrl.Command = SPI_NCCMD_VERSION;
318
                  last_error_code = ErrorCode;
320
                  last_error_code = ErrorCode;
319
                  enable_injecting = 0;
321
                  enable_injecting = 0;
320
                 }
322
                 }
321
                else
323
                else
322
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
324
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
323
                 {
325
                 {
324
                  ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
326
                  ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
325
                  last_wp_event = FC_WP_EventChannel;
327
                  last_wp_event = FC_WP_EventChannel;
326
                  enable_injecting = 0;
328
                  enable_injecting = 0;
327
                 }
329
                 }
328
                else
330
                else
329
                 {
331
                 {
330
                  ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
332
                  ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
331
                // restart command cycle at the end
333
                // restart command cycle at the end
332
                  if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
334
                  if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
333
                  if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
335
                  if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
334
                 }
336
                 }
335
 
337
 
336
#define FLAG_GPS_AID 0x01
338
#define FLAG_GPS_AID 0x01
337
                switch (ToFlightCtrl.Command)
339
                switch (ToFlightCtrl.Command)
338
                {
340
                {
339
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
341
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
340
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
342
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
341
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
343
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
342
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
344
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
343
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
345
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
344
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
346
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
345
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
347
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
346
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
348
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
347
                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
349
                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
348
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
350
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
349
                                {
351
                                {
350
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
352
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
351
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
353
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
352
                                }
354
                                }
353
                                else
355
                                else
354
                                {
356
                                {
355
                                        ToFlightCtrl.Param.sInt[4] = -1;
357
                                        ToFlightCtrl.Param.sInt[4] = -1;
356
                                }
358
                                }
357
 
359
 
358
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
360
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
359
                                {
361
                                {
360
                                        POI_KameraNick = tmp;
362
                                        POI_KameraNick = tmp;
361
                                }
363
                                }
362
                                else
364
                                else
363
                                {
365
                                {
364
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
366
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
365
                                        POI_KameraNick = CAM_Orientation.Elevation;
367
                                        POI_KameraNick = CAM_Orientation.Elevation;
366
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
368
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
367
                                }
369
                                }
368
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
370
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
369
                                break;
371
                                break;
370
 
372
 
371
                        case SPI_NCCMD_VERSION:
373
                        case SPI_NCCMD_VERSION:
372
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
374
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
373
                        //+ higher than the maximum allowed altitude
375
                        //+ higher than the maximum allowed altitude
374
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
376
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
375
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
377
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
376
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
378
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
377
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
379
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
378
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
380
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
379
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
381
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
380
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
382
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
381
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
383
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
382
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
384
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
383
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
385
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
384
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
386
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
385
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
387
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
386
                                if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
388
                                if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
387
                                else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
389
                                else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
388
                                break;
390
                                break;
389
                        case SPI_MISC:
391
                        case SPI_MISC:
390
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
392
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
391
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
393
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
392
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
394
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
393
                                ToFlightCtrl.Param.Byte[3] = SpeakHoTT;
395
                                ToFlightCtrl.Param.Byte[3] = SpeakHoTT;
394
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
396
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
395
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
397
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
396
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
398
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
397
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
399
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
398
                                ToFlightCtrl.Param.Byte[9] = 0;
400
                                ToFlightCtrl.Param.Byte[9] = 0;
399
                                ToFlightCtrl.Param.Byte[10] = 0;
401
                                ToFlightCtrl.Param.Byte[10] = 0;
400
                                ToFlightCtrl.Param.Byte[11] = 0;
402
                                ToFlightCtrl.Param.Byte[11] = 0;
401
                                SpeakHoTT = 0;
403
                                SpeakHoTT = 0;
402
                                break;
404
                                break;
403
 
405
 
404
                        case SPI_NCCMD_GPSINFO:
406
                        case SPI_NCCMD_GPSINFO:
405
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
407
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
406
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
408
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
407
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
409
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
408
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
410
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
409
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
411
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
410
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
412
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
411
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
413
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
412
                                if(FC_WP_EventChannel) LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
414
                                if(FC_WP_EventChannel) LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
413
  // ++++++++++++++++++++++++++++++++++
415
  // ++++++++++++++++++++++++++++++++++
414
  // Waypoint event +++++++++++++++++++
416
  // Waypoint event +++++++++++++++++++
415
  // ++++++++++++++++++++++++++++++++++
417
  // ++++++++++++++++++++++++++++++++++
416
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
418
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
417
                                {
419
                                {
418
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
420
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
419
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
421
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
420
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
422
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
421
                                }
423
                                }
422
                                else
424
                                else
423
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
425
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
424
                                {
426
                                {
425
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
427
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
426
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
428
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
427
                                }
429
                                }
428
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
430
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
429
                                FC_WP_EventChannel = 0; // the GPS-Routine will set it again
431
                                FC_WP_EventChannel = 0; // the GPS-Routine will set it again
430
  // ++++++++++++++++++++++++++++++++++
432
  // ++++++++++++++++++++++++++++++++++
431
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
433
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
432
                                {
434
                                {
433
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
435
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
434
                                }
436
                                }
435
                                else
437
                                else
436
                                {
438
                                {
437
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
439
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
438
                                }
440
                                }
439
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
441
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
440
                                {
442
                                {
441
                                        ToFlightCtrl.Param.sInt[5] = tmp;
443
                                        ToFlightCtrl.Param.sInt[5] = tmp;
442
                                }
444
                                }
443
                                else
445
                                else
444
                                {
446
                                {
445
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
447
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
446
                                }
448
                                }
447
                                break;
449
                                break;
448
                        case SPI_NCCMD_HOTT_INFO:
450
                        case SPI_NCCMD_HOTT_INFO:
449
                                switch(hott_index++)
451
                                switch(hott_index++)
450
                                {
452
                                {
451
                                case 0:
453
                                case 0:
452
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
454
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
453
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
455
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
454
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
456
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
455
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
457
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
456
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
458
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
457
                                        //-----------------------------
459
                                        //-----------------------------
458
                                        ToFlightCtrl.Param.Byte[2] = FromFlightCtrl.GyroHeading / 20;//NaviData.HomePositionDeviation.Bearing / 2; 
460
                                        ToFlightCtrl.Param.Byte[2] = GyroCompassCorrected / 20;//NaviData.HomePositionDeviation.Bearing / 2; 
459
                                        i1 = GPSData.Speed_Ground; // in cm/sec
461
                                        i1 = GPSData.Speed_Ground; // in cm/sec
460
                                        i1 *= 36;
462
                                        i1 *= 36;
461
                                        i1 /= 1000;
463
                                        i1 /= 1000;
462
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
464
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
463
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
465
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
464
                                        //-----------------------------
466
                                        //-----------------------------
465
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
467
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
466
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
468
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
467
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
469
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
468
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
470
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
469
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
471
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
470
                                        i1 *= 100;
472
                                        i1 *= 100;
471
                                // Minuten
473
                                // Minuten
472
                                i2 *= 6;
474
                                i2 *= 6;
473
                                i2 /= 10;
475
                                i2 /= 10;
474
                                        i1 += i2 / 100000;
476
                                        i1 += i2 / 100000;
475
                                        i2  = i2 % 100000;
477
                                        i2  = i2 % 100000;
476
                                        i2 /= 10;
478
                                        i2 /= 10;
477
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
479
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
478
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
480
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
479
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
481
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
480
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
482
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
481
                                        break;
483
                                        break;
482
                                case 1:
484
                                case 1:
483
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
485
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
484
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
486
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
485
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
487
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
486
                                        //-----------------------------
488
                                        //-----------------------------
487
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
489
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
488
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
490
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
489
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
491
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
490
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
492
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
491
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
493
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
492
                                        i1 *= 100;
494
                                        i1 *= 100;
493
                                // Minuten
495
                                // Minuten
494
                                i2 *= 6;
496
                                i2 *= 6;
495
                                i2 /= 10;
497
                                i2 /= 10;
496
                                        i1 += i2 / 100000;
498
                                        i1 += i2 / 100000;
497
                                        i2  = i2 % 100000;
499
                                        i2  = i2 % 100000;
498
                                        i2 /= 10;
500
                                        i2 /= 10;
499
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
501
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
500
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
502
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
501
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
503
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
502
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
504
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
503
                                        //-----------------------------
505
                                        //-----------------------------
504
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
506
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
505
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
507
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
506
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
508
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
507
                                        break;
509
                                        break;
508
                                 case 2:
510
                                 case 2:
509
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
511
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
510
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
512
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
511
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
513
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
512
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
514
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
513
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
515
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
514
                                        break;
516
                                        break;
515
                                 case 3:
517
                                 case 3:
516
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
518
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
517
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
519
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
518
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
520
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
519
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
521
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
520
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
522
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
521
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
523
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
522
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
524
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
523
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
525
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
524
                                        i2 *= 6;
526
                                        i2 *= 6;
525
                                        i2 /= 1000;
527
                                        i2 /= 1000;
526
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
528
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
527
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
529
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
528
                                        break;
530
                                        break;
529
                                 case 4:
531
                                 case 4:
530
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
532
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
531
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
533
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
532
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
534
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
533
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
535
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
534
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
536
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
535
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
537
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
536
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
538
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
537
                                        i2 *= 6;
539
                                        i2 *= 6;
538
                                        i2 /= 1000;
540
                                        i2 /= 1000;
539
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
541
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
540
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
542
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
541
                        hott_index = 0;
543
                        hott_index = 0;
542
                                        break;
544
                                        break;
543
                                 default:
545
                                 default:
544
                                        ToFlightCtrl.Param.Byte[0] = 255;
546
                                        ToFlightCtrl.Param.Byte[0] = 255;
545
                                        hott_index = 0;
547
                                        hott_index = 0;
546
                                        break;
548
                                        break;
547
                                }
549
                                }
548
                                break;
550
                                break;
549
                        default:
551
                        default:
550
                                break;
552
                                break;
551
// 0 = 0,1
553
// 0 = 0,1
552
// 1 = 2,3
554
// 1 = 2,3
553
// 2 = 4,5
555
// 2 = 4,5
554
// 3 = 6,7
556
// 3 = 6,7
555
// 4 = 8,9
557
// 4 = 8,9
556
// 5 = 10,11
558
// 5 = 10,11
557
                }
559
                }
558
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
560
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
559
                switch(FromFlightCtrl.Command)
561
                switch(FromFlightCtrl.Command)
560
                {
562
                {
561
                        case SPI_FCCMD_USER:
563
                        case SPI_FCCMD_USER:
562
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
564
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
563
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
565
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
564
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
566
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
565
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
567
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
566
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
568
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
567
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
569
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
568
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
570
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
569
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
571
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
570
                                if(ClearFCStatusFlags)
572
                                if(ClearFCStatusFlags)
571
                                {
573
                                {
572
                                        FC.StatusFlags = 0;
574
                                        FC.StatusFlags = 0;
573
                                        ClearFCStatusFlags = 0;
575
                                        ClearFCStatusFlags = 0;
574
                                }
576
                                }
575
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
577
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
576
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
578
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
577
                                {
579
                                {
578
                                        HeadFreeStartAngle = Compass_Heading * 10;
580
                                        HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600;
579
                                        Compass_Init();
581
                                        Compass_Init();
580
                                        FCCalibActive = 10;
582
                                        FCCalibActive = 10;
581
                                        FC_is_Calibrated = 0;
583
                                        FC_is_Calibrated = 0;
582
                                }
584
                                }
583
                                else
585
                                else
584
                                {
586
                                {
585
                                        if(FCCalibActive) if(--FCCalibActive == 0) FC_is_Calibrated = 1;
587
                                        if(FCCalibActive) if(--FCCalibActive == 0) FC_is_Calibrated = 1;
586
                                }
588
                                }
587
                                if(FC.StatusFlags & FC_STATUS_START)
589
                                if(FC.StatusFlags & FC_STATUS_START)
588
                             {
590
                             {
589
                                   if(Compass_Heading != -1) HeadFreeStartAngle = Compass_Heading * 10; else
591
                                   if(Compass_Heading != -1) HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600; else
590
                                   HeadFreeStartAngle = FromFlightCtrl.GyroHeading;
592
                                   HeadFreeStartAngle = GyroCompassCorrected;
591
                                 }
593
                                 }
592
 
594
 
593
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
595
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
594
                                  {
596
                                  {
595
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
597
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
596
                                    {
598
                                    {
597
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
599
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
598
                                         {
600
                                         {
599
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
601
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
600
                                         }
602
                                         }
601
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
603
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
602
                                          HeadFreeStartAngle = (3600 + FromFlightCtrl.GyroHeading /*+ Parameter.OrientationAngle * 150*/) % 3600; // in 0.1°      
604
                                          HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°   
603
                                        }
605
                                        }
604
                                }
606
                                }
605
 
607
 
606
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
608
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
607
                                DebugOut.Analog[5] = FC.StatusFlags;
609
                                DebugOut.Analog[5] = FC.StatusFlags;
608
                                NaviData.FCStatusFlags = FC.StatusFlags;
610
                                NaviData.FCStatusFlags = FC.StatusFlags;
609
if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
611
if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
610
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
612
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
611
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
613
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
612
 
614
 
613
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
615
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
614
                                else
616
                                else
615
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
617
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
616
 
618
 
617
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
619
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
618
                                else
620
                                else
619
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
621
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
620
 
622
 
621
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
623
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
622
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
624
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
623
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[10];
625
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[10];
624
                                break;
626
                                break;
625
 
627
 
626
                        case SPI_FCCMD_BL_ACCU:
628
                        case SPI_FCCMD_BL_ACCU:
627
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
629
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
628
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
630
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
629
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
631
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
630
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[5];
632
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[5];
631
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[6];
633
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[6];
632
                                Motor[FromFlightCtrl.Param.Byte[7]].MaxPWM = FromFlightCtrl.Param.Byte[8];
634
                                Motor[FromFlightCtrl.Param.Byte[7]].MaxPWM = FromFlightCtrl.Param.Byte[8];
633
                                Motor[FromFlightCtrl.Param.Byte[7]].State = FromFlightCtrl.Param.Byte[9];
635
                                Motor[FromFlightCtrl.Param.Byte[7]].State = FromFlightCtrl.Param.Byte[9];
634
                                Motor[FromFlightCtrl.Param.Byte[7]].Temperature = FromFlightCtrl.Param.Byte[10];
636
                                Motor[FromFlightCtrl.Param.Byte[7]].Temperature = FromFlightCtrl.Param.Byte[10];
635
                                Motor[FromFlightCtrl.Param.Byte[7]].Current = FromFlightCtrl.Param.Byte[11];
637
                                Motor[FromFlightCtrl.Param.Byte[7]].Current = FromFlightCtrl.Param.Byte[11];
636
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
638
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
637
                                 {
639
                                 {
638
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
640
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
639
                                 }
641
                                 }
640
                                NaviData.UBat = FC.BAT_Voltage;
642
                                NaviData.UBat = FC.BAT_Voltage;
641
                                NaviData.Current = FC.BAT_Current;
643
                                NaviData.Current = FC.BAT_Current;
642
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
644
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
643
                                break;
645
                                break;
644
                        case SPI_FCCMD_PARAMETER1:
646
                        case SPI_FCCMD_PARAMETER1:
645
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
647
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
646
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
648
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
647
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
649
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
648
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
650
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
649
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
651
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
650
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
652
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
651
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
653
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
652
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
654
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
653
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
655
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
654
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
656
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
655
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
657
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
656
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
658
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
657
                                break;
659
                                break;
658
                        case SPI_FCCMD_PARAMETER2:
660
                        case SPI_FCCMD_PARAMETER2:
659
                                CHK_POTI_MM(Parameter.NaviOut1Parameter,FromFlightCtrl.Param.Byte[0],0,255);
661
                                CHK_POTI_MM(Parameter.NaviOut1Parameter,FromFlightCtrl.Param.Byte[0],0,255);
660
                                if(FromFlightCtrl.Param.Byte[1]) FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];     // will be cleared in the SD-Logging
662
                                if(FromFlightCtrl.Param.Byte[1]) FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];     // will be cleared in the SD-Logging
661
                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[2];
663
                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[2];
662
                                break;
664
                                break;
663
                        case SPI_FCCMD_STICK:
665
                        case SPI_FCCMD_STICK:
664
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
666
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
665
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
667
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
666
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
668
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
667
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
669
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
668
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
670
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
669
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
671
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
670
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
672
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
671
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
673
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
672
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
674
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
673
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
675
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
674
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
676
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
675
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
677
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
676
                            CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
678
                            CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
677
                                break;
679
                                break;
678
 
680
 
679
                        case SPI_FCCMD_MISC:
681
                        case SPI_FCCMD_MISC:
680
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
682
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
681
                                {       // put only new CompassCalState into queue to send via I2C
683
                                {       // put only new CompassCalState into queue to send via I2C
682
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0) 
684
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0) 
683
                                         {
685
                                         {
684
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
686
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
685
                                           Compass_SetCalState(CompassCalState);
687
                                           Compass_SetCalState(CompassCalState);
686
                                         }
688
                                         }
687
//                                       else CompassCalState = 0;
689
//                                       else CompassCalState = 0;
688
                                }
690
                                }
689
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
691
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
690
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
692
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
691
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
693
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
692
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
694
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
693
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
695
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
694
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
696
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
695
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
697
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
696
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
698
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
697
                                NaviData.RC_Quality = FC.RC_Quality;
699
                                NaviData.RC_Quality = FC.RC_Quality;
698
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
700
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
699
//                              FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
701
//                              FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
700
//                              if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
702
//                              if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
701
//                              NaviData.RC_RSSI = FC.RC_RSSI;
703
//                              NaviData.RC_RSSI = FC.RC_RSSI;
702
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
704
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
703
                                break;
705
                                break;
704
 
706
 
705
                        case SPI_FCCMD_SERVOS:
707
                        case SPI_FCCMD_SERVOS:
706
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
708
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
707
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
709
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
708
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
710
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
709
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
711
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
710
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
712
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
711
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
713
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
712
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
714
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
713
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
715
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
714
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
716
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
715
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
717
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
716
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[10];
718
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[10];
717
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[11];
719
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[11];
718
                                break;
720
                                break;
719
 
721
 
720
                        case SPI_FCCMD_VERSION:
722
                        case SPI_FCCMD_VERSION:
721
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
723
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
722
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
724
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
723
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
725
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
724
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
726
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
725
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
727
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
726
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
728
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
727
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
729
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
728
//      frei            FC.Error[2]                     |= FromFlightCtrl.Param.Byte[7];
730
//      frei            FC.Error[2]                     |= FromFlightCtrl.Param.Byte[7];
729
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
731
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
730
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
732
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
731
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
733
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
732
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
734
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
733
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
735
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
734
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
736
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
735
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
737
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
736
                                else DebugOut.StatusRed &= ~AMPEL_FC;
738
                                else DebugOut.StatusRed &= ~AMPEL_FC;
737
                                break;
739
                                break;
738
                        default:
740
                        default:
739
                                break;
741
                                break;
740
                }
742
                }
741
 
-
 
742
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
743
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
743
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
744
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
744
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
745
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
745
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
746
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
746
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
747
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
747
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
748
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
748
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
749
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
749
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
750
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
750
                // every time we got new data from the FC via SPI call the navigation routine
751
                // every time we got new data from the FC via SPI call the navigation routine
751
                // and update GPSStick that are returned to FC
752
                // and update GPSStick that are returned to FC
752
                SPI_RxBuffer_Request = 0;
753
                SPI_RxBuffer_Request = 0;
753
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
754
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
754
                ClearFCStatusFlags = 1;
755
                ClearFCStatusFlags = 1;
755
                if(counter)
756
                if(counter)
756
                {
757
                {
757
                        counter--;                                       // count down to enable servo
758
                        counter--;                                       // count down to enable servo
758
                        if(!counter) TIMER2_Init();  // enable Servo Output
759
                        if(!counter) TIMER2_Init();  // enable Servo Output
759
                }
760
                }
760
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
761
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
761
 
762
 
762
        }       // EOF if(SPI_RxBuffer_Request)
763
        }       // EOF if(SPI_RxBuffer_Request)
763
        else // no new SPI data
764
        else // no new SPI data
764
        {
765
        {
765
                if(CheckDelay(timeout) && (counter == 0))
766
                if(CheckDelay(timeout) && (counter == 0))
766
                {
767
                {
767
                        TIMER2_Deinit();  // disable Servo Output
768
                        TIMER2_Deinit();  // disable Servo Output
768
                        counter = 50;     // reset counter for enabling Servo Output
769
                        counter = 50;     // reset counter for enabling Servo Output
769
                }
770
                }
770
        }
771
        }
771
}
772
}
772
 
773
 
773
//------------------------------------------------------
774
//------------------------------------------------------
774
void SPI0_GetFlightCtrlVersion(void)
775
void SPI0_GetFlightCtrlVersion(void)
775
{
776
{
776
        u32 timeout;
777
        u32 timeout;
777
        u8 repeat;
778
        u8 repeat;
778
        u8 msg[64];
779
        u8 msg[64];
779
 
780
 
780
        UART1_PutString("\r\n Looking for FlightControl");
781
        UART1_PutString("\r\n Looking for FlightControl");
781
        FC_Version.Major = 0xFF;
782
        FC_Version.Major = 0xFF;
782
        FC_Version.Minor = 0xFF;
783
        FC_Version.Minor = 0xFF;
783
        FC_Version.Patch = 0xFF;
784
        FC_Version.Patch = 0xFF;
784
        FC_Version.Compatible = 0xFF;
785
        FC_Version.Compatible = 0xFF;
785
 
786
 
786
        // polling FC version info
787
        // polling FC version info
787
        repeat = 0;
788
        repeat = 0;
788
        do
789
        do
789
        {
790
        {
790
                timeout = SetDelay(250);
791
                timeout = SetDelay(250);
791
                do
792
                do
792
                {
793
                {
793
                        SPI0_UpdateBuffer();
794
                        SPI0_UpdateBuffer();
794
                        if (FC_Version.Major != 0xFF)  break;
795
                        if (FC_Version.Major != 0xFF)  break;
795
                }while (!CheckDelay(timeout));
796
                }while (!CheckDelay(timeout));
796
                UART1_PutString(".");
797
                UART1_PutString(".");
797
                repeat++;
798
                repeat++;
798
                FCCalibActive = 1;
799
                FCCalibActive = 1;
799
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
800
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
800
        // if we got it
801
        // if we got it
801
        if (FC_Version.Major != 0xFF)
802
        if (FC_Version.Major != 0xFF)
802
        {
803
        {
803
                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);
804
                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);
804
                UART1_PutString(msg);
805
                UART1_PutString(msg);
805
        }
806
        }
806
        else UART1_PutString("\n\r not found!");
807
        else UART1_PutString("\n\r not found!");
807
}
808
}
808
 
809
 
809
 
810
 
810
 
811