Subversion Repositories NaviCtrl

Rev

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

Rev 330 Rev 331
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
 
57
 
58
#include <string.h>
58
#include <string.h>
-
 
59
#include <math.h>
59
#include "91x_lib.h"
60
#include "91x_lib.h"
60
#include "led.h"
61
#include "led.h"
61
#include "gps.h"
62
#include "gps.h"
62
#include "uart1.h"
63
#include "uart1.h"
63
#include "spi_slave.h"
64
#include "spi_slave.h"
64
#include "compass.h"
65
#include "compass.h"
65
#include "timer1.h"
66
#include "timer1.h"
66
#include "timer2.h"
67
#include "timer2.h"
67
#include "config.h"
68
#include "config.h"
68
#include "main.h"
69
#include "main.h"
69
#include "compass.h"
70
#include "compass.h"
70
#include "params.h"
71
#include "params.h"
71
 
72
 
72
#define SPI_RXSYNCBYTE1 0xAA
73
#define SPI_RXSYNCBYTE1 0xAA
73
#define SPI_RXSYNCBYTE2 0x83
74
#define SPI_RXSYNCBYTE2 0x83
74
#define SPI_TXSYNCBYTE1 0x81
75
#define SPI_TXSYNCBYTE1 0x81
75
#define SPI_TXSYNCBYTE2 0x55
76
#define SPI_TXSYNCBYTE2 0x55
76
 
77
 
77
//communication packets
78
//communication packets
78
FromFlightCtrl_t   FromFlightCtrl;
79
FromFlightCtrl_t   FromFlightCtrl;
79
ToFlightCtrl_t     ToFlightCtrl;
80
ToFlightCtrl_t     ToFlightCtrl;
80
#define SPI0_TIMEOUT 500 // 500ms
81
#define SPI0_TIMEOUT 500 // 500ms
81
volatile u32 SPI0_Timeout = 0;
82
volatile u32 SPI0_Timeout = 0;
82
 
83
 
83
// tx packet buffer
84
// tx packet buffer
84
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
85
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
85
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
86
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
86
volatile u8 SPI_TxBufferIndex = 0;
87
volatile u8 SPI_TxBufferIndex = 0;
87
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
88
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
88
 
89
 
89
// rx packet buffer
90
// rx packet buffer
90
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
91
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
91
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
92
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
92
volatile u8 SPI_RxBufferIndex = 0;
93
volatile u8 SPI_RxBufferIndex = 0;
93
volatile u8 SPI_RxBuffer_Request = 0;
94
volatile u8 SPI_RxBuffer_Request = 0;
94
#define SPI_COMMAND_INDEX 0
95
#define SPI_COMMAND_INDEX 0
95
 
96
 
96
s32 Kalman_K = 32;
97
s32 Kalman_K = 32;
97
s32 Kalman_MaxDrift = 5 * 16;
98
s32 Kalman_MaxDrift = 5 * 16;
98
s32 Kalman_MaxFusion = 64;
99
s32 Kalman_MaxFusion = 64;
99
s32 Kalman_Kompass = 32;
100
s32 Kalman_Kompass = 32;
100
s32 ToFcGpsZ = 0;
101
s32 ToFcGpsZ = 0;
101
 
102
 
102
u8 SPI_CommandSequence[] = { SPI_NCCMD_VERSION, SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO, SPI_NCCMD_KALMAN};
103
u8 SPI_CommandSequence[] = { SPI_NCCMD_VERSION, SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO, SPI_NCCMD_KALMAN};
103
u8 SPI_CommandCounter = 0;
104
u8 SPI_CommandCounter = 0;
104
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
105
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
105
s32 HeadFreeStartAngle = 0;
106
s32 HeadFreeStartAngle = 0;
106
s16 FC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
107
s16 FC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
107
u32 ToFC_AltitudeRate = 0;
108
u32 ToFC_AltitudeRate = 0;
108
s32 ToFC_AltitudeSetpoint = 0;
109
s32 ToFC_AltitudeSetpoint = 0;
109
u8  FromFC_VarioCharacter = ' ';
110
u8  FromFC_VarioCharacter = ' ';
110
u8 DisableFC_Sticks = 0;
111
u8 GPS_Aid_StickMultiplikator = 0;
111
u8 NC_GPS_ModeCharacter = ' ';
112
u8 NC_GPS_ModeCharacter = ' ';
112
u8 FCCalibActive = 0;
113
u8 FCCalibActive = 0;
113
u8 FC_is_Calibrated = 0;
114
u8 FC_is_Calibrated = 0;
114
 
115
 
115
SPI_Version_t FC_Version;
116
SPI_Version_t FC_Version;
116
 
117
 
117
//--------------------------------------------------------------
118
//--------------------------------------------------------------
118
void SSP0_IRQHandler(void)
119
void SSP0_IRQHandler(void)
119
{
120
{
120
        static u8 rxchksum = 0;
121
        static u8 rxchksum = 0;
121
        u8 rxdata;
122
        u8 rxdata;
122
 
123
 
123
        #define SPI_SYNC1       0
124
        #define SPI_SYNC1       0
124
        #define SPI_SYNC2       1
125
        #define SPI_SYNC2       1
125
        #define SPI_DATA        2
126
        #define SPI_DATA        2
126
        static u8 SPI_State = SPI_SYNC1;
127
        static u8 SPI_State = SPI_SYNC1;
127
 
128
 
128
        IENABLE;
129
        IENABLE;
129
 
130
 
130
        // clear pending bits
131
        // clear pending bits
131
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
132
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
132
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
133
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
133
 
134
 
134
        // while RxFIFO not empty
135
        // while RxFIFO not empty
135
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
136
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
136
        {
137
        {
137
                rxdata =  SSP0->DR; // catch the received byte
138
                rxdata =  SSP0->DR; // catch the received byte
138
                // Fill TxFIFO while its not full or end of packet is reached
139
                // Fill TxFIFO while its not full or end of packet is reached
139
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
140
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
140
                {
141
                {
141
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
142
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
142
                        {
143
                        {
143
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
144
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
144
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
145
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
145
                                SPI_TxBufferIndex++; // pointer to next byte
146
                                SPI_TxBufferIndex++; // pointer to next byte
146
                        }
147
                        }
147
                        else // end of packet is reached reset and copy data to tx buffer
148
                        else // end of packet is reached reset and copy data to tx buffer
148
                        {
149
                        {
149
                                SPI_TxBufferIndex = 0;  // reset buffer index
150
                                SPI_TxBufferIndex = 0;  // reset buffer index
150
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
151
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
151
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
152
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
152
                                BeepTime = 0; // reset local beeptime
153
                                BeepTime = 0; // reset local beeptime
153
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
154
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
154
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
155
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
155
                        }
156
                        }
156
                }
157
                }
157
                switch (SPI_State)
158
                switch (SPI_State)
158
                {
159
                {
159
                        case SPI_SYNC1:
160
                        case SPI_SYNC1:
160
                                SPI_RxBufferIndex = 0; // reset buffer index
161
                                SPI_RxBufferIndex = 0; // reset buffer index
161
                                rxchksum = rxdata;     // init checksum
162
                                rxchksum = rxdata;     // init checksum
162
                                if (rxdata == SPI_RXSYNCBYTE1)
163
                                if (rxdata == SPI_RXSYNCBYTE1)
163
                                {   // 1st syncbyte ok
164
                                {   // 1st syncbyte ok
164
                                        SPI_State = SPI_SYNC2;  // step to sync2
165
                                        SPI_State = SPI_SYNC2;  // step to sync2
165
                                }
166
                                }
166
                                break;
167
                                break;
167
                        case SPI_SYNC2:
168
                        case SPI_SYNC2:
168
                                if (rxdata == SPI_RXSYNCBYTE2)
169
                                if (rxdata == SPI_RXSYNCBYTE2)
169
                                {  // 2nd Syncbyte ok
170
                                {  // 2nd Syncbyte ok
170
                                        rxchksum += rxdata;
171
                                        rxchksum += rxdata;
171
                                        SPI_State = SPI_DATA;
172
                                        SPI_State = SPI_DATA;
172
                                }  // 2nd Syncbyte does not match
173
                                }  // 2nd Syncbyte does not match
173
                                else
174
                                else
174
                                {
175
                                {
175
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
176
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
176
                                }
177
                                }
177
                                break;
178
                                break;
178
                        case SPI_DATA:
179
                        case SPI_DATA:
179
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
180
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
180
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
181
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
181
                                {
182
                                {
182
                                        if (rxdata == rxchksum) // verify checksum byte
183
                                        if (rxdata == rxchksum) // verify checksum byte
183
                                        {
184
                                        {
184
                                                // copy SPI_RxBuffer -> FromFlightCtrl
185
                                                // copy SPI_RxBuffer -> FromFlightCtrl
185
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
186
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
186
                                                {
187
                                                {
187
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
188
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
188
                                                        SPI_RxBuffer_Request = 1;
189
                                                        SPI_RxBuffer_Request = 1;
189
                                                }
190
                                                }
190
                                                // reset timeout counter on good packet
191
                                                // reset timeout counter on good packet
191
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
192
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
192
                                                DebugOut.Analog[13]++;
193
                                                DebugOut.Analog[13]++;
193
                                        }
194
                                        }
194
                                        else // bad checksum byte
195
                                        else // bad checksum byte
195
                                        {
196
                                        {
196
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
197
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
197
                                        }
198
                                        }
198
                                        SPI_State  = SPI_SYNC1; // reset state
199
                                        SPI_State  = SPI_SYNC1; // reset state
199
                                }
200
                                }
200
                                else // end of packet not reached
201
                                else // end of packet not reached
201
                                {
202
                                {
202
                                        rxchksum += rxdata;      // update checksum
203
                                        rxchksum += rxdata;      // update checksum
203
                                }
204
                                }
204
                                break;
205
                                break;
205
                        default:
206
                        default:
206
                                SPI_State  = SPI_SYNC1;
207
                                SPI_State  = SPI_SYNC1;
207
                                break;
208
                                break;
208
                }
209
                }
209
        }
210
        }
210
 
211
 
211
        IDISABLE;
212
        IDISABLE;
212
}
213
}
213
 
214
 
214
//--------------------------------------------------------------
215
//--------------------------------------------------------------
215
void SPI0_Init(void)
216
void SPI0_Init(void)
216
{
217
{
217
        GPIO_InitTypeDef GPIO_InitStructure;
218
        GPIO_InitTypeDef GPIO_InitStructure;
218
        SSP_InitTypeDef   SSP_InitStructure;
219
        SSP_InitTypeDef   SSP_InitStructure;
219
 
220
 
220
        UART1_PutString("\r\n SPI init...");
221
        UART1_PutString("\r\n SPI init...");
221
 
222
 
222
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
223
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
223
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
224
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
224
 
225
 
225
        GPIO_DeInit(GPIO2);
226
        GPIO_DeInit(GPIO2);
226
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
227
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
227
        GPIO_StructInit(&GPIO_InitStructure);
228
        GPIO_StructInit(&GPIO_InitStructure);
228
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
229
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
229
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
230
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
230
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
231
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
231
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
232
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
232
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
233
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
233
        GPIO_Init (GPIO2, &GPIO_InitStructure);
234
        GPIO_Init (GPIO2, &GPIO_InitStructure);
234
 
235
 
235
        // SSP0_MISO pin GPIO2.6
236
        // SSP0_MISO pin GPIO2.6
236
        GPIO_StructInit(&GPIO_InitStructure);
237
        GPIO_StructInit(&GPIO_InitStructure);
237
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
238
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
238
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
239
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
239
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
240
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
240
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
241
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
241
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
242
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
242
        GPIO_Init (GPIO2, &GPIO_InitStructure);
243
        GPIO_Init (GPIO2, &GPIO_InitStructure);
243
 
244
 
244
        SSP_DeInit(SSP0);
245
        SSP_DeInit(SSP0);
245
        SSP_StructInit(&SSP_InitStructure);
246
        SSP_StructInit(&SSP_InitStructure);
246
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
247
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
247
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
248
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
248
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
249
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
249
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
250
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
250
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
251
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
251
        SSP_InitStructure.SSP_ClockRate = 0;
252
        SSP_InitStructure.SSP_ClockRate = 0;
252
 
253
 
253
        SSP_Init(SSP0, &SSP_InitStructure);
254
        SSP_Init(SSP0, &SSP_InitStructure);
254
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
255
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
255
 
256
 
256
        SSP_Cmd(SSP0, ENABLE);
257
        SSP_Cmd(SSP0, ENABLE);
257
        // initialize the syncbytes in the tx buffer
258
        // initialize the syncbytes in the tx buffer
258
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
259
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
259
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
260
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
260
        // set the pointer to the checksum byte in the tx buffer
261
        // set the pointer to the checksum byte in the tx buffer
261
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
262
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
262
 
263
 
263
        ToFlightCtrl.GPSStick.Nick = 0;
264
        ToFlightCtrl.GPSStick.Nick = 0;
264
        ToFlightCtrl.GPSStick.Roll = 0;
265
        ToFlightCtrl.GPSStick.Roll = 0;
265
        ToFlightCtrl.GPSStick.Yaw = 0;
266
        ToFlightCtrl.GPSStick.Yaw = 0;
266
 
267
 
267
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
268
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
268
        VIC_ITCmd(SSP0_ITLine, ENABLE);
269
        VIC_ITCmd(SSP0_ITLine, ENABLE);
269
 
270
 
270
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
271
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
271
 
272
 
272
        UART1_PutString("ok");
273
        UART1_PutString("ok");
273
}
274
}
274
 
275
 
275
 
276
 
276
//------------------------------------------------------
277
//------------------------------------------------------
277
void SPI0_UpdateBuffer(void)
278
void SPI0_UpdateBuffer(void)
278
{
279
{
279
        static u32 timeout = 0;
280
        static u32 timeout = 0;
280
        static u8 counter = 50,hott_index = 0;
281
        static u8 counter = 50,hott_index = 0;
281
        static u8 CompassCalState = 0;
282
        static u8 CompassCalState = 0;
282
        s16 tmp;
283
        s16 tmp;
283
        s32 i1,i2;
284
        s32 i1,i2;
284
 
285
 
285
        if (SPI_RxBuffer_Request)
286
        if (SPI_RxBuffer_Request)
286
        {
287
        {
287
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
288
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
288
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
289
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
289
                ToFlightCtrl.CompassHeading = Compass_Heading;
290
                ToFlightCtrl.CompassHeading = Compass_Heading;
290
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
291
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
291
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
292
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
292
//              ToFlightCtrl.MagVecX = MagVector.X;
293
//              ToFlightCtrl.MagVecX = MagVector.X;
293
//              ToFlightCtrl.MagVecY = MagVector.Y;
294
//              ToFlightCtrl.MagVecY = MagVector.Y;
294
                ToFlightCtrl.MagVecZ = MagVector.Z;
295
                ToFlightCtrl.MagVecZ = MagVector.Z;
295
                ToFlightCtrl.NCStatus = 0;
296
                ToFlightCtrl.NCStatus = 0;
296
                // cycle spi commands
297
                // cycle spi commands
297
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
298
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
298
                // restart command cycle at the end
299
                // restart command cycle at the end
299
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
300
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
300
#define FLAG_GPS_AID 0x01
301
#define FLAG_GPS_AID 0x01
301
                switch (ToFlightCtrl.Command)
302
                switch (ToFlightCtrl.Command)
302
                {
303
                {
303
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
304
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
304
                                CalcHeadFree();
305
                                CalcHeadFree();
305
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
306
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
306
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
307
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
307
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
308
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
308
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
309
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
309
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
310
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
310
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
311
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
311
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
312
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
312
                if(DisableFC_Sticks) ToFlightCtrl.Param.Byte[7] = FLAG_GPS_AID; else ToFlightCtrl.Param.Byte[7] = 0x00;
313
                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
313
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
314
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
314
                                {
315
                                {
315
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
316
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
316
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
317
                                        CAM_Orientation.UpdateMask &= ~CAM_UPDATE_AZIMUTH;
317
                                }
318
                                }
318
                                else
319
                                else
319
                                {
320
                                {
320
                                        ToFlightCtrl.Param.sInt[4] = -1;
321
                                        ToFlightCtrl.Param.sInt[4] = -1;
321
                                }
322
                                }
322
 
323
 
323
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
324
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
324
                                {
325
                                {
325
                                        ToFlightCtrl.Param.sInt[5] = tmp;
326
                                        ToFlightCtrl.Param.sInt[5] = tmp;
326
                                }
327
                                }
327
                                else
328
                                else
328
                                {
329
                                {
329
                                        if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) ToFlightCtrl.Param.sInt[5] = CAM_Orientation.Elevation; // only, if carefree is active
330
                                        if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) ToFlightCtrl.Param.sInt[5] = CAM_Orientation.Elevation; // only, if carefree is active
330
                                        else ToFlightCtrl.Param.sInt[5] = 0;
331
                                        else ToFlightCtrl.Param.sInt[5] = 0;
331
                                }
332
                                }
332
                                break;
333
                                break;
333
 
334
 
334
                        case SPI_NCCMD_VERSION:
335
                        case SPI_NCCMD_VERSION:
335
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
336
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
336
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
337
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
337
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
338
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
338
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
339
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
339
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
340
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
340
                                ToFlightCtrl.Param.Byte[5] = DebugOut.Status[0];
341
                                ToFlightCtrl.Param.Byte[5] = DebugOut.Status[0];
341
                                ToFlightCtrl.Param.Byte[6] = DebugOut.Status[1];
342
                                ToFlightCtrl.Param.Byte[6] = DebugOut.Status[1];
342
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;
343
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;
343
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
344
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
344
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
345
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
345
                                break;
346
                                break;
346
 
347
 
347
                        case SPI_NCCMD_GPSINFO:
348
                        case SPI_NCCMD_GPSINFO:
348
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
349
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
349
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
350
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
350
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
351
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
351
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
352
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
352
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
353
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
353
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
354
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
354
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;
355
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;
355
                                ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
356
                                ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
356
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
357
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
357
                                {
358
                                {
358
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
359
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
359
                                }
360
                                }
360
                                else
361
                                else
361
                                {
362
                                {
362
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
363
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
363
                                }
364
                                }
364
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
365
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
365
                                {
366
                                {
366
                                        ToFlightCtrl.Param.sInt[5] = tmp;
367
                                        ToFlightCtrl.Param.sInt[5] = tmp;
367
                                }
368
                                }
368
                                else
369
                                else
369
                                {
370
                                {
370
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
371
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
371
                                }
372
                                }
372
                                break;
373
                                break;
373
/*
374
/*
374
typedef struct
375
typedef struct
375
{
376
{
376
  unsigned char StartByte;  //0         // 0x7C
377
  unsigned char StartByte;  //0         // 0x7C
377
  unsigned char Packet_ID;  //1         // 0x89  - Vario ID
378
  unsigned char Packet_ID;  //1         // 0x89  - Vario ID
378
  unsigned char WarnBeep;   //2         // Anzahl der Töne 0..36
379
  unsigned char WarnBeep;   //2         // Anzahl der Töne 0..36
379
  unsigned char Heading;        //3     // 1 = 2°
380
  unsigned char Heading;        //3     // 1 = 2°
380
  unsigned int Speed;           //4+5   // in km/h
381
  unsigned int Speed;           //4+5   // in km/h
381
  unsigned char Lat_North;      //6    
382
  unsigned char Lat_North;      //6    
382
  unsigned char Lat_G;      //7
383
  unsigned char Lat_G;      //7
383
  unsigned char Lat_M;      //8
384
  unsigned char Lat_M;      //8
384
  unsigned char Lat_Sek1;       //9    
385
  unsigned char Lat_Sek1;       //9    
385
  unsigned char Lat_Sek2;       //10    
386
  unsigned char Lat_Sek2;       //10    
386
  unsigned char Lon_East;       //11    
387
  unsigned char Lon_East;       //11    
387
  unsigned char Lon_G;      //12
388
  unsigned char Lon_G;      //12
388
  unsigned char Lon_M;      //13
389
  unsigned char Lon_M;      //13
389
  unsigned char Lon_Sek1;       //14    
390
  unsigned char Lon_Sek1;       //14    
390
  unsigned char Lon_Sek2;       //15    
391
  unsigned char Lon_Sek2;       //15    
391
  unsigned int Distance;        //16+17    // 9000 = 0m
392
  unsigned int Distance;        //16+17    // 9000 = 0m
392
  unsigned int Altitude;        //18+19    // 500 = 0m
393
  unsigned int Altitude;        //18+19    // 500 = 0m
393
  unsigned int m_sec;           //20+21    // 3000 = 0
394
  unsigned int m_sec;           //20+21    // 3000 = 0
394
  unsigned int m_3sec;          //22+23    // 3000 = 0
395
  unsigned int m_3sec;          //22+23    // 3000 = 0
395
  unsigned int m_10sec;         //24+25    // 3000 = 0
396
  unsigned int m_10sec;         //24+25    // 3000 = 0
396
  unsigned char NullByte;       // 0x00
397
  unsigned char NullByte;       // 0x00
397
  unsigned char NullByte1;      // 0x00
398
  unsigned char NullByte1;      // 0x00
398
  unsigned char EndByte;                // 0x7D
399
  unsigned char EndByte;                // 0x7D
399
} GPSPacket_t;
400
} GPSPacket_t;
400
*/
401
*/
401
/*
402
/*
402
typedef struct
403
typedef struct
403
{
404
{
404
  unsigned char StartByte;      //0 0x7C
405
  unsigned char StartByte;      //0 0x7C
405
  unsigned char Packet_ID;      //1 HOTT_GENERAL_PACKET_ID     
406
  unsigned char Packet_ID;      //1 HOTT_GENERAL_PACKET_ID     
406
  unsigned char WarnBeep;       //2 Anzahl der Töne 0..36
407
  unsigned char WarnBeep;       //2 Anzahl der Töne 0..36
407
  unsigned char VoltageCell1;   //3 208 = 4,16V  (Voltage * 50 = Wert)
408
  unsigned char VoltageCell1;   //3 208 = 4,16V  (Voltage * 50 = Wert)
408
  unsigned char VoltageCell2;   //4
409
  unsigned char VoltageCell2;   //4
409
  unsigned char VoltageCell3;   //5
410
  unsigned char VoltageCell3;   //5
410
  unsigned char VoltageCell4;   //6
411
  unsigned char VoltageCell4;   //6
411
  unsigned char VoltageCell5;   //7
412
  unsigned char VoltageCell5;   //7
412
  unsigned char VoltageCell6;   //8
413
  unsigned char VoltageCell6;   //8
413
  unsigned int  Battery1;               //9   51  = 5,1V
414
  unsigned int  Battery1;               //9   51  = 5,1V
414
  unsigned int  Battery2;               //11  51  = 5,1V
415
  unsigned int  Battery2;               //11  51  = 5,1V
415
  unsigned char Temperature1;   //13  44 = 24°C, 0 = -20°C
416
  unsigned char Temperature1;   //13  44 = 24°C, 0 = -20°C
416
  unsigned char Temperature2;   //14  44 = 24°C, 0 = -20°C
417
  unsigned char Temperature2;   //14  44 = 24°C, 0 = -20°C
417
  unsigned char FuelPercent;    //15
418
  unsigned char FuelPercent;    //15
418
    signed int  FuelCapacity;   //16
419
    signed int  FuelCapacity;   //16
419
  unsigned int  Rpm;  
420
  unsigned int  Rpm;  
420
  unsigned int  Altitude;      
421
  unsigned int  Altitude;      
421
  unsigned int  m_sec;              // 3000 = 0
422
  unsigned int  m_sec;              // 3000 = 0
422
  unsigned char m_3sec;             // 120 = 0
423
  unsigned char m_3sec;             // 120 = 0
423
  unsigned int  Current;                // 1 = 0.1A
424
  unsigned int  Current;                // 1 = 0.1A
424
  unsigned int  InputVoltage;   // 66  = 6,6V
425
  unsigned int  InputVoltage;   // 66  = 6,6V
425
  unsigned int  Capacity;               // 1  = 10mAh
426
  unsigned int  Capacity;               // 1  = 10mAh
426
  unsigned char NullByte1;      // 0x00
427
  unsigned char NullByte1;      // 0x00
427
  unsigned char NullByte2;      // 0x00
428
  unsigned char NullByte2;      // 0x00
428
  unsigned char EndByte;                // 0x7D
429
  unsigned char EndByte;                // 0x7D
429
} HoTTGeneral_t;
430
} HoTTGeneral_t;
430
*/
431
*/
431
                        case SPI_NCCMD_HOTT_INFO:
432
                        case SPI_NCCMD_HOTT_INFO:
432
                                switch(hott_index++)
433
                                switch(hott_index++)
433
                                {
434
                                {
434
                                case 0:
435
                                case 0:
435
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
436
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
436
                                        //53.285788 7.4847269    --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
437
                                        //53.285788 7.4847269    --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017" 
437
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
438
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
438
                                        ToFlightCtrl.Param.Byte[0] = 3; // index
439
                                        ToFlightCtrl.Param.Byte[0] = 3; // index
439
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
440
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
440
                                        //-----------------------------
441
                                        //-----------------------------
441
                                        ToFlightCtrl.Param.Byte[2] = NaviData.HomePositionDeviation.Bearing / 2;
442
                                        ToFlightCtrl.Param.Byte[2] = NaviData.HomePositionDeviation.Bearing / 2;
442
                                        i1 = GPSData.Speed_Ground; // in cm/sec
443
                                        i1 = GPSData.Speed_Ground; // in cm/sec
443
                                        i1 *= 36;
444
                                        i1 *= 36;
444
                                        i1 /= 1000;
445
                                        i1 /= 1000;
445
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
446
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
446
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
447
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
447
                                        //-----------------------------
448
                                        //-----------------------------
448
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = S
449
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = S
449
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
450
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
450
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
451
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
451
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
452
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
452
                                        i1 *= 100;
453
                                        i1 *= 100;
453
                                        i1 += i2 / 100000;
454
                                        i1 += i2 / 100000;
454
                                        i2  = i2 % 100000;
455
                                        i2  = i2 % 100000;
455
                                        i2 /= 10;
456
                                        i2 /= 10;
456
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
457
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
457
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
458
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
458
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
459
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
459
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
460
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
460
                                        break;
461
                                        break;
461
                                case 1:
462
                                case 1:
462
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
463
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
463
                                        ToFlightCtrl.Param.Byte[0] = 11;        // index
464
                                        ToFlightCtrl.Param.Byte[0] = 11;        // index
464
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
465
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
465
                                        //-----------------------------
466
                                        //-----------------------------
466
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
467
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
467
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
468
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
468
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
469
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
469
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
470
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
470
                                        i1 *= 100;
471
                                        i1 *= 100;
471
                                        i1 += i2 / 100000;
472
                                        i1 += i2 / 100000;
472
                                        i2  = i2 % 100000;
473
                                        i2  = i2 % 100000;
473
                                        i2 /= 10;
474
                                        i2 /= 10;
474
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
475
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
475
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
476
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
476
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
477
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
477
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
478
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
478
                                        //-----------------------------
479
                                        //-----------------------------
479
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
480
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m 
480
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
481
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
481
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
482
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
482
                                        break;
483
                                        break;
483
                                 case 2:
484
                                 case 2:
484
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
485
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
485
                                        ToFlightCtrl.Param.Byte[0] = 5; // index
486
                                        ToFlightCtrl.Param.Byte[0] = 5; // index
486
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
487
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
487
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
488
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
488
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
489
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
489
                                        hott_index = 0;
490
                                        hott_index = 0;
490
                                        break;
491
                                        break;
491
                                 default:
492
                                 default:
492
                                        ToFlightCtrl.Param.Byte[0] = 255;
493
                                        ToFlightCtrl.Param.Byte[0] = 255;
493
                                        hott_index = 0;
494
                                        hott_index = 0;
494
                                        break;
495
                                        break;
495
                                }
496
                                }
496
                                break;
497
                                break;
497
                        default:
498
                        default:
498
                                break;
499
                                break;
499
// 0 = 0,1
500
// 0 = 0,1
500
// 1 = 2,3
501
// 1 = 2,3
501
// 2 = 4,5
502
// 2 = 4,5
502
// 3 = 6,7
503
// 3 = 6,7
503
// 4 = 8,9
504
// 4 = 8,9
504
// 5 = 10,11
505
// 5 = 10,11
505
                }
506
                }
506
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
507
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
507
 
508
 
508
                switch(FromFlightCtrl.Command)
509
                switch(FromFlightCtrl.Command)
509
                {
510
                {
510
                        case SPI_FCCMD_USER:
511
                        case SPI_FCCMD_USER:
511
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
512
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
512
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
513
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
513
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
514
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
514
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
515
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
515
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
516
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
516
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
517
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
517
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
518
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
518
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
519
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
519
                                if(ClearFCStatusFlags)
520
                                if(ClearFCStatusFlags)
520
                                {
521
                                {
521
                                        FC.StatusFlags = 0;
522
                                        FC.StatusFlags = 0;
522
                                        ClearFCStatusFlags = 0;
523
                                        ClearFCStatusFlags = 0;
523
                                }
524
                                }
524
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
525
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
525
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
526
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
526
                                {
527
                                {
527
                                        HeadFreeStartAngle = Compass_Heading * 10;
528
                                        HeadFreeStartAngle = Compass_Heading * 10;
528
                                        Compass_Init();
529
                                        Compass_Init();
529
                                        FCCalibActive = 10;
530
                                        FCCalibActive = 10;
530
                                }
531
                                }
531
                                else
532
                                else
532
                                {
533
                                {
533
                                        if(FCCalibActive) if(--FCCalibActive == 0) FC_is_Calibrated = 1;
534
                                        if(FCCalibActive) if(--FCCalibActive == 0) FC_is_Calibrated = 1;
534
                                }
535
                                }
535
                                if(FC.StatusFlags & FC_STATUS_START)
536
                                if(FC.StatusFlags & FC_STATUS_START)
536
                             {
537
                             {
537
                                   if(Compass_Heading != -1) HeadFreeStartAngle = Compass_Heading * 10; else
538
                                   if(Compass_Heading != -1) HeadFreeStartAngle = Compass_Heading * 10; else
538
                                   HeadFreeStartAngle = FromFlightCtrl.GyroHeading;
539
                                   HeadFreeStartAngle = FromFlightCtrl.GyroHeading;
539
                                 }
540
                                 }
540
 
541
 
541
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
542
                                 if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
542
                                  {
543
                                  {
543
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
544
                                   if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
544
                                    {
545
                                    {
545
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
546
                                         if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))       // nur bei ausreichender Distance -> 20m
546
                                         {
547
                                         {
547
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
548
                                          HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
548
                                         }
549
                                         }
549
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
550
                                         else                                                                                            // Ansonsten die aktuelle Richtung übernehmen
550
                                          HeadFreeStartAngle = (3600 + FromFlightCtrl.GyroHeading /*+ Parameter.OrientationAngle * 150*/) % 3600; // in 0.1°      
551
                                          HeadFreeStartAngle = (3600 + FromFlightCtrl.GyroHeading /*+ Parameter.OrientationAngle * 150*/) % 3600; // in 0.1°      
551
                                        }
552
                                        }
552
                                }
553
                                }
553
 
554
 
554
//DebugOut.Analog[16] = HeadFreeStartAngle;
555
//DebugOut.Analog[16] = HeadFreeStartAngle;
555
 
556
 
556
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
557
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
557
                                DebugOut.Analog[5] = FC.StatusFlags;
558
                                DebugOut.Analog[5] = FC.StatusFlags;
558
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
559
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[11];
559
                                NaviData.FCStatusFlags = FC.StatusFlags;
560
                                NaviData.FCStatusFlags = FC.StatusFlags;
560
                                NaviData.FCStatusFlags2 = FC.StatusFlags2;
561
                                NaviData.FCStatusFlags2 = FC.StatusFlags2;
561
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[10];
562
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[10];
562
                                break;
563
                                break;
563
 
564
 
564
                        case SPI_FCCMD_ACCU:
565
                        case SPI_FCCMD_ACCU:
565
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
566
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
566
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
567
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
567
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
568
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
568
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[5];
569
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[5];
569
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[6];
570
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[6];
570
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[7];
571
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[7];
571
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[8];
572
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[8];
572
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
573
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
573
                                 {
574
                                 {
574
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
575
                                  NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
575
                                 }
576
                                 }
576
                                NaviData.UBat = FC.BAT_Voltage;
577
                                NaviData.UBat = FC.BAT_Voltage;
577
                                NaviData.Current = FC.BAT_Current;
578
                                NaviData.Current = FC.BAT_Current;
578
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
579
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
579
                                break;
580
                                break;
580
 
581
 
581
#define CHK_POTI(b,a) { if(a < 248) b = a; else b = FC.Poti[255 - a]; }
582
#define CHK_POTI(b,a) { if(a < 248) b = a; else b = FC.Poti[255 - a]; }
582
#define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max); }
583
#define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max); }
583
 
584
 
584
                        case SPI_FCCMD_PARAMETER1:
585
                        case SPI_FCCMD_PARAMETER1:
585
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
586
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
586
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
587
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
587
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
588
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
588
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
589
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
589
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
590
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
590
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
591
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
591
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
592
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
592
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
593
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
593
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
594
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
594
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
595
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
595
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
596
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
596
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
597
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
597
                                break;
598
                                break;
598
 
599
 
599
                        case SPI_FCCMD_STICK:
600
                        case SPI_FCCMD_STICK:
600
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
601
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
601
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
602
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
602
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
603
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
603
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
604
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
604
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
605
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
605
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
606
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
606
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
607
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
607
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
608
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
608
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
609
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
609
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
610
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
610
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
611
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
611
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
612
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
612
                                break;
613
                                break;
613
 
614
 
614
                        case SPI_FCCMD_MISC:
615
                        case SPI_FCCMD_MISC:
615
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
616
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
616
                                {       // put only new CompassCalState into queue to send via I2C
617
                                {       // put only new CompassCalState into queue to send via I2C
617
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
618
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
618
                                        Compass_SetCalState(CompassCalState);
619
                                        Compass_SetCalState(CompassCalState);
619
                                }
620
                                }
620
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
621
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
621
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
622
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
622
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
623
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
623
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
624
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
624
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
625
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
625
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
626
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
626
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
627
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
627
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
628
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
628
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
629
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
629
                                if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
630
                                if(!FC.RC_RSSI) NaviData.RC_Quality = FC.RC_Quality; else NaviData.RC_Quality = FC.RC_RSSI;
630
//                              NaviData.RC_RSSI = FC.RC_RSSI;
631
//                              NaviData.RC_RSSI = FC.RC_RSSI;
631
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
632
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
632
                                break;
633
                                break;
633
 
634
 
634
                        case SPI_FCCMD_SERVOS:
635
                        case SPI_FCCMD_SERVOS:
635
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
636
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
636
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
637
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
637
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
638
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
638
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
639
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
639
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
640
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
640
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
641
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
641
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
642
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
642
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
643
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
643
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
644
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
644
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
645
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
645
                                break;
646
                                break;
646
 
647
 
647
                        case SPI_FCCMD_VERSION:
648
                        case SPI_FCCMD_VERSION:
648
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
649
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
649
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
650
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
650
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
651
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
651
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
652
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
652
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
653
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
653
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
654
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[5];
654
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
655
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[6];
655
                                FC.Error[2]                     |= FromFlightCtrl.Param.Byte[7];
656
                                FC.Error[2]                     |= FromFlightCtrl.Param.Byte[7];
656
                                FC.Error[3]                     |= FromFlightCtrl.Param.Byte[8];
657
                                FC.Error[3]                     |= FromFlightCtrl.Param.Byte[8];
657
                                FC.Error[4]                     |= FromFlightCtrl.Param.Byte[9];
658
                                FC.Error[4]                     |= FromFlightCtrl.Param.Byte[9];
658
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
659
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
659
                                DebugOut.Status[0] |= 0x01; // status of FC Present
660
                                DebugOut.Status[0] |= 0x01; // status of FC Present
660
                                DebugOut.Status[0] |= 0x02; // status of BL Present
661
                                DebugOut.Status[0] |= 0x02; // status of BL Present
661
                                if(FC.Error[0] || FC.Error[1] || FC.Error[2] || FC.Error[3] || FC.Error[4]) DebugOut.Status[1] |= 0x01;
662
                                if(FC.Error[0] || FC.Error[1] || FC.Error[2] || FC.Error[3] || FC.Error[4]) DebugOut.Status[1] |= 0x01;
662
                                else DebugOut.Status[1] &= ~0x01;
663
                                else DebugOut.Status[1] &= ~0x01;
663
                                break;
664
                                break;
664
                        default:
665
                        default:
665
                                break;
666
                                break;
666
                }
667
                }
667
 
668
 
668
                // 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
669
                // and update GPSStick that are returned to FC
670
                // and update GPSStick that are returned to FC
670
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
671
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
671
                ClearFCStatusFlags = 1;
672
                ClearFCStatusFlags = 1;
672
 
673
 
673
                if(counter)
674
                if(counter)
674
                {
675
                {
675
                        counter--;                                       // count down to enable servo
676
                        counter--;                                       // count down to enable servo
676
                        if(!counter) TIMER2_Init();  // enable Servo Output
677
                        if(!counter) TIMER2_Init();  // enable Servo Output
677
                }
678
                }
678
 
679
 
679
                SPI_RxBuffer_Request = 0;
680
                SPI_RxBuffer_Request = 0;
680
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
681
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
681
 
682
 
682
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
683
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
683
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
684
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
684
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
685
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
685
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
686
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
686
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
687
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
687
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
688
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
688
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
689
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
689
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
690
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
690
        }       // EOF if(SPI_RxBuffer_Request)
691
        }       // EOF if(SPI_RxBuffer_Request)
691
        else // no new SPI data
692
        else // no new SPI data
692
        {
693
        {
693
                if(CheckDelay(timeout) && (counter == 0))
694
                if(CheckDelay(timeout) && (counter == 0))
694
                {
695
                {
695
                        TIMER2_Deinit();  // disable Servo Output
696
                        TIMER2_Deinit();  // disable Servo Output
696
                        counter = 50;     // reset counter for enabling Servo Output
697
                        counter = 50;     // reset counter for enabling Servo Output
697
                }
698
                }
698
        }
699
        }
699
}
700
}
700
 
701
 
701
//------------------------------------------------------
702
//------------------------------------------------------
702
void SPI0_GetFlightCtrlVersion(void)
703
void SPI0_GetFlightCtrlVersion(void)
703
{
704
{
704
        u32 timeout;
705
        u32 timeout;
705
        u8 repeat;
706
        u8 repeat;
706
        u8 msg[64];
707
        u8 msg[64];
707
 
708
 
708
        UART1_PutString("\r\n Looking for FlightControl");
709
        UART1_PutString("\r\n Looking for FlightControl");
709
        FC_Version.Major = 0xFF;
710
        FC_Version.Major = 0xFF;
710
        FC_Version.Minor = 0xFF;
711
        FC_Version.Minor = 0xFF;
711
        FC_Version.Patch = 0xFF;
712
        FC_Version.Patch = 0xFF;
712
        FC_Version.Compatible = 0xFF;
713
        FC_Version.Compatible = 0xFF;
713
 
714
 
714
        // polling FC version info
715
        // polling FC version info
715
        repeat = 0;
716
        repeat = 0;
716
        do
717
        do
717
        {
718
        {
718
                timeout = SetDelay(250);
719
                timeout = SetDelay(250);
719
                do
720
                do
720
                {
721
                {
721
                        SPI0_UpdateBuffer();
722
                        SPI0_UpdateBuffer();
722
                        if (FC_Version.Major != 0xFF)  break;
723
                        if (FC_Version.Major != 0xFF)  break;
723
                }while (!CheckDelay(timeout));
724
                }while (!CheckDelay(timeout));
724
                UART1_PutString(".");
725
                UART1_PutString(".");
725
                repeat++;
726
                repeat++;
726
                FCCalibActive = 1;
727
                FCCalibActive = 1;
727
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
728
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
728
        // if we got it
729
        // if we got it
729
        if (FC_Version.Major != 0xFF)
730
        if (FC_Version.Major != 0xFF)
730
        {
731
        {
731
                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);
732
                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);
732
                UART1_PutString(msg);
733
                UART1_PutString(msg);
733
        }
734
        }
734
        else UART1_PutString("\n\r not found!");
735
        else UART1_PutString("\n\r not found!");
735
}
736
}
736
 
737
 
737
 
738
 
738
 
739