Subversion Repositories NaviCtrl

Rev

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

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