Subversion Repositories NaviCtrl

Rev

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

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