Subversion Repositories NaviCtrl

Rev

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

Rev 486 Rev 487
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
#include "ncmag.h"
73
#include "ncmag.h"
74
 
74
 
75
#define SPI_RXSYNCBYTE1 0xAA
75
#define SPI_RXSYNCBYTE1 0xAA
76
#define SPI_RXSYNCBYTE2 0x83
76
#define SPI_RXSYNCBYTE2 0x83
77
#define SPI_TXSYNCBYTE1 0x81
77
#define SPI_TXSYNCBYTE1 0x81
78
#define SPI_TXSYNCBYTE2 0x55
78
#define SPI_TXSYNCBYTE2 0x55
79
 
79
 
80
//communication packets
80
//communication packets
81
FromFlightCtrl_t   FromFlightCtrl;
81
FromFlightCtrl_t   FromFlightCtrl;
82
ToFlightCtrl_t     ToFlightCtrl;
82
ToFlightCtrl_t     ToFlightCtrl;
83
#define SPI0_TIMEOUT     2500  //ms
83
#define SPI0_TIMEOUT     2500  //ms
84
volatile u32 SPI0_Timeout = 0;
84
volatile u32 SPI0_Timeout = 0;
85
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
85
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
86
u8 SpeakHoTT = 0;
86
u8 SpeakHoTT = 0;
87
 
87
 
88
// tx packet buffer
88
// tx packet buffer
89
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
89
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
90
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN + 10];
90
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN + 10];
91
volatile u8 SPI_TxBufferIndex = 0;
91
volatile u8 SPI_TxBufferIndex = 0;
92
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
92
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
93
 
93
 
94
// rx packet buffer
94
// rx packet buffer
95
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
95
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
96
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN+10];
96
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN+10];
97
volatile u8 SPI_RxBufferIndex = 0;
97
volatile u8 SPI_RxBufferIndex = 0;
98
volatile u8 SPI_RxBuffer_Request = 0;
98
volatile u8 SPI_RxBuffer_Request = 0;
99
#define SPI_COMMAND_INDEX 0
99
#define SPI_COMMAND_INDEX 0
100
 
100
 
101
s32 Kalman_K = 32;
101
s32 Kalman_K = 32;
102
s32 Kalman_MaxDrift = 5 * 16;
102
s32 Kalman_MaxDrift = 5 * 16;
103
s32 Kalman_MaxFusion = 64;
103
s32 Kalman_MaxFusion = 64;
104
s32 Kalman_Kompass = 32;
104
s32 Kalman_Kompass = 32;
105
s32 ToFcGpsZ = 0;
105
s32 ToFcGpsZ = 0;
106
u8 CompassCalState = 0;
106
u8 CompassCalState = 0;
107
 
107
 
108
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_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 };
109
u8 SPI_CommandCounter = 0;
109
u8 SPI_CommandCounter = 0;
110
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
110
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
111
s32 HeadFreeStartAngle = 0;
111
s32 HeadFreeStartAngle = 0;
112
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
112
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
113
u32 ToFC_AltitudeRate = 0;
113
u32 ToFC_AltitudeRate = 0;
114
s32 ToFC_AltitudeSetpoint = 0;
114
s32 ToFC_AltitudeSetpoint = 0;
115
u8  FromFC_VarioCharacter = ' ';
115
u8  FromFC_VarioCharacter = ' ';
116
u8 GPS_Aid_StickMultiplikator = 0;
116
u8 GPS_Aid_StickMultiplikator = 0;
117
u8 NC_GPS_ModeCharacter = ' ';
117
u8 NC_GPS_ModeCharacter = ' ';
118
u8 FCCalibActive = 0;
118
u8 FCCalibActive = 0;
119
u8 FC_is_Calibrated = 0;
119
u8 FC_is_Calibrated = 0;
120
Motor_t Motor[12];
120
Motor_t Motor[12];
121
u8 Motor_Version[12]; // das kann nicht in die struct, weil der PC die Struktur bekommt
121
u8 Motor_Version[12]; // das kann nicht in die struct, weil der PC die Struktur bekommt
122
u8 NC_To_FC_Flags = 0;
122
u8 NC_To_FC_Flags = 0;
123
u8 BL_MinOfMaxPWM;  // indication if all BL-controllers run on full power
123
u8 BL_MinOfMaxPWM;  // indication if all BL-controllers run on full power
124
u32 FC_I2C_ErrorConter;
124
u32 FC_I2C_ErrorConter;
125
SPI_Version_t FC_Version;
125
SPI_Version_t FC_Version;
126
s16 POI_KameraNick = 0;
126
s16 POI_KameraNick = 0;
127
u8 NC_Wait_for_LED = 0;
127
u8 NC_Wait_for_LED = 0;
128
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
128
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
-
 
129
u8 FromFC_LowVoltageHomeActive = 0;
129
 
130
 
130
//--------------------------------------------------------------
131
//--------------------------------------------------------------
131
void SSP0_IRQHandler(void)
132
void SSP0_IRQHandler(void)
132
{
133
{
133
        static u8 rxchksum = 0;
134
        static u8 rxchksum = 0;
134
        u8 rxdata;
135
        u8 rxdata;
135
 
136
 
136
        #define SPI_SYNC1       0
137
        #define SPI_SYNC1       0
137
        #define SPI_SYNC2       1
138
        #define SPI_SYNC2       1
138
        #define SPI_DATA        2
139
        #define SPI_DATA        2
139
        static u8 SPI_State = SPI_SYNC1;
140
        static u8 SPI_State = SPI_SYNC1;
140
        //IENABLE;
141
        //IENABLE;
141
 
142
 
142
        // clear pending bits
143
        // clear pending bits
143
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
144
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
144
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
145
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
145
 
146
 
146
        // while RxFIFO not empty
147
        // while RxFIFO not empty
147
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
148
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
148
        {
149
        {
149
                rxdata =  SSP0->DR; // catch the received byte
150
                rxdata =  SSP0->DR; // catch the received byte
150
                // Fill TxFIFO while its not full or end of packet is reached
151
                // Fill TxFIFO while its not full or end of packet is reached
151
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
152
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
152
                {
153
                {
153
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
154
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
154
                        {
155
                        {
155
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
156
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
156
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
157
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
157
                                if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
158
                                if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
158
                                SPI_TxBufferIndex++; // pointer to next byte
159
                                SPI_TxBufferIndex++; // pointer to next byte
159
                        }
160
                        }
160
                        else // end of packet is reached reset and copy data to tx buffer
161
                        else // end of packet is reached reset and copy data to tx buffer
161
                        {
162
                        {
162
                                SPI_TxBufferIndex = 0;  // reset buffer index
163
                                SPI_TxBufferIndex = 0;  // reset buffer index
163
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
164
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
164
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
165
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
165
                                BeepTime = 0; // reset local beeptime
166
                                BeepTime = 0; // reset local beeptime
166
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
167
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
167
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
168
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
168
                        }
169
                        }
169
                }
170
                }
170
                switch (SPI_State)
171
                switch (SPI_State)
171
                {
172
                {
172
                        case SPI_SYNC1:
173
                        case SPI_SYNC1:
173
                                SPI_RxBufferIndex = 0; // reset buffer index
174
                                SPI_RxBufferIndex = 0; // reset buffer index
174
                                rxchksum = rxdata;     // init checksum
175
                                rxchksum = rxdata;     // init checksum
175
                                if (rxdata == SPI_RXSYNCBYTE1)
176
                                if (rxdata == SPI_RXSYNCBYTE1)
176
                                {   // 1st syncbyte ok
177
                                {   // 1st syncbyte ok
177
                                        SPI_State = SPI_SYNC2;  // step to sync2
178
                                        SPI_State = SPI_SYNC2;  // step to sync2
178
                                }
179
                                }
179
                                break;
180
                                break;
180
                        case SPI_SYNC2:
181
                        case SPI_SYNC2:
181
                                if (rxdata == SPI_RXSYNCBYTE2)
182
                                if (rxdata == SPI_RXSYNCBYTE2)
182
                                {  // 2nd Syncbyte ok
183
                                {  // 2nd Syncbyte ok
183
                                        rxchksum += rxdata;
184
                                        rxchksum += rxdata;
184
                                        SPI_State = SPI_DATA;
185
                                        SPI_State = SPI_DATA;
185
                                }  // 2nd Syncbyte does not match
186
                                }  // 2nd Syncbyte does not match
186
                                else
187
                                else
187
                                {
188
                                {
188
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
189
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
189
                                }
190
                                }
190
                                break;
191
                                break;
191
                        case SPI_DATA:
192
                        case SPI_DATA:
192
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
193
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
193
                                if(SPI_RxBufferIndex >= SPI_RXBUFFER_LEN)  // end of packet is reached
194
                                if(SPI_RxBufferIndex >= SPI_RXBUFFER_LEN)  // end of packet is reached
194
                                {
195
                                {
195
                                        if (rxdata == rxchksum) // verify checksum byte
196
                                        if (rxdata == rxchksum) // verify checksum byte
196
                                        {
197
                                        {
197
                                                // copy SPI_RxBuffer -> FromFlightCtrl
198
                                                // copy SPI_RxBuffer -> FromFlightCtrl
198
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
199
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
199
                                                {
200
                                                {
200
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
201
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
201
                                                        SPI_RxBuffer_Request = 1;
202
                                                        SPI_RxBuffer_Request = 1;
202
                                                }
203
                                                }
203
                                                // reset timeout counter on good packet
204
                                                // reset timeout counter on good packet
204
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
205
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
205
                                                DebugOut.Analog[13]++;
206
                                                DebugOut.Analog[13]++;
206
                                        }
207
                                        }
207
                                        else // bad checksum byte
208
                                        else // bad checksum byte
208
                                        {
209
                                        {
209
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
210
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
210
                                        }
211
                                        }
211
                                        SPI_State  = SPI_SYNC1; // reset state
212
                                        SPI_State  = SPI_SYNC1; // reset state
212
                                }
213
                                }
213
                                else // end of packet not reached
214
                                else // end of packet not reached
214
                                {
215
                                {
215
                                        rxchksum += rxdata;      // update checksum
216
                                        rxchksum += rxdata;      // update checksum
216
                                }
217
                                }
217
                                break;
218
                                break;
218
                        default:
219
                        default:
219
                                SPI_State  = SPI_SYNC1;
220
                                SPI_State  = SPI_SYNC1;
220
                                break;
221
                                break;
221
                }
222
                }
222
        }
223
        }
223
 
224
 
224
//      IDISABLE;
225
//      IDISABLE;
225
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
226
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
226
}
227
}
227
 
228
 
228
//--------------------------------------------------------------
229
//--------------------------------------------------------------
229
void SPI0_Init(void)
230
void SPI0_Init(void)
230
{
231
{
231
        GPIO_InitTypeDef GPIO_InitStructure;
232
        GPIO_InitTypeDef GPIO_InitStructure;
232
        SSP_InitTypeDef   SSP_InitStructure;
233
        SSP_InitTypeDef   SSP_InitStructure;
233
 
234
 
234
        UART1_PutString("\r\n SPI init...");
235
        UART1_PutString("\r\n SPI init...");
235
 
236
 
236
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
237
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
237
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
238
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
238
 
239
 
239
        GPIO_DeInit(GPIO2);
240
        GPIO_DeInit(GPIO2);
240
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
241
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
241
        GPIO_StructInit(&GPIO_InitStructure);
242
        GPIO_StructInit(&GPIO_InitStructure);
242
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
243
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
243
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
244
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
244
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
245
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
245
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
246
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
246
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
247
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
247
        GPIO_Init (GPIO2, &GPIO_InitStructure);
248
        GPIO_Init (GPIO2, &GPIO_InitStructure);
248
 
249
 
249
        // SSP0_MISO pin GPIO2.6
250
        // SSP0_MISO pin GPIO2.6
250
        GPIO_StructInit(&GPIO_InitStructure);
251
        GPIO_StructInit(&GPIO_InitStructure);
251
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
252
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
252
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
253
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
253
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
254
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
254
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
255
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
255
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
256
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
256
        GPIO_Init (GPIO2, &GPIO_InitStructure);
257
        GPIO_Init (GPIO2, &GPIO_InitStructure);
257
 
258
 
258
        SSP_DeInit(SSP0);
259
        SSP_DeInit(SSP0);
259
        SSP_StructInit(&SSP_InitStructure);
260
        SSP_StructInit(&SSP_InitStructure);
260
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
261
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
261
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
262
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
262
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
263
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
263
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
264
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
264
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
265
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
265
        SSP_InitStructure.SSP_ClockRate = 0;
266
        SSP_InitStructure.SSP_ClockRate = 0;
266
 
267
 
267
        SSP_Init(SSP0, &SSP_InitStructure);
268
        SSP_Init(SSP0, &SSP_InitStructure);
268
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
269
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
269
        SSP_Cmd(SSP0, ENABLE);
270
        SSP_Cmd(SSP0, ENABLE);
270
        // initialize the syncbytes in the tx buffer
271
        // initialize the syncbytes in the tx buffer
271
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
272
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
272
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
273
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
273
        // set the pointer to the checksum byte in the tx buffer
274
        // set the pointer to the checksum byte in the tx buffer
274
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
275
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
275
 
276
 
276
        ToFlightCtrl.GPSStick.Nick = 0;
277
        ToFlightCtrl.GPSStick.Nick = 0;
277
        ToFlightCtrl.GPSStick.Roll = 0;
278
        ToFlightCtrl.GPSStick.Roll = 0;
278
//      ToFlightCtrl.GPSStick.Yaw = 0;
279
//      ToFlightCtrl.GPSStick.Yaw = 0;
279
 
280
 
280
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
281
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
281
        VIC_ITCmd(SSP0_ITLine, ENABLE);
282
        VIC_ITCmd(SSP0_ITLine, ENABLE);
282
 
283
 
283
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
284
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
284
 
285
 
285
        UART1_PutString("ok");
286
        UART1_PutString("ok");
286
}
287
}
287
 
288
 
288
 
289
 
289
//------------------------------------------------------
290
//------------------------------------------------------
290
void SPI0_UpdateBuffer(void)
291
void SPI0_UpdateBuffer(void)
291
{
292
{
292
        static u32 timeout = 0;
293
        static u32 timeout = 0;
293
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
294
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
294
        static s16 last_wp_event = 0;
295
        static s16 last_wp_event = 0;
295
        s16 tmp;
296
        s16 tmp;
296
        s32 i1,i2;
297
        s32 i1,i2;
297
/*
298
/*
298
        union
299
        union
299
        {
300
        {
300
                unsigned char Byte[4];
301
                unsigned char Byte[4];
301
                unsigned int Int[2];
302
                unsigned int Int[2];
302
                unsigned long Long;
303
                unsigned long Long;
303
        } Temp;
304
        } Temp;
304
*/     
305
*/     
305
    SPIWatchDog = 3500;    // stop communication to FC after this timeout
306
    SPIWatchDog = 3500;    // stop communication to FC after this timeout
306
        if(SPI_RxBuffer_Request)
307
        if(SPI_RxBuffer_Request)
307
        {
308
        {
308
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
309
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
309
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
310
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
310
                ToFlightCtrl.CompassHeading = Compass_Heading;
311
                ToFlightCtrl.CompassHeading = Compass_Heading;
311
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
312
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
312
GyroCompassCorrected = (3600 + FromFlightCtrl.GyroHeading + FC.FromFC_CompassOffset - GeoMagDec) % 3600;
313
GyroCompassCorrected = (3600 + FromFlightCtrl.GyroHeading + FC.FromFC_CompassOffset - GeoMagDec) % 3600;
313
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
314
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
314
                ToFlightCtrl.MagVecX = MagVector.X;
315
                ToFlightCtrl.MagVecX = MagVector.X;
315
                ToFlightCtrl.MagVecY = MagVector.Y;
316
                ToFlightCtrl.MagVecY = MagVector.Y;
316
                ToFlightCtrl.MagVecZ = MagVector.Z;
317
                ToFlightCtrl.MagVecZ = MagVector.Z;
317
//              ToFlightCtrl.NCStatus = 0;
318
//              ToFlightCtrl.NCStatus = 0;
318
                // cycle spi commands
319
                // cycle spi commands
319
                if(ErrorCode != last_error_code && enable_injecting)
320
                if(ErrorCode != last_error_code && enable_injecting)
320
                 {
321
                 {
321
                  ToFlightCtrl.Command = SPI_NCCMD_VERSION;
322
                  ToFlightCtrl.Command = SPI_NCCMD_VERSION;
322
                  last_error_code = ErrorCode;
323
                  last_error_code = ErrorCode;
323
                  enable_injecting = 0;
324
                  enable_injecting = 0;
324
                 }
325
                 }
325
                else
326
                else
326
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
327
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
327
                 {
328
                 {
328
                  ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
329
                  ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
329
                  last_wp_event = FC_WP_EventChannel;
330
                  last_wp_event = FC_WP_EventChannel;
330
                  enable_injecting = 0;
331
                  enable_injecting = 0;
331
                 }
332
                 }
332
                else
333
                else
333
                 {
334
                 {
334
                  ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
335
                  ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
335
                // restart command cycle at the end
336
                // restart command cycle at the end
336
                  if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
337
                  if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
337
                  if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
338
                  if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
338
                 }
339
                 }
339
 
340
 
340
#define FLAG_GPS_AID 0x01
341
#define FLAG_GPS_AID 0x01
341
                switch (ToFlightCtrl.Command)
342
                switch (ToFlightCtrl.Command)
342
                {
343
                {
343
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
344
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
344
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
345
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
345
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
346
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
346
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
347
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
347
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
348
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
348
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
349
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
349
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
350
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
350
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
351
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
351
                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
352
                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
352
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
353
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
353
                                {
354
                                {
354
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
355
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
355
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
356
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
356
                                }
357
                                }
357
                                else
358
                                else
358
                                {
359
                                {
359
                                        ToFlightCtrl.Param.sInt[4] = -1;
360
                                        ToFlightCtrl.Param.sInt[4] = -1;
360
                                }
361
                                }
361
 
362
 
362
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
363
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
363
                                {
364
                                {
364
                                        POI_KameraNick = tmp;
365
                                        POI_KameraNick = tmp;
365
                                }
366
                                }
366
                                else
367
                                else
367
                                {
368
                                {
368
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
369
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
369
                                        POI_KameraNick = CAM_Orientation.Elevation;
370
                                        POI_KameraNick = CAM_Orientation.Elevation;
370
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
371
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
371
                                }
372
                                }
372
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
373
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
373
                                break;
374
                                break;
374
 
375
 
375
                        case SPI_NCCMD_VERSION:
376
                        case SPI_NCCMD_VERSION:
376
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
377
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
377
                        //+ higher than the maximum allowed altitude
378
                        //+ higher than the maximum allowed altitude
378
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
379
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
379
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
380
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
380
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
381
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
381
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
382
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
382
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
383
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
383
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
384
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
384
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
385
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
385
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
386
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
386
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
387
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
387
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
388
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
388
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
389
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
389
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
390
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
390
                                if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
391
                                if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
391
                                else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
392
                                else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
392
                                break;
393
                                break;
393
                        case SPI_MISC:
394
                        case SPI_MISC:
394
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
395
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
395
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
396
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
396
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
397
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
397
                                ToFlightCtrl.Param.Byte[3] = SpeakHoTT;
398
                                ToFlightCtrl.Param.Byte[3] = SpeakHoTT;
398
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
399
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
399
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
400
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
400
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
401
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
401
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
402
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
402
                                ToFlightCtrl.Param.Byte[9] = 0;
403
                                ToFlightCtrl.Param.Byte[9] = 0;
403
                                ToFlightCtrl.Param.Byte[10] = 0;
404
                                ToFlightCtrl.Param.Byte[10] = 0;
404
                                ToFlightCtrl.Param.Byte[11] = 0;
405
                                ToFlightCtrl.Param.Byte[11] = 0;
405
                                SpeakHoTT = 0;
406
                                SpeakHoTT = 0;
406
                                break;
407
                                break;
407
 
408
 
408
                        case SPI_NCCMD_GPSINFO:
409
                        case SPI_NCCMD_GPSINFO:
409
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
410
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
410
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
411
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
411
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
412
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
412
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
413
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
413
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
414
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
414
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
415
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
415
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
416
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
416
                                if(FC_WP_EventChannel) LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
417
                                if(FC_WP_EventChannel) LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
417
  // ++++++++++++++++++++++++++++++++++
418
  // ++++++++++++++++++++++++++++++++++
418
  // Waypoint event +++++++++++++++++++
419
  // Waypoint event +++++++++++++++++++
419
  // ++++++++++++++++++++++++++++++++++
420
  // ++++++++++++++++++++++++++++++++++
420
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
421
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
421
                                {
422
                                {
422
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
423
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
423
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
424
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
424
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
425
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
425
                                }
426
                                }
426
                                else
427
                                else
427
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
428
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
428
                                {
429
                                {
429
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
430
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
430
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
431
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
431
                                }
432
                                }
432
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
433
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
433
                                FC_WP_EventChannel = 0; // the GPS-Routine will set it again
434
                                FC_WP_EventChannel = 0; // the GPS-Routine will set it again
434
  // ++++++++++++++++++++++++++++++++++
435
  // ++++++++++++++++++++++++++++++++++
435
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
436
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
436
                                {
437
                                {
437
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
438
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
438
                                }
439
                                }
439
                                else
440
                                else
440
                                {
441
                                {
441
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
442
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
442
                                }
443
                                }
443
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
444
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
444
                                {
445
                                {
445
                                        ToFlightCtrl.Param.sInt[5] = tmp;
446
                                        ToFlightCtrl.Param.sInt[5] = tmp;
446
                                }
447
                                }
447
                                else
448
                                else
448
                                {
449
                                {
449
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
450
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
450
                                }
451
                                }
451
                                break;
452
                                break;
452
                        case SPI_NCCMD_HOTT_INFO:
453
                        case SPI_NCCMD_HOTT_INFO:
453
                                switch(hott_index++)
454
                                switch(hott_index++)
454
                                {
455
                                {
455
                                case 0:
456
                                case 0:
456
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
457
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
457
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
458
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
458
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
459
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
459
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
460
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
460
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
461
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
461
                                        //-----------------------------
462
                                        //-----------------------------
462
                                        ToFlightCtrl.Param.Byte[2] = GyroCompassCorrected / 20;//NaviData.HomePositionDeviation.Bearing / 2; 
463
                                        ToFlightCtrl.Param.Byte[2] = GyroCompassCorrected / 20;//NaviData.HomePositionDeviation.Bearing / 2; 
463
                                        i1 = GPSData.Speed_Ground; // in cm/sec
464
                                        i1 = GPSData.Speed_Ground; // in cm/sec
464
                                        i1 *= 36;
465
                                        i1 *= 36;
465
                                        i1 /= 1000;
466
                                        i1 /= 1000;
466
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
467
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
467
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
468
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
468
                                        //-----------------------------
469
                                        //-----------------------------
469
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
470
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
470
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
471
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
471
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
472
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
472
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
473
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
473
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
474
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
474
                                        i1 *= 100;
475
                                        i1 *= 100;
475
                                // Minuten
476
                                // Minuten
476
                                i2 *= 6;
477
                                i2 *= 6;
477
                                i2 /= 10;
478
                                i2 /= 10;
478
                                        i1 += i2 / 100000;
479
                                        i1 += i2 / 100000;
479
                                        i2  = i2 % 100000;
480
                                        i2  = i2 % 100000;
480
                                        i2 /= 10;
481
                                        i2 /= 10;
481
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
482
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
482
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
483
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
483
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
484
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
484
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
485
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
485
                                        break;
486
                                        break;
486
                                case 1:
487
                                case 1:
487
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
488
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
488
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
489
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
489
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
490
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
490
                                        //-----------------------------
491
                                        //-----------------------------
491
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
492
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
492
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
493
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
493
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
494
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
494
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
495
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
495
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
496
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
496
                                        i1 *= 100;
497
                                        i1 *= 100;
497
                                // Minuten
498
                                // Minuten
498
                                i2 *= 6;
499
                                i2 *= 6;
499
                                i2 /= 10;
500
                                i2 /= 10;
500
                                        i1 += i2 / 100000;
501
                                        i1 += i2 / 100000;
501
                                        i2  = i2 % 100000;
502
                                        i2  = i2 % 100000;
502
                                        i2 /= 10;
503
                                        i2 /= 10;
503
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
504
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
504
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
505
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
505
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
506
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
506
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
507
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
507
                                        //-----------------------------
508
                                        //-----------------------------
508
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
509
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
509
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
510
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
510
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
511
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
511
                                        break;
512
                                        break;
512
                                 case 2:
513
                                 case 2:
513
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
514
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
514
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
515
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
515
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
516
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
516
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
517
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
517
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
518
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
518
                                        break;
519
                                        break;
519
                                 case 3:
520
                                 case 3:
520
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
521
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
521
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
522
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
522
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
523
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
523
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
524
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
524
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
525
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
525
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
526
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
526
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
527
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
527
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
528
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
528
                                        i2 *= 6;
529
                                        i2 *= 6;
529
                                        i2 /= 1000;
530
                                        i2 /= 1000;
530
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
531
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
531
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
532
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
532
                                        break;
533
                                        break;
533
                                 case 4:
534
                                 case 4:
534
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
535
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
535
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
536
                                        ToFlightCtrl.Param.Byte[0] = 0; // index   
536
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
537
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
537
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
538
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
538
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
539
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
539
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
540
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
540
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
541
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
541
                                        i2 *= 6;
542
                                        i2 *= 6;
542
                                        i2 /= 1000;
543
                                        i2 /= 1000;
543
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
544
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
544
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
545
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
545
                        hott_index = 0;
546
                        hott_index = 0;
546
                                        break;
547
                                        break;
547
                                 default:
548
                                 default:
548
                                        ToFlightCtrl.Param.Byte[0] = 255;
549
                                        ToFlightCtrl.Param.Byte[0] = 255;
549
                                        hott_index = 0;
550
                                        hott_index = 0;
550
                                        break;
551
                                        break;
551
                                }
552
                                }
552
                                break;
553
                                break;
553
                        default:
554
                        default:
554
                                break;
555
                                break;
555
// 0 = 0,1
556
// 0 = 0,1
556
// 1 = 2,3
557
// 1 = 2,3
557
// 2 = 4,5
558
// 2 = 4,5
558
// 3 = 6,7
559
// 3 = 6,7
559
// 4 = 8,9
560
// 4 = 8,9
560
// 5 = 10,11
561
// 5 = 10,11
561
                }
562
                }
562
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
563
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
563
                switch(FromFlightCtrl.Command)
564
                switch(FromFlightCtrl.Command)
564
                {
565
                {
565
                        case SPI_FCCMD_USER:
566
                        case SPI_FCCMD_USER:
566
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
567
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
567
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
568
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
568
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
569
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
569
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
570
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
570
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
571
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
571
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
572
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
572
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
573
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
573
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
574
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
574
                                if(ClearFCStatusFlags)
575
                                if(ClearFCStatusFlags)
575
                                {
576
                                {
576
                                        FC.StatusFlags = 0;
577
                                        FC.StatusFlags = 0;
577
                                        ClearFCStatusFlags = 0;
578
                                        ClearFCStatusFlags = 0;
578
                                }
579
                                }
579
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
580
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
580
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
581
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
581
                                {
582
                                {
582
                                        HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600;
583
                                        HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600;
583
                                        Compass_Init();
584
                                        Compass_Init();
584
                                        FCCalibActive = 10;
585
                                        FCCalibActive = 10;
585
                                        FC_is_Calibrated = 0;
586
                                        FC_is_Calibrated = 0;
586
                                }
587
                                }
587
                                else
588
                                else
588
                                {
589
                                {
589
                                        if(FCCalibActive)
590
                                        if(FCCalibActive)
590
                                         {
591
                                         {
591
                                            if(--FCCalibActive == 0)
592
                                            if(--FCCalibActive == 0)
592
                                                 {
593
                                                 {
593
                                                  FC_is_Calibrated = 1;
594
                                                  FC_is_Calibrated = 1;
594
                                                  ExtCompassOrientation = GetExtCompassOrientation();
595
                                                  ExtCompassOrientation = GetExtCompassOrientation();
595
if(ExtCompassOrientation != Calibration.Version / 16) NCMAG_IsCalibrated = 0;
596
if(ExtCompassOrientation != Calibration.Version / 16) NCMAG_IsCalibrated = 0;
596
                                                 }
597
                                                 }
597
                                         }
598
                                         }
598
                                }
599
                                }
599
                                if(FC.StatusFlags & FC_STATUS_START)
600
                                if(FC.StatusFlags & FC_STATUS_START)
600
                             {
601
                             {
601
                                   if(Compass_Heading != -1) HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600; else
602
                                   if(Compass_Heading != -1) HeadFreeStartAngle = (3600 + Compass_Heading * 10 - GeoMagDec) % 3600; else
602
                                   HeadFreeStartAngle = GyroCompassCorrected;
603
                                   HeadFreeStartAngle = GyroCompassCorrected;
603
                                 }
604
                                 }
604
 
605
 
605
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
606
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
606
                                  {
607
                                  {
607
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
608
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
608
                                    {
609
                                    {
609
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
610
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
610
                                         {
611
                                         {
611
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
612
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
612
                                         }
613
                                         }
613
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
614
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
614
                                          HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°   
615
                                          HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°   
615
                                        }
616
                                        }
616
                                }
617
                                }
617
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
618
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
618
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[10];
619
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[10];
619
                                DebugOut.Analog[7] = FC.BAT_Voltage;
620
                                DebugOut.Analog[7] = FC.BAT_Voltage;
620
                                DebugOut.Analog[5] = FC.StatusFlags;
621
                                DebugOut.Analog[5] = FC.StatusFlags;
621
                                NaviData.FCStatusFlags = FC.StatusFlags;
622
                                NaviData.FCStatusFlags = FC.StatusFlags;
622
                                if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
623
                                if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
623
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
624
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
624
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
625
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
625
 
626
 
626
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
627
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
627
                                else
628
                                else
628
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
629
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
629
 
630
 
630
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
631
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
631
                                else
632
                                else
632
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
633
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
633
 
634
 
634
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
635
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
635
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
636
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
636
                                break;
637
                                break;
637
 
638
 
638
                        case SPI_FCCMD_BL_ACCU:
639
                        case SPI_FCCMD_BL_ACCU:
639
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
640
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
640
                                DebugOut.Analog[8] = FC.BAT_Current;
641
                                DebugOut.Analog[8] = FC.BAT_Current;
641
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
642
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
642
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[4];
643
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[4];
643
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[5];
644
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[5];
644
                                Motor_Version[FromFlightCtrl.Param.Byte[6]] = FromFlightCtrl.Param.Byte[7];
645
                                Motor_Version[FromFlightCtrl.Param.Byte[6]] = FromFlightCtrl.Param.Byte[7];
645
                                Motor[FromFlightCtrl.Param.Byte[6]].MaxPWM = FromFlightCtrl.Param.Byte[8];
646
                                Motor[FromFlightCtrl.Param.Byte[6]].MaxPWM = FromFlightCtrl.Param.Byte[8];
646
                                Motor[FromFlightCtrl.Param.Byte[6]].State = FromFlightCtrl.Param.Byte[9];
647
                                Motor[FromFlightCtrl.Param.Byte[6]].State = FromFlightCtrl.Param.Byte[9];
647
                                Motor[FromFlightCtrl.Param.Byte[6]].Temperature = FromFlightCtrl.Param.Byte[10];
648
                                Motor[FromFlightCtrl.Param.Byte[6]].Temperature = FromFlightCtrl.Param.Byte[10];
648
                                Motor[FromFlightCtrl.Param.Byte[6]].Current = FromFlightCtrl.Param.Byte[11];
649
                                Motor[FromFlightCtrl.Param.Byte[6]].Current = FromFlightCtrl.Param.Byte[11];
649
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
650
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
650
                                 {
651
                                 {
651
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
652
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
652
                                 }
653
                                 }
653
                                NaviData.UBat = FC.BAT_Voltage;
654
                                NaviData.UBat = FC.BAT_Voltage;
654
                                NaviData.Current = FC.BAT_Current;
655
                                NaviData.Current = FC.BAT_Current;
655
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
656
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
656
                                break;
657
                                break;
657
                        case SPI_FCCMD_PARAMETER1:
658
                        case SPI_FCCMD_PARAMETER1:
658
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
659
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
659
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
660
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
660
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
661
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
661
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
662
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
662
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
663
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
663
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
664
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
664
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
665
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
665
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
666
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
666
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
667
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
667
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
668
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
668
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
669
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
669
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
670
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
670
                                break;
671
                                break;
671
                        case SPI_FCCMD_PARAMETER2:
672
                        case SPI_FCCMD_PARAMETER2:
672
                                CHK_POTI_MM(Parameter.NaviOut1Parameter,FromFlightCtrl.Param.Byte[0],0,255);
673
                                CHK_POTI_MM(Parameter.NaviOut1Parameter,FromFlightCtrl.Param.Byte[0],0,255);
673
                                if(FromFlightCtrl.Param.Byte[1]) FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];     // will be cleared in the SD-Logging
674
                                if(FromFlightCtrl.Param.Byte[1]) FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];     // will be cleared in the SD-Logging
674
                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[2];
675
                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[2];
675
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[3];
676
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[3];
-
 
677
                                FromFC_LowVoltageHomeActive = FromFlightCtrl.Param.Byte[4];
676
                                break;
678
                                break;
677
                        case SPI_FCCMD_STICK:
679
                        case SPI_FCCMD_STICK:
678
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
680
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
679
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
681
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
680
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
682
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
681
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
683
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
682
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
684
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
683
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
685
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
684
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
686
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
685
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
687
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
686
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
688
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
687
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
689
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
688
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
690
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
689
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
691
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
690
                            CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
692
                            CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
691
                                break;
693
                                break;
692
 
694
 
693
                        case SPI_FCCMD_MISC:
695
                        case SPI_FCCMD_MISC:
694
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
696
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
695
                                {       // put only new CompassCalState into queue to send via I2C
697
                                {       // put only new CompassCalState into queue to send via I2C
696
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0) 
698
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0) 
697
                                         {
699
                                         {
698
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
700
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
699
                                           Compass_SetCalState(CompassCalState);
701
                                           Compass_SetCalState(CompassCalState);
700
                                         }
702
                                         }
701
//                                       else CompassCalState = 0;
703
//                                       else CompassCalState = 0;
702
                                }
704
                                }
703
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
705
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
704
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
706
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
705
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
707
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
706
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
708
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
707
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
709
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
708
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
710
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
709
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
711
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
710
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
712
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
711
                                NaviData.RC_Quality = FC.RC_Quality;
713
                                NaviData.RC_Quality = FC.RC_Quality;
712
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
714
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
713
//                              FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
715
//                              FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
714
//                              if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
716
//                              if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
715
//                              NaviData.RC_RSSI = FC.RC_RSSI;
717
//                              NaviData.RC_RSSI = FC.RC_RSSI;
716
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
718
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
717
                                break;
719
                                break;
718
 
720
 
719
                        case SPI_FCCMD_SERVOS:
721
                        case SPI_FCCMD_SERVOS:
720
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
722
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
721
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
723
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
722
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
724
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
723
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
725
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
724
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
726
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
725
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
727
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
726
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
728
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
727
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
729
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
728
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
730
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
729
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
731
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
730
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[10];
732
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[10];
731
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[11];
733
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[11];
732
                                break;
734
                                break;
733
 
735
 
734
                        case SPI_FCCMD_VERSION:
736
                        case SPI_FCCMD_VERSION:
735
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
737
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
736
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
738
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
737
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
739
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
738
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
740
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
739
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
741
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
740
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
742
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
741
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
743
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
742
                                if(FromFlightCtrl.Param.Byte[7] >= 68 && FromFlightCtrl.Param.Byte[7] <= 188)
744
                                if(FromFlightCtrl.Param.Byte[7] >= 68 && FromFlightCtrl.Param.Byte[7] <= 188)
743
                                {
745
                                {
744
                                 FC.FromFC_DisableDeclination = 1;
746
                                 FC.FromFC_DisableDeclination = 1;
745
                                 FC.FromFC_CompassOffset = 10 * (signed char) ((unsigned char) FromFlightCtrl.Param.Byte[7] - 128);
747
                                 FC.FromFC_CompassOffset = 10 * (signed char) ((unsigned char) FromFlightCtrl.Param.Byte[7] - 128);
746
                                 GeoMagDec = 0;
748
                                 GeoMagDec = 0;
747
                                }
749
                                }
748
                                else
750
                                else
749
                                {
751
                                {
750
                                 FC.FromFC_DisableDeclination = 0;
752
                                 FC.FromFC_DisableDeclination = 0;
751
                                 FC.FromFC_CompassOffset = 10 * (signed char) FromFlightCtrl.Param.Byte[7];
753
                                 FC.FromFC_CompassOffset = 10 * (signed char) FromFlightCtrl.Param.Byte[7];
752
                                }
754
                                }
753
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
755
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
754
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
756
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
755
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
757
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
756
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
758
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
757
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
759
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
758
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
760
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
759
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
761
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
760
                                else DebugOut.StatusRed &= ~AMPEL_FC;
762
                                else DebugOut.StatusRed &= ~AMPEL_FC;
761
                                break;
763
                                break;
762
                        default:
764
                        default:
763
                                break;
765
                                break;
764
                }
766
                }
765
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
767
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
766
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
768
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
767
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
769
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
768
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
770
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
769
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
771
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
770
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
772
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
771
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
773
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
772
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
774
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
773
                // every time we got new data from the FC via SPI call the navigation routine
775
                // every time we got new data from the FC via SPI call the navigation routine
774
                // and update GPSStick that are returned to FC
776
                // and update GPSStick that are returned to FC
775
                SPI_RxBuffer_Request = 0;
777
                SPI_RxBuffer_Request = 0;
776
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
778
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
777
                ClearFCStatusFlags = 1;
779
                ClearFCStatusFlags = 1;
778
                if(counter)
780
                if(counter)
779
                {
781
                {
780
                        counter--;                                       // count down to enable servo
782
                        counter--;                                       // count down to enable servo
781
                        if(!counter) TIMER2_Init();  // enable Servo Output
783
                        if(!counter) TIMER2_Init();  // enable Servo Output
782
                }
784
                }
783
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
785
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
784
 
786
 
785
        }       // EOF if(SPI_RxBuffer_Request)
787
        }       // EOF if(SPI_RxBuffer_Request)
786
        else // no new SPI data
788
        else // no new SPI data
787
        {
789
        {
788
                if(CheckDelay(timeout) && (counter == 0))
790
                if(CheckDelay(timeout) && (counter == 0))
789
                {
791
                {
790
                        TIMER2_Deinit();  // disable Servo Output
792
                        TIMER2_Deinit();  // disable Servo Output
791
                        counter = 50;     // reset counter for enabling Servo Output
793
                        counter = 50;     // reset counter for enabling Servo Output
792
                }
794
                }
793
        }
795
        }
794
}
796
}
795
 
797
 
796
//------------------------------------------------------
798
//------------------------------------------------------
797
void SPI0_GetFlightCtrlVersion(void)
799
void SPI0_GetFlightCtrlVersion(void)
798
{
800
{
799
        u32 timeout;
801
        u32 timeout;
800
        u8 repeat;
802
        u8 repeat;
801
        u8 msg[64];
803
        u8 msg[64];
802
 
804
 
803
        UART1_PutString("\r\n Looking for FlightControl");
805
        UART1_PutString("\r\n Looking for FlightControl");
804
        FC_Version.Major = 0xFF;
806
        FC_Version.Major = 0xFF;
805
        FC_Version.Minor = 0xFF;
807
        FC_Version.Minor = 0xFF;
806
        FC_Version.Patch = 0xFF;
808
        FC_Version.Patch = 0xFF;
807
        FC_Version.Compatible = 0xFF;
809
        FC_Version.Compatible = 0xFF;
808
 
810
 
809
        // polling FC version info
811
        // polling FC version info
810
        repeat = 0;
812
        repeat = 0;
811
        do
813
        do
812
        {
814
        {
813
                timeout = SetDelay(250);
815
                timeout = SetDelay(250);
814
                do
816
                do
815
                {
817
                {
816
                        SPI0_UpdateBuffer();
818
                        SPI0_UpdateBuffer();
817
                        if (FC_Version.Major != 0xFF)  break;
819
                        if (FC_Version.Major != 0xFF)  break;
818
                }while (!CheckDelay(timeout));
820
                }while (!CheckDelay(timeout));
819
                UART1_PutString(".");
821
                UART1_PutString(".");
820
                repeat++;
822
                repeat++;
821
//              FCCalibActive = 1;
823
//              FCCalibActive = 1;
822
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
824
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
823
        // if we got it
825
        // if we got it
824
        if (FC_Version.Major != 0xFF)
826
        if (FC_Version.Major != 0xFF)
825
        {
827
        {
826
                sprintf(msg, " FC V%d.%02d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
828
                sprintf(msg, " FC V%d.%02d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
827
                UART1_PutString(msg);
829
                UART1_PutString(msg);
828
        }
830
        }
829
        else UART1_PutString("\n\r not found!");
831
        else UART1_PutString("\n\r not found!");
830
}
832
}
831
 
833
 
832
 
834
 
833
 
835