Subversion Repositories NaviCtrl

Rev

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

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