Subversion Repositories NaviCtrl

Rev

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

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