Subversion Repositories NaviCtrl

Rev

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

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