Subversion Repositories NaviCtrl

Rev

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

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